Search in sources :

Example 1 with PostgresType

use of io.debezium.connector.postgresql.PostgresType in project debezium by debezium.

the class Wal2JsonReplicationMessage method transform.

private List<ReplicationMessage.Column> transform(final Document data, final String nameField, final String typeField, final String valueField, final String optionalsField) {
    final Array columnNames = data.getArray(nameField);
    final Array columnTypes = data.getArray(typeField);
    final Array columnValues = data.getArray(valueField);
    final Array columnOptionals = data.getArray(optionalsField);
    if (columnNames.size() != columnTypes.size() || columnNames.size() != columnValues.size()) {
        throw new ConnectException("Column related arrays do not have the same size");
    }
    final List<ReplicationMessage.Column> columns = new ArrayList<>(columnNames.size());
    for (int i = 0; i < columnNames.size(); i++) {
        final String columnName = columnNames.get(i).asString();
        final String columnTypeName = columnTypes.get(i).asString();
        final boolean columnOptional = columnOptionals != null ? columnOptionals.get(i).asBoolean() : false;
        final Value rawValue = columnValues.get(i);
        final PostgresType columnType = typeRegistry.get(parseType(columnName, columnTypeName));
        columns.add(new AbstractReplicationMessageColumn(columnName, columnType, columnTypeName, columnOptional, true) {

            @Override
            public Object getValue(PgConnectionSupplier connection, boolean includeUnknownDatatypes) {
                return Wal2JsonReplicationMessage.this.getValue(columnName, columnType, columnTypeName, rawValue, connection, includeUnknownDatatypes);
            }
        });
    }
    return columns;
}
Also used : AbstractReplicationMessageColumn(io.debezium.connector.postgresql.connection.AbstractReplicationMessageColumn) ArrayList(java.util.ArrayList) PGpoint(org.postgresql.geometric.PGpoint) Array(io.debezium.document.Array) PgArray(org.postgresql.jdbc.PgArray) PostgresType(io.debezium.connector.postgresql.PostgresType) AbstractReplicationMessageColumn(io.debezium.connector.postgresql.connection.AbstractReplicationMessageColumn) Value(io.debezium.document.Value) PgConnectionSupplier(io.debezium.connector.postgresql.RecordsStreamProducer.PgConnectionSupplier) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 2 with PostgresType

use of io.debezium.connector.postgresql.PostgresType in project debezium by debezium.

the class PostgresConnection method initTypeRegistry.

private static TypeRegistry initTypeRegistry(Connection db, Map<String, Integer> nameToJdbc) {
    final TypeInfo typeInfo = ((BaseConnection) db).getTypeInfo();
    TypeRegistry.Builder typeRegistryBuilder = TypeRegistry.create(typeInfo);
    try {
        try (final Statement statement = db.createStatement()) {
            // Read non-array types
            try (final ResultSet rs = statement.executeQuery(SQL_NON_ARRAY_TYPES)) {
                while (rs.next()) {
                    final int oid = rs.getInt("oid");
                    typeRegistryBuilder.addType(new PostgresType(rs.getString("name"), oid, nameToJdbc.get(rs.getString("name")), typeInfo));
                }
            }
            // Read array types
            try (final ResultSet rs = statement.executeQuery(SQL_ARRAY_TYPES)) {
                while (rs.next()) {
                    // int2vector and oidvector will not be treated as arrays
                    final int oid = rs.getInt("oid");
                    typeRegistryBuilder.addType(new PostgresType(rs.getString("name"), oid, nameToJdbc.get(rs.getString("name")), typeInfo, typeRegistryBuilder.get(rs.getInt("element"))));
                }
            }
        }
    } catch (SQLException e) {
        throw new ConnectException("Could not intialize type registry", e);
    }
    return typeRegistryBuilder.build();
}
Also used : PostgresType(io.debezium.connector.postgresql.PostgresType) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) TypeInfo(org.postgresql.core.TypeInfo) TypeRegistry(io.debezium.connector.postgresql.TypeRegistry) BaseConnection(org.postgresql.core.BaseConnection) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

PostgresType (io.debezium.connector.postgresql.PostgresType)2 ConnectException (org.apache.kafka.connect.errors.ConnectException)2 PgConnectionSupplier (io.debezium.connector.postgresql.RecordsStreamProducer.PgConnectionSupplier)1 TypeRegistry (io.debezium.connector.postgresql.TypeRegistry)1 AbstractReplicationMessageColumn (io.debezium.connector.postgresql.connection.AbstractReplicationMessageColumn)1 Array (io.debezium.document.Array)1 Value (io.debezium.document.Value)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 BaseConnection (org.postgresql.core.BaseConnection)1 TypeInfo (org.postgresql.core.TypeInfo)1 PGpoint (org.postgresql.geometric.PGpoint)1 PgArray (org.postgresql.jdbc.PgArray)1