@Before public void beforeEach() { editor = Column.editor(); column = null; }
@Override public void enterAlterByAddColumns(MySqlParser.AlterByAddColumnsContext ctx) { // multiple columns are added. Initialize a list of column editors for them parser.runIfNotNull(() -> { columnEditors = new ArrayList<>(ctx.uid().size()); for (MySqlParser.UidContext uidContext : ctx.uid()) { String columnName = parser.parseName(uidContext); columnEditors.add(Column.editor().name(columnName)); } columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, columnEditors.get(0), parser.dataTypeResolver(), parser.getConverters()); listeners.add(columnDefinitionListener); }, tableEditor); super.enterAlterByAddColumns(ctx); }
protected Column createColumnFromConstant(String columnName, String constantValue) { ColumnEditor column = Column.editor().name(columnName); try { if (constantValue.startsWith("'") || constantValue.startsWith("\"")) { column.type("CHAR"); column.jdbcType(Types.CHAR); column.length(constantValue.length() - 2); } else if (constantValue.equalsIgnoreCase("TRUE") || constantValue.equalsIgnoreCase("FALSE")) { column.type("BOOLEAN"); column.jdbcType(Types.BOOLEAN); } else { setTypeInfoForConstant(constantValue, column); } } catch (Throwable t) { logger.debug("Unable to create an artificial column for the constant: " + constantValue); } return column.create(); }
/** * Determine the column definitions for the supplied result set and add each column to the specified consumer. * * @param resultSet the query result set; may not be null * @param consumer the consumer of the definitions; may not be null * @throws SQLException if an error occurs while using the result set */ public static void columnsFor(ResultSet resultSet, Consumer<Column> consumer) throws SQLException { ResultSetMetaData metadata = resultSet.getMetaData(); ColumnEditor column = Column.editor(); for (int position = 1; position <= metadata.getColumnCount(); ++position) { String columnLabel = metadata.getColumnLabel(position); column.name(columnLabel != null ? columnLabel : metadata.getColumnName(position)); column.type(metadata.getColumnTypeName(position)); column.jdbcType(metadata.getColumnType(position)); column.length(metadata.getPrecision(position)); column.scale(metadata.getScale(position)); column.optional(isNullable(metadata.isNullable(position))); column.autoIncremented(metadata.isAutoIncrement(position)); column.generated(false); consumer.accept(column.create()); } }
/** * Returns a {@link ColumnEditor} representing the current record of the given result set of column metadata, if * included in the column whitelist. */ protected Optional<ColumnEditor> readTableColumn(ResultSet columnMetadata, TableId tableId, ColumnNameFilter columnFilter) throws SQLException { final String columnName = columnMetadata.getString(4); if (columnFilter == null || columnFilter.matches(tableId.catalog(), tableId.schema(), tableId.table(), columnName)) { final ColumnEditor column = Column.editor().name(columnName); column.jdbcType(columnMetadata.getInt(5)); column.type(columnMetadata.getString(6)); column.length(columnMetadata.getInt(7)); if (columnMetadata.getObject(9) != null) { column.scale(columnMetadata.getInt(9)); } column.optional(isNullable(columnMetadata.getInt(11))); column.position(columnMetadata.getInt(17)); column.autoIncremented("YES".equalsIgnoreCase(columnMetadata.getString(23))); String autogenerated = null; try { autogenerated = columnMetadata.getString(24); } catch (SQLException e) { // ignore, some drivers don't have this index - e.g. Postgres } column.generated("YES".equalsIgnoreCase(autogenerated)); column.nativeType(resolveNativeType(column.typeName())); return Optional.of(column); } return Optional.empty(); }
@Before public void beforeEach() { editor = Table.editor(); table = null; columnEditor = Column.editor(); }
@Override public void enterCreateView(MySqlParser.CreateViewContext ctx) { if (!parser.skipViews()) { tableEditor = parser.databaseTables().editOrCreateTable(parser.parseQualifiedTableId(ctx.fullId())); // create new columns just with specified name for now if (ctx.uidList() != null) { ctx.uidList().uid().stream().map(parser::parseName).forEach(columnName -> { tableEditor.addColumn(Column.editor().name(columnName).create()); }); } selectColumnsListener = new ViewSelectedColumnsParserListener(tableEditor, parser); listeners.add(selectColumnsListener); } super.enterCreateView(ctx); }
@Override public void enterAlterByAddColumn(MySqlParser.AlterByAddColumnContext ctx) { parser.runIfNotNull(() -> { String columnName = parser.parseName(ctx.uid(0)); ColumnEditor columnEditor = Column.editor().name(columnName); columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, columnEditor, parser.dataTypeResolver(), parser.getConverters()); listeners.add(columnDefinitionListener); }, tableEditor); super.exitAlterByAddColumn(ctx); }
@Override public void enterColumnDeclaration(MySqlParser.ColumnDeclarationContext ctx) { parser.runIfNotNull(() -> { String columnName = parser.parseName(ctx.uid()); ColumnEditor columnEditor = Column.editor().name(columnName); if (columnDefinitionListener == null) { columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, columnEditor, parser.dataTypeResolver(), parser.getConverters()); listeners.add(columnDefinitionListener); } else { columnDefinitionListener.setColumnEditor(columnEditor); } }, tableEditor); super.enterColumnDeclaration(ctx); }
@Override public void enterAlterView(MySqlParser.AlterViewContext ctx) { if (!parser.skipViews()) { TableId tableId = parser.parseQualifiedTableId(ctx.fullId()); tableEditor = parser.databaseTables().editTable(tableId); if (tableEditor == null) { throw new ParsingException(null, "Trying to alter view " + tableId.toString() + ", which does not exist. Query:" + AntlrDdlParser.getText(ctx)); } // alter view will override existing columns for a new one tableEditor.columnNames().forEach(tableEditor::removeColumn); // create new columns just with specified name for now if (ctx.uidList() != null) { ctx.uidList().uid().stream().map(parser::parseName).forEach(columnName -> { tableEditor.addColumn(Column.editor().name(columnName).create()); }); } selectColumnsListener = new ViewSelectedColumnsParserListener(tableEditor, parser); listeners.add(selectColumnsListener); } super.enterAlterView(ctx); }
protected Column parseCreateColumn(Marker start, TableEditor table, String columnName, String newColumnName) { // Obtain the column editor ... Column existingColumn = table.columnWithName(columnName); ColumnEditor column = existingColumn != null ? existingColumn.edit() : Column.editor().name(columnName); AtomicBoolean isPrimaryKey = new AtomicBoolean(false); parseColumnDefinition(start, columnName, tokens, table, column, isPrimaryKey); convertDefaultValueToSchemaType(column); // Update the table ... Column newColumnDefn = column.create(); table.addColumns(newColumnDefn); if (isPrimaryKey.get()) { table.setPrimaryKeyNames(newColumnDefn.name()); } if (newColumnName != null && !newColumnName.equalsIgnoreCase(columnName)) { table.renameColumn(columnName, newColumnName); columnName = newColumnName; } // ALTER TABLE allows reordering the columns after the definition ... if (tokens.canConsume("FIRST")) { table.reorderColumn(columnName, null); } else if (tokens.canConsume("AFTER")) { table.reorderColumn(columnName, tokens.consume()); } return table.columnWithName(newColumnDefn.name()); }
private Table tableFromFromMessage(List<ReplicationMessage.Column> columns, Table table) { return table.edit() .setColumns(columns.stream() .map(column -> { final PostgresType type = column.getType(); final ColumnEditor columnEditor = Column.editor() .name(column.getName()) .jdbcType(type.getJdbcId()) .type(type.getName()) .optional(column.isOptional()) .nativeType(type.getOid()); columnEditor.length(column.getTypeMetadata().getLength()); columnEditor.scale(column.getTypeMetadata().getScale()); return columnEditor.create(); }) .collect(Collectors.toList()) ) .setPrimaryKeyNames(table.filterColumnNames(c -> table.isPrimaryKeyColumn(c.name()))).create(); } }
@Before public void beforeEach() { column = Column.editor().name("firstName").jdbcType(Types.VARCHAR).type("VARCHAR").position(1).create(); column2 = Column.editor().name("lastName").jdbcType(Types.VARCHAR).type("VARCHAR").position(2).create(); column3 = Column.editor().name("otherColumn").jdbcType(Types.VARCHAR).type("VARCHAR").position(3).create(); fullyQualifiedNames = tableId + "." + column.name() + "," + tableId + "." + column3.name() + ","; }
protected void parseTableElement(Marker start, TableEditor table) { if (tokens.matchesAnyOf("CONSTRAINT", "UNIQUE", "PRIMARY", "FOREIGN", "CHECK")) { parseTableConstraintDefinition(start, table); } else if (tokens.matches("LIKE")) { parseTableLikeClause(start, table); } else if (tokens.matches("REF", "IS")) { parseSelfReferencingColumnSpec(start, table); } else { // Obtain the column editor ... String columnName = tokens.consume(); Column existingColumn = table.columnWithName(columnName); ColumnEditor column = existingColumn != null ? existingColumn.edit() : Column.editor().name(columnName); AtomicBoolean isPrimaryKey = new AtomicBoolean(false); if (tokens.matches("WITH", "OPTIONS")) { parseColumnOptions(start, columnName, tokens, column); } else { parseColumnDefinition(start, columnName, tokens, table, column, isPrimaryKey); } // Update the table ... Column newColumnDefn = column.create(); table.addColumns(newColumnDefn); if (isPrimaryKey.get()) { table.setPrimaryKeyNames(newColumnDefn.name()); } } }
@Test @FixFor("DBZ-644") public void shouldAddTypeInformation() { int length = 255; Column column = Column.editor() .name("col") .type("VARCHAR") .jdbcType(Types.VARCHAR) .length(length) .create(); SchemaBuilder schemaBuilder = SchemaBuilder.string(); new PropagateSourceTypeToSchemaParameter().alterFieldSchema(column, schemaBuilder); assertThat(schemaBuilder.parameters().get("__debezium.source.column.type")).isEqualTo("VARCHAR"); assertThat(schemaBuilder.parameters().get("__debezium.source.column.length")).isEqualTo(String.valueOf(length)); }
@Test @FixFor("DBZ-1073") public void shouldAddColumnScale() { int length = 5; int scale = 2; Column column = Column.editor() .name("col") .type("NUMERIC") .jdbcType(Types.NUMERIC) .length(length) .scale(scale) .create(); SchemaBuilder schemaBuilder = SchemaBuilder.string(); new PropagateSourceTypeToSchemaParameter().alterFieldSchema(column, schemaBuilder); assertThat(schemaBuilder.parameters().get("__debezium.source.column.type")).isEqualTo("NUMERIC"); assertThat(schemaBuilder.parameters().get("__debezium.source.column.length")).isEqualTo(String.valueOf(length)); assertThat(schemaBuilder.parameters().get("__debezium.source.column.scale")).isEqualTo(String.valueOf(scale)); } }
private ValueConverter createArrayConverter(Column column, Field fieldDefn) { PostgresType arrayType = typeRegistry.get(column.nativeType()); PostgresType elementType = arrayType.getElementType(); final String elementTypeName = elementType.getName(); final String elementColumnName = column.name() + "-element"; final Column elementColumn = Column.editor() .name(elementColumnName) .jdbcType(elementType.getJdbcId()) .nativeType(elementType.getOid()) .type(elementTypeName) .optional(true) .scale(column.scale().orElse(null)) .length(column.length()) .create(); Schema elementSchema = schemaBuilder(elementColumn) .optional() .build(); final Field elementField = new Field(elementColumnName, 0, elementSchema); final ValueConverter elementConverter = converter(elementColumn, elementField); return data -> convertArray(column, fieldDefn, elementConverter, data); }
@Override public ColumnEditor edit() { final ColumnEditor editor = Column.editor() .name(name()) .type(typeName(), typeExpression()) .jdbcType(jdbcType()) .nativeType(nativeType) .charsetName(charsetName) .length(length()) .scale(scale().orElse(null)) .position(position()) .optional(isOptional()) .autoIncremented(isAutoIncremented()) .generated(isGenerated()); if (hasDefaultValue()) { editor.defaultValue(defaultValue()); } return editor; } }
@Before public void beforeEach() { table = Table.editor() .tableId(id) .addColumns(Column.editor().name("C1") .type("VARCHAR").jdbcType(Types.VARCHAR).length(10) .generated(true) .optional(false) .create(), Column.editor().name("C2") .type("NUMBER").jdbcType(Types.NUMERIC).length(5) .optional(false) .create(), Column.editor().name("C3") .type("DATE").jdbcType(Types.DATE).length(4) .optional(true) .create(), Column.editor().name("C4") .type("COUNTER").jdbcType(Types.INTEGER) .autoIncremented(true) .optional(true) .create()) .setPrimaryKeyNames("C1", "C2") .create(); c1 = table.columnWithName("C1"); c2 = table.columnWithName("C2"); c3 = table.columnWithName("C3"); c4 = table.columnWithName("C4"); }
.addColumn(Column.editor() .name("first") .jdbcType(Types.VARCHAR)