/** * INTERNAL: * Return the delete SQL string. */ public String deletionStringFor(DatabaseAccessor accessor) { return "DROP TABLE " + this.getName(); }
/** * INTERNAL: * Return a TableDefinition specifying sequence table. * Cache the table definition for re-use (during CREATE and DROP) */ public TableDefinition buildTableDefinition() { if (tableDefinition == null) { tableDefinition = new TableDefinition(); tableDefinition.setTable(getSequenceTable()); tableDefinition.setName(getSequenceTableName()); tableDefinition.setQualifier(getSequenceTableQualifier()); tableDefinition.addPrimaryKeyField(getSequenceNameFieldName(), String.class, 50); tableDefinition.addField(getSequenceCounterFieldName(), BigDecimal.class); tableDefinition.setIndexes(getSequenceTableIndexes()); } return tableDefinition; }
/** * INTERNAL: * Build the foreign key constraints. */ protected void buildFieldTypes(AbstractSession session) { // The ForeignKeyConstraint object is the newer way of doing things. // We support FieldDefinition.getForeignKeyFieldName() due to backwards compatibility // by converting it. To allow mixing both ways, we just add converted one to foreignKeys list. for (FieldDefinition field : getFields()) { if (field.getForeignKeyFieldName() != null) { addForeignKeyConstraint(buildForeignKeyConstraint(field, session.getPlatform())); } } }
/** * Drop (delete) the table named tableName from the database. */ public void dropTable(String tableName) throws EclipseLinkException { TableDefinition tableDefinition; tableDefinition = new TableDefinition(); tableDefinition.setName(tableName); dropObject(tableDefinition); }
/** * INTERNAL: * Return a TableDefinition specifying a unary sequence table. */ public TableDefinition buildTableDefinition() { TableDefinition definition = new TableDefinition(); definition.setName(getName()); definition.addField(getSequenceCounterFieldName(), BigDecimal.class); return definition; }
protected void addUniqueKeyConstraints(TableDefinition sourceTableDef, Map<String, List<List<String>>> uniqueConstraintsMap) { int serialNumber = -1; for (String name : uniqueConstraintsMap.keySet()) { List<List<String>> uniqueConstraints = uniqueConstraintsMap.get(name); for (List<String> uniqueConstraint : uniqueConstraints) { if (uniqueConstraint != null) { // To keep the serialNumber consecutive, increment it only // if the name is not specified. if (name == null || name.equals("")) { serialNumber++; } sourceTableDef.addUniqueKeyConstraint(sourceTableDef.buildUniqueKeyConstraint(name, uniqueConstraint, serialNumber, databasePlatform)); } } } } }
= DatabaseObjectDefinition.getFieldTypeDefinition(platform, type, typeName); String qualifiedName = table.getFullName() + '.' + name; boolean shouldPrintFieldIdentityClause = isIdentity && platform.shouldPrintFieldIdentityClause(session, qualifiedName); platform.printFieldTypeSize(writer, this, fieldType, shouldPrintFieldIdentityClause); if (platform.supportsUniqueColumns()) { if (!isPrimaryKey || table.getPrimaryKeyFieldNames().size() > 1) { platform.printFieldUnique(writer, shouldPrintFieldIdentityClause); } else { String constraintName = table.buildUniqueKeyConstraintName(table.getName(), table.getFields().indexOf(this), platform.getMaxUniqueKeyNameSize()); table.addUniqueKeyConstraint(constraintName, name);
TableDefinition tableDef = new TableDefinition(); Collection fields; if(supportsLocalTempTables()) { fieldDef.setIsPrimaryKey(true); tableDef.addField(fieldDef); tableDef.setCreationPrefix(getCreateTempTableSqlPrefix()); tableDef.setName(getTempTableForTable(table).getQualifiedNameDelimited(this)); tableDef.setCreationSuffix(getCreateTempTableSqlSuffix()); tableDef.buildCreationWriter(session, writer); } else { writer.write(getCreateTempTableSqlPrefix());
/** * INTERNAL: * Execute the SQL alter table constraint creation string. */ public void dropConstraints(AbstractSession session, Writer schemaWriter) throws EclipseLinkException { if (schemaWriter == null) { dropConstraintsOnDatabase(session); } else { if (session.getPlatform().supportsForeignKeyConstraints()){ for (ForeignKeyConstraint foreignKey : getForeignKeyMap().values()) { buildConstraintDeletionWriter(session, foreignKey, schemaWriter); writeLineSeperator(session, schemaWriter); } } if (session.getPlatform().supportsUniqueKeyConstraints() && (!session.getPlatform().requiresUniqueConstraintCreationOnTableCreate())) { for (UniqueKeyConstraint uniqueKey : getUniqueKeys()) { buildUniqueConstraintDeletionWriter(session, uniqueKey, schemaWriter); writeLineSeperator(session, schemaWriter); } } } }
/** * Build a table definition object from a database table object */ protected TableDefinition getTableDefFromDBTable(DatabaseTable databaseTable) { TableDefinition tableDefinition = this.tableMap.get(databaseTable.getName()); if (tableDefinition == null) { //table not built yet, simply built it tableDefinition = new TableDefinition(); tableDefinition.setTable(databaseTable); tableDefinition.setName(databaseTable.getNameDelimited(databasePlatform)); tableDefinition.setQualifier(databaseTable.getTableQualifier()); if (databaseTable.hasUniqueConstraints()) { addUniqueKeyConstraints(tableDefinition, databaseTable.getUniqueConstraints()); } if (databaseTable.hasIndexes()) { tableDefinition.getIndexes().addAll(databaseTable.getIndexes()); } if (databaseTable.getCreationSuffix() !=null){ tableDefinition.setCreationSuffix(databaseTable.getCreationSuffix()); } // Add the foreign key constraints that were set on the table. if (databaseTable.hasForeignKeyConstraints()) { tableDefinition.setUserDefinedForeignKeyConstraints(databaseTable.getForeignKeyConstraints()); } tableMap.put(databaseTable.getName(), tableDefinition); } return tableDefinition; }
/** * INTERNAL: * Return the index drop statement. */ public Writer buildIndexDeletionWriter(AbstractSession session, String key, Writer writer) throws ValidationException { try { String indexName = buildIndexName(getName(), key, session.getPlatform().getMaxIndexNameSize(), session.getPlatform()); writer.write(session.getPlatform().buildDropIndex(getFullName(), indexName)); } catch (IOException ioException) { throw ValidationException.fileError(ioException); } return writer; }
for (String pkField : targetTableDef.getPrimaryKeyFieldNames()) { String fkField = targetToFkField.get(pkField); if (fkField == null) { for (UniqueKeyConstraint uniqueConstraint : targetTableDef.getUniqueKeys()) { orderedFkFields.clear(); orderedTargetFields.clear(); ForeignKeyConstraint constraint = sourceTableDef.buildForeignKeyConstraint(fkFieldNames, targetFieldNames, targetTableDef, this.databasePlatform); constraint.setShouldCascadeOnDelete(cascadeOnDelete); sourceTableDef.addForeignKeyConstraint(constraint);
/** * Build a foreign key constraint. */ protected ForeignKeyConstraint buildForeignKeyConstraint(Vector fkFieldNames, Vector pkFieldNames, TableDefinition targetTable, DatabasePlatform platform) { assert fkFieldNames.size() > 0 && fkFieldNames.size() == pkFieldNames.size(); ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(); for(int i=0; i<fkFieldNames.size(); i++) { fkConstraint.getSourceFields().add(fkFieldNames.get(i)); fkConstraint.getTargetFields().add(pkFieldNames.get(i)); } fkConstraint.setTargetTable(targetTable.getFullName()); String fkFieldName = (String)fkFieldNames.get(0); String name = buildForeignKeyConstraintName(this.getName(), fkFieldName, platform.getMaxForeignKeyNameSize(), platform); fkConstraint.setName(name); return fkConstraint; }
/** * INTERNAL: * Return a TableDefinition specifying sequence table. */ public TableDefinition buildTableDefinition() { TableDefinition definition = new TableDefinition(); definition.setName(getSequenceTableName()); definition.addPrimaryKeyField(getSequenceNameFieldName(), String.class, 50); definition.addField(getSequenceCounterFieldName(), BigDecimal.class); return definition; }
void createForeignConstraints(final AbstractSession session, final Writer schemaWriter) throws ValidationException { if (schemaWriter == null) { createForeignConstraintsOnDatabase(session); return; } if (session.getPlatform().supportsForeignKeyConstraints()) { for (ForeignKeyConstraint foreignKey : getForeignKeyMap().values()) { if (! foreignKey.disableForeignKey()) { buildConstraintCreationWriter(session, foreignKey, schemaWriter); writeLineSeperator(session, schemaWriter); } } } }
/** * PUBLIC: * Add the field to the table, default sizes are used. * @param type is the Java class type corresponding to the database type. */ public void addField(String fieldName, Class type) { this.addField(new FieldDefinition(fieldName, type)); }
/** * Build a foreign key constraint using FieldDefinition.getForeignKeyFieldName(). */ protected ForeignKeyConstraint buildForeignKeyConstraint(FieldDefinition field, DatabasePlatform platform) { Vector sourceFields = new Vector(); Vector targetFields = new Vector(); ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(); DatabaseField tempTargetField = new DatabaseField(field.getForeignKeyFieldName()); DatabaseField tempSourceField = new DatabaseField(field.getName()); sourceFields.addElement(tempSourceField.getName()); targetFields.addElement(tempTargetField.getName()); fkConstraint.setSourceFields(sourceFields); fkConstraint.setTargetFields(targetFields); fkConstraint.setTargetTable(tempTargetField.getTable().getQualifiedNameDelimited(platform)); String tempName = buildForeignKeyConstraintName(this.getName(), tempSourceField.getName(), platform.getMaxForeignKeyNameSize(), platform); fkConstraint.setName(tempName); return fkConstraint; }
void createForeignConstraints(final AbstractSession session, final Writer schemaWriter) throws ValidationException { if (schemaWriter == null) { createForeignConstraintsOnDatabase(session); return; } if (session.getPlatform().supportsForeignKeyConstraints()) { for (ForeignKeyConstraint foreignKey : getForeignKeyMap().values()) { buildConstraintCreationWriter(session, foreignKey, schemaWriter).toString(); try { if (createSQLFiles) { schemaWriter.write(session.getPlatform().getStoredProcedureTerminationToken()); } schemaWriter.write("\n"); } catch (IOException exception) { throw ValidationException.fileError(exception); } } } }
/** * Build a table definition object from a database table object */ private TableDefinition getTableDefFromDBTable(DatabaseTable dbTbl) { TableDefinition tblDef = this.tableMap.get(dbTbl.getName()); if (tblDef == null) { //table not built yet, simply built it tblDef = new TableDefinition(); tblDef.setName(dbTbl.getNameDelimited(databasePlatform)); tblDef.setQualifier(dbTbl.getTableQualifier()); addUniqueKeyConstraints(tblDef, dbTbl.getUniqueConstraints()); tableMap.put(dbTbl.getName(), tblDef); } return tblDef; }
private void dropForeignConstraintsOnDatabase(final AbstractSession session) throws ValidationException { if ((!session.getPlatform().supportsForeignKeyConstraints()) || getForeignKeyMap().isEmpty()) { return; } for (ForeignKeyConstraint foreignKey : getForeignKeyMap().values()) { try { session.executeNonSelectingCall(new SQLCall(buildConstraintDeletionWriter(session, foreignKey, new StringWriter()).toString())); } catch (DatabaseException ex) {/* ignore */ } } }