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 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DBZ-6967: Handle bytea target field with Postgres
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/main/java/io/debezium/connector/jdbc/dialect/postgres/BytesType.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,60 @@ | ||
/* | ||
* 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.postgres; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.sql.Types; | ||
|
||
import org.apache.kafka.connect.data.Schema; | ||
import org.hibernate.engine.jdbc.Size; | ||
import org.hibernate.query.Query; | ||
|
||
import io.debezium.connector.jdbc.dialect.DatabaseDialect; | ||
import io.debezium.connector.jdbc.type.AbstractType; | ||
import io.debezium.connector.jdbc.type.Type; | ||
import io.debezium.connector.jdbc.util.ByteArrayUtils; | ||
|
||
/** | ||
* An implementation of {@link Type} for {@code BYTES} column types. | ||
* | ||
* @author Bertrand Paquet | ||
*/ | ||
class BytesType extends AbstractType { | ||
|
||
public static final BytesType INSTANCE = new BytesType(); | ||
|
||
@Override | ||
public String[] getRegistrationKeys() { | ||
return new String[]{ "BYTES" }; | ||
} | ||
|
||
@Override | ||
public String getDefaultValueBinding(DatabaseDialect dialect, Schema schema, Object value) { | ||
return String.format(dialect.getByteArrayFormat(), ByteArrayUtils.getByteArrayAsHex(value)); | ||
} | ||
|
||
@Override | ||
public String getTypeName(DatabaseDialect dialect, Schema schema, boolean key) { | ||
final int columnSize = Integer.parseInt(getSourceColumnSize(schema).orElse("0")); | ||
if (columnSize > 0) { | ||
return dialect.getTypeName(Types.VARBINARY, Size.length(columnSize)); | ||
} | ||
else if (key) { | ||
return dialect.getTypeName(Types.VARBINARY, Size.length(dialect.getMaxVarbinaryLength())); | ||
} | ||
return dialect.getTypeName(Types.VARBINARY); | ||
} | ||
|
||
@Override | ||
public int bind(Query<?> query, int index, Schema schema, Object value) { | ||
if (value instanceof ByteBuffer) { | ||
value = ((ByteBuffer) value).array(); | ||
} | ||
query.setParameter(index, value); | ||
|
||
return 1; | ||
} | ||
} |
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
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