/** * Performs the foreign key DDL operations. * * @param datasource the datasource * @param ddlMode the DDL Mode * @param entity the entity to perform DDL against * @throws BatooException thrown in case of an underlying exception * @since $version * @author hceylan */ public void performForeignKeysDdl( DataSource datasource, DDLMode ddlMode, EntityTypeImpl<?> entity) { if ((ddlMode == DDLMode.NONE)) { return; } MetamodelImpl.LOG.info( "Performing foreign key DDL operations for entiy {0}, mode {1}", entity.getName(), ddlMode); for (final EntityTable table : entity.getTables()) { // skip parent tables if (table.getEntity() != entity) { continue; } MetamodelImpl.LOG.info( "Performing foreign key DDL operations for table {0}, mode {1}", table.getQName(), ddlMode); for (final ForeignKey foreignKey : table.getForeignKeys()) { this.jdbcAdaptor.createForeignKey(datasource, foreignKey); } } for (final AssociationMapping<?, ?, ?> mapping : entity.getAssociations()) { final JoinTable table = mapping.getTable(); // skip not applicable join tables if ((table == null) || (table.getEntity() != entity)) { continue; } MetamodelImpl.LOG.info( "Performing foreign key DDL operations for join table {0}, mode {1}", table.getQName(), ddlMode); for (final ForeignKey foreignKey : table.getForeignKeys()) { this.jdbcAdaptor.createForeignKey(datasource, foreignKey); } } for (final PluralMapping<?, ?, ?> mapping : entity.getMappingsPlural()) { if (!mapping.isAssociation()) { final AbstractTable table = (AbstractTable) mapping.getTable(); MetamodelImpl.LOG.info( "Performing foreign key DDL operations for join table {0}, mode {1}", table.getQName(), ddlMode); for (final ForeignKey foreignKey : table.getForeignKeys()) { this.jdbcAdaptor.createForeignKey(datasource, foreignKey); } } } }
/** * Drops all the tables in the database. * * @param datasource the datasource * @since $version * @author hceylan */ public void dropAllTables(DataSource datasource) { final Set<AbstractTable> tables = Sets.newHashSet(); for (final EntityTypeImpl<?> entity : this.entities.values()) { // collect the entity tables for (final EntityTable table : entity.getTables()) { // if table belongs to parent then skip if (table.getEntity() != entity) { continue; } tables.add(table); } // collect the join tables for (final AssociationMapping<?, ?, ?> mapping : entity.getAssociations()) { final JoinTable table = mapping.getTable(); // skip not applicable tables if ((table == null) || (table.getEntity() != entity)) { continue; } tables.add(table); } // collect the join tables for (final PluralMapping<?, ?, ?> mapping : entity.getMappingsPlural()) { if (!mapping.isAssociation()) { final AbstractTable table = (AbstractTable) mapping.getTable(); if (table != null) { tables.add(table); } } } } try { this.jdbcAdaptor.dropAllForeignKeys(datasource, tables); this.jdbcAdaptor.dropAllTables(datasource, tables); this.jdbcAdaptor.dropAllSequences(datasource, this.sequenceGenerators.values()); } catch (final SQLException e) { throw new PersistenceException("Cannot drop tables", e); } }
/** * Performs the table DDL operations. * * @param datasource the datasource * @param ddlMode the DDL Mode * @param entity the entity to perform DDL against * @throws BatooException thrown in case of an underlying exception * @since $version * @author hceylan */ public void performTablesDdl(DataSource datasource, DDLMode ddlMode, EntityTypeImpl<?> entity) { MetamodelImpl.LOG.info( "Performing DDL operations for entity {0}, mode {1}", entity.getName(), ddlMode); // create the entity tables for (final EntityTable table : entity.getTables()) { // if table belongs to parent then skip if (table.getEntity() != entity) { continue; } MetamodelImpl.LOG.info( "Performing DDL operations for {0}, mode {1}", table.getQName(), ddlMode); this.jdbcAdaptor.createOrUpdateTable(table, datasource, ddlMode); } // create the join tables for (final AssociationMapping<?, ?, ?> mapping : entity.getAssociations()) { final JoinTable table = mapping.getTable(); // skip not applicable tables if ((table == null) || (table.getEntity() != entity)) { continue; } this.jdbcAdaptor.createOrUpdateTable(mapping.getTable(), datasource, ddlMode); } // create the join tables for (final PluralMapping<?, ?, ?> mapping : entity.getMappingsPlural()) { if (!mapping.isAssociation()) { final AbstractTable table = (AbstractTable) mapping.getTable(); this.jdbcAdaptor.createOrUpdateTable(table, datasource, ddlMode); } } }