protected TableImpl(TableId id, List<Column> sortedColumns, List<String> pkColumnNames, String defaultCharsetName) { this.id = id; this.columnDefs = Collections.unmodifiableList(sortedColumns); this.pkColumnNames = pkColumnNames == null ? Collections.emptyList() : Collections.unmodifiableList(pkColumnNames); Map<String, Column> defsByLowercaseName = new LinkedHashMap<>(); for (Column def : this.columnDefs) { defsByLowercaseName.put(def.name().toLowerCase(), def); } this.columnsByLowercaseName = Collections.unmodifiableMap(defsByLowercaseName); this.defaultCharsetName = defaultCharsetName; }
protected Field[] fieldsForColumns(Schema schema, List<Column> columns) { Field[] fields = new Field[columns.size()]; AtomicInteger i = new AtomicInteger(0); columns.forEach(column -> { Field field = schema.field(column.name()); // may be null if the field is unused ... fields[i.getAndIncrement()] = field; }); return fields; }
/** * Get the value mapping function for the given column. * * @param tableId the identifier of the table to which the column belongs; may not be null * @param column the column; may not be null * @return the mapping function, or null if there is no mapping function */ public ColumnMapper mapperFor(TableId tableId, Column column) { ColumnId id = new ColumnId(tableId, column.name()); Optional<MapperRule> matchingRule = rules.stream().filter(rule -> rule.matches(id)).findFirst(); if (matchingRule.isPresent()) { return matchingRule.get().mapper; } return null; }
/** * Determine if the named column is part of the primary key. * * @param columnName the name of the column * @return {@code true} if a column exists in this table and it is part of the primary key, or {@code false} otherwise */ default boolean isPrimaryKeyColumn( String columnName ) { Column column = columnWithName(columnName); return column == null ? false : primaryKeyColumnNames().contains(column.name()); }
/** * Get the columns that are not included in the primary key for this table. * @return the list of columns that are not part of the primary key; never null but possibly empty */ default List<Column> nonPrimaryKeyColumns() { return filterColumns(col->!isPrimaryKeyColumn(col.name())); }
/** * Get the columns that make up the primary key for this table. * @return the list of columns that make up the primary key; never null but possibly empty */ default List<Column> primaryKeyColumns() { return filterColumns(col->isPrimaryKeyColumn(col.name())); }
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 void exitAlterByRenameColumn(MySqlParser.AlterByRenameColumnContext ctx) { parser.runIfNotNull(() -> { Column column = columnDefinitionListener.getColumn(); tableEditor.addColumn(column); String newColumnName = parser.parseName(ctx.newColumn); if (newColumnName != null && !column.name().equalsIgnoreCase(newColumnName)) { tableEditor.renameColumn(column.name(), newColumnName); } listeners.remove(columnDefinitionListener); }, tableEditor, columnDefinitionListener); super.exitAlterByRenameColumn(ctx); } }
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 exitAlterByModifyColumn(MySqlParser.AlterByModifyColumnContext ctx) { parser.runIfNotNull(() -> { Column column = columnDefinitionListener.getColumn(); tableEditor.addColumn(column); if (ctx.FIRST() != null) { tableEditor.reorderColumn(column.name(), null); } else if (ctx.AFTER() != null) { String afterColumn = parser.parseName(ctx.uid(1)); tableEditor.reorderColumn(column.name(), afterColumn); } listeners.remove(columnDefinitionListener); }, tableEditor, columnDefinitionListener); super.exitAlterByModifyColumn(ctx); }
@Override public void exitAlterByAddColumn(MySqlParser.AlterByAddColumnContext ctx) { parser.runIfNotNull(() -> { Column column = columnDefinitionListener.getColumn(); tableEditor.addColumn(column); String columnName = column.name(); if (ctx.FIRST() != null) { tableEditor.reorderColumn(columnName, null); } else if (ctx.AFTER() != null) { String afterColumn = parser.parseName(ctx.uid(1)); tableEditor.reorderColumn(columnName, afterColumn); } listeners.remove(columnDefinitionListener); }, tableEditor, columnDefinitionListener); super.exitAlterByAddColumn(ctx); }
@Test public void shouldFindNonExistingColumnByNameIndependentOfCase() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).position(1).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).position(1).create(); editor.addColumns(c1, c2, c3); editor.columns().forEach(col -> { assertThat(editor.columnWithName(col.name())).isNotNull(); assertThat(editor.columnWithName(col.name().toUpperCase())).isNotNull(); assertThat(editor.columnWithName(col.name().toLowerCase())).isNotNull(); }); assertThat(editor.columnWithName("WOOPS")).isNull(); }
@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 assertColumn(Table table, String name, String typeName, int jdbcType, int length, int scale, boolean optional, boolean generated, boolean autoIncremented ) { Column column = table.columnWithName(name); assertThat(column.name()).isEqualTo(name); assertThat(column.typeName()).isEqualTo(typeName); assertThat(column.jdbcType()).isEqualTo(jdbcType); assertThat(column.length()).isEqualTo(length); if (scale == Column.UNSET_INT_VALUE) { assertFalse(column.scale().isPresent()); } else { assertThat(column.scale().get()).isEqualTo(scale); } assertThat(column.isOptional()).isEqualTo(optional); assertThat(column.isGenerated()).isEqualTo(generated); assertThat(column.isAutoIncremented()).isEqualTo(autoIncremented); }
protected void assertColumn(Table table, String name, String typeName, int jdbcType, int length, String charsetName, boolean optional) { Column column = table.columnWithName(name); assertThat(column.name()).isEqualTo(name); assertThat(column.typeName()).isEqualTo(typeName); assertThat(column.jdbcType()).isEqualTo(jdbcType); assertThat(column.length()).isEqualTo(length); assertThat(column.charsetName()).isEqualTo(charsetName); assertFalse(column.scale().isPresent()); assertThat(column.isOptional()).isEqualTo(optional); assertThat(column.isGenerated()).isFalse(); assertThat(column.isAutoIncremented()).isFalse(); }
@Test public void shouldCreateColumnWithAllFieldsSetToDefaults() { Column column = editor.create(); assertThat(column.name()).isNull(); assertThat(column.typeName()).isNull(); assertThat(column.jdbcType()).isEqualTo(Types.INTEGER); assertThat(column.length()).isEqualTo(-1); Assert.assertFalse(column.scale().isPresent()); assertThat(column.position()).isEqualTo(1); assertThat(column.isOptional()).isTrue(); assertThat(column.isAutoIncremented()).isFalse(); assertThat(column.isGenerated()).isFalse(); }
@Test public void shouldCreateColumnWithAllFieldsSetToNonDefaults() { createColumnWithAllFieldsSetToNonDefaults(); assertThat(column.name()).isEqualTo("price"); assertThat(column.typeName()).isEqualTo("NUMBER"); assertThat(column.jdbcType()).isEqualTo(Types.DOUBLE); assertThat(column.length()).isEqualTo(5); assertThat(column.scale().get()).isEqualTo(2); assertThat(column.position()).isEqualTo(4); assertThat(column.isOptional()).isTrue(); assertThat(column.isAutoIncremented()).isTrue(); assertThat(column.isGenerated()).isTrue(); }
@Test public void shouldFindGeneratedColumns() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).generated(true).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).generated(true).create(); editor.addColumns(c1, c2, c3); editor.setPrimaryKeyNames("C1"); table = editor.create(); assertThat(table.retrieveColumnNames()).containsExactly("C1", "C2", "C3"); table.columns().forEach(col -> { assertThat(table.isGenerated(col.name())).isEqualTo(col.isGenerated()); }); assertValidPositions(editor); }
@Test public void shouldFindAutoIncrementedColumns() { editor.tableId(id); Column c1 = columnEditor.name("C1").type("VARCHAR").jdbcType(Types.VARCHAR).length(10).position(1).create(); Column c2 = columnEditor.name("C2").type("NUMBER").jdbcType(Types.NUMERIC).length(5).autoIncremented(true).create(); Column c3 = columnEditor.name("C3").type("DATE").jdbcType(Types.DATE).autoIncremented(true).create(); editor.addColumns(c1, c2, c3); editor.setPrimaryKeyNames("C1"); table = editor.create(); assertThat(table.retrieveColumnNames()).containsExactly("C1", "C2", "C3"); table.columns().forEach(col -> { assertThat(table.isAutoIncremented(col.name())).isEqualTo(col.isAutoIncremented()); }); assertValidPositions(editor); }