/** * Generates all keys except those that are created by the DB on INSERT. * * @param keyColumnToGenerator * columns and their generators, may be <code>null</code> (no generators) * @throws FeatureStoreException * if an error occurs during generation */ protected void generateImmediateKeys( Map<SQLIdentifier, IDGenerator> keyColumnToGenerator ) throws FeatureStoreException { if ( keyColumnToGenerator != null ) { for ( SQLIdentifier autoKeyColumn : keyColumnToGenerator.keySet() ) { IDGenerator idGenerator = keyColumnToGenerator.get( autoKeyColumn ); if ( idGenerator instanceof SequenceIDGenerator ) { int seqVal = getSequenceNextVal( ( (SequenceIDGenerator) idGenerator ).getSequence() ); LOG.debug( "Got key value for column '" + autoKeyColumn.getName() + "' from sequence: " + seqVal ); addPreparedArgument( autoKeyColumn, seqVal ); } else if ( idGenerator instanceof UUIDGenerator ) { String uuid = UUID.randomUUID().toString(); LOG.debug( "Got key value for column '" + autoKeyColumn.getName() + "' from UUID: " + uuid ); addPreparedArgument( autoKeyColumn, uuid ); } else if ( idGenerator instanceof AutoIDGenerator ) { LOG.debug( "Key for column '" + autoKeyColumn.getName() + "' will be generated on insert by DB." ); } else { LOG.warn( "Unhandled ID generator: " + idGenerator.getClass().getName() ); } } } }
@Override protected Set<SQLIdentifier> getAutogenColumns( boolean propagateNonFidAutoGenColumns ) { Set<SQLIdentifier> cols = super.getAutogenColumns( propagateNonFidAutoGenColumns ); for ( Pair<SQLIdentifier, BaseType> fidColumn : fidMapping.getColumns() ) { cols.add( fidColumn.first ); } return cols; }
@Override public String toString() { return getSql(); }
public void processHeap() throws SQLException, FeatureStoreException { while ( !rootRows.isEmpty() ) { List<InsertRow> rootRemoves = new ArrayList<InsertRow>(); List<InsertRow> rootAdds = new ArrayList<InsertRow>(); for ( InsertRow row : rootRows ) { LOG.debug( "Inserting row " + row ); row.performInsert( conn, rowToChildRows.get( row ) != null ); delayedRows.remove( row ); rootRemoves.add( row ); // update child rows List<InsertRow> childRows = rowToChildRows.get( row ); if ( childRows != null ) { for ( InsertRow childRow : childRows ) { LOG.debug( "Child row: " + childRow ); childRow.removeParent( row ); if ( !childRow.hasParents() ) { rootAdds.add( childRow ); } } rowToChildRows.remove( row ); } } rootRows.removeAll( rootRemoves ); rootRows.addAll( rootAdds ); } }
if ( primitiveValue != null ) { String column = ( (DBField) me ).getColumn(); currentRow.addPreparedArgument( column, primitiveValue, converter ); ParticleConverter<Geometry> converter = (ParticleConverter<Geometry>) fs.getConverter( mapping ); String column = ( (DBField) me ).getColumn(); currentRow.addPreparedArgument( column, geom, converter ); } else { String column = ( (DBField) me ).getColumn(); row.addPreparedArgument( column, href ); currentRow.addParent( ref ); List<InsertRow> children = rowToChildRows.get( subFeatureRow ); if ( children == null ) { currentRow.removeParent( subFeatureRow ); if ( join.isNumberedOrder() ) { for ( SQLIdentifier col : join.getOrderColumns() ) { if ( currentRow.get( col ) == null ) { currentRow.addLiteralValue( col, "" + childIdx++ );
void removeParent( InsertRow parent ) { ParentRowReference ref = parentToRef.get( parent ); // propagate values from parent (foreign key values) List<SQLIdentifier> fromColumns = ref.getKeyPropagation().getPrimaryKeyColumns(); List<SQLIdentifier> toColumns = ref.getKeyPropagation().getForeignKeyColumns(); for ( int i = 0; i < fromColumns.size(); i++ ) { SQLIdentifier fromColumn = fromColumns.get( i ); SQLIdentifier toColumn = toColumns.get( i ); Object key = parent.get( fromColumn ); if ( key == null ) { String msg = "Unable to create foreign key relation. Encountered NULL value for foreign key column '" + fromColumn + "'."; throw new IllegalArgumentException( msg ); } addPreparedArgument( toColumn, key ); } // if parent is a feature row, set value for href column if ( ref.isHrefed( this ) && parent instanceof FeatureRow ) { addPreparedArgument( ref.getHrefColum( this ), "#" + ( (FeatureRow) parent ).getNewId() ); } parentToRef.remove( parent ); }
String sql = getSql(); PreparedStatement stmt = null; Set<SQLIdentifier> autoGenColumns = getAutogenColumns( propagateNonFidAutoGenColumns ); if ( autoGenColumns.isEmpty() ) { stmt = conn.prepareStatement( sql );
if ( !insertRow.hasParents() ) { rootRows.add( insertRow );
@Override void performInsert( Connection conn, boolean propagateAutoGenColumns ) throws SQLException, FeatureStoreException { super.performInsert( conn, propagateAutoGenColumns ); newId = buildNewFid(); if ( newId == null ) { String msg = "Internal/configuration error. Feature id must be assignable after feature row INSERT."; throw new FeatureStoreException( msg ); } // clear everything, but keep key columns (values may still be needed by referencing rows) Map<SQLIdentifier, Object> keyColumnToValue = new HashMap<SQLIdentifier, Object>(); Set<SQLIdentifier> genColumns = mgr.getKeyColumns( table ); if ( genColumns != null ) { for ( SQLIdentifier genColumn : genColumns ) { keyColumnToValue.put( genColumn, get( genColumn ) ); } } columnToLiteral.clear(); columnToObject.clear(); columnToObject.putAll( keyColumnToValue ); }
private JoinRow buildJoinRow( InsertRow row, TableJoin join ) throws FeatureStoreException { JoinRow newRow = new JoinRow( this, join ); delayedRows.add( newRow ); KeyPropagation keyPropagation = tableDeps.findKeyPropagation( join.getFromTable(), join.getFromColumns(), join.getToTable(), join.getToColumns() ); if ( keyPropagation == null ) { String msg = "Internal error: table dependencies don't contain join " + join; throw new FeatureStoreException( msg ); } if ( keyPropagation.getSourceTable().equals( join.getFromTable() ) ) { ParentRowReference ref = new ParentRowReference( row, keyPropagation ); newRow.addParent( ref ); List<InsertRow> children = rowToChildRows.get( row ); if ( children == null ) { children = new ArrayList<InsertRow>(); rowToChildRows.put( row, children ); } children.add( newRow ); } else { ParentRowReference ref = new ParentRowReference( newRow, keyPropagation ); row.addParent( ref ); List<InsertRow> children = new ArrayList<InsertRow>(); rowToChildRows.put( newRow, children ); children.add( row ); } return newRow; }
if ( !insertRow.hasParents() ) { rootRows.add( insertRow );