Package io.debezium.connector.sqlserver
Class SqlServerConnection
- java.lang.Object
-
- io.debezium.jdbc.JdbcConnection
-
- io.debezium.connector.sqlserver.SqlServerConnection
-
- All Implemented Interfaces:
AutoCloseable
public class SqlServerConnection extends JdbcConnection
JdbcConnectionextension to be used with Microsoft SQL Server- Author:
- Horia Chiorean (hchiorea@redhat.com), Jiri Pechanec
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSqlServerConnection.CdcEnabledTable-
Nested classes/interfaces inherited from class io.debezium.jdbc.JdbcConnection
JdbcConnection.BlockingMultiResultSetConsumer, JdbcConnection.BlockingResultSetConsumer, JdbcConnection.CallPreparer, JdbcConnection.ConnectionFactory, JdbcConnection.MultiResultSetConsumer, JdbcConnection.Operations, JdbcConnection.ParameterResultSetConsumer, JdbcConnection.ResultSetConsumer, JdbcConnection.ResultSetExtractor<T extends Object>, JdbcConnection.ResultSetMapper<T extends Object>, JdbcConnection.StatementFactory, JdbcConnection.StatementPreparer
-
-
Field Summary
Fields Modifier and Type Field Description private static StringCLOSING_QUOTING_CHARACTERprivate static StringDATABASE_NAME_PLACEHOLDERprivate SqlServerDefaultValueConverterdefaultValueConverterprivate Stringget_all_changes_for_tableprivate static StringGET_ALL_CHANGES_FOR_TABLEprivate static StringGET_CAPTURED_COLUMNSQueries the list of captured column names and their change table identifiers in the given database.private static StringGET_CHANGE_TABLESQueries the list of capture instances in the given database.private static StringGET_DATABASE_NAMEprivate static StringGET_MAX_LSNprivate static StringGET_MAX_TRANSACTION_LSNprivate static StringGET_MIN_LSNprivate static StringGET_NEW_CHANGE_TABLESprivate static StringGET_NTH_TRANSACTION_LSN_FROM_BEGINNINGprivate static StringGET_NTH_TRANSACTION_LSN_FROM_LASTprivate StringgetAllChangesForTableprivate static StringINCREMENT_LSNstatic StringINSTANCE_NAMEprivate static StringLOCK_TABLEprivate static org.slf4j.LoggerLOGGERprotected static StringLSN_TIMESTAMP_SELECT_STATEMENTprivate booleanmultiPartitionModeprivate static StringOPENING_QUOTING_CHARACTERprivate booleanoptionRecompileprivate intqueryFetchSizestatic StringSERVER_TIMEZONE_PROP_NAMEDeprecated.The connector will determine the database server timezone offset automatically.private static StringSTATEMENTS_PLACEHOLDERprivate static StringURL_PATTERN
-
Constructor Summary
Constructors Constructor Description SqlServerConnection(Configuration config, SourceTimestampMode sourceTimestampMode, SqlServerValueConverters valueConverters, Supplier<ClassLoader> classLoaderSupplier, Set<Envelope.Operation> skippedOperations, boolean multiPartitionMode)Creates a new connection using the supplied configuration.SqlServerConnection(Configuration config, SourceTimestampMode sourceTimestampMode, SqlServerValueConverters valueConverters, Supplier<ClassLoader> classLoaderSupplier, Set<Envelope.Operation> skippedOperations, boolean multiPartitionMode, boolean optionRecompile)Creates a new connection using the supplied configuration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringbuildSelectWithRowLimits(TableId tableId, int limit, String projection, Optional<String> condition, String orderBy)private StringcdcNameForTable(TableId tableId)Connectionconnection(boolean executeOnConnect)StringconnectionString()Returns a JDBC connection string for the current configuration.private static JdbcConnection.ConnectionFactorycreateConnectionFactory(boolean multiPartitionMode)private static StringcreateUrlPattern(boolean multiPartitionMode)voidgetChangesForTables(String databaseName, SqlServerChangeTable[] changeTables, Lsn intervalFromLsn, Lsn intervalToLsn, JdbcConnection.BlockingMultiResultSetConsumer consumer)Provides all changes recorder by the SQL Server CDC capture process for a set of tables.List<SqlServerChangeTable>getChangeTables(String databaseName)List<SqlServerChangeTable>getChangeTables(String databaseName, Lsn toLsn)<T extends DatabaseSchema<TableId>>
ObjectgetColumnValue(ResultSet rs, int columnIndex, Column column, Table table, T schema)SqlServerDefaultValueConvertergetDefaultValueConverter()private LsngetFromLsn(String databaseName, SqlServerChangeTable changeTable, Lsn intervalFromLsn)LsngetMaxLsn(String databaseName)LsngetMaxTransactionLsn(String databaseName)LsngetMinLsn(String databaseName, String changeTableName)StringgetNameOfChangeTable(String captureName)List<SqlServerChangeTable>getNewChangeTables(String databaseName, Lsn fromLsn, Lsn toLsn)LsngetNthTransactionLsnFromBeginning(String databaseName, int maxOffset)LsngetNthTransactionLsnFromLast(String databaseName, Lsn lastLsn, int maxOffset)TablegetTableSchemaFromTable(String databaseName, SqlServerChangeTable changeTable)LsnincrementLsn(String databaseName, Lsn lsn)Obtain the next available position in the database log.protected booleanisTableUniqueIndexIncluded(String indexName, String columnName)voidlockTable(TableId tableId)Creates an exclusive lock for a given table.StringquotedTableIdString(TableId tableId)private StringreplaceDatabaseNamePlaceholder(String sql, String databaseName)StringretrieveRealDatabaseName(String databaseName)Retrieve the name of the database in the original case as it's defined on the server.-
Methods inherited from class io.debezium.jdbc.JdbcConnection
call, close, commit, config, connect, connection, connectionString, database, execute, execute, executeWithoutCommitting, isConnected, isNullable, isValid, overrideColumn, parseSqlStatementString, patternBasedFactory, patternBasedFactory, prepareQuery, prepareQuery, prepareQuery, prepareQuery, prepareQuery, prepareQueryAndMap, prepareQueryWithBlockingConsumer, prepareUpdate, print, print, query, query, queryAndMap, queryAndMap, querySingleValue, queryWithBlockingConsumer, quotedColumnIdString, readAllCatalogNames, readAllSchemaNames, readAllTableNames, readPrimaryKeyNames, readPrimaryKeyOrUniqueIndexNames, readSchema, readTableColumn, readTableNames, readTablePreparedStatement, readTableStatement, readTableUniqueIndices, resolveCatalogName, resolveJdbcType, resolveNativeType, rollback, rowToArray, setAutoCommit, singleResultMapper, tableTypes, username
-
-
-
-
Field Detail
-
SERVER_TIMEZONE_PROP_NAME
@Deprecated public static final String SERVER_TIMEZONE_PROP_NAME
Deprecated.The connector will determine the database server timezone offset automatically.- See Also:
- Constant Field Values
-
INSTANCE_NAME
public static final String INSTANCE_NAME
- See Also:
- Constant Field Values
-
GET_DATABASE_NAME
private static final String GET_DATABASE_NAME
- See Also:
- Constant Field Values
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
STATEMENTS_PLACEHOLDER
private static final String STATEMENTS_PLACEHOLDER
- See Also:
- Constant Field Values
-
DATABASE_NAME_PLACEHOLDER
private static final String DATABASE_NAME_PLACEHOLDER
- See Also:
- Constant Field Values
-
GET_MAX_LSN
private static final String GET_MAX_LSN
- See Also:
- Constant Field Values
-
GET_MAX_TRANSACTION_LSN
private static final String GET_MAX_TRANSACTION_LSN
- See Also:
- Constant Field Values
-
GET_NTH_TRANSACTION_LSN_FROM_BEGINNING
private static final String GET_NTH_TRANSACTION_LSN_FROM_BEGINNING
- See Also:
- Constant Field Values
-
GET_NTH_TRANSACTION_LSN_FROM_LAST
private static final String GET_NTH_TRANSACTION_LSN_FROM_LAST
- See Also:
- Constant Field Values
-
GET_MIN_LSN
private static final String GET_MIN_LSN
- See Also:
- Constant Field Values
-
LOCK_TABLE
private static final String LOCK_TABLE
- See Also:
- Constant Field Values
-
INCREMENT_LSN
private static final String INCREMENT_LSN
- See Also:
- Constant Field Values
-
GET_ALL_CHANGES_FOR_TABLE
private static final String GET_ALL_CHANGES_FOR_TABLE
- See Also:
- Constant Field Values
-
get_all_changes_for_table
private final String get_all_changes_for_table
-
LSN_TIMESTAMP_SELECT_STATEMENT
protected static final String LSN_TIMESTAMP_SELECT_STATEMENT
- See Also:
- Constant Field Values
-
GET_CAPTURED_COLUMNS
private static final String GET_CAPTURED_COLUMNS
Queries the list of captured column names and their change table identifiers in the given database.- See Also:
- Constant Field Values
-
GET_CHANGE_TABLES
private static final String GET_CHANGE_TABLES
Queries the list of capture instances in the given database. If two or more capture instances with the same start LSN are available for a given source table, only the newest one will be returned. We use a query instead ofsys.sp_cdc_help_change_data_capturebecause: 1. The stored procedure doesn't allow filtering capture instances by start LSN. 2. There is no way to use the result returned by a stored procedure in a query.- See Also:
- Constant Field Values
-
GET_NEW_CHANGE_TABLES
private static final String GET_NEW_CHANGE_TABLES
- See Also:
- Constant Field Values
-
OPENING_QUOTING_CHARACTER
private static final String OPENING_QUOTING_CHARACTER
- See Also:
- Constant Field Values
-
CLOSING_QUOTING_CHARACTER
private static final String CLOSING_QUOTING_CHARACTER
- See Also:
- Constant Field Values
-
URL_PATTERN
private static final String URL_PATTERN
-
multiPartitionMode
private final boolean multiPartitionMode
-
getAllChangesForTable
private final String getAllChangesForTable
-
queryFetchSize
private final int queryFetchSize
-
defaultValueConverter
private final SqlServerDefaultValueConverter defaultValueConverter
-
optionRecompile
private boolean optionRecompile
-
-
Constructor Detail
-
SqlServerConnection
public SqlServerConnection(Configuration config, SourceTimestampMode sourceTimestampMode, SqlServerValueConverters valueConverters, Supplier<ClassLoader> classLoaderSupplier, Set<Envelope.Operation> skippedOperations, boolean multiPartitionMode)
Creates a new connection using the supplied configuration.- Parameters:
config-Configurationinstance, may not be null.sourceTimestampMode- strategy for populatingsource.ts_ms.valueConverters-SqlServerValueConvertersinstanceclassLoaderSupplier- class loader supplierskippedOperations- a set ofEnvelope.Operationto skip in streaming
-
SqlServerConnection
public SqlServerConnection(Configuration config, SourceTimestampMode sourceTimestampMode, SqlServerValueConverters valueConverters, Supplier<ClassLoader> classLoaderSupplier, Set<Envelope.Operation> skippedOperations, boolean multiPartitionMode, boolean optionRecompile)
Creates a new connection using the supplied configuration.- Parameters:
config-Configurationinstance, may not be null.sourceTimestampMode- strategy for populatingsource.ts_ms.valueConverters-SqlServerValueConvertersinstanceclassLoaderSupplier- class loader supplierskippedOperations- a set ofEnvelope.Operationto skip in streamingoptionRecompile- Includes query option RECOMPILE on incremental snapshots
-
-
Method Detail
-
createUrlPattern
private static String createUrlPattern(boolean multiPartitionMode)
-
createConnectionFactory
private static JdbcConnection.ConnectionFactory createConnectionFactory(boolean multiPartitionMode)
-
connectionString
public String connectionString()
Returns a JDBC connection string for the current configuration.- Returns:
- a
Stringwhere the variables inurlPatternare replaced with values from the configuration
-
connection
public Connection connection(boolean executeOnConnect) throws SQLException
- Overrides:
connectionin classJdbcConnection- Throws:
SQLException
-
getMaxLsn
public Lsn getMaxLsn(String databaseName) throws SQLException
- Returns:
- the current largest log sequence number
- Throws:
SQLException
-
getNthTransactionLsnFromBeginning
public Lsn getNthTransactionLsnFromBeginning(String databaseName, int maxOffset) throws SQLException
- Returns:
- the log sequence number of the most recent transaction
that isn't further than
maxOffsetfrom the beginning. - Throws:
SQLException
-
getNthTransactionLsnFromLast
public Lsn getNthTransactionLsnFromLast(String databaseName, Lsn lastLsn, int maxOffset) throws SQLException
- Returns:
- the log sequence number of the most recent transaction
that isn't further than
maxOffsetfromlastLsn. - Throws:
SQLException
-
getMaxTransactionLsn
public Lsn getMaxTransactionLsn(String databaseName) throws SQLException
- Returns:
- the log sequence number of the most recent transaction.
- Throws:
SQLException
-
getMinLsn
public Lsn getMinLsn(String databaseName, String changeTableName) throws SQLException
- Returns:
- the smallest log sequence number of table
- Throws:
SQLException
-
getChangesForTables
public void getChangesForTables(String databaseName, SqlServerChangeTable[] changeTables, Lsn intervalFromLsn, Lsn intervalToLsn, JdbcConnection.BlockingMultiResultSetConsumer consumer) throws SQLException, InterruptedException
Provides all changes recorder by the SQL Server CDC capture process for a set of tables.- Parameters:
databaseName- - the name of the database to querychangeTables- - the requested tables to obtain changes forintervalFromLsn- - closed lower bound of interval of changes to be providedintervalToLsn- - closed upper bound of interval of changes to be providedconsumer- - the change processor- Throws:
SQLExceptionInterruptedException
-
getFromLsn
private Lsn getFromLsn(String databaseName, SqlServerChangeTable changeTable, Lsn intervalFromLsn) throws SQLException
- Throws:
SQLException
-
incrementLsn
public Lsn incrementLsn(String databaseName, Lsn lsn) throws SQLException
Obtain the next available position in the database log.- Parameters:
databaseName- - the name of the database that the LSN belongs tolsn- - LSN of the current position- Returns:
- LSN of the next position in the database
- Throws:
SQLException
-
lockTable
public void lockTable(TableId tableId) throws SQLException
Creates an exclusive lock for a given table.- Parameters:
tableId- to be locked- Throws:
SQLException
-
getChangeTables
public List<SqlServerChangeTable> getChangeTables(String databaseName) throws SQLException
- Throws:
SQLException
-
getChangeTables
public List<SqlServerChangeTable> getChangeTables(String databaseName, Lsn toLsn) throws SQLException
- Throws:
SQLException
-
getNewChangeTables
public List<SqlServerChangeTable> getNewChangeTables(String databaseName, Lsn fromLsn, Lsn toLsn) throws SQLException
- Throws:
SQLException
-
getTableSchemaFromTable
public Table getTableSchemaFromTable(String databaseName, SqlServerChangeTable changeTable) throws SQLException
- Throws:
SQLException
-
retrieveRealDatabaseName
public String retrieveRealDatabaseName(String databaseName)
Retrieve the name of the database in the original case as it's defined on the server. Although SQL Server supports case-insensitive collations, the connector uses the database name to build the produced records' source info and, subsequently, the keys of its committed offset messages. This value must remain the same during the lifetime of the connector regardless of the case used in the connector configuration.
-
isTableUniqueIndexIncluded
protected boolean isTableUniqueIndexIncluded(String indexName, String columnName)
- Overrides:
isTableUniqueIndexIncludedin classJdbcConnection
-
getColumnValue
public <T extends DatabaseSchema<TableId>> Object getColumnValue(ResultSet rs, int columnIndex, Column column, Table table, T schema) throws SQLException
- Overrides:
getColumnValuein classJdbcConnection- Throws:
SQLException
-
buildSelectWithRowLimits
public String buildSelectWithRowLimits(TableId tableId, int limit, String projection, Optional<String> condition, String orderBy)
- Overrides:
buildSelectWithRowLimitsin classJdbcConnection
-
quotedTableIdString
public String quotedTableIdString(TableId tableId)
- Overrides:
quotedTableIdStringin classJdbcConnection
-
replaceDatabaseNamePlaceholder
private String replaceDatabaseNamePlaceholder(String sql, String databaseName)
-
getDefaultValueConverter
public SqlServerDefaultValueConverter getDefaultValueConverter()
-
-