public DatabaseMapping getMappingFromQueryKey() { QueryKey queryKey = getQueryKeyOrNull(); if ((queryKey == null) || (!(queryKey instanceof DirectQueryKey))) { throw QueryException.cannotConformExpression(); } mapping = queryKey .getDescriptor() .getObjectBuilder() .getMappingForField(((DirectQueryKey) queryKey).getField()); if (mapping == null) { throw QueryException.cannotConformExpression(); } return mapping; }
public void test() { ReadAllQuery query = new ReadAllQuery(Employee.class); query.dontMaintainCache(); query.addPartialAttribute("nonExistant"); try { getSession().executeQuery(query); } catch (QueryException exception) { if (exception.getErrorCode() == QueryException.SPECIFIED_PARTIAL_ATTRIBUTE_DOES_NOT_EXIST) { correctException = true; } else { correctException = false; throw exception; } } }
/** INTERNAL: Return if the expression is for a direct mapped attribute. */ public boolean isAttribute() { if (isAttributeExpression == null) { if (getSession() == null) { // We can't tell, so say no. return false; } QueryKey queryKey = getQueryKeyOrNull(); if (queryKey != null) { isAttributeExpression = Boolean.valueOf(queryKey.isDirectQueryKey()); } else { DatabaseMapping mapping = getMapping(); if (mapping != null) { if (mapping.isVariableOneToOneMapping()) { throw QueryException.cannotQueryAcrossAVariableOneToOneMapping( mapping, mapping.getDescriptor()); } else { isAttributeExpression = Boolean.valueOf(mapping.isDirectToFieldMapping()); } } else { isAttributeExpression = Boolean.FALSE; } } } return isAttributeExpression.booleanValue(); }
/** * INTERNAL: Return the lowlevel database accessor. The database accessor is used for direct * database access. The right accessor for this broker will be returned. */ public Accessor getAccessor(Class domainClass) { if (domainClass == null) { // CR#... this occurs if a data query is used without a session-name, needs to throw an error throw QueryException.unnamedQueryOnSessionBroker(null); } // session may be a broker too. Eventually a non session broker wil be found return getSessionForClass(domainClass).getAccessor(domainClass); }
/** Do any required validation for this node. Throw an exception if it's incorrect. */ public void validateNode() { if ((getQueryKeyOrNull() == null) && (getMapping() == null)) { throw QueryException.invalidQueryKeyInExpression(getName()); } QueryKey queryKey = getQueryKeyOrNull(); DatabaseMapping mapping = getMapping(); Object theOneThatsNotNull = null; boolean qkIsToMany = false; if (queryKey != null) { theOneThatsNotNull = queryKey; qkIsToMany = queryKey.isManyToManyQueryKey() || queryKey.isOneToManyQueryKey(); } boolean isNestedMapping = false; if (mapping != null) { // Bug 2847621 - Add Aggregate Collection to the list of valid items for outer join. if (shouldUseOuterJoin && (!(mapping.isOneToOneMapping() || mapping.isOneToManyMapping() || mapping.isManyToManyMapping() || mapping.isAggregateCollectionMapping() || mapping.isDirectCollectionMapping()))) { throw QueryException.outerJoinIsOnlyValidForOneToOneMappings(getMapping()); } qkIsToMany = mapping.isCollectionMapping(); if (index != null) { if (qkIsToMany) { CollectionMapping collectionMapping = (CollectionMapping) getMapping(); if (collectionMapping.getListOrderField() != null) { index.setField(collectionMapping.getListOrderField()); if (collectionMapping.shouldUseListOrderFieldTableExpression()) { Expression newBase = getTable(collectionMapping.getListOrderField().getTable()); index.setBaseExpression(newBase); } else { addDerivedField(index); } } else { throw QueryException.indexRequiresCollectionMappingWithListOrderField( this, collectionMapping); } } else { throw QueryException.indexRequiresCollectionMappingWithListOrderField(this, mapping); } } isNestedMapping = mapping.isNestedTableMapping(); theOneThatsNotNull = mapping; } else { if (index != null) { throw QueryException.indexRequiresCollectionMappingWithListOrderField(this, null); } } if ((!shouldQueryToManyRelationship()) && qkIsToMany && (!isNestedMapping)) { throw QueryException.invalidUseOfToManyQueryKeyInExpression(theOneThatsNotNull); } if (shouldQueryToManyRelationship() && !qkIsToMany) { throw QueryException.invalidUseOfAnyOfInExpression(theOneThatsNotNull); } }
/** Append the string containing the SQL insert string for the given table. */ protected SQLCall buildCallWithoutReturning(AbstractSession session) { SQLCall call = new SQLCall(); call.returnNothing(); Writer writer = new CharArrayWriter(200); try { writer.write("INSERT "); if (getHintString() != null) { writer.write(getHintString()); writer.write(" "); } writer.write("INTO "); writer.write(getTable().getQualifiedNameDelimited(session.getPlatform())); writer.write(" ("); Vector fieldsForTable = new Vector(); for (Enumeration fieldsEnum = getModifyRow().keys(); fieldsEnum.hasMoreElements(); ) { DatabaseField field = (DatabaseField) fieldsEnum.nextElement(); if (field.getTable().equals(getTable()) || (!field.hasTableName())) { fieldsForTable.addElement(field); } } if (fieldsForTable.isEmpty()) { throw QueryException.objectToInsertIsEmpty(getTable()); } for (int i = 0; i < fieldsForTable.size(); i++) { writer.write( ((DatabaseField) fieldsForTable.elementAt(i)).getNameDelimited(session.getPlatform())); if ((i + 1) < fieldsForTable.size()) { writer.write(", "); } } writer.write(") VALUES ("); for (int i = 0; i < fieldsForTable.size(); i++) { DatabaseField field = (DatabaseField) fieldsForTable.elementAt(i); call.appendModify(writer, field); if ((i + 1) < fieldsForTable.size()) { writer.write(", "); } } writer.write(")"); call.setSQLString(writer.toString()); } catch (IOException exception) { throw ValidationException.fileError(exception); } return call; }
/** INTERNAL: Answers the session to be used for the given query. */ protected AbstractSession getSessionForQuery(DatabaseQuery query) { if (query.hasSessionName()) { return getSessionForName(query.getSessionName()); } Class queryClass; if (query.getDescriptor() != null) { queryClass = query.getDescriptor().getJavaClass(); } else { queryClass = query.getReferenceClass(); if (queryClass == null) { throw QueryException.unnamedQueryOnSessionBroker(query); } } return getSessionForClass(queryClass); }
public QueryKey getQueryKeyOrNull() { if (!hasQueryKey) { return null; } // Oct 19, 2000 JED // Added try/catch. This was throwing a NPE in the following case // expresssionBuilder.get("firstName").get("bob") // moved by Gordon Yorke to cover validate and normalize if (getContainingDescriptor() == null) { throw QueryException.invalidQueryKeyInExpression(getName()); } if (queryKey == null) { queryKey = getContainingDescriptor().getQueryKeyNamed(getName()); if (queryKey == null) { hasQueryKey = false; } } return queryKey; }
/** * INTERNAL This method iterates through a collection and gets the values from the objects to * conform in an in-memory query. Creation date: (1/19/01 1:18:27 PM) */ public Object valuesFromCollection( Object object, AbstractSession session, int valueHolderPolicy, boolean isObjectUnregistered) { // in case the mapping is null - this can happen if a query key is being used // In this case, check for the query key and find it's mapping. boolean readMappingFromQueryKey = false; if (getMapping() == null) { getMappingFromQueryKey(); readMappingFromQueryKey = true; } // For bug 2780817 get the mapping directly from the object. In EJB 2.0 // inheritance, each child must override mappings defined in an abstract // class with its own. DatabaseMapping mapping = this.mapping; ClassDescriptor descriptor = mapping.getDescriptor(); if (descriptor.hasInheritance() && (descriptor.getJavaClass() != object.getClass())) { mapping = session .getDescriptor(object.getClass()) .getObjectBuilder() .getMappingForAttributeName(getName()); descriptor = mapping.getDescriptor(); } // fetch group support if (descriptor.hasFetchGroupManager()) { FetchGroupManager fetchGroupManager = descriptor.getFetchGroupManager(); if (fetchGroupManager.isPartialObject(object) && (!fetchGroupManager.isAttributeFetched(object, mapping.getAttributeName()))) { // the conforming attribute is not fetched, simply throw exception throw QueryException.cannotConformUnfetchedAttribute(mapping.getAttributeName()); } } if (mapping.isDirectToFieldMapping()) { return ((AbstractDirectMapping) mapping).valueFromObject(object, mapping.getField(), session); } else if (mapping.isForeignReferenceMapping()) { // CR 3677 integration of a ValueHolderPolicy Object valueFromMapping = mapping.getAttributeValueFromObject(object); if (!((ForeignReferenceMapping) mapping) .getIndirectionPolicy() .objectIsInstantiated(valueFromMapping)) { if (valueHolderPolicy != InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION) { // If the client wishes us to trigger the indirection then we should do so, // Other wise throw the exception throw QueryException .mustInstantiateValueholders(); // you should instantiate the valueholder for this to // work } // maybe we should throw this exception from the start, to save time } Object valueToIterate = mapping.getRealAttributeValueFromObject(object, session); UnitOfWorkImpl uow = isObjectUnregistered ? (UnitOfWorkImpl) session : null; // First check that object in fact is unregistered. // toDo: ?? Why is this commented out? Why are we supporting the unregistered thing at all? // Does not seem to be any public API for this, nor every used internally? // if (isObjectUnregistered) { // isObjectUnregistered = !uow.getCloneMapping().containsKey(object); // } if (mapping.isCollectionMapping() && (valueToIterate != null)) { // For bug 2766379 must use the correct version of vectorFor to // unwrap the result same time. valueToIterate = mapping.getContainerPolicy().vectorFor(valueToIterate, session); // toDo: If the value is empty, need to support correct inner/outer join filtering // symantics. // For CR 2612601, try to partially replace the result with already // registered objects. if (isObjectUnregistered && (uow.getCloneMapping().get(object) == null)) { Vector objectValues = (Vector) valueToIterate; for (int i = 0; i < objectValues.size(); i++) { Object original = objectValues.elementAt(i); Object clone = uow.getIdentityMapAccessorInstance() .getIdentityMapManager() .getFromIdentityMap(original); if (clone != null) { objectValues.setElementAt(clone, i); } } } // For CR 2612601, conforming without registering, a query could be // bob.get("address").get("city").equal("Ottawa"); where the address // has been registered and modified in the UOW, but bob has not. Thus // even though bob does not point to the modified address now, it will // as soon as it is registered, so should point to it here. } else if (isObjectUnregistered && (uow.getCloneMapping().get(object) == null)) { Object clone = uow.getIdentityMapAccessorInstance() .getIdentityMapManager() .getFromIdentityMap(valueToIterate); if (clone != null) { valueToIterate = clone; } } return valueToIterate; } else if (mapping.isAggregateMapping()) { Object aggregateValue = mapping.getAttributeValueFromObject(object); // Bug 3995468 - if this query key is to a mapping in an aggregate object, get the object from // actual mapping rather than the aggregate mapping while (readMappingFromQueryKey && mapping.isAggregateObjectMapping() && !((AggregateObjectMapping) mapping) .getReferenceClass() .equals(queryKey.getDescriptor().getJavaClass())) { mapping = mapping .getReferenceDescriptor() .getObjectBuilder() .getMappingForField(((DirectQueryKey) queryKey).getField()); aggregateValue = mapping.getRealAttributeValueFromObject(aggregateValue, session); } return aggregateValue; } else { throw QueryException.cannotConformExpression(); } }
/** * INTERNAL: Possible for future development, not currently supported. * * <p>Retrieve the value through using batch reading. This executes a single query to read the * target for all of the objects and stores the result of the batch query in the original query to * allow the other objects to share the results. */ @Override protected Object batchedValueFromRow(AbstractRecord row, ReadAllQuery query) { throw QueryException.batchReadingNotSupported(this, query); }