private Map<String, String> initFindAssociatedEntityQuery(EntityKeyMetadata ownerEntityKeyMetadata, TupleTypeContext tupleTypeContext) { if ( tupleTypeContext != null ) { Map<String, AssociatedEntityKeyMetadata> allAssociatedEntityKeyMetadata = tupleTypeContext.getAllAssociatedEntityKeyMetadata(); Map<String, String> queries = new HashMap<>( allAssociatedEntityKeyMetadata.size() ); for ( Entry<String, AssociatedEntityKeyMetadata> entry : allAssociatedEntityKeyMetadata.entrySet() ) { EntityKeyMetadata targetKeyMetadata = entry.getValue().getEntityKeyMetadata(); String associationRole = tupleTypeContext.getRole( entry.getKey() ); StringBuilder queryBuilder = new StringBuilder( "MATCH " ); appendEntityNode( ENTITY_ALIAS, ownerEntityKeyMetadata, queryBuilder ); queryBuilder.append( " -[r:" ); appendRelationshipType( queryBuilder, associationRole ); queryBuilder.append( "]-> " ); appendEntityNode( "target", targetKeyMetadata, queryBuilder, 0, false ); queryBuilder.append( " RETURN target" ); queries.put( associationRole, queryBuilder.toString() ); } return queries; } return Collections.emptyMap(); }
/** * @param associationMetadata * @return true - is association through third table */ public static boolean isThirdTableAssociation(AssociationKeyMetadata associationMetadata) { return !associationMetadata.getTable().equals( associationMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata().getTable() ); } }
private Map<String, String> initUpdateToOneQuery(EntityKeyMetadata ownerEntityKeyMetadata, TupleTypeContext tupleTypeContext) { if ( tupleTypeContext != null ) { Map<String, AssociatedEntityKeyMetadata> allAssociatedEntityKeyMetadata = tupleTypeContext.getAllAssociatedEntityKeyMetadata(); Map<String, String> queries = new HashMap<>( allAssociatedEntityKeyMetadata.size() ); for ( Entry<String, AssociatedEntityKeyMetadata> entry : allAssociatedEntityKeyMetadata.entrySet() ) { String associationRole = tupleTypeContext.getRole( entry.getKey() ); AssociatedEntityKeyMetadata associatedEntityKeyMetadata = entry.getValue(); EntityKeyMetadata targetKeyMetadata = associatedEntityKeyMetadata.getEntityKeyMetadata(); StringBuilder queryBuilder = new StringBuilder( "MATCH " ); appendEntityNode( ENTITY_ALIAS, ownerEntityKeyMetadata, queryBuilder ); queryBuilder.append( ", " ); appendEntityNode( "target", targetKeyMetadata, queryBuilder, ownerEntityKeyMetadata.getColumnNames().length ); queryBuilder.append( " OPTIONAL MATCH (" ); queryBuilder.append( ENTITY_ALIAS ); queryBuilder.append( ")" ); queryBuilder.append( " -[r:" ); appendRelationshipType( queryBuilder, associationRole ); queryBuilder.append( "]-> () DELETE r " ); queryBuilder.append( "CREATE (" ); queryBuilder.append( ENTITY_ALIAS ); queryBuilder.append( ") -[:" ); appendRelationshipType( queryBuilder, associationRole ); queryBuilder.append( "]-> (target)" ); queries.put( associationRole, queryBuilder.toString() ); } return queries; } return Collections.emptyMap(); }
private static String initFindRelationshipQuery(EntityKeyMetadata ownerEntityKeyMetadata, AssociationKeyMetadata associationKeyMetadata) { int offset = 0; StringBuilder queryBuilder = new StringBuilder( "MATCH " ); queryBuilder.append( "(n:" ); queryBuilder.append( ENTITY ); queryBuilder.append( ":" ); appendLabel( ownerEntityKeyMetadata, queryBuilder ); appendProperties( ownerEntityKeyMetadata, queryBuilder ); queryBuilder.append( ") - " ); queryBuilder.append( "[r" ); queryBuilder.append( ":" ); appendRelationshipType( queryBuilder, associationKeyMetadata ); offset = ownerEntityKeyMetadata.getColumnNames().length; if ( associationKeyMetadata.getRowKeyIndexColumnNames().length > 0 ) { appendProperties( queryBuilder, associationKeyMetadata.getRowKeyIndexColumnNames(), offset ); queryBuilder.append( "] - (t" ); } else { queryBuilder.append( "] - (t" ); appendProperties( queryBuilder, associationKeyMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata().getColumnNames(), offset ); } queryBuilder.append( ")" ); queryBuilder.append( " RETURN r" ); return queryBuilder.toString(); }
queryBuilder.append( associationKeyMetadata.isInverse() ? "]- " : "]-> " ); if ( associationKeyMetadata.getAssociationKind() == AssociationKind.ASSOCIATION ) { EntityKeyMetadata associatedEntityMetadata = associationKeyMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata(); appendEntityNode( "target", associatedEntityMetadata, queryBuilder, 0, false );
appendProperties( queryBuilder, associationKeyMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata().getColumnNames(), offset );
private static String initCreateRelationshipQuery(EntityKeyMetadata ownerEntityKeyMetadata, AssociationKeyMetadata associationKeyMetadata) { EntityKeyMetadata targetEntityKeyMetadata = associationKeyMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata(); int offset = 0; StringBuilder queryBuilder = new StringBuilder( "MATCH " ); appendEntityNode( "n", ownerEntityKeyMetadata, queryBuilder ); queryBuilder.append( ", " ); offset += ownerEntityKeyMetadata.getColumnNames().length; appendEntityNode( "t", targetEntityKeyMetadata, queryBuilder, offset ); queryBuilder.append( " MERGE (n)" ); queryBuilder.append( " -[r" ); queryBuilder.append( ":" ); appendRelationshipType( queryBuilder, associationKeyMetadata ); offset = ownerEntityKeyMetadata.getColumnNames().length; if ( associationKeyMetadata.getRowKeyIndexColumnNames().length > 0 ) { offset += targetEntityKeyMetadata.getColumnNames().length; appendProperties( queryBuilder, associationKeyMetadata.getRowKeyIndexColumnNames(), offset ); } queryBuilder.append( "]-> (t)" ); queryBuilder.append( " RETURN r" ); return queryBuilder.toString(); }
private boolean isAJoinColumn(AssociationKey key) { AssociationKeyMetadata metadata = key.getMetadata(); if ( AssociationKind.EMBEDDED_COLLECTION.equals( metadata.getAssociationKind() ) ) { return false; } AssociatedEntityKeyMetadata entityKeyMetadata = metadata.getAssociatedEntityKeyMetadata(); return metadata.getTable().equals( entityKeyMetadata.getEntityKeyMetadata().getTable() ); }
public IgniteAssociationRowSnapshot(Object id, BinaryObject binaryObject, AssociationKeyMetadata associationMetadata) { this.id = id; this.binaryObject = binaryObject; this.associationMetadata = associationMetadata; this.thirdTableLink = IgniteAssociationSnapshot.isThirdTableAssociation( associationMetadata ); if ( this.thirdTableLink ) { Set<String> cn = new HashSet<>(); Collections.addAll( cn, associationMetadata.getRowKeyColumnNames() ); Collections.addAll( cn, associationMetadata.getAssociatedEntityKeyMetadata().getAssociationKeyColumns() ); this.columnNames = Collections.unmodifiableSet( cn ); this.isSimpleId = true; //vk: not used in this case } else { Set<String> idColumnNames = new HashSet<>(); EntityKeyMetadata entityKeyMetadata = associationMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata(); for ( String columnName : entityKeyMetadata.getColumnNames() ) { if ( entityKeyMetadata.isKeyColumn( columnName ) ) { idColumnNames.add( columnName ); } } if ( idColumnNames.isEmpty() ) { throw new UnsupportedOperationException( "There is no id column in entity " + entityKeyMetadata.getTable() + ". Hmm..." ); } this.columnNames = CollectionHelper.asSet( entityKeyMetadata.getColumnNames() ); this.isSimpleId = idColumnNames.size() == 1; } }
/** * Returns the key of the entity targeted by the represented association, retrieved from the given tuple. * * @param tuple the tuple from which to retrieve the referenced entity key * @return the key of the entity targeted by the represented association */ protected EntityKey getEntityKey(Tuple tuple, AssociatedEntityKeyMetadata associatedEntityKeyMetadata) { Object[] columnValues = new Object[ associatedEntityKeyMetadata.getAssociationKeyColumns().length]; int i = 0; for ( String associationKeyColumn : associatedEntityKeyMetadata.getAssociationKeyColumns() ) { columnValues[i] = tuple.get( associationKeyColumn ); i++; } return new EntityKey( associatedEntityKeyMetadata.getEntityKeyMetadata(), columnValues ); }
queryBuilder.append( EMBEDDED ); queryBuilder.append( ":" ); escapeIdentifier( queryBuilder, associationKey.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata().getTable() ); int index = 0; int embeddedNumber = 0;
private static boolean associationStoredWithinEntityEntry(AssociationKey key, AssociationContext associationContext) { final String cacheName = cacheName( key ); final String entityTableName = associationContext.getAssociationTypeContext().getAssociatedEntityKeyMetadata().getEntityKeyMetadata().getTable(); // in the case of embedded collections, the entityTableName and the cacheName are equal but they reference the name of a // join table so the association is not stored in the entity entry. return cacheName.equals( entityTableName ) && ! key.getMetadata().getAssociationKind().equals( AssociationKind.EMBEDDED_COLLECTION ); }
queryBuilder.append( EMBEDDED ); queryBuilder.append( ":" ); escapeIdentifier( queryBuilder, associationKey.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata().getTable() );
@Override public Object get(String column) { Object result = null; if ( !isEmpty() ) { if ( !thirdTableLink && associationMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata().isKeyColumn( column ) ) { result = isSimpleId ? id : ( (BinaryObject) id ).field( StringHelper.stringAfterPoint( column ) ); } else { result = binaryObject.field( StringHelper.realColumnName( column ) ); } } return result; }
public Object createAssociationKeyObject( RowKey rowKey, AssociationKeyMetadata keyMetadata ) { Object result = null; if ( IgniteAssociationSnapshot.isThirdTableAssociation( keyMetadata ) ) { result = UUID.randomUUID().toString(); } else { String associationKeyColumns[] = keyMetadata.getAssociatedEntityKeyMetadata().getAssociationKeyColumns(); if ( associationKeyColumns.length == 1 ) { result = rowKey.getColumnValue( associationKeyColumns[0] ); } else { BinaryObjectBuilder builder = createBinaryObjectBuilder( findKeyType( keyMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata() ) ); for ( int i = 0; i < associationKeyColumns.length; i++ ) { builder.setField( StringHelper.stringAfterPoint( associationKeyColumns[i] ), rowKey.getColumnValue( associationKeyColumns[i] ) ); } result = builder.build(); } } return result; }
/** * Returns the entity key on the other side of association row represented by the given row key. * <p> * <b>Note:</b> May only be invoked if the row key actually contains all the columns making up that entity key. * Specifically, it may <b>not</b> be invoked if the association has index columns (maps, ordered collections), as * the entity key columns will not be part of the row key in this case. */ private EntityKey getEntityKey(AssociationKey associationKey, RowKey rowKey) { String[] associationKeyColumns = associationKey.getMetadata().getAssociatedEntityKeyMetadata().getAssociationKeyColumns(); Object[] columnValues = new Object[associationKeyColumns.length]; int i = 0; for ( String associationKeyColumn : associationKeyColumns ) { columnValues[i] = rowKey.getColumnValue( associationKeyColumn ); i++; } EntityKeyMetadata entityKeyMetadata = associationKey.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata(); return new EntityKey( entityKeyMetadata, columnValues ); }
/** * Returns the entity key on the other side of association row represented by the given row key. * <p> * <b>Note:</b> May only be invoked if the row key actually contains all the columns making up that entity key. * Specifically, it may <b>not</b> be invoked if the association has index columns (maps, ordered collections), as * the entity key columns will not be part of the row key in this case. */ private EntityKey getEntityKey(AssociationKey associationKey, RowKey rowKey) { String[] associationKeyColumns = associationKey.getMetadata().getAssociatedEntityKeyMetadata().getAssociationKeyColumns(); Object[] columnValues = new Object[associationKeyColumns.length]; int i = 0; for ( String associationKeyColumn : associationKeyColumns ) { columnValues[i] = rowKey.getColumnValue( associationKeyColumn ); i++; } EntityKeyMetadata entityKeyMetadata = associationKey.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata(); return new EntityKey( entityKeyMetadata, columnValues ); }
public IgniteEmbeddedAssociationSnapshot(AssociationKey associationKey, Tuple tuple) { this.associationMetadata = associationKey.getMetadata(); this.tuple = tuple; BinaryObject obj = ( (IgniteTupleSnapshot) tuple.getSnapshot() ).getCacheValue(); Object objects[] = obj != null ? (Object[]) obj.field( StringHelper.realColumnName( associationMetadata.getCollectionRole() ) ) : null; rows = new HashMap<>(); if ( objects != null ) { String rowKeyColumnNames[] = new String[ associationMetadata.getRowKeyColumnNames().length ]; for ( int i = 0; i < rowKeyColumnNames.length; i++ ) { rowKeyColumnNames[i] = StringHelper.stringAfterPoint( associationMetadata.getRowKeyColumnNames()[i] ); } for ( int i = 0; i < objects.length; i++ ) { BinaryObject itemObject = (BinaryObject) objects[i]; Object rowKeyColumnValues[] = new Object[rowKeyColumnNames.length]; for ( int j = 0; j < rowKeyColumnNames.length; j++ ) { rowKeyColumnValues[j] = itemObject.field( rowKeyColumnNames[j] ); } RowKey rowKey = new RowKey( associationMetadata.getRowKeyColumnNames(), rowKeyColumnValues ); this.rows.put( rowKey, new IgniteTupleSnapshot( null, itemObject, associationMetadata.getAssociatedEntityKeyMetadata().getEntityKeyMetadata() ) ); } } }
boolean hasChanges = false; for ( String columnName : currentStateTuple.getColumnNames() ) { if ( key.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata().isKeyColumn( columnName ) ) { continue; if ( key.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata().isKeyColumn( columnName ) ) { continue; EntityKeyMetadata itemMetadata = key.getMetadata().getAssociatedEntityKeyMetadata().getEntityKeyMetadata(); for ( AssociationOperation op : association.getOperations() ) { int index = findIndexByRowKey( associationObjects, op.getKey(), indexColumnName );