/** * {@inheritDoc} */ public String toString() { StringBuilder result = new StringBuilder(); result.append("Database [name="); result.append(getName()); result.append("; "); result.append(getTableCount()); result.append(" tables]"); return result.toString(); }
@Override public void init() { Connection connection = null; try { Database _database = identifyNewTables(); if(_database.getTableCount() >0 ) { LOG.info("Creating {} new tables (totally {} tables)", _database.getTableCount(),database.getTableCount()); connection = ConnectionManagerFactory.getInstance().getConnection(); this.platform.createTables(connection,_database, false, true); LOG.info("Created {} new tables: ",_database.getTableCount(),_database.getTables()); } else { LOG.debug("All the {} tables have already been created, no new tables", database.getTableCount()); } } catch (Exception e) { LOG.error(e.getMessage(),e); throw new IllegalStateException(e); } finally { if(connection != null){ try { connection.close(); } catch (SQLException e) { LOG.warn(e.getMessage(),e); } } } }
@Override public void init() { Connection connection = null; try { Database _database = identifyNewTables(); if(_database.getTableCount() >0 ) { LOG.info("Creating {} new tables (totally {} tables)", _database.getTableCount(),database.getTableCount()); connection = ConnectionManagerFactory.getInstance().getConnection(); this.platform.createTables(connection,_database, false, true); LOG.info("Created {} new tables: ",_database.getTableCount(),_database.getTables()); } else { LOG.debug("All the {} tables have already been created, no new tables", database.getTableCount()); } } catch (Exception e) { LOG.error(e.getMessage(),e); throw new IllegalStateException(e); } finally { if(connection != null){ try { connection.close(); } catch (SQLException e) { LOG.warn(e.getMessage(),e); } } } }
/** * Creates the external foreignkey creation statements for all tables in the database. * * @param database The database */ public void createForeignKeys(Database database) throws IOException { for (int idx = 0; idx < database.getTableCount(); idx++) { createForeignKeys(database, database.getTable(idx)); } }
public void init(Config config) { Connection connection = null; try { this.platform = PlatformFactory.createNewPlatformInstance("mysql"); connection = MetadataUtils.getJdbcConnection(config); String dbName = config.getString(MetadataUtils.JDBC_DATABASE_PATH); this.database = platform.readModelFromDatabase(connection, dbName); LOG.info("Loaded " + database); Database _database = identifyNewTables(); if (_database.getTableCount() > 0) { LOG.info("Creating {} new tables (totally {} tables)", _database.getTableCount(), database.getTableCount()); this.platform.createTables(connection, _database, false, true); LOG.info("Created {} new tables: ", _database.getTableCount(), _database.getTables()); } else { LOG.debug("All the {} tables have already been created, no new tables", database.getTableCount()); } } catch (Exception e) { LOG.error(e.getMessage(), e); throw new IllegalStateException(e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { LOG.warn(e.getMessage(), e); } } } }
/** * * {@inheritDoc} */ public void createForeignKeys(Database database) throws IOException { for (int idx = 0; idx < database.getTableCount(); idx++) { createForeignKeys(database, database.getTable(idx)); } }
/** * Returns a verbose string representation of this database. * * @return The string representation */ public String toVerboseString() { StringBuilder result = new StringBuilder(); result.append("Database ["); result.append(getName()); result.append("] tables:"); for (int idx = 0; idx < getTableCount(); idx++) { result.append(" "); result.append(getTable(idx).toVerboseString()); } return result.toString(); } }
for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++)
/** * Sorts the foreign keys in the tables of the model. * * @param model The model */ protected void sortForeignKeys(Database model) { for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) { model.getTable(tableIdx).sortForeignKeys(getPlatform().isDelimitedIdentifierModeOn()); } }
/** * {@inheritDoc} */ public void start() throws DataSinkException { _fkTables.clear(); _waitingObjects.clear(); if (_ensureFkOrder) { for (int tableIdx = 0; tableIdx < _model.getTableCount(); tableIdx++) { Table table = _model.getTable(tableIdx); for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { ForeignKey curFk = table.getForeignKey(fkIdx); _fkTables.add(curFk.getForeignTable()); } } } try { _connection = _platform.borrowConnection(); } catch (DatabaseOperationException ex) { throw new DataSinkException(ex); } }
/** * Allows the platform to postprocess the model just read from the database. * * @param model The model */ protected void postprocessModelFromDatabase(Database model) { // Default values for CHAR/VARCHAR/LONGVARCHAR columns have quotation marks // around them which we'll remove now for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) { Table table = model.getTable(tableIdx); for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) { Column column = table.getColumn(columnIdx); if (TypeMap.isTextType(column.getTypeCode()) || TypeMap.isDateTimeType(column.getTypeCode())) { String defaultValue = column.getDefaultValue(); if ((defaultValue != null) && (defaultValue.length() >= 2) && defaultValue.startsWith("'") && defaultValue.endsWith("'")) { defaultValue = defaultValue.substring(1, defaultValue.length() - 1); column.setDefaultValue(defaultValue); } } } } }
changes.addAll(checkForRemovedTables(sourceModel, intermediateModel, targetModel)); for (int tableIdx = 0; tableIdx < intermediateModel.getTableCount(); tableIdx++)
for (int idx = 0; idx < model.getTableCount(); idx++)
for (int idx = database.getTableCount() - 1; idx >= 0; idx--) for (int idx = database.getTableCount() - 1; idx >= 0; idx--)
/** * Determines whether one of the tables in the list has a foreign key to a table outside of the list, * or a table outside of the list has a foreign key to one of the tables in the list. * * @param model The database model * @param tables The tables * @throws ModelException If such a foreign key exists */ public void checkForForeignKeysToAndFromTables(Database model, Table[] tables) throws ModelException { List tableList = Arrays.asList(tables); for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) { Table curTable = model.getTable(tableIdx); boolean curTableIsInList = tableList.contains(curTable); for (int fkIdx = 0; fkIdx < curTable.getForeignKeyCount(); fkIdx++) { ForeignKey curFk = curTable.getForeignKey(fkIdx); if (curTableIsInList != tableList.contains(curFk.getForeignTable())) { throw new ModelException("The table " + curTable.getQualifiedName() + " has a foreign key to table " + curFk.getForeignTable().getQualifiedName()); } } } }
/** * {@inheritDoc} */ public void apply(Database database, boolean caseSensitive) { // we only need to replace the table in the model, as there can't be a // foreign key from or to it when these kind of changes are created for (int tableIdx = 0; tableIdx < database.getTableCount(); tableIdx++) { Table curTable = database.getTable(tableIdx); if ((caseSensitive && curTable.getQualifiedName().equals(getChangedTable())) || (!caseSensitive && curTable.getQualifiedName().equalsIgnoreCase(getChangedTable()))) { database.removeTable(tableIdx); database.addTable(tableIdx, new CloneHelper().clone(_targetTable, true, false, database, caseSensitive)); break; } } } }
/** * Writes the database model to the given XML writer. * * @param model The database model * @param xmlWriter The XML writer */ private void writeDatabaseElement(Database model, PrettyPrintingXmlWriter xmlWriter) throws DdlUtilsXMLException { writeElementStart(xmlWriter, QNAME_ELEMENT_DATABASE); xmlWriter.writeNamespace(null, DDLUTILS_NAMESPACE); writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, model.getName()); writeAttribute(xmlWriter, QNAME_ATTRIBUTE_DEFAULT_ID_METHOD, model.getIdMethod()); writeAttribute(xmlWriter, QNAME_ATTRIBUTE_VERSION, model.getVersion()); if (model.getTableCount() > 0) { xmlWriter.printlnIfPrettyPrinting(); for (int idx = 0; idx < model.getTableCount(); idx++) { writeTableElement(model.getTable(idx), xmlWriter); } } writeElementEnd(xmlWriter); }
/** * Outputs the DDL required to drop (if requested) and (re)create all tables in the database model. * * @param database The database * @param params The parameters used in the creation * @param dropTables Whether to drop tables before creating them */ public void createTables(Database database, CreationParameters params, boolean dropTables) throws IOException { if (dropTables) { dropTables(database); } for (int idx = 0; idx < database.getTableCount(); idx++) { Table table = database.getTable(idx); writeTableComment(table); createTable(database, table, params == null ? null : params.getParametersFor(table)); } // we're writing the external foreignkeys last to ensure that all referenced tables are already defined createForeignKeys(database); }
protected void writeAddIdentityUsingAlterTable(Platform platform, Database model) throws DataSinkException { // if identity column has to be set using alter table then // do it after loading the data if (_addIdentityUsingAlterTable && platform.getPlatformInfo() .isAddingIdentityUsingAlterTableSupported()) { StringWriter buffer = new StringWriter(); SqlBuilder builder = platform.getSqlBuilder(); builder.setWriter(buffer); for (int index = 0; index < model.getTableCount(); index++) { Table table = model.getTable(index); for (int col = 0; col < table.getColumnCount(); col++) { Column column = table.getColumn(col); if (column.isAutoIncrement()) { try { builder.writeAddIdentityColumnUsingAlterTable(table, column); } catch (IOException ioe) { throw new DataSinkException(ioe); } } } } String sql = buffer.toString(); platform.evaluateBatch(sql, false); } } // GemStone changes END
/** * Outputs the DDL required to drop the given table. This method also * drops foreign keys to the table. * * @param database The database * @param table The table */ public void dropTable(Database database, Table table) throws IOException { // we're dropping the foreignkeys to the table first for (int idx = database.getTableCount() - 1; idx >= 0; idx--) { Table otherTable = database.getTable(idx); ForeignKey[] fks = otherTable.getForeignKeys(); for (int fkIdx = 0; (fks != null) && (fkIdx < fks.length); fkIdx++) { if (fks[fkIdx].getForeignTable().equals(table)) { dropForeignKey(otherTable, fks[fkIdx]); } } } // and the foreign keys from the table dropForeignKeys(table); writeTableComment(table); dropTable(table); }