@Override public Table create() { if (id == null) throw new IllegalStateException("Unable to create a table from an editor that has no table ID"); List<Column> columns = new ArrayList<>(); sortedColumns.values().forEach(column->{ column = column.edit().charsetNameOfTable(defaultCharsetName).create(); columns.add(column); }); return new TableImpl(id, columns, primaryKeyColumnNames(), defaultCharsetName); } }
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()); }
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()); } } }
protected void updatePositions() { AtomicInteger position = new AtomicInteger(1); sortedColumns.replaceAll((name, defn) -> { // Decrement the position ... int nextPosition = position.getAndIncrement(); if (defn.position() != nextPosition) { return defn.edit().position(nextPosition).create(); } return defn; }); }
if (table.columns().isEmpty()) { selectedColumnsByAlias.forEach((columnName, fromTableColumn) -> { if (fromTableColumn != null && columnName != null) table.addColumn(fromTableColumn.edit().name(columnName).create()); }); } else { changedColumns.add(column.edit() .jdbcType(selectedColumn.jdbcType()) .type(selectedColumn.typeName(), selectedColumn.typeExpression())
protected void add(Column defn) { if (defn != null) { Column existing = columnWithName(defn.name()); int position = existing != null ? existing.position() : sortedColumns.size() + 1; sortedColumns.put(defn.name().toLowerCase(), defn.edit().position(position).create()); } assert positionsAreValid(); }
ColumnEditor column = existingColumn != null ? existingColumn.edit() : Column.editor().name(columnName); parseAlterColumn(start, column);
private void addColumnFromTable(TableEditor table, String columnName, String newColumnName, Table selectedTable) { for (Column column : selectedTable.columns()) { if (column.name().equals(columnName)) { table.addColumn(column.edit().name(newColumnName).create()); break; } } } }
@Override public void enterAlterByChangeDefault(MySqlParser.AlterByChangeDefaultContext ctx) { parser.runIfNotNull(() -> { String columnName = parser.parseName(ctx.uid()); Column column = tableEditor.columnWithName(columnName); if (column != null) { ColumnEditor columnEditor = column.edit(); columnEditor.generated(ctx.DROP() != null); } }, tableEditor); super.enterAlterByChangeDefault(ctx); }
@Override public TableEditor renameColumn(String existingName, String newName) { final Column existing = columnWithName(existingName); if (existing == null) throw new IllegalArgumentException("No column with name '" + existingName + "'"); Column newColumn = existing.edit().name(newName).create(); // Determine the primary key names ... List<String> newPkNames = null; if ( !hasUniqueValues() && primaryKeyColumnNames().contains(existing.name())) { newPkNames = new ArrayList<>(primaryKeyColumnNames()); newPkNames.replaceAll(name->existing.name().equals(name) ? newName : name); } // Add the new column, move it before the existing column, and remove the old column ... addColumn(newColumn); reorderColumn(newColumn.name(), existing.name()); removeColumn(existing.name()); if (newPkNames != null) { setPrimaryKeyNames(newPkNames); } return this; }
final ColumnEditor ce = column.edit().optional(false); if (ce.hasDefaultValue() && ce.defaultValue() == null) { ce.unsetDefaultValue();
@Override public void enterAlterByRenameColumn(MySqlParser.AlterByRenameColumnContext ctx) { parser.runIfNotNull(() -> { String oldColumnName = parser.parseName(ctx.oldColumn); Column existingColumn = tableEditor.columnWithName(oldColumnName); if (existingColumn != null) { // DBZ-771 unset previously set default value, as it's not kept by MySQL; for any column modifications a new // default value (which could be the same) has to be provided by the column_definition which we'll parse later // on; only in 8.0 (not yet supported by this parser) columns can be renamed without repeating the full column // definition; so in fact it's arguably not correct to use edit() on the existing column to begin with, but // I'm going to leave this as is for now, to be prepared for the ability of updating column definitions in 8.0 ColumnEditor columnEditor = existingColumn.edit(); // columnEditor.unsetDefaultValue(); columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, columnEditor, parser.dataTypeResolver(), parser.getConverters()); listeners.add(columnDefinitionListener); } else { throw new ParsingException(null, "Trying to change column " + oldColumnName + " in " + tableEditor.tableId().toString() + " table, which does not exist. Query: " + getText(ctx)); } }, tableEditor); super.enterAlterByRenameColumn(ctx); }
@Override public void enterAlterByModifyColumn(MySqlParser.AlterByModifyColumnContext ctx) { parser.runIfNotNull(() -> { String columnName = parser.parseName(ctx.uid(0)); Column column = tableEditor.columnWithName(columnName); if (column != null) { columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, column.edit(), parser.dataTypeResolver(), parser.getConverters()); listeners.add(columnDefinitionListener); } else { throw new ParsingException(null, "Trying to change column " + columnName + " in " + tableEditor.tableId().toString() + " table, which does not exist. Query: " + getText(ctx)); } }, tableEditor); super.enterAlterByModifyColumn(ctx); }
@Override public void enterAlterByChangeColumn(MySqlParser.AlterByChangeColumnContext ctx) { parser.runIfNotNull(() -> { String oldColumnName = parser.parseName(ctx.oldColumn); Column existingColumn = tableEditor.columnWithName(oldColumnName); if (existingColumn != null) { // DBZ-771 unset previously set default value, as it's not kept by MySQL; for any column modifications a new // default value (which could be the same) has to be provided by the column_definition which we'll parse later // on; only in 8.0 (not yet supported by this parser) columns can be renamed without repeating the full column // definition; so in fact it's arguably not correct to use edit() on the existing column to begin with, but // I'm going to leave this as is for now, to be prepared for the ability of updating column definitions in 8.0 ColumnEditor columnEditor = existingColumn.edit(); columnEditor.unsetDefaultValue(); columnDefinitionListener = new ColumnDefinitionParserListener(tableEditor, columnEditor, parser.dataTypeResolver(), parser.getConverters()); listeners.add(columnDefinitionListener); } else { throw new ParsingException(null, "Trying to change column " + oldColumnName + " in " + tableEditor.tableId().toString() + " table, which does not exist. Query: " + getText(ctx)); } }, tableEditor); super.enterAlterByChangeColumn(ctx); }
Column c = table.columnWithName(name); if (c != null && c.isOptional()) { final ColumnEditor ce = c.edit().optional(false); if (ce.hasDefaultValue() && ce.defaultValue() == null) { ce.unsetDefaultValue();
ColumnEditor columnEditor = table.columnWithName(columnName).edit(); parseDefaultClause(start, columnEditor);
@Override public Table create() { if (id == null) throw new IllegalStateException("Unable to create a table from an editor that has no table ID"); List<Column> columns = new ArrayList<>(); sortedColumns.values().forEach(column->{ column = column.edit().charsetNameOfTable(defaultCharsetName).create(); columns.add(column); }); return new TableImpl(id, columns, primaryKeyColumnNames(), defaultCharsetName); } }
protected void updatePositions() { AtomicInteger position = new AtomicInteger(1); sortedColumns.replaceAll((name, defn) -> { // Decrement the position ... int nextPosition = position.getAndIncrement(); if (defn.position() != nextPosition) { return defn.edit().position(nextPosition).create(); } return defn; }); }
protected void add(Column defn) { if (defn != null) { Column existing = columnWithName(defn.name()); int position = existing != null ? existing.position() : sortedColumns.size() + 1; sortedColumns.put(defn.name().toLowerCase(), defn.edit().position(position).create()); } assert positionsAreValid(); }
@Override public TableEditor renameColumn(String existingName, String newName) { final Column existing = columnWithName(existingName); if (existing == null) throw new IllegalArgumentException("No column with name '" + existingName + "'"); Column newColumn = existing.edit().name(newName).create(); // Determine the primary key names ... List<String> newPkNames = null; if ( !hasUniqueValues() && primaryKeyColumnNames().contains(existing.name())) { newPkNames = new ArrayList<>(primaryKeyColumnNames()); newPkNames.replaceAll(name->existing.name().equals(name) ? newName : name); } // Add the new column, move it before the existing column, and remove the old column ... addColumn(newColumn); reorderColumn(newColumn.name(), existing.name()); removeColumn(existing.name()); if (newPkNames != null) { setPrimaryKeyNames(newPkNames); } return this; }