public abstract class AbstractJDBCResourceManager<T extends JdbcDatabaseContainer<?>> extends TestContainerResourceManager<JdbcDatabaseContainer<?>> implements JDBCResourceManager
JDBCResourceManager
interface.
The class supports one database, and multiple tables per database object. A database is created when the container first spins up, if one is not given.
The database name is formed using testId. The database name will be "{testId}-{ISO8601 time, microsecond precision}", with additional formatting.
The class is thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
AbstractJDBCResourceManager.Builder<T extends JdbcDatabaseContainer<?>>
Builder for
AbstractJDBCResourceManager . |
JDBCResourceManager.JDBCSchema
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
databaseName |
protected static java.lang.String |
DEFAULT_JDBC_USERNAME |
protected org.apache.beam.it.jdbc.JDBCDriverFactory |
driver |
protected java.lang.String |
password |
protected java.lang.String |
username |
HOST_IP, port
Modifier | Constructor and Description |
---|---|
protected |
AbstractJDBCResourceManager(T container,
AbstractJDBCResourceManager.Builder<T> builder) |
Modifier and Type | Method and Description |
---|---|
boolean |
createTable(java.lang.String tableName,
JDBCResourceManager.JDBCSchema schema)
Creates a table within the current database given a table name and JDBC schema.
|
java.lang.String |
getDatabaseName()
Returns the name of the Database that this JDBC manager will operate in.
|
protected java.lang.String |
getFirstRow(java.lang.String tableName)
Retrieves the first row from the table.
|
protected abstract int |
getJDBCPort()
Return the default port that this JDBC implementation listens on.
|
abstract java.lang.String |
getJDBCPrefix() |
java.lang.String |
getPassword()
Returns the password used to log in to the JDBC database.
|
long |
getRowCount(java.lang.String tableName)
Gets the number of rows in table.
|
java.util.List<java.lang.String> |
getTableSchema(java.lang.String tableName)
Returns the schema of the given table as a list of strings.
|
java.lang.String |
getUri()
Returns the URI connection string to the JDBC Database.
|
java.lang.String |
getUsername()
Returns the username used to log in to the JDBC database.
|
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
readTable(java.lang.String tableName)
Reads all the rows in a table and returns in the format of a list of Maps, which contain all
the columns (including ID).
|
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> |
runSQLQuery(java.lang.String sql)
Run the given SQL query.
|
void |
runSQLUpdate(java.lang.String sql)
Run the given SQL DML statement (INSERT, UPDATE and DELETE).
|
boolean |
write(java.lang.String tableName,
java.util.List<java.util.Map<java.lang.String,java.lang.Object>> rows)
Writes the given mapped rows into the specified columns.
|
cleanupAll, getDockerImageName, getHost, getPort
protected static final java.lang.String DEFAULT_JDBC_USERNAME
protected final org.apache.beam.it.jdbc.JDBCDriverFactory driver
protected final java.lang.String databaseName
protected final java.lang.String username
protected final java.lang.String password
protected AbstractJDBCResourceManager(T container, AbstractJDBCResourceManager.Builder<T> builder)
protected abstract int getJDBCPort()
public java.lang.String getUsername()
JDBCResourceManager
getUsername
in interface JDBCResourceManager
public java.lang.String getPassword()
JDBCResourceManager
getPassword
in interface JDBCResourceManager
public java.lang.String getUri()
JDBCResourceManager
getUri
in interface JDBCResourceManager
public abstract java.lang.String getJDBCPrefix()
public java.lang.String getDatabaseName()
JDBCResourceManager
getDatabaseName
in interface JDBCResourceManager
public boolean createTable(java.lang.String tableName, JDBCResourceManager.JDBCSchema schema)
JDBCResourceManager
createTable
in interface JDBCResourceManager
tableName
- The name of the table.schema
- A JDBCResourceManager.JDBCSchema
object that defines the table.public boolean write(java.lang.String tableName, java.util.List<java.util.Map<java.lang.String,java.lang.Object>> rows) throws JDBCResourceManagerException
JDBCResourceManager.createTable(String, JDBCSchema)
to be called for the target table
beforehand.
The rows map must use the row id as the key, and the values will be inserted into the columns at the row with that id. i.e. {0: [val1, val2, ...], 1: [val1, val2, ...], ...}
write
in interface JDBCResourceManager
tableName
- The name of the table to insert the given rows into.rows
- A map representing the rows to be inserted into the table.JDBCResourceManagerException
- if method is called after resources have been cleaned up,
if the manager object has no dataset, if the table does not exist or if there is an
Exception when attempting to insert the rows.public java.util.List<java.util.Map<java.lang.String,java.lang.Object>> readTable(java.lang.String tableName)
JDBCResourceManager
readTable
in interface JDBCResourceManager
tableName
- The name of the table to read rows from.public java.util.List<java.lang.String> getTableSchema(java.lang.String tableName)
JDBCResourceManager
getTableSchema
in interface JDBCResourceManager
tableName
- the name of the table to fetch the schema of.protected java.lang.String getFirstRow(java.lang.String tableName)
tableName
- the name of the table to query.public java.util.List<java.util.Map<java.lang.String,java.lang.Object>> runSQLQuery(java.lang.String sql)
JDBCResourceManager
runSQLQuery
in interface JDBCResourceManager
sql
- The SQL query to run.public void runSQLUpdate(java.lang.String sql)
JDBCResourceManager
runSQLUpdate
in interface JDBCResourceManager
sql
- The SQL DML statement to run.public long getRowCount(java.lang.String tableName)
JDBCResourceManager
getRowCount
in interface JDBCResourceManager
tableName
- The name of the table.