@Override public String toString() { return getSql(); }
void addParent( ParentRowReference ref ) { parentToRef.put( ref.getTarget(), ref ); }
protected Set<SQLIdentifier> getAutogenColumns( boolean propagateNonFidAutoGenColumns ) { Set<SQLIdentifier> cols = new LinkedHashSet<SQLIdentifier>(); if ( propagateNonFidAutoGenColumns ) { if ( mgr.getGenColumns( table ) != null ) { cols.addAll( mgr.getGenColumns( table ) ); } } return cols; }
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 ); }
void assign( Feature feature ) throws FeatureStoreException { FeatureTypeMapping ftMapping = mgr.getSchema().getFtMapping( feature.getName() ); this.table = ftMapping.getFtTable(); this.fidMapping = ftMapping.getFidMapping(); switch ( mgr.getIdGenMode() ) { case GENERATE_NEW: { Map<SQLIdentifier, IDGenerator> keyColumnToGenerator = new HashMap<SQLIdentifier, IDGenerator>(); for ( Pair<SQLIdentifier, BaseType> columnAndType : ftMapping.getFidMapping().getColumns() ) { SQLIdentifier fidColumn = columnAndType.first; keyColumnToGenerator.put( fidColumn, ftMapping.getFidMapping().getIdGenerator() ); generateImmediateKeys( keyColumnToGenerator ); } break; } case USE_EXISTING: { preInsertUseExisting( ftMapping ); break; } case REPLACE_DUPLICATE: { throw new UnsupportedOperationException( "REPLACE_DUPLICATE id generation mode is not implemented yet." ); } } newId = buildNewFid(); }
@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 ); }
featureRow = lookupFeatureRow( feature ); buildInsertRows( feature, particleMapping, featureRow, allRows ); if ( !insertRow.hasParents() ) { rootRows.add( insertRow ); processHeap(); LOG.debug( "After heap run: uninserted rows: " + delayedRows.size() + ", root rows: " + rootRows.size() );
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; }
IdAnalysis analysis = mgr.getSchema().analyzeId( getOriginalId() ); idKernels = analysis.getIdKernels(); if ( !analysis.getFeatureType().getName().equals( ftMapping.getFeatureType() ) ) { String msg = "Cannot insert feature with id '" + getOriginalId() + "' and id generation mode 'UseExisting'. " + "Id does not match configured feature id pattern."; PrimitiveType type = new PrimitiveType( baseType ); PrimitiveValue primitiveValue = new PrimitiveValue( value, type ); PrimitiveParticleConverter primitiveConverter = mgr.getDialect().getPrimitiveConverter( idColumn.first.getName(), type ); addPreparedArgument( idColumn.getFirst(), primitiveValue, primitiveConverter );
private FeatureRow lookupFeatureRow( Feature feature ) throws FeatureStoreException { FeatureRow featureRow = origFidToFeatureRow.get( feature.getId() ); if ( featureRow == null ) { featureRow = new FeatureRow( this, feature.getId() ); delayedRows.add( featureRow ); if ( feature.getId() != null ) { origFidToFeatureRow.put( feature.getId(), featureRow ); } } if ( !featureRow.isAssigned() ) { featureRow.assign( feature ); } return featureRow; }
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 ); } }
allRows.add( featureRow ); buildInsertRows( feature, mapping, featureRow, allRows ); if ( !insertRow.hasParents() ) { rootRows.add( insertRow ); processHeap(); LOG.debug( "After heap run: uninserted rows: " + delayedRows.size() + ", root rows: " + rootRows.size() );
private String buildNewFid() throws FeatureStoreException { if ( get( fidMapping.getColumns().get( 0 ).getFirst() ) == null ) { // fid columns not available yet return null; } String newId = fidMapping.getPrefix(); List<Pair<SQLIdentifier, BaseType>> fidColumns = fidMapping.getColumns(); newId += checkFIDParticle( fidColumns.get( 0 ).first ); for ( int i = 1; i < fidColumns.size(); i++ ) { newId += fidMapping.getDelimiter() + checkFIDParticle( fidColumns.get( i ).first ); } return newId; }
/** * 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() ); } } } }
private int getSequenceNextVal( String sequenceName ) throws FeatureStoreException { String sql = mgr.getDialect().getSelectSequenceNextVal( sequenceName ); Statement stmt = null; ResultSet rs = null; try { stmt = mgr.getConnection().createStatement(); LOG.debug( "Determing feature ID from db sequence: " + sql ); rs = stmt.executeQuery( sql ); if ( rs.next() ) { return rs.getInt( 1 ); } else { String msg = "Error determining ID from db sequence. No value returned for: " + sql; throw new FeatureStoreException( msg ); } } catch ( SQLException e ) { String msg = "Error determining ID from db sequence. No value returned for: " + sql; throw new FeatureStoreException( msg, e ); } finally { JDBCUtils.close( rs, stmt, null, LOG ); } }
private void addRelationallyMappedMultiProperty( ParsedPropertyReplacement replacement, Mapping mapping, FeatureTypeMapping ftMapping, List<ResourceId> list ) throws FilterEvaluationException, FeatureStoreException, SQLException { UpdateAction action = replacement.getUpdateAction(); if ( action == null ) { action = UpdateAction.INSERT_AFTER; } switch ( action ) { case INSERT_BEFORE: case REMOVE: case REPLACE: LOG.warn( "Updating of multi properties is currently only supported for 'insertAfter' update action. Omitting." ); break; case INSERT_AFTER: break; default: break; } InsertRowManager mgr = new InsertRowManager( fs, conn, null ); List<Property> props = Collections.singletonList( replacement.getNewValue() ); for ( ResourceId id : list ) { IdAnalysis analysis = schema.analyzeId( id.getRid() ); FeatureType featureType = schema.getFeatureType( ftMapping.getFeatureType() ); Feature f = featureType.newFeature( id.getRid(), props, null ); mgr.updateFeature( f, ftMapping, analysis.getIdKernels(), mapping, replacement ); } }
private FeatureRow lookupFeatureRow( String fid ) throws FeatureStoreException { FeatureRow featureRow = origFidToFeatureRow.get( fid ); if ( featureRow == null ) { featureRow = new FeatureRow( this, fid ); origFidToFeatureRow.put( fid, featureRow ); delayedRows.add( featureRow ); } return featureRow; }
private Object checkFIDParticle( SQLIdentifier column ) throws FeatureStoreException { Object value = get( column ); if ( value == null ) { throw new FeatureStoreException( "FIDMapping error: No value for feature id column '" + column + "'." ); } return value; }
@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; }
/** * Creates a new {@link JoinRow} instance. * * @param mgr * manager for the insert rows, must not be <code>null</code> * @param join * table join that leads to this join row, must not be <code>null</code> * @throws FeatureStoreException */ JoinRow( InsertRowManager mgr, TableJoin join ) throws FeatureStoreException { super( mgr ); this.table = join.getToTable(); generateImmediateKeys( join.getKeyColumnToGenerator() ); }