/** * PUBLIC: * Returns the name of the reference table */ public String getReferenceTableName() { if (getReferenceTable() == null) { return null; } return getReferenceTable().getName(); }
/** * PUBLIC: * Returns the qualified name of the reference table */ public String getReferenceTableQualifiedName() {//CR#2407 if (getReferenceTable() == null) { return null; } return getReferenceTable().getQualifiedName(); }
protected void initializeReferenceTable(AbstractSession session) throws DescriptorException { Platform platform = session.getDatasourcePlatform(); if (getReferenceTable() == null) { throw DescriptorException.referenceTableNotSpecified(this); } if (platform.getTableQualifier().length() == 0) { return; } if (getReferenceTable().getTableQualifier().length() == 0) { getReferenceTable().setTableQualifier(platform.getTableQualifier()); } }
/** * The reference keys on the reference table are initalized */ protected void initializeReferenceKeys(AbstractSession session) throws DescriptorException { if (getReferenceKeyFields().size() == 0) { throw DescriptorException.noReferenceKeyIsSpecified(this); } for (Enumeration referenceEnum = getReferenceKeyFields().elements(); referenceEnum.hasMoreElements();) { DatabaseField field = (DatabaseField)referenceEnum.nextElement(); if (field.hasTableName() && (!(field.getTableName().equals(getReferenceTable().getName())))) { throw DescriptorException.referenceKeyFieldNotProperlySpecified(field, this); } field.setTable(getReferenceTable()); } }
protected void initializeSelectionCriteria(AbstractSession session) { Expression exp1; Expression exp2; Expression expression; Expression criteria = null; Enumeration referenceKeysEnum; Enumeration sourceKeysEnum; ExpressionBuilder base = new ExpressionBuilder(); TableExpression table = (TableExpression)base.getTable(getReferenceTable()); referenceKeysEnum = getReferenceKeyFields().elements(); sourceKeysEnum = getSourceKeyFields().elements(); for (; referenceKeysEnum.hasMoreElements();) { DatabaseField referenceKey = (DatabaseField)referenceKeysEnum.nextElement(); DatabaseField sourceKey = (DatabaseField)sourceKeysEnum.nextElement(); exp1 = table.getField(referenceKey); exp2 = base.getParameter(sourceKey); expression = exp1.equal(exp2); if (criteria == null) { criteria = expression; } else { criteria = expression.and(criteria); } } setSelectionCriteria(criteria); }
/** * The field name on the reference table is initialized and cached. */ protected void initializeDirectField(AbstractSession session) throws DescriptorException { if (getDirectField() == null) { throw DescriptorException.directFieldNameNotSet(this); } getDirectField().setTable(getReferenceTable()); getDirectField().setIndex(0); }
targetTable = ((DirectCollectionMapping)outerExpression.getMapping()).getReferenceTable(); } else { targetTable = (DatabaseTable)outerExpression.getMapping().getReferenceDescriptor().getTables().firstElement(); writer.write(newAlias.getQualifiedName()); } else if (outerExpression.getMapping().isDirectCollectionMapping()) {// for many to many mappings, you need to do some funky stuff to get the relation table's alias DatabaseTable newTarget = ((DirectCollectionMapping)outerExpression.getMapping()).getReferenceTable(); DatabaseTable newAlias = relationExpression.aliasForTable(newTarget); writer.write(", OUTER ");
/** * Build direct collection table definitions in a TopLink desciptor */ private void buildDirectCollectionTableDefinition(DirectCollectionMapping mapping, ClassDescriptor desc) { //first create direct collection table TableDefinition tblDef = getTableDefFromDBTable(mapping.getReferenceTable()); DatabaseField dbField = null; //add the table reference key(s) Vector refPkFields = mapping.getReferenceKeyFields(); for (int index = 0; index < refPkFields.size(); index++) { dbField = resolveDatabaseField((DatabaseField) refPkFields.get(index), (DatabaseField) mapping.getSourceKeyFields().get(index)); tblDef.addField(getDirectCollectionReferenceKeyFieldDefFromDBField(dbField)); } //add the direct collection field to the table. tblDef.addField(getFieldDefFromDBField(mapping.getDirectField(), false)); //if the mapping is direct-map field, add the direct key field to the table as well. if (mapping.isDirectMapMapping()) { dbField = ((DirectMapMapping) mapping).getDirectKeyField(); tblDef.addField(getFieldDefFromDBField(dbField, false)); } }
if(outerExpression != null) { if (outerExpression.getMapping().isDirectCollectionMapping()) { targetTable = ((DirectCollectionMapping)outerExpression.getMapping()).getReferenceTable(); } else { targetTable = (DatabaseTable)outerExpression.getMapping().getReferenceDescriptor().getTables().firstElement();
protected void initializeSelectionStatement(AbstractSession session) { SQLSelectStatement statement = new SQLSelectStatement(); statement.addTable(getReferenceTable()); statement.addField((DatabaseField)getDirectField().clone()); statement.setWhereClause(getSelectionCriteria()); statement.normalize(session, null); getSelectionQuery().setSQLStatement(statement); }
/** * Initialize insert query. This query is used to insert the collection of objects into the * reference table. */ protected void initializeInsertQuery(AbstractSession session) { if (!getInsertQuery().hasSessionName()) { getInsertQuery().setSessionName(session.getName()); } if (hasCustomInsertQuery()) { return; } SQLInsertStatement statement = new SQLInsertStatement(); statement.setTable(getReferenceTable()); AbstractRecord directRow = new DatabaseRecord(); for (Enumeration referenceEnum = getReferenceKeyFields().elements(); referenceEnum.hasMoreElements();) { directRow.put((DatabaseField)referenceEnum.nextElement(), null); } directRow.put(getDirectField(), null); statement.setModifyRow(directRow); getInsertQuery().setSQLStatement(statement); getInsertQuery().setModifyRow(directRow); }
statement.setTable(getReferenceTable()); getDeleteAllQuery().setSQLStatement(statement);
protected void initializeDeleteQuery(AbstractSession session) { if (!getDeleteQuery().hasSessionName()) { getDeleteQuery().setSessionName(session.getName()); } if (hasCustomDeleteQuery()) { return; } Expression builder = new ExpressionBuilder(); Expression directExp = builder.getField(getDirectField()).equal(builder.getParameter(getDirectField())); Expression expression = null; SQLDeleteStatement statement = new SQLDeleteStatement(); // Construct an expression to delete from the relation table. for (int index = 0; index < getReferenceKeyFields().size(); index++) { DatabaseField referenceKey = (DatabaseField)getReferenceKeyFields().elementAt(index); DatabaseField sourceKey = (DatabaseField)getSourceKeyFields().elementAt(index); Expression subExp1 = builder.getField(referenceKey); Expression subExp2 = builder.getParameter(sourceKey); Expression subExpression = subExp1.equal(subExp2); expression = subExpression.and(expression); } expression = expression.and(directExp); statement.setWhereClause(expression); statement.setTable(getReferenceTable()); getDeleteQuery().setSQLStatement(statement); }
/** * INTERNAL: * Print SQL onto the stream, using the ExpressionPrinter for context */ public void printSQL(ExpressionSQLPrinter printer) { if (isAttribute()) { printer.printField(getAliasedField()); } // If the mapping is a direct collection then this falls into a gray area. // It must be treated as an attribute at this moment for it has a direct field. // However it is not an attribute in the sense that it also represents a foreign // reference and a mapping criteria has been added. // For bug 2900974 these are now handled as non-attributes during normalize but // as attributes when printing SQL. // if ((getMapping() != null) && getMapping().isDirectCollectionMapping()) { DirectCollectionMapping directCollectionMapping = (DirectCollectionMapping)getMapping(); // The aliased table comes for free as it was a required part of the join criteria. TableExpression table = (TableExpression)getTable(directCollectionMapping.getReferenceTable()); DatabaseTable aliasedTable = table.aliasForTable(table.getTable()); DatabaseField aliasedField = (DatabaseField)directCollectionMapping.getDirectField().clone(); aliasedField.setTable(aliasedTable); printer.printField(aliasedField); } }