private Document toDocument(Table table) { Document document = Document.create(); document.set("defaultCharsetName", table.defaultCharsetName()); document.set("primaryKeyColumnNames", Array.create(table.primaryKeyColumnNames())); List<Document> columns = table.columns() .stream() .map(this::toDocument) .collect(Collectors.toList()); document.setArray("columns", Array.create(columns)); return document; }
protected TableImpl(Table table) { this(table.id(), table.columns(), table.primaryKeyColumnNames(), table.defaultCharsetName()); }
@Override public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Table) { Table that = (Table) obj; return this.id().equals(that.id()) && this.columns().equals(that.columns()) && this.primaryKeyColumnNames().equals(that.primaryKeyColumnNames()) && Strings.equalsIgnoreCase(this.defaultCharsetName(), that.defaultCharsetName()); } return false; }
/** * Add or update the definition for the identified table. * * @param tableId the identifier of the table * @param changer the function that accepts the current {@link Table} and returns either the same or an updated * {@link Table}; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table updateTable(TableId tableId, Function<Table, Table> changer) { return lock.write(() -> { TableImpl existing = tablesByTableId.get(tableId); Table updated = changer.apply(existing); if (updated != existing) { tablesByTableId.put(tableId, new TableImpl(tableId, updated.columns(), updated.primaryKeyColumnNames(), updated.defaultCharsetName())); } changes.add(tableId); return existing; }); }
@Override public void exitCopyCreateTable(MySqlParser.CopyCreateTableContext ctx) { TableId tableId = parser.parseQualifiedTableId(ctx.tableName(0).fullId()); TableId originalTableId = parser.parseQualifiedTableId(ctx.tableName(1).fullId()); Table original = parser.databaseTables().forTable(originalTableId); if (original != null) { parser.databaseTables().overwriteTable(tableId, original.columns(), original.primaryKeyColumnNames(), original.defaultCharsetName()); parser.signalCreateTable(tableId, ctx); } super.exitCopyCreateTable(ctx); }
/** * Rename an existing table. * * @param existingTableId the identifier of the existing table to be renamed; may not be null * @param newTableId the new identifier for the table; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table renameTable(TableId existingTableId, TableId newTableId) { return lock.write(() -> { Table existing = forTable(existingTableId); if (existing == null) return null; tablesByTableId.remove(existing.id()); TableImpl updated = new TableImpl(newTableId, existing.columns(), existing.primaryKeyColumnNames(), existing.defaultCharsetName()); try { return tablesByTableId.put(updated.id(), updated); } finally { changes.add(existingTableId); changes.add(updated.id()); } }); }
Table original = databaseTables.forTable(originalId); if (original != null) { databaseTables.overwriteTable(tableId, original.columns(), original.primaryKeyColumnNames(), original.defaultCharsetName());
protected TableImpl(Table table) { this(table.id(), table.columns(), table.primaryKeyColumnNames(), table.defaultCharsetName()); }
private Document toDocument(Table table) { Document document = Document.create(); document.set("defaultCharsetName", table.defaultCharsetName()); document.set("primaryKeyColumnNames", Array.create(table.primaryKeyColumnNames())); List<Document> columns = table.columns() .stream() .map(this::toDocument) .collect(Collectors.toList()); document.setArray("columns", Array.create(columns)); return document; }
@Override public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Table) { Table that = (Table) obj; return this.id().equals(that.id()) && this.columns().equals(that.columns()) && this.primaryKeyColumnNames().equals(that.primaryKeyColumnNames()) && Strings.equalsIgnoreCase(this.defaultCharsetName(), that.defaultCharsetName()); } return false; }
/** * Add or update the definition for the identified table. * * @param tableId the identifier of the table * @param changer the function that accepts the current {@link Table} and returns either the same or an updated * {@link Table}; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table updateTable(TableId tableId, Function<Table, Table> changer) { return lock.write(() -> { TableImpl existing = tablesByTableId.get(tableId); Table updated = changer.apply(existing); if (updated != existing) { tablesByTableId.put(tableId, new TableImpl(tableId, updated.columns(), updated.primaryKeyColumnNames(), updated.defaultCharsetName())); } changes.add(tableId); return existing; }); }
/** * Rename an existing table. * * @param existingTableId the identifier of the existing table to be renamed; may not be null * @param newTableId the new identifier for the table; may not be null * @return the previous table definition, or null if there was no prior table definition */ public Table renameTable(TableId existingTableId, TableId newTableId) { return lock.write(() -> { Table existing = forTable(existingTableId); if (existing == null) return null; tablesByTableId.remove(existing.id()); TableImpl updated = new TableImpl(newTableId, existing.columns(), existing.primaryKeyColumnNames(), existing.defaultCharsetName()); try { return tablesByTableId.put(updated.id(), updated); } finally { changes.add(existingTableId); changes.add(updated.id()); } }); }