/** {@inheritDoc} */ @Override public void flush(Connection connection, boolean removals, boolean force) throws SQLException { if (this.removed(connection, removals)) { return; } final ManagedInstance<?> instance = this.getManagedInstance(); final PluralMapping<?, ?, V> mapping = this.getMapping(); // forced creation of relations for the new entities if (force) { this.attachChildren(connection, instance, mapping, this.delegate.keySet()); return; } if (this.snapshot == null) { return; } if (removals) { // delete the removals final Collection<K> childrenRemoved = BatooUtils.subtract(this.snapshot.keySet(), this.delegate.keySet()); for (final K key : childrenRemoved) { mapping.detach(connection, instance, key, this.snapshot.get(key)); } } else { // create the additions final Collection<K> childrenAdded = BatooUtils.subtract(this.delegate.keySet(), this.snapshot.keySet()); this.attachChildren(connection, instance, mapping, childrenAdded); } }
/** * 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); } } }
private void attachChildren( Connection connection, final ManagedInstance<?> instance, final PluralMapping<?, ?, V> mapping, Collection<K> keySet) throws SQLException { final Joinable[] batch = new Joinable[SessionImpl.BATCH_SIZE]; final Iterator<K> i = keySet.iterator(); while (i.hasNext()) { int batchSize = 0; while (i.hasNext() && (batchSize < SessionImpl.BATCH_SIZE)) { final K key = i.next(); final V child = this.delegate.get(key); batch[batchSize] = new Joinable(key, child, 0); batchSize++; } if (batchSize > 0) { mapping.attach(connection, instance, batch, batchSize); } } }