SerializableAssociationKey(AssociationKey key) { columnNames = key.getColumnNames(); columnValues = key.getColumnValues(); table = key.getTable(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append( getClass().getSimpleName() ); sb.append( "[" ); sb.append( getEntityKey() ); sb.append( ", collectionRole=" ); sb.append( getAssociationKey().getMetadata().getCollectionRole() ); sb.append( "]" ); return sb.toString(); }
@Override public EntityKey getEntityKey() { return getAssociationKey().getEntityKey(); }
PerTableSerializableAssociationKey(AssociationKey key) { columnNames = key.getColumnNames(); columnValues = key.getColumnValues(); }
/** * Returns the value of the given column if part of this key. Use {@link AssociationKeyMetadata#isKeyColumn(String)} * to check whether a given column is part of this key prior to invoking this method. * * @param columnName the name of interest * @return the value of the given column. */ public Object getColumnValue(String columnName) { for ( int i = 0; i < getColumnNames().length; i++ ) { String name = getColumnNames()[i]; if ( name.equals( columnName ) ) { return getColumnValues()[i]; } } throw new AssertionFailure( String.format( "Given column %s is not part of this key: %s", columnName, this.toString() ) ); } }
protected String initCreateEmbeddedAssociationQuery(AssociationKey associationKey, EntityKey embeddedKey) { String collectionRole = associationKey.getMetadata().getCollectionRole(); String[] embeddedColumnNames = embeddedKey.getColumnNames(); Object[] embeddedColumnValues = embeddedKey.getColumnValues(); String[] columnNames = associationKey.getEntityKey().getMetadata().getColumnNames(); StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append( matchOwnerEntityNode ); if ( isCollectionOfPrimitives( collectionRole, embeddedColumnNames ) ) { createRelationshipForCollectionOfPrimitivesOrMap( associationKey, collectionRole, columnNames, queryBuilder ); } else { createRelationshipforCollectionOfComponents( associationKey, collectionRole, embeddedColumnNames, embeddedColumnValues, queryBuilder ); } return queryBuilder.toString(); }
String[] rowKeyColumnNames = associationKey.getMetadata().getRowKeyColumnNames(); for ( int i = 0; i < associationKey.getColumnNames().length; i++ ) { String key = associationKey.getEntityKey().getColumnNames()[i]; if ( ownerNode.containsKey( key ) ) { properties.put( associationKey.getColumnNames()[i], ownerNode.get( key ) );
@Override public void removeAssociation(AssociationKey key, AssociationContext associationContext) { if ( key.getMetadata().isInverse() ) { return; Table tableMetadata = provider.getMetaDataCache().get( key.getTable() ); Set<String> keyColumnNames = new HashSet<String>(); for ( Object columnObject : tableMetadata.getPrimaryKey().getColumns() ) { for ( String columnName : key.getColumnNames() ) { if ( !keyColumnNames.contains( columnName ) ) { deleteSelection.column( quote( columnName ) ); Delete delete = deleteSelection.from( quote( key.getTable() ) ); List<Object> columnValues = new LinkedList<>(); for ( String columnName : key.getColumnNames() ) { if ( keyColumnNames.contains( columnName ) ) { delete.where( eq( quote( columnName ), QueryBuilder.bindMarker( columnName ) ) ); columnValues.add( key.getColumnValue( columnName ) ); hasWhereClause = true;
private String createAssociationQuery(AssociationKey key, boolean selectObjects) { StringBuilder sb = new StringBuilder(); if ( selectObjects ) { sb.append( "SELECT _KEY, _VAL FROM " ); } else { sb.append( "SELECT _KEY FROM " ); } sb.append( key.getMetadata().getTable() ).append( " WHERE " ); boolean first = true; for ( String columnName : key.getColumnNames() ) { if ( !first ) { sb.append( " AND " ); } else { first = false; } sb.append( StringHelper.realColumnName( columnName ) ).append( "=?" ); } return sb.toString(); }
private void createRelationshipForCollectionOfPrimitivesOrMap(AssociationKey associationKey, String collectionRole, String[] columnNames, StringBuilder queryBuilder) { String relationshipType = collectionRole; if ( isPartOfEmbedded( collectionRole ) ) { queryBuilder.append( " MERGE (owner) " ); String[] pathToEmbedded = appendEmbeddedNodes( collectionRole, queryBuilder ); relationshipType = pathToEmbedded[pathToEmbedded.length - 1]; queryBuilder.append( " CREATE (e) -[r:" ); } else { queryBuilder.append( " CREATE (owner) -[r:" ); } escapeIdentifier( queryBuilder, relationshipType ); int offset = ownerEntityKeyMetadata.getColumnNames().length; appendProperties( queryBuilder, associationKey.getMetadata().getRowKeyIndexColumnNames(), offset ); queryBuilder.append( "]-> (new:" ); queryBuilder.append( EMBEDDED ); queryBuilder.append( ":" ); escapeIdentifier( queryBuilder, associationKey.getTable() ); queryBuilder.append( " {" ); // THe name of the property is the same as the relationship type escapeIdentifier( queryBuilder, relationshipType ); queryBuilder.append( ": {" ); offset += associationKey.getMetadata().getRowKeyIndexColumnNames().length; queryBuilder.append( offset ); queryBuilder.append( "}" ); queryBuilder.append( "}" ); queryBuilder.append( ")" ); queryBuilder.append( " RETURN r" ); }
IgniteCache<Object, BinaryObject> associationCache = provider.getAssociationCache( key.getMetadata() ); throw log.cacheNotFound( key.getMetadata().getTable() ); if ( key.getMetadata().getAssociationKind() == AssociationKind.ASSOCIATION ) { QueryHints.Builder hintsBuilder = new QueryHints.Builder(); Boolean isCollocated = associationContext.getAssociationTypeContext().getOptionsContext().getUnique( CollocatedAssociationOption.class ); SqlFieldsQuery sqlQuery = provider.createSqlFieldsQueryWithLog( createAssociationQuery( key, true ), hints, key.getColumnValues() ); Iterable<List<?>> list = executeWithHints( associationCache, sqlQuery, hints ); else if ( key.getMetadata().getAssociationKind() == AssociationKind.EMBEDDED_COLLECTION ) { result = new Association( new IgniteEmbeddedAssociationSnapshot( key, associationContext.getEntityTuplePointer().getTuple() ) ); throw new UnsupportedOperationException( "Unknown association kind " + key.getMetadata().getAssociationKind() );
private static String cacheName(AssociationKey key) { return key.getTable(); }
@Override public Object get(String column) { return associationKey.getMetadata().isKeyColumn( column ) ? associationKey.getColumnValue( column ) : accessor.get( row, column ); }
private static Set<String> buildColumnNames(AssociationKey associationKey, Set<String> columnsFromRow) { Set<String> columnNames = new HashSet<String>( columnsFromRow.size() + associationKey.getColumnNames().length ); columnNames.addAll( columnsFromRow ); Collections.addAll( columnNames, associationKey.getColumnNames() ); return columnNames; }
/** * Converting association key to cache key * * @param key - association key * @return string key */ public Object createParentKeyObject(AssociationKey key) { Object result = null; if ( key.getColumnValues().length == 1 ) { result = key.getColumnValues()[0]; } else { throw new UnsupportedOperationException( "Not implemented yet" ); } return result; }
@Override public AssociationKey readObject(ObjectInput input) throws IOException, ClassNotFoundException { VersionChecker.readAndCheckVersion( input, VERSION, AssociationKey.class ); String tableName = input.readUTF(); String[] columnNames = (String[]) input.readObject(); Object[] values = (Object[]) input.readObject(); AssociationKeyMetadata associationKeyMetadata = new DefaultAssociationKeyMetadata.Builder() .table( tableName ) .columnNames( columnNames ) .build(); // the engine never accesses deserialized key instances so it's ok to leave the additional attributes // null; we should still consider extract these attributes to avoid potential confusion return new AssociationKey( associationKeyMetadata, values, null ); }
@Override protected String initCreateEmbeddedAssociationQuery(AssociationKey associationKey, EntityKey embeddedKey) { String collectionRole = associationKey.getMetadata().getCollectionRole(); String[] embeddedColumnNames = embeddedKey.getColumnNames(); Object[] embeddedColumnValues = embeddedKey.getColumnValues(); String[] columnNames = associationKey.getEntityKey().getMetadata().getColumnNames(); StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append( matchOwnerEntityNode ); if ( isCollectionOfPrimitives( collectionRole, embeddedColumnNames ) ) { createRelationshipForCollectionOfPrimitivesOrMap( associationKey, collectionRole, columnNames, queryBuilder ); } else { createRelationshipforCollectionOfComponents( associationKey, collectionRole, embeddedColumnNames, embeddedColumnValues, queryBuilder ); } return queryBuilder.toString(); }
public static PersistentAssociationKey fromAssociationKey(AssociationKey key) { return new PersistentAssociationKey( key.getColumnNames(), key.getColumnValues() ); }
private static Map<String, Object> collectProperties(Relationship relationship, AssociationKey associationKey, AssociatedEntityKeyMetadata associatedEntityKeyMetadata) { Map<String, Object> properties = new HashMap<String, Object>(); String[] rowKeyColumnNames = associationKey.getMetadata().getRowKeyColumnNames(); if ( isPartOfEmbedded( targetColumnName ) ) { String collectionRole = associationKey.getMetadata().getCollectionRole(); if ( targetColumnName.equals( collectionRole ) ) { for ( int i = 0; i < associationKey.getColumnNames().length; i++ ) { if ( ownerNode.hasProperty( associationKey.getEntityKey().getColumnNames()[i] ) ) { properties.put( associationKey.getColumnNames()[i], ownerNode.getProperty( associationKey.getEntityKey().getColumnNames()[i] ) );
if ( key.getMetadata().isInverse() ) { return; IgniteCache<Object, BinaryObject> associationCache = provider.getAssociationCache( key.getMetadata() ); if ( key.getMetadata().getAssociationKind() == AssociationKind.ASSOCIATION ) { Map<Object, BinaryObject> changedObjects = new HashMap<>(); Set<Object> removedObjects = new HashSet<>(); boolean thirdTableAssociation = IgniteAssociationSnapshot.isThirdTableAssociation( key.getMetadata() ); if ( clearBo != null ) { BinaryObjectBuilder clearBoBuilder = provider.createBinaryObjectBuilder( clearBo ); for ( String columnName : key.getColumnNames() ) { clearBoBuilder.removeField( columnName ); for ( String columnName : key.getMetadata().getRowKeyIndexColumnNames() ) { clearBoBuilder.removeField( columnName ); Object currentId = null; if ( currentStateTuple.getSnapshot().isEmpty() ) { currentId = provider.createAssociationKeyObject( op.getKey(), key.getMetadata() ); boolean hasChanges = false; for ( String columnName : currentStateTuple.getColumnNames() ) { if ( key.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata().isKeyColumn( columnName ) ) { continue; putBoBuilder = provider.createBinaryObjectBuilder( provider.getEntityTypeName( key.getMetadata().getTable() ) );