Class GeneralDatabaseDialect

java.lang.Object
io.debezium.connector.jdbc.dialect.GeneralDatabaseDialect
All Implemented Interfaces:
DatabaseDialect
Direct Known Subclasses:
Db2DatabaseDialect, MySqlDatabaseDialect, OracleDatabaseDialect, PostgresDatabaseDialect, SqlServerDatabaseDialect

public class GeneralDatabaseDialect extends Object implements DatabaseDialect
A generalized ANSI92 compliant DatabaseDialect implementation.
Author:
Chris Cranford
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • DATE_FORMATTER

      private static final DateTimeFormatter DATE_FORMATTER
    • connectorConfig

      private final JdbcSinkConnectorConfig connectorConfig
    • dialect

      private final org.hibernate.dialect.Dialect dialect
    • ddlTypeRegistry

      private final org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry ddlTypeRegistry
    • identifierHelper

      private final org.hibernate.engine.jdbc.env.spi.IdentifierHelper identifierHelper
    • columnNamingStrategy

      private final ColumnNamingStrategy columnNamingStrategy
    • typeRegistry

      private final Map<String,Type> typeRegistry
    • typeCoercions

      private final Map<String,String> typeCoercions
    • jdbcTimeZone

      private final boolean jdbcTimeZone
  • Constructor Details

    • GeneralDatabaseDialect

      public GeneralDatabaseDialect(JdbcSinkConnectorConfig config, org.hibernate.SessionFactory sessionFactory)
  • Method Details

    • getTableId

      public TableId getTableId(String tableName)
      Description copied from interface: DatabaseDialect
      Resolves the table id for table name.
      Specified by:
      getTableId in interface DatabaseDialect
      Parameters:
      tableName - the table name.
      Returns:
      the parsed table identifier, never null.
    • tableExists

      public boolean tableExists(Connection connection, TableId tableId) throws SQLException
      Description copied from interface: DatabaseDialect
      Check whether the specified table exists.
      Specified by:
      tableExists in interface DatabaseDialect
      Parameters:
      connection - the database connection to be used, should not be null.
      tableId - the table identifier, should not be null.
      Returns:
      true if the table exists, false otherwise
      Throws:
      SQLException - if a database exception occurs
    • readTable

      public TableDescriptor readTable(Connection connection, TableId tableId) throws SQLException
      Description copied from interface: DatabaseDialect
      Read the table structure data from the database.
      Specified by:
      readTable in interface DatabaseDialect
      Parameters:
      connection - the database connection to be used, should not be null.
      tableId - the table identifier, should not be null.
      Returns:
      the table relational model if it exists
      Throws:
      SQLException - if the table does not exist or a database exception occurs
    • resolveMissingFields

      public Set<String> resolveMissingFields(SinkRecordDescriptor record, TableDescriptor table)
      Description copied from interface: DatabaseDialect
      Resolves what fields are missing from the provided table compared against the incoming record.
      Specified by:
      resolveMissingFields in interface DatabaseDialect
      Parameters:
      record - the current sink record being processed, should not be null
      table - the relational table model, should not be null
      Returns:
      a collection of field names that are missing from the database table, can be empty.
    • resolveColumnName

      protected String resolveColumnName(SinkRecordDescriptor.FieldDescriptor field)
    • getCreateTableStatement

      public String getCreateTableStatement(SinkRecordDescriptor record, TableId tableId)
      Description copied from interface: DatabaseDialect
      Construct a CREATE TABLE statement specific for this dialect based on the provided record.
      Specified by:
      getCreateTableStatement in interface DatabaseDialect
      Parameters:
      record - the current sink record being processed, should not be null
      tableId - the tableidentifier to be used, should not be null
      Returns:
      the create table SQL statement to be executed, never null
    • getAlterTablePrefix

      public String getAlterTablePrefix()
      Description copied from interface: DatabaseDialect
      Gets the prefix used before adding column-clauses in ALTER TABLE statements.
      Specified by:
      getAlterTablePrefix in interface DatabaseDialect
      Returns:
      the alter table column-clauses prefix
    • getAlterTableSuffix

      public String getAlterTableSuffix()
      Description copied from interface: DatabaseDialect
      Gets the suffix used after adding the column-clauses in ALTER TABLE statements.
      Specified by:
      getAlterTableSuffix in interface DatabaseDialect
      Returns:
      the alter table column-clauses suffix
    • getAlterTableColumnPrefix

      public String getAlterTableColumnPrefix()
      Description copied from interface: DatabaseDialect
      Gets the prefix used before adding each column-clause to ALTER TABLE statements.
      Specified by:
      getAlterTableColumnPrefix in interface DatabaseDialect
      Returns:
      the alter table prefix just before each column-clause
    • getAlterTableColumnSuffix

      public String getAlterTableColumnSuffix()
      Description copied from interface: DatabaseDialect
      Gets the suffix used after adding each column-clause to ALTER TABLE statements.
      Specified by:
      getAlterTableColumnSuffix in interface DatabaseDialect
      Returns:
      the alter table suffix just after each column-clause
    • getAlterTableColumnDelimiter

      public String getAlterTableColumnDelimiter()
      Description copied from interface: DatabaseDialect
      Gets the field delimiter used when constructing ALTER TABLE statements.
      Specified by:
      getAlterTableColumnDelimiter in interface DatabaseDialect
      Returns:
      the field delimiter for alter table SQL statement
    • getAlterTableStatement

      public String getAlterTableStatement(TableDescriptor table, SinkRecordDescriptor record, Set<String> missingFields)
      Description copied from interface: DatabaseDialect
      Construct a ALTER TABLE statement specific for this dialect.
      Specified by:
      getAlterTableStatement in interface DatabaseDialect
      Parameters:
      table - the current relational table model, should not be null
      record - the current sink record being processed, should not be null
      missingFields - the fields that have been determined as missing from the relational model, should not be null
      Returns:
      the alter table SQL statement to be executed, never null
    • getInsertStatement

      public String getInsertStatement(TableDescriptor table, SinkRecordDescriptor record)
      Description copied from interface: DatabaseDialect
      Construct a INSERT INTO statement specific for this dialect.
      Specified by:
      getInsertStatement in interface DatabaseDialect
      Parameters:
      table - the current relational table model, should not be null
      record - the current sink record being processed, should not be null
      Returns:
      the insert SQL statement to be executed, never null
    • getUpsertStatement

      public String getUpsertStatement(TableDescriptor table, SinkRecordDescriptor record)
      Description copied from interface: DatabaseDialect
      Construct a UPSERT statement specific for this dialect.
      Specified by:
      getUpsertStatement in interface DatabaseDialect
      Parameters:
      table - the current relational table model, should not be null
      record - the current sink record being processed, should not be null
      Returns:
      the upsert SQL statement to be executed, never null
    • getUpdateStatement

      public String getUpdateStatement(TableDescriptor table, SinkRecordDescriptor record)
      Description copied from interface: DatabaseDialect
      Construct a UPDATE statement specific for this dialect.
      Specified by:
      getUpdateStatement in interface DatabaseDialect
      Parameters:
      table - the current relational table model, should not be null
      record - the current sink record being processed, should not be null
      Returns:
      the update SQL statement to be executed, never null
    • getDeleteStatement

      public String getDeleteStatement(TableDescriptor table, SinkRecordDescriptor record)
      Description copied from interface: DatabaseDialect
      Construct a DELETE statement specific for this dialect.
      Specified by:
      getDeleteStatement in interface DatabaseDialect
      Parameters:
      table - the current relational table model, should not be null
      record - the current sink record being processed, should not be null
      Returns:
      the delete SQL statement to be executed, never null
    • getTruncateStatement

      public String getTruncateStatement(TableDescriptor table)
      Description copied from interface: DatabaseDialect
      Construct a TRUNCATE statement specific for this dialect.
      Specified by:
      getTruncateStatement in interface DatabaseDialect
      Parameters:
      table - the current relational table model, should not be null
      Returns:
      the truncate SQL statement to be executed, never null
    • getQueryBindingWithValueCast

      public String getQueryBindingWithValueCast(ColumnDescriptor column, org.apache.kafka.connect.data.Schema schema, Type type)
      Description copied from interface: DatabaseDialect
      Returns the SQL binding fragment for a column, schema, and type mapping.
      Specified by:
      getQueryBindingWithValueCast in interface DatabaseDialect
      Parameters:
      column - the relational column type, never null
      schema - the field schema type, never null
      type - the resolved field type, never null
      Returns:
      the query binding SQL fragment
    • bindValue

      public int bindValue(SinkRecordDescriptor.FieldDescriptor field, org.hibernate.query.NativeQuery<?> query, int startIndex, Object value)
      Description copied from interface: DatabaseDialect
      Bind the specified value to the query.
      Specified by:
      bindValue in interface DatabaseDialect
      Parameters:
      field - the field being bound, should never be null
      query - the query the value is to be bound, should never be null
      startIndex - the starting index of the parameter binding
      value - the value to be bound, may be null
      Returns:
      the next bind offset that should be used when binding multiple values
    • getMaxVarcharLengthInKey

      public int getMaxVarcharLengthInKey()
      Description copied from interface: DatabaseDialect
      Gets the maximum length of a VARCHAR field in a primary key column.
      Specified by:
      getMaxVarcharLengthInKey in interface DatabaseDialect
      Returns:
      maximum varchar field length when participating in the primary key
    • getMaxNVarcharLengthInKey

      public int getMaxNVarcharLengthInKey()
      Description copied from interface: DatabaseDialect
      Gets the maximum length of a nationalized VARCHAR field in a primary key column.
      Specified by:
      getMaxNVarcharLengthInKey in interface DatabaseDialect
      Returns:
      maximum varchar field length when participating in the primary key
    • getMaxVarbinaryLength

      public int getMaxVarbinaryLength()
      Description copied from interface: DatabaseDialect
      Gets the maximum length of a variable binary field in a primary key column.
      Specified by:
      getMaxVarbinaryLength in interface DatabaseDialect
      Returns:
      maximum field length when participating in the primary key
    • isTimeZoneSet

      public boolean isTimeZoneSet()
      Description copied from interface: DatabaseDialect
      Returns whether the user has specified a time zone JDBC property or whether the connector configuration property database.time_zone has been specified.
      Specified by:
      isTimeZoneSet in interface DatabaseDialect
      Returns:
      true if the properties have been specified; false otherwise.
    • shouldBindTimeWithTimeZoneAsDatabaseTimeZone

      public boolean shouldBindTimeWithTimeZoneAsDatabaseTimeZone()
      Description copied from interface: DatabaseDialect
      Returns whether a time with time zone details be bound using the database time zone.
      Specified by:
      shouldBindTimeWithTimeZoneAsDatabaseTimeZone in interface DatabaseDialect
      Returns:
      true if the value should be shifted; false otherwise (the default).
    • getSchemaType

      public Type getSchemaType(org.apache.kafka.connect.data.Schema schema)
      Description copied from interface: DatabaseDialect
      Resolve the type for a given connect schema.
      Specified by:
      getSchemaType in interface DatabaseDialect
      Parameters:
      schema - connect schema, never null
      Returns:
      resolved type to use
    • getVersion

      public org.hibernate.dialect.DatabaseVersion getVersion()
      Description copied from interface: DatabaseDialect
      Gets the dialect's database version.
      Specified by:
      getVersion in interface DatabaseDialect
      Returns:
      database version details
    • getDefaultDecimalPrecision

      public int getDefaultDecimalPrecision()
      Description copied from interface: DatabaseDialect
      Get the default decimal data type precision for the dialect.
      Specified by:
      getDefaultDecimalPrecision in interface DatabaseDialect
      Returns:
      default decimal precision
    • getDefaultTimestampPrecision

      public int getDefaultTimestampPrecision()
      Description copied from interface: DatabaseDialect
      Get the default timestamp precision for the dialect.
      Specified by:
      getDefaultTimestampPrecision in interface DatabaseDialect
      Returns:
      default timestamp precision
    • isNegativeScaleAllowed

      public boolean isNegativeScaleAllowed()
      Description copied from interface: DatabaseDialect
      Returns whether the dialect permits negative scale.
      Specified by:
      isNegativeScaleAllowed in interface DatabaseDialect
      Returns:
      true if the dialect permits using negative scale values
    • getTypeName

      public String getTypeName(int jdbcType)
      Description copied from interface: DatabaseDialect
      Resolves a JDBC type to a given SQL type name.
      Specified by:
      getTypeName in interface DatabaseDialect
      Parameters:
      jdbcType - the JDBC type
      Returns:
      the resolved type name
    • getTypeName

      public String getTypeName(int jdbcType, org.hibernate.engine.jdbc.Size size)
      Description copied from interface: DatabaseDialect
      Resolves a JDBC type with optional size parameters to a given SQL type name.
      Specified by:
      getTypeName in interface DatabaseDialect
      Parameters:
      jdbcType - the JDBC type
      size - the optional size parameters, should not be null
      Returns:
      the resolved type name
    • getByteArrayFormat

      public String getByteArrayFormat()
      Description copied from interface: DatabaseDialect
      Returns the default format for binding a byte array
      Specified by:
      getByteArrayFormat in interface DatabaseDialect
      Returns:
      the format for binding a byte array
    • getFormattedBoolean

      public String getFormattedBoolean(boolean value)
      Description copied from interface: DatabaseDialect
      Format a boolean.
      Specified by:
      getFormattedBoolean in interface DatabaseDialect
      Parameters:
      value - the boolean value
      Returns:
      the formatted string value
    • getFormattedDate

      public String getFormattedDate(TemporalAccessor value)
      Description copied from interface: DatabaseDialect
      Format a date.
      Specified by:
      getFormattedDate in interface DatabaseDialect
      Parameters:
      value - the value to tbe formatted, never null
      Returns:
      the formatted string value
    • getFormattedTime

      public String getFormattedTime(TemporalAccessor value)
      Description copied from interface: DatabaseDialect
      Format a time.
      Specified by:
      getFormattedTime in interface DatabaseDialect
      Parameters:
      value - the value to be formatted, never null
      Returns:
      the formatted string value
    • getFormattedTimeWithTimeZone

      public String getFormattedTimeWithTimeZone(String value)
      Description copied from interface: DatabaseDialect
      Format a time with time zone.
      Specified by:
      getFormattedTimeWithTimeZone in interface DatabaseDialect
      Parameters:
      value - the value to be formatted, never null
      Returns:
      the formatted string value
    • getFormattedDateTime

      public String getFormattedDateTime(TemporalAccessor value)
      Description copied from interface: DatabaseDialect
      Format a date and time.
      Specified by:
      getFormattedDateTime in interface DatabaseDialect
      Parameters:
      value - the value to be formatted, never null
      Returns:
      the formatted string value
    • getFormattedDateTimeWithNanos

      public String getFormattedDateTimeWithNanos(TemporalAccessor value)
      Description copied from interface: DatabaseDialect
      Format a date and time with nonoseconds.
      Specified by:
      getFormattedDateTimeWithNanos in interface DatabaseDialect
      Parameters:
      value - the value to be formatted, never null
      Returns:
      the formatted string value
    • getFormattedTimestamp

      public String getFormattedTimestamp(TemporalAccessor value)
      Description copied from interface: DatabaseDialect
      Format a timestamp.
      Specified by:
      getFormattedTimestamp in interface DatabaseDialect
      Parameters:
      value - the value to be formatted, never null
      Returns:
      the formatted string value
    • getFormattedTimestampWithTimeZone

      public String getFormattedTimestampWithTimeZone(String value)
      Description copied from interface: DatabaseDialect
      Format a timestamp with time zone.
      Specified by:
      getFormattedTimestampWithTimeZone in interface DatabaseDialect
      Parameters:
      value - the value to be formatted, never null
      Returns:
      the formatted string value.
    • getTypeName

      protected String getTypeName(int jdbcType, int length)
    • getDatabaseTimeZone

      protected String getDatabaseTimeZone(org.hibernate.SessionFactory sessionFactory)
    • getDatabaseTimeZoneQuery

      protected Optional<String> getDatabaseTimeZoneQuery()
    • getDatabaseTimeZoneQueryResult

      protected String getDatabaseTimeZoneQueryResult(ResultSet rs) throws SQLException
      Throws:
      SQLException
    • registerTypes

      protected void registerTypes()
    • registerType

      protected void registerType(Type type)
    • getColumnNamingStrategy

      protected ColumnNamingStrategy getColumnNamingStrategy()
    • getConfig

      protected JdbcSinkConnectorConfig getConfig()
    • getDatabaseVersion

      protected org.hibernate.dialect.DatabaseVersion getDatabaseVersion()
    • getIdentifierHelper

      protected org.hibernate.engine.jdbc.env.spi.IdentifierHelper getIdentifierHelper()
    • addColumnDefaultValue

      protected void addColumnDefaultValue(SinkRecordDescriptor.FieldDescriptor field, StringBuilder columnSpec)
    • columnQueryBindingFromField

      protected String columnQueryBindingFromField(String fieldName, TableDescriptor table, SinkRecordDescriptor record)
    • getColumnValueFromKeyField

      private Object getColumnValueFromKeyField(String fieldName, SinkRecordDescriptor record, String columnName)
    • getColumnValueFromValueField

      private Object getColumnValueFromValueField(String fieldName, SinkRecordDescriptor record)
    • getColumnValueForKafkaKeyMode

      private Object getColumnValueForKafkaKeyMode(String columnName, SinkRecordDescriptor record)
    • columnNameFromField

      protected String columnNameFromField(String fieldName, SinkRecordDescriptor record)
    • columnNameFromField

      protected String columnNameFromField(String fieldName, String prefix, SinkRecordDescriptor record)
    • toIdentifier

      protected String toIdentifier(String text)
    • toIdentifier

      protected String toIdentifier(TableId tableId)
    • resolveColumnNameFromField

      protected String resolveColumnNameFromField(String fieldName)
    • isIdentifierUppercaseWhenNotQuoted

      protected boolean isIdentifierUppercaseWhenNotQuoted()
    • getQualifiedTableName

      protected String getQualifiedTableName(TableId tableId)
    • columnNameEqualsBinding

      private String columnNameEqualsBinding(String fieldName, TableDescriptor table, SinkRecordDescriptor record)
    • isColumnNullable

      private static boolean isColumnNullable(String columnName, Collection<String> primaryKeyColumnNames, int nullability)
    • unwrapSessionFactory

      private static org.hibernate.engine.spi.SessionFactoryImplementor unwrapSessionFactory(org.hibernate.SessionFactory sessionFactory)