This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
DBZ-7874 Separation of MariaDB dialect file #77
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
src/main/java/io/debezium/connector/jdbc/dialect/mysql/MariaDbDatabaseDialect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Copyright Debezium Authors. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package io.debezium.connector.jdbc.dialect.mysql; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.hibernate.SessionFactory; | ||
import org.hibernate.StatelessSession; | ||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.dialect.MariaDBDialect; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.debezium.connector.jdbc.JdbcSinkConnectorConfig; | ||
import io.debezium.connector.jdbc.SinkRecordDescriptor; | ||
import io.debezium.connector.jdbc.dialect.DatabaseDialect; | ||
import io.debezium.connector.jdbc.dialect.DatabaseDialectProvider; | ||
import io.debezium.connector.jdbc.dialect.GeneralDatabaseDialect; | ||
import io.debezium.connector.jdbc.dialect.SqlStatementBuilder; | ||
import io.debezium.connector.jdbc.relational.TableDescriptor; | ||
import io.debezium.time.ZonedTimestamp; | ||
import io.debezium.util.Strings; | ||
|
||
/** | ||
* A {@link DatabaseDialect} implementation for MariaDB. | ||
* | ||
*/ | ||
public class MariaDbDatabaseDialect extends GeneralDatabaseDialect { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(MariaDbDatabaseDialect.class); | ||
|
||
private static final List<String> NO_DEFAULT_VALUE_TYPES = Arrays.asList( | ||
"tinytext", "mediumtext", "longtext", "text", "tinyblob", "mediumblob", "longblob"); | ||
|
||
private static final DateTimeFormatter ISO_LOCAL_DATE_TIME_WITH_SPACE = new DateTimeFormatterBuilder() | ||
.parseCaseInsensitive() | ||
.append(DateTimeFormatter.ISO_LOCAL_DATE) | ||
.appendLiteral(' ') | ||
.append(DateTimeFormatter.ISO_LOCAL_TIME) | ||
.toFormatter(); | ||
|
||
public static class MariaDbDatabaseDialectProvider implements DatabaseDialectProvider { | ||
@Override | ||
public boolean supports(Dialect dialect) { | ||
return dialect instanceof MariaDBDialect; | ||
} | ||
|
||
@Override | ||
public Class<?> name() { | ||
return MariaDbDatabaseDialect.class; | ||
} | ||
|
||
@Override | ||
public DatabaseDialect instantiate(JdbcSinkConnectorConfig config, SessionFactory sessionFactory) { | ||
LOGGER.info("MariaDB Dialect instantiated."); | ||
return new MariaDbDatabaseDialect(config, sessionFactory); | ||
} | ||
} | ||
|
||
private final boolean connectionTimeZoneSet; | ||
|
||
private MariaDbDatabaseDialect(JdbcSinkConnectorConfig config, SessionFactory sessionFactory) { | ||
super(config, sessionFactory); | ||
try (StatelessSession session = sessionFactory.openStatelessSession()) { | ||
this.connectionTimeZoneSet = session.doReturningWork(connection -> connection.getMetaData().getURL().contains("connectionTimeZone=")); | ||
} | ||
} | ||
|
||
@Override | ||
protected Optional<String> getDatabaseTimeZoneQuery() { | ||
return Optional.of("SELECT @@global.time_zone, @@session.time_zone"); | ||
} | ||
|
||
@Override | ||
protected String getDatabaseTimeZoneQueryResult(ResultSet rs) throws SQLException { | ||
return rs.getString(1) + " (global), " + rs.getString(2) + " (system)"; | ||
} | ||
|
||
@Override | ||
public boolean isTimeZoneSet() { | ||
return connectionTimeZoneSet || super.isTimeZoneSet(); | ||
} | ||
|
||
@Override | ||
public boolean shouldBindTimeWithTimeZoneAsDatabaseTimeZone() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void registerTypes() { | ||
super.registerTypes(); | ||
|
||
registerType(BooleanType.INSTANCE); | ||
registerType(BitType.INSTANCE); | ||
registerType(BytesType.INSTANCE); | ||
registerType(EnumType.INSTANCE); | ||
registerType(SetType.INSTANCE); | ||
registerType(MediumIntType.INSTANCE); | ||
registerType(IntegerType.INSTANCE); | ||
registerType(TinyIntType.INSTANCE); | ||
registerType(YearType.INSTANCE); | ||
registerType(JsonType.INSTANCE); | ||
registerType(MapToJsonType.INSTANCE); | ||
registerType(GeometryType.INSTANCE); | ||
registerType(PointType.INSTANCE); | ||
registerType(ZonedTimestampType.INSTANCE); | ||
registerType(ZonedTimeType.INSTANCE); | ||
} | ||
|
||
@Override | ||
public int getMaxVarcharLengthInKey() { | ||
return 255; | ||
} | ||
|
||
@Override | ||
public String getFormattedTime(TemporalAccessor value) { | ||
return String.format("'%s'", DateTimeFormatter.ISO_LOCAL_TIME.format(value)); | ||
} | ||
|
||
@Override | ||
public String getFormattedDateTime(TemporalAccessor value) { | ||
return String.format("'%s'", ISO_LOCAL_DATE_TIME_WITH_SPACE.format(value)); | ||
} | ||
|
||
@Override | ||
public String getFormattedTimestamp(TemporalAccessor value) { | ||
return String.format("'%s'", ISO_LOCAL_DATE_TIME_WITH_SPACE.format(value)); | ||
} | ||
|
||
@Override | ||
public String getFormattedTimestampWithTimeZone(String value) { | ||
final ZonedDateTime zonedDateTime = ZonedDateTime.parse(value, ZonedTimestamp.FORMATTER); | ||
return String.format("'%s'", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(zonedDateTime)); | ||
} | ||
|
||
@Override | ||
public String getAlterTablePrefix() { | ||
return "ADD COLUMN ("; | ||
} | ||
|
||
/* | ||
* The use of VALUES() to refer to the new row and columns is deprecated in recent versions of MySQL, | ||
* but not followed by MariaDB yet. | ||
*/ | ||
@Override | ||
public String getUpsertStatement(TableDescriptor table, SinkRecordDescriptor record) { | ||
final SqlStatementBuilder builder = new SqlStatementBuilder(); | ||
builder.append("INSERT INTO "); | ||
builder.append(getQualifiedTableName(table.getId())); | ||
builder.append(" ("); | ||
builder.appendLists(", ", record.getKeyFieldNames(), record.getNonKeyFieldNames(), name -> columnNameFromField(name, record)); | ||
builder.append(") VALUES ("); | ||
builder.appendLists(", ", record.getKeyFieldNames(), record.getNonKeyFieldNames(), name -> columnQueryBindingFromField(name, table, record)); | ||
builder.append(") "); | ||
|
||
final List<String> updateColumnNames = record.getNonKeyFieldNames().isEmpty() | ||
? record.getKeyFieldNames() | ||
: record.getNonKeyFieldNames(); | ||
|
||
builder.append("ON DUPLICATE KEY UPDATE "); | ||
builder.appendList(",", updateColumnNames, name -> { | ||
final String columnName = columnNameFromField(name, record); | ||
return columnName + "=VALUES(" + columnName + ")"; | ||
}); | ||
|
||
return builder.build(); | ||
} | ||
|
||
@Override | ||
protected void addColumnDefaultValue(SinkRecordDescriptor.FieldDescriptor field, StringBuilder columnSpec) { | ||
final String fieldType = field.getTypeName(); | ||
if (!Strings.isNullOrBlank(fieldType)) { | ||
if (NO_DEFAULT_VALUE_TYPES.contains(fieldType.toLowerCase())) { | ||
return; | ||
} | ||
} | ||
super.addColumnDefaultValue(field, columnSpec); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/io.debezium.connector.jdbc.dialect.DatabaseDialectProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be no need for version here as the dependency is/should be defined at BOM lovel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, ok.
Is there anything else to do besides removing the version?