Package io.debezium.jdbc
Class JdbcConnection
- java.lang.Object
-
- io.debezium.jdbc.JdbcConnection
-
- All Implemented Interfaces:
AutoCloseable
@NotThreadSafe public class JdbcConnection extends Object implements AutoCloseable
A utility that simplifies using a JDBC connection and executing transactions composed of multiple statements.- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
JdbcConnection.BlockingMultiResultSetConsumer
static interface
JdbcConnection.BlockingResultSetConsumer
static interface
JdbcConnection.CallPreparer
static interface
JdbcConnection.ConnectionFactory
Establishes JDBC connections.private class
JdbcConnection.ConnectionFactoryDecorator
static interface
JdbcConnection.MultiResultSetConsumer
static interface
JdbcConnection.Operations
Defines multiple JDBC operations.static interface
JdbcConnection.ParameterResultSetConsumer
static interface
JdbcConnection.ResultSetConsumer
static interface
JdbcConnection.ResultSetExtractor<T>
Extracts a data of resultset..static interface
JdbcConnection.ResultSetMapper<T>
static interface
JdbcConnection.StatementFactory
A function to create a statement from a connection.static interface
JdbcConnection.StatementPreparer
-
Field Summary
Fields Modifier and Type Field Description private Configuration
config
private Connection
conn
private JdbcConnection.ConnectionFactory
factory
private JdbcConnection.Operations
initialOps
private static org.slf4j.Logger
LOGGER
private static int
STATEMENT_CACHE_CAPACITY
private static char
STATEMENT_DELIMITER
private Map<String,PreparedStatement>
statementCache
private static int
WAIT_FOR_CLOSE_SECONDS
-
Constructor Summary
Constructors Modifier Constructor Description JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory)
Create a new instance with the given configuration and connection factory.JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations)
Create a new instance with the given configuration and connection factory, and specify the operations that should be run against each newly-established connection.protected
JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations, Consumer<Configuration.Builder> adapter)
Create a new instance with the given configuration and connection factory, and specify the operations that should be run against each newly-established connection.protected
JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations, Consumer<Configuration.Builder> adapter, Supplier<ClassLoader> classLoaderSupplier)
Create a new instance with the given configuration and connection factory, and specify the operations that should be run against each newly-established connection.JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, Supplier<ClassLoader> classLoaderSupplier)
Create a new instance with the given configuration and connection factory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
buildSelectWithRowLimits(TableId tableId, int limit, String projection, Optional<String> condition, String orderBy)
JdbcConnection
call(String sql, JdbcConnection.CallPreparer callPreparer, JdbcConnection.ResultSetConsumer resultSetConsumer)
Execute a stored procedure.private void
cleanupPreparedStatement(PreparedStatement statement)
void
close()
Close the connection and release any resources.private static Field[]
combineVariables(Field[] overriddenVariables, Field... defaultVariables)
JdbcConnection
commit()
JdbcConfiguration
config()
Obtain the configuration for this connection.JdbcConnection
connect()
Ensure a connection to the database is established.Connection
connection()
Connection
connection(boolean executeOnConnect)
String
connectionString(String urlPattern)
Returns a JDBC connection string using the current configuration and url.private PreparedStatement
createPreparedStatement(String preparedQueryString)
String
database()
Returns the database name for this connectionprivate String
delimiter(int columnCount, int[] columnSizes)
private void
doClose()
JdbcConnection
execute(JdbcConnection.Operations operations)
Execute a series of operations as a single transaction.JdbcConnection
execute(String... sqlStatements)
Execute a series of SQL statements as a single transaction.JdbcConnection
executeWithoutCommitting(String... statements)
Executes a series of statements without explicitly committing the connection.private static String
findAndReplace(String url, String name, Properties props, String defaultValue)
private static String
findAndReplace(String url, Properties props, Field... variables)
private int[]
findMaxLength(ResultSet resultSet)
private Map<TableId,List<Column>>
getColumnsDetails(String databaseCatalog, String schemaNamePattern, String tableName, Tables.TableFilter tableFilter, Tables.ColumnNameFilter columnFilter, DatabaseMetaData metadata, Set<TableId> viewIds)
<T extends DatabaseSchema<TableId>>
ObjectgetColumnValue(ResultSet rs, int columnIndex, Column column, Table table, T schema)
Reads a value from JDBC result set and execute per-connector conversion if neededprotected Optional<Object>
getDefaultValue(Column column, String defaultValue)
boolean
isConnected()
protected static boolean
isNullable(int jdbcNullable)
protected boolean
isTableUniqueIndexIncluded(String indexName, String columnName)
Allows the connector implementation to determine if a table's unique index should be include when resolving a table's unique indices.protected List<String>
parseSqlStatementString(String statements)
static JdbcConnection.ConnectionFactory
patternBasedFactory(String urlPattern, Field... variables)
Create aJdbcConnection.ConnectionFactory
that replaces variables in the supplied URL pattern.static JdbcConnection.ConnectionFactory
patternBasedFactory(String urlPattern, String driverClassName, ClassLoader classloader, Field... variables)
Create aJdbcConnection.ConnectionFactory
that uses the specific JDBC driver class loaded with the given class loader, and obtains the connection URL by replacing the following variables in the URL pattern:${hostname}
${port}
${dbname}
${username}
${password}
JdbcConnection
prepareQuery(String[] multiQuery, JdbcConnection.StatementPreparer[] preparers, JdbcConnection.BlockingMultiResultSetConsumer resultConsumer)
Execute multiple SQL prepared queries where each query is executed with the same set of parameters.JdbcConnection
prepareQuery(String[] multiQuery, JdbcConnection.StatementPreparer preparer, JdbcConnection.BlockingMultiResultSetConsumer resultConsumer)
Execute multiple SQL prepared queries where each query is executed with the same set of parameters.JdbcConnection
prepareQuery(String preparedQueryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.ResultSetConsumer resultConsumer)
Execute a SQL prepared query.JdbcConnection
prepareQuery(String preparedQueryString, List<?> parameters, JdbcConnection.ParameterResultSetConsumer resultConsumer)
Execute a SQL prepared query.<T> T
prepareQueryAndMap(String preparedQueryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.ResultSetMapper<T> mapper)
Execute a SQL prepared query and map the result set into an expected type..JdbcConnection
prepareQueryWithBlockingConsumer(String preparedQueryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.BlockingResultSetConsumer resultConsumer)
Execute a SQL prepared query.JdbcConnection
prepareUpdate(String stmt, JdbcConnection.StatementPreparer preparer)
Execute a SQL update via a prepared statement.void
print(ResultSet resultSet)
void
print(ResultSet resultSet, Consumer<String> lines)
private static Properties
propsWithMaskedPassword(Properties props)
JdbcConnection
query(String query, JdbcConnection.ResultSetConsumer resultConsumer)
Execute a SQL query.JdbcConnection
query(String query, JdbcConnection.StatementFactory statementFactory, JdbcConnection.ResultSetConsumer resultConsumer)
Execute a SQL query.<T> T
queryAndMap(String query, JdbcConnection.ResultSetMapper<T> mapper)
Execute a SQL query and map the result set into an expected type.<T> T
queryAndMap(String query, JdbcConnection.StatementFactory statementFactory, JdbcConnection.ResultSetMapper<T> mapper)
Execute a SQL query and map the result set into an expected type.static <T> T
querySingleValue(Connection connection, String queryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.ResultSetExtractor<T> extractor)
JdbcConnection
queryWithBlockingConsumer(String query, JdbcConnection.StatementFactory statementFactory, JdbcConnection.BlockingResultSetConsumer resultConsumer)
String
quotedTableIdString(TableId tableId)
Converts a table id into a string with all components of the id quoted so non-alphanumeric characters are properly handled.Set<String>
readAllCatalogNames()
Get the names of all of the catalogs.Set<String>
readAllSchemaNames(Predicate<String> filter)
Get the names of all of the schemas, optionally applying a filter.Set<TableId>
readAllTableNames(String[] tableTypes)
Get the identifiers of all available tables.List<String>
readPrimaryKeyNames(DatabaseMetaData metadata, TableId id)
protected List<String>
readPrimaryKeyOrUniqueIndexNames(DatabaseMetaData metadata, TableId id)
void
readSchema(Tables tables, String databaseCatalog, String schemaNamePattern, Tables.TableFilter tableFilter, Tables.ColumnNameFilter columnFilter, boolean removeTablesNotFoundInJdbc)
Create definitions for each tables in the database, given the catalog name, schema pattern, table filter, and column filter.protected Optional<ColumnEditor>
readTableColumn(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnFilter)
Returns aColumnEditor
representing the current record of the given result set of column metadata, if included in column.include.list.Set<TableId>
readTableNames(String databaseCatalog, String schemaNamePattern, String tableNamePattern, String[] tableTypes)
Get the identifiers of the tables.PreparedStatement
readTablePreparedStatement(CommonConnectorConfig connectorConfig, String sql, OptionalLong tableSize)
Allow per-connector prepared query creation to override for best database performance depending on the table size.Statement
readTableStatement(CommonConnectorConfig connectorConfig, OptionalLong tableSize)
Allow per-connector query creation to override for best database performance depending on the table size.List<String>
readTableUniqueIndices(DatabaseMetaData metadata, TableId id)
protected int
resolveJdbcType(int metadataJdbcType, int nativeType)
Resolves the supplied metadata JDBC type to a final JDBC type.protected int
resolveNativeType(String typeName)
Provides a native type for the given type name.JdbcConnection
rollback()
<T extends DatabaseSchema<TableId>>
Object[]rowToArray(Table table, T databaseSchema, ResultSet rs, ColumnUtils.ColumnArray columnArray)
Converts aResultSet
row to an array of ObjectsJdbcConnection
setAutoCommit(boolean autoCommit)
<T> JdbcConnection.ResultSetMapper<T>
singleResultMapper(JdbcConnection.ResultSetExtractor<T> extractor, String error)
String[]
tableTypes()
String
username()
Returns the username for this connection
-
-
-
Field Detail
-
WAIT_FOR_CLOSE_SECONDS
private static final int WAIT_FOR_CLOSE_SECONDS
- See Also:
- Constant Field Values
-
STATEMENT_DELIMITER
private static final char STATEMENT_DELIMITER
- See Also:
- Constant Field Values
-
STATEMENT_CACHE_CAPACITY
private static final int STATEMENT_CACHE_CAPACITY
- See Also:
- Constant Field Values
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
statementCache
private final Map<String,PreparedStatement> statementCache
-
config
private final Configuration config
-
factory
private final JdbcConnection.ConnectionFactory factory
-
initialOps
private final JdbcConnection.Operations initialOps
-
conn
private volatile Connection conn
-
-
Constructor Detail
-
JdbcConnection
public JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory)
Create a new instance with the given configuration and connection factory.- Parameters:
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be null
-
JdbcConnection
public JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, Supplier<ClassLoader> classLoaderSupplier)
Create a new instance with the given configuration and connection factory.- Parameters:
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be null
-
JdbcConnection
public JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations)
Create a new instance with the given configuration and connection factory, and specify the operations that should be run against each newly-established connection.- Parameters:
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be nullinitialOperations
- the initial operations that should be run on each new connection; may be null
-
JdbcConnection
protected JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations, Consumer<Configuration.Builder> adapter)
Create a new instance with the given configuration and connection factory, and specify the operations that should be run against each newly-established connection.- Parameters:
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be nullinitialOperations
- the initial operations that should be run on each new connection; may be nulladapter
- the function that can be called to update the configuration with defaults
-
JdbcConnection
protected JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations, Consumer<Configuration.Builder> adapter, Supplier<ClassLoader> classLoaderSupplier)
Create a new instance with the given configuration and connection factory, and specify the operations that should be run against each newly-established connection.- Parameters:
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be nullinitialOperations
- the initial operations that should be run on each new connection; may be nulladapter
- the function that can be called to update the configuration with defaultsclassLoaderSupplier
- class loader supplier
-
-
Method Detail
-
patternBasedFactory
public static JdbcConnection.ConnectionFactory patternBasedFactory(String urlPattern, Field... variables)
Create aJdbcConnection.ConnectionFactory
that replaces variables in the supplied URL pattern. Variables include:${hostname}
${port}
${dbname}
${username}
${password}
- Parameters:
urlPattern
- the URL pattern string; may not be nullvariables
- any custom or overridden configuration variables- Returns:
- the connection factory
-
patternBasedFactory
public static JdbcConnection.ConnectionFactory patternBasedFactory(String urlPattern, String driverClassName, ClassLoader classloader, Field... variables)
Create aJdbcConnection.ConnectionFactory
that uses the specific JDBC driver class loaded with the given class loader, and obtains the connection URL by replacing the following variables in the URL pattern:${hostname}
${port}
${dbname}
${username}
${password}
This method attempts to instantiate the JDBC driver class and use that instance to connect to the database.
- Parameters:
urlPattern
- the URL pattern string; may not be nulldriverClassName
- the name of the JDBC driver class; may not be nullclassloader
- the ClassLoader that should be used to load the JDBC driver class given by `driverClassName`; may be null if this class' class loader should be usedvariables
- any custom or overridden configuration variables- Returns:
- the connection factory
-
propsWithMaskedPassword
private static Properties propsWithMaskedPassword(Properties props)
-
combineVariables
private static Field[] combineVariables(Field[] overriddenVariables, Field... defaultVariables)
-
findAndReplace
private static String findAndReplace(String url, Properties props, Field... variables)
-
findAndReplace
private static String findAndReplace(String url, String name, Properties props, String defaultValue)
-
config
public JdbcConfiguration config()
Obtain the configuration for this connection.- Returns:
- the JDBC configuration; never null
-
setAutoCommit
public JdbcConnection setAutoCommit(boolean autoCommit) throws SQLException
- Throws:
SQLException
-
commit
public JdbcConnection commit() throws SQLException
- Throws:
SQLException
-
rollback
public JdbcConnection rollback() throws SQLException
- Throws:
SQLException
-
connect
public JdbcConnection connect() throws SQLException
Ensure a connection to the database is established.- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database
-
execute
public JdbcConnection execute(String... sqlStatements) throws SQLException
Execute a series of SQL statements as a single transaction.- Parameters:
sqlStatements
- the SQL statements that are to be performed as a single transaction- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
execute
public JdbcConnection execute(JdbcConnection.Operations operations) throws SQLException
Execute a series of operations as a single transaction.- Parameters:
operations
- the function that will be called with a newly-createdStatement
, and that performs one or more operations on that statement object- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements
-
query
public JdbcConnection query(String query, JdbcConnection.ResultSetConsumer resultConsumer) throws SQLException
Execute a SQL query.- Parameters:
query
- the SQL queryresultConsumer
- the consumer of the query results- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
queryAndMap
public <T> T queryAndMap(String query, JdbcConnection.ResultSetMapper<T> mapper) throws SQLException
Execute a SQL query and map the result set into an expected type.- Type Parameters:
T
- type returned by the mapper- Parameters:
query
- the SQL querymapper
- the function processing the query results- Returns:
- the result of the mapper calculation
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
call
public JdbcConnection call(String sql, JdbcConnection.CallPreparer callPreparer, JdbcConnection.ResultSetConsumer resultSetConsumer) throws SQLException
Execute a stored procedure.- Parameters:
sql
- the SQL query; may not benull
callPreparer
- aJdbcConnection.CallPreparer
instance which can be used to set additional parameters; may be nullresultSetConsumer
- aJdbcConnection.ResultSetConsumer
instance which can be used to process the results; may be null- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if anything unexpected fails
-
query
public JdbcConnection query(String query, JdbcConnection.StatementFactory statementFactory, JdbcConnection.ResultSetConsumer resultConsumer) throws SQLException
Execute a SQL query.- Parameters:
query
- the SQL querystatementFactory
- the function that should be used to create the statement from the connection; may not be nullresultConsumer
- the consumer of the query results- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
prepareQuery
public JdbcConnection prepareQuery(String[] multiQuery, JdbcConnection.StatementPreparer preparer, JdbcConnection.BlockingMultiResultSetConsumer resultConsumer) throws SQLException, InterruptedException
Execute multiple SQL prepared queries where each query is executed with the same set of parameters.- Parameters:
multiQuery
- the array of prepared queriespreparer
- the function that supplies arguments to the prepared statement; may not be nullresultConsumer
- the consumer of the query results- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statementsInterruptedException
- See Also:
execute(Operations)
-
prepareQuery
public JdbcConnection prepareQuery(String[] multiQuery, JdbcConnection.StatementPreparer[] preparers, JdbcConnection.BlockingMultiResultSetConsumer resultConsumer) throws SQLException, InterruptedException
Execute multiple SQL prepared queries where each query is executed with the same set of parameters.- Parameters:
multiQuery
- the array of prepared queriespreparers
- the array of functions that supply arguments to the prepared statements; may not be nullresultConsumer
- the consumer of the query results- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statementsInterruptedException
- See Also:
execute(Operations)
-
queryAndMap
public <T> T queryAndMap(String query, JdbcConnection.StatementFactory statementFactory, JdbcConnection.ResultSetMapper<T> mapper) throws SQLException
Execute a SQL query and map the result set into an expected type.- Type Parameters:
T
- type returned by the mapper- Parameters:
query
- the SQL querystatementFactory
- the function that should be used to create the statement from the connection; may not be nullmapper
- the function processing the query results- Returns:
- the result of the mapper calculation
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
queryWithBlockingConsumer
public JdbcConnection queryWithBlockingConsumer(String query, JdbcConnection.StatementFactory statementFactory, JdbcConnection.BlockingResultSetConsumer resultConsumer) throws SQLException, InterruptedException
- Throws:
SQLException
InterruptedException
-
prepareQueryWithBlockingConsumer
public JdbcConnection prepareQueryWithBlockingConsumer(String preparedQueryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.BlockingResultSetConsumer resultConsumer) throws SQLException, InterruptedException
Execute a SQL prepared query.- Parameters:
preparedQueryString
- the prepared query stringpreparer
- the function that supplied arguments to the prepared statement; may not be nullresultConsumer
- the consumer of the query results- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statementsInterruptedException
- See Also:
execute(Operations)
-
prepareQuery
public JdbcConnection prepareQuery(String preparedQueryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.ResultSetConsumer resultConsumer) throws SQLException
Execute a SQL prepared query.- Parameters:
preparedQueryString
- the prepared query stringpreparer
- the function that supplied arguments to the prepared statement; may not be nullresultConsumer
- the consumer of the query results- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
prepareQueryAndMap
public <T> T prepareQueryAndMap(String preparedQueryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.ResultSetMapper<T> mapper) throws SQLException
Execute a SQL prepared query and map the result set into an expected type..- Type Parameters:
T
- type returned by the mapper- Parameters:
preparedQueryString
- the prepared query stringpreparer
- the function that supplied arguments to the prepared statement; may not be nullmapper
- the function processing the query results- Returns:
- the result of the mapper calculation
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
prepareUpdate
public JdbcConnection prepareUpdate(String stmt, JdbcConnection.StatementPreparer preparer) throws SQLException
Execute a SQL update via a prepared statement.- Parameters:
stmt
- the statement stringpreparer
- the function that supplied arguments to the prepared stmt; may be null- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
prepareQuery
public JdbcConnection prepareQuery(String preparedQueryString, List<?> parameters, JdbcConnection.ParameterResultSetConsumer resultConsumer) throws SQLException
Execute a SQL prepared query.- Parameters:
preparedQueryString
- the prepared query stringparameters
- the list of values for parameters in the query; may not be nullresultConsumer
- the consumer of the query results- Returns:
- this object for chaining methods together
- Throws:
SQLException
- if there is an error connecting to the database or executing the statements- See Also:
execute(Operations)
-
print
public void print(ResultSet resultSet)
-
delimiter
private String delimiter(int columnCount, int[] columnSizes)
-
findMaxLength
private int[] findMaxLength(ResultSet resultSet) throws SQLException
- Throws:
SQLException
-
isConnected
public boolean isConnected() throws SQLException
- Throws:
SQLException
-
connection
public Connection connection() throws SQLException
- Throws:
SQLException
-
connection
public Connection connection(boolean executeOnConnect) throws SQLException
- Throws:
SQLException
-
close
public void close() throws SQLException
Close the connection and release any resources.- Specified by:
close
in interfaceAutoCloseable
- Throws:
SQLException
-
doClose
private void doClose() throws SQLException
- Throws:
SQLException
-
readAllCatalogNames
public Set<String> readAllCatalogNames() throws SQLException
Get the names of all of the catalogs.- Returns:
- the set of catalog names; never null but possibly empty
- Throws:
SQLException
- if an error occurs while accessing the database metadata
-
readAllSchemaNames
public Set<String> readAllSchemaNames(Predicate<String> filter) throws SQLException
Get the names of all of the schemas, optionally applying a filter.- Parameters:
filter
- aPredicate
to test each schema name; may be null in which case all schema names are returned- Returns:
- the set of catalog names; never null but possibly empty
- Throws:
SQLException
- if an error occurs while accessing the database metadata
-
tableTypes
public String[] tableTypes() throws SQLException
- Throws:
SQLException
-
readAllTableNames
public Set<TableId> readAllTableNames(String[] tableTypes) throws SQLException
Get the identifiers of all available tables.- Parameters:
tableTypes
- the set of table types to include in the results, which may be null for all table types- Returns:
- the set of
TableId
s; never null but possibly empty - Throws:
SQLException
- if an error occurs while accessing the database metadata
-
readTableNames
public Set<TableId> readTableNames(String databaseCatalog, String schemaNamePattern, String tableNamePattern, String[] tableTypes) throws SQLException
Get the identifiers of the tables.- Parameters:
databaseCatalog
- the name of the catalog, which is typically the database name; may be an empty string for tables that have no catalog, ornull
if the catalog name should not be used to narrow the list of table identifiersschemaNamePattern
- the pattern used to match database schema names, which may be "" to match only those tables with no schema ornull
if the schema name should not be used to narrow the list of table identifierstableNamePattern
- the pattern used to match database table names, which may be null to match all table namestableTypes
- the set of table types to include in the results, which may be null for all table types- Returns:
- the set of
TableId
s; never null but possibly empty - Throws:
SQLException
- if an error occurs while accessing the database metadata
-
connectionString
public String connectionString(String urlPattern)
Returns a JDBC connection string using the current configuration and url.- Parameters:
urlPattern
- aString
representing a JDBC connection with variables that will be replaced- Returns:
- a
String
where the variables inurlPattern
are replaced with values from the configuration
-
username
public String username()
Returns the username for this connection- Returns:
- a
String
, nevernull
-
database
public String database()
Returns the database name for this connection- Returns:
- a
String
, nevernull
-
resolveNativeType
protected int resolveNativeType(String typeName)
Provides a native type for the given type name. There isn't a standard way to obtain this information via JDBC APIs so this method exists to allow database specific information to be set in addition to the JDBC Type.- Parameters:
typeName
- the name of the type whose native type we are looking for- Returns:
- A type constant for the specific database or -1.
-
resolveJdbcType
protected int resolveJdbcType(int metadataJdbcType, int nativeType)
Resolves the supplied metadata JDBC type to a final JDBC type.- Parameters:
metadataJdbcType
- the JDBC type from the underlying driver's metadata lookupnativeType
- the database native type or -1 for unknown- Returns:
- the resolved JDBC type
-
readSchema
public void readSchema(Tables tables, String databaseCatalog, String schemaNamePattern, Tables.TableFilter tableFilter, Tables.ColumnNameFilter columnFilter, boolean removeTablesNotFoundInJdbc) throws SQLException
Create definitions for each tables in the database, given the catalog name, schema pattern, table filter, and column filter.- Parameters:
tables
- the set of table definitions to be modified; may not be nulldatabaseCatalog
- the name of the catalog, which is typically the database name; may be null if all accessible databases are to be processedschemaNamePattern
- the pattern used to match database schema names, which may be "" to match only those tables with no schema or null to process all accessible tables regardless of database schema nametableFilter
- used to determine for which tables are to be processed; may be null if all accessible tables are to be processedcolumnFilter
- used to determine which columns should be included as fields in its table's definition; may be null if all columns for all tables are to be includedremoveTablesNotFoundInJdbc
-true
if this method should remove fromtables
any definitions for tables that are not found in the database metadata, orfalse
if such tables should be left untouched- Throws:
SQLException
- if an error occurs while accessing the database metadata
-
getColumnsDetails
private Map<TableId,List<Column>> getColumnsDetails(String databaseCatalog, String schemaNamePattern, String tableName, Tables.TableFilter tableFilter, Tables.ColumnNameFilter columnFilter, DatabaseMetaData metadata, Set<TableId> viewIds) throws SQLException
- Throws:
SQLException
-
readTableColumn
protected Optional<ColumnEditor> readTableColumn(ResultSet columnMetadata, TableId tableId, Tables.ColumnNameFilter columnFilter) throws SQLException
Returns aColumnEditor
representing the current record of the given result set of column metadata, if included in column.include.list.- Throws:
SQLException
-
readPrimaryKeyNames
public List<String> readPrimaryKeyNames(DatabaseMetaData metadata, TableId id) throws SQLException
- Throws:
SQLException
-
readTableUniqueIndices
public List<String> readTableUniqueIndices(DatabaseMetaData metadata, TableId id) throws SQLException
- Throws:
SQLException
-
readPrimaryKeyOrUniqueIndexNames
protected List<String> readPrimaryKeyOrUniqueIndexNames(DatabaseMetaData metadata, TableId id) throws SQLException
- Throws:
SQLException
-
isTableUniqueIndexIncluded
protected boolean isTableUniqueIndexIncluded(String indexName, String columnName)
Allows the connector implementation to determine if a table's unique index should be include when resolving a table's unique indices.- Parameters:
indexName
- the index namecolumnName
- the column name- Returns:
- true if the index is to be included; false otherwise.
-
cleanupPreparedStatement
private void cleanupPreparedStatement(PreparedStatement statement)
-
createPreparedStatement
private PreparedStatement createPreparedStatement(String preparedQueryString)
-
executeWithoutCommitting
public JdbcConnection executeWithoutCommitting(String... statements) throws SQLException
Executes a series of statements without explicitly committing the connection.- Parameters:
statements
- a series of statements to execute- Returns:
- this object so methods can be chained together; never null
- Throws:
SQLException
- if anything fails
-
isNullable
protected static boolean isNullable(int jdbcNullable)
-
singleResultMapper
public <T> JdbcConnection.ResultSetMapper<T> singleResultMapper(JdbcConnection.ResultSetExtractor<T> extractor, String error) throws SQLException
- Throws:
SQLException
-
querySingleValue
public static <T> T querySingleValue(Connection connection, String queryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.ResultSetExtractor<T> extractor) throws SQLException
- Throws:
SQLException
-
buildSelectWithRowLimits
public String buildSelectWithRowLimits(TableId tableId, int limit, String projection, Optional<String> condition, String orderBy)
-
readTableStatement
public Statement readTableStatement(CommonConnectorConfig connectorConfig, OptionalLong tableSize) throws SQLException
Allow per-connector query creation to override for best database performance depending on the table size.- Throws:
SQLException
-
readTablePreparedStatement
public PreparedStatement readTablePreparedStatement(CommonConnectorConfig connectorConfig, String sql, OptionalLong tableSize) throws SQLException
Allow per-connector prepared query creation to override for best database performance depending on the table size.- Throws:
SQLException
-
getColumnValue
public <T extends DatabaseSchema<TableId>> Object getColumnValue(ResultSet rs, int columnIndex, Column column, Table table, T schema) throws SQLException
Reads a value from JDBC result set and execute per-connector conversion if needed- Throws:
SQLException
-
rowToArray
public <T extends DatabaseSchema<TableId>> Object[] rowToArray(Table table, T databaseSchema, ResultSet rs, ColumnUtils.ColumnArray columnArray) throws SQLException
Converts aResultSet
row to an array of Objects- Throws:
SQLException
-
-