public void setup() { Vector employees = getSomeEmployees(); // Bug 223005: Verify that we have at least 1 employee with the required field length otherwise // an EclipseLinkException will be thrown Employee emp = getEmployeeWithRequiredNameLength(employees, MIN_FIRSTNAME_LENGTH, getName()); String partialFirstName = "%" + emp.getFirstName().substring(0, 3) + "%"; ReadAllQuery raq = new ReadAllQuery(); raq.setReferenceClass(Employee.class); Vector parameters = new Vector(); parameters.add(partialFirstName); ExpressionBuilder eb = new ExpressionBuilder(); Expression whereClause = eb.get("firstName").like(partialFirstName); raq.setSelectionCriteria(whereClause); employees = (Vector) getSession().executeQuery(raq); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName LIKE ?1"; setEjbqlString(ejbqlString); setOriginalOject(employees); setArguments(parameters); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); setArgumentNames(myArgumentNames); super.setup(); }
public void setup() { Employee emp1; Employee emp2; Employee emp3; emp1 = (Employee) getSomeEmployees().firstElement(); emp2 = (Employee) getSomeEmployees().elementAt(1); emp3 = (Employee) getSomeEmployees().elementAt(2); ExpressionBuilder builder = new ExpressionBuilder(); Vector idVector = new Vector(); idVector.add(emp1.getId()); idVector.add(emp2.getId()); idVector.add(emp3.getId()); Expression whereClause = builder.get("id").notIn(idVector); ReadAllQuery raq = new ReadAllQuery(); raq.setReferenceClass(Employee.class); raq.setSelectionCriteria(whereClause); setOriginalOject(getSession().executeQuery(raq)); getSession().getIdentityMapAccessor().initializeAllIdentityMaps(); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE emp.id NOT IN ("; ejbqlString = ejbqlString + emp1.getId().toString() + ", "; ejbqlString = ejbqlString + emp2.getId().toString() + ", "; ejbqlString = ejbqlString + emp3.getId().toString(); ejbqlString = ejbqlString + ")"; setEjbqlString(ejbqlString); super.setup(); }
/** INTERNAL: Transform the object-level value into a database-level value */ public Object getFieldValue(Object objectValue, AbstractSession session) { DatabaseMapping mapping = getMapping(); Object fieldValue = objectValue; if ((mapping != null) && (mapping.isDirectToFieldMapping() || mapping.isDirectCollectionMapping())) { // CR#3623207, check for IN Collection here not in mapping. if (objectValue instanceof Collection) { // This can actually be a collection for IN within expressions... however it would be better // for expressions to handle this. Collection values = (Collection) objectValue; Vector fieldValues = new Vector(values.size()); for (Iterator iterator = values.iterator(); iterator.hasNext(); ) { Object value = iterator.next(); if (!(value instanceof Expression)) { value = getFieldValue(value, session); } fieldValues.add(value); } fieldValue = fieldValues; } else { if (mapping.isDirectToFieldMapping()) { fieldValue = ((AbstractDirectMapping) mapping).getFieldValue(objectValue, session); } else if (mapping.isDirectCollectionMapping()) { fieldValue = ((DirectCollectionMapping) mapping).getFieldValue(objectValue, session); } } } return fieldValue; }
/** INTERNAL: */ protected Vector<DatabaseField> getVectorOfFieldsToGenerate(int operation, DatabaseTable table) { if (this.main[operation][ALL] == null) { return null; } if (this.tableToFieldsForGenerationMap == null) { // the method is called for the first time tableToFieldsForGenerationMap = new HashMap[NUM_OPERATIONS]; } if (this.tableToFieldsForGenerationMap[operation] == null) { // the method is called for the first time for this operation this.tableToFieldsForGenerationMap[operation] = new HashMap(); } Vector<DatabaseField> fieldsForGeneration = this.tableToFieldsForGenerationMap[operation].get(table); if (fieldsForGeneration == null) { // the method is called for the first time for this operation and this table fieldsForGeneration = new NonSynchronizedVector(); Iterator it = this.main[operation][ALL].iterator(); while (it.hasNext()) { DatabaseField field = (DatabaseField) it.next(); if (field.getTable().equals(table)) { fieldsForGeneration.add(field); } } this.tableToFieldsForGenerationMap[operation].put(table, fieldsForGeneration); } return fieldsForGeneration; }
/** * INTERNAL: Convert all the class-name-based settings in this InheritancePolicy to actual * class-based settings. This method is used when converting a project that has been built with * class names to a project with classes. It will also convert referenced classes to the versions * of the classes from the classLoader. */ public void convertClassNamesToClasses(ClassLoader classLoader) { Vector newParentInterfaces = new Vector(); for (Iterator iterator = getParentInterfaceNames().iterator(); iterator.hasNext(); ) { String interfaceName = (String) iterator.next(); Class interfaceClass = null; try { if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) { try { interfaceClass = (Class) AccessController.doPrivileged( new PrivilegedClassForName(interfaceName, true, classLoader)); } catch (PrivilegedActionException exception) { throw ValidationException.classNotFoundWhileConvertingClassNames( interfaceName, exception.getException()); } } else { interfaceClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName( interfaceName, true, classLoader); } } catch (ClassNotFoundException exc) { throw ValidationException.classNotFoundWhileConvertingClassNames(interfaceName, exc); } newParentInterfaces.add(interfaceClass); } this.parentInterfaces = newParentInterfaces; }
public static BinaryOperatorWithParameterTest getNumericParameterDivideTestWithBracketsAfterComparison() { BinaryOperatorWithParameterTest theTest = new BinaryOperatorWithParameterTest(); theTest.setName("Divide with parameter test with brackets after comparison"); String parameterName = "amountToDivide"; theTest.setExpressionParameters(new Vector()); theTest.getExpressionParameters().add(parameterName); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = ExpressionBuilder.fromConstant(new Integer(20000), builder) .greaterThan( ExpressionMath.divide( builder.get("salary"), (builder.getParameter(parameterName)))); theTest.setOriginalObjectExpression(whereClause); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE 20000 > (emp.salary / ?1)"; theTest.setEjbqlString(ejbqlString); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); theTest.setArgumentNames(myArgumentNames); theTest.setArguments(new Vector()); theTest.getArguments().addElement(new Integer(2)); return theTest; }
public static BinaryOperatorWithParameterTest getNumericParameterDivideTest() { BinaryOperatorWithParameterTest theTest = new BinaryOperatorWithParameterTest(); theTest.setName("Divide with parameter test"); String parameterName = "amountToDivide"; theTest.setExpressionParameters(new Vector()); theTest.getExpressionParameters().add(parameterName); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = ExpressionMath.divide(builder.get("salary"), (builder.getParameter(parameterName))) .lessThanEqual(20000); theTest.setOriginalObjectExpression(whereClause); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE emp.salary / ?1 <= 20000"; theTest.setEjbqlString(ejbqlString); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); theTest.setArgumentNames(myArgumentNames); theTest.setArguments(new Vector()); theTest.getArguments().addElement(new Integer(2)); return theTest; }
public static BinaryOperatorWithParameterTest getNumericParameterMultiplyTestWithBracketsBeforeComparison() { BinaryOperatorWithParameterTest theTest = new BinaryOperatorWithParameterTest(); theTest.setName("Multiply with parameter test with brackets before comparison"); String parameterName = "amountToMultiply"; theTest.setExpressionParameters(new Vector()); theTest.getExpressionParameters().add(parameterName); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = ExpressionMath.multiply(builder.get("salary"), (builder.getParameter(parameterName))) .lessThanEqual(100000); theTest.setOriginalObjectExpression(whereClause); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE (emp.salary * ?1) <= 100000"; theTest.setEjbqlString(ejbqlString); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); theTest.setArgumentNames(myArgumentNames); theTest.setArguments(new Vector()); theTest.getArguments().addElement(new Integer(2)); return theTest; }
protected void test() { for (int i = 0; i <= 1; i++) { getSession().getPlatform().setShouldBindAllParameters(i != 0); for (int j = 0; j <= 2; j++) { query = new ReadAllQuery(Employee.class); ExpressionBuilder builder = new ExpressionBuilder(); Vector vExp = new Vector(2); vExp.add(builder.getParameter("p1")); query.addArgument("p1"); vExp.add(builder.getParameter("p2")); query.addArgument("p2"); Expression exp = builder.get("id").in(vExp); query.setSelectionCriteria(exp); switch (j) { case 0: // nothing to do - just test the default: // query.bindAllParameters == Undefined break; case 1: // query.bindAllParameters == False query.setShouldBindAllParameters(false); break; case 2: // query.bindAllParameters == True query.setShouldBindAllParameters(true); break; } // clear the writer's buffer ((StringWriter) getSession().getLog()).getBuffer().setLength(0); try { getSession().executeQuery(query, v); } catch (DatabaseException e) { throw new TestProblemException("executeQuery threw DatabaseException"); } if (shouldBind() != wasBound()) { return; } } } }
public void setup() { // Get the baseline employees for the verify Employee emp = (Employee) getSomeEmployees().firstElement(); String parameterName = "firstName"; ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = builder.get("firstName").equal(builder.getParameter(parameterName)); ReadAllQuery raq = new ReadAllQuery(); raq.setReferenceClass(Employee.class); raq.setSelectionCriteria(whereClause); raq.addArgument(parameterName); Vector parameters = new Vector(); parameters.add(emp.getFirstName()); Vector employees = (Vector) getSession().executeQuery(raq, parameters); emp = (Employee) employees.firstElement(); // Set up the EJBQL using the retrieved employees String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE "; ejbqlString = ejbqlString + "?1 = emp.firstName "; setEjbqlString(ejbqlString); setOriginalOject(employees); setArguments(parameters); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); setArgumentNames(myArgumentNames); // Finish the setup super.setup(); }
/** * Verify that the query ran successfully, and that the number of objects returned matches the * number of managed employees. */ protected void verify() throws Exception { Vector params = new Vector(); params.add(someManager.getId()); // numberOfManagedEmployees try { Vector results = (Vector) getSession().executeQuery(query, params); if (!(numberOfManagedEmployees == results.size())) { throw new TestErrorException( results.size() + " objects were read from the database, but originially there were, " + numberOfManagedEmployees + "."); } } catch (org.eclipse.persistence.exceptions.DatabaseException e) { throw new TestErrorException( "Custom SQL subquery with parameters failed with a DatabaseException."); } }
public static BinaryOperatorWithParameterTest getNumericTwoParameterMultipleOperatorsWithBracketsAroundMultiply() { BinaryOperatorWithParameterTest theTest = new BinaryOperatorWithParameterTest(); theTest.setName("Multiple operators with two parameters with brackets around multiply"); String parameterNameForDivide = "amountToDivide"; String parameterNameForMultiply = "amountToMultiply"; theTest.setExpressionParameters(new Vector()); theTest.getExpressionParameters().add(parameterNameForDivide); theTest.getExpressionParameters().add(parameterNameForMultiply); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = builder .get("salary") .greaterThan( ExpressionMath.subtract( ExpressionMath.add( ExpressionBuilder.fromConstant(new Integer(50000), builder), 10000), ExpressionMath.divide( ExpressionBuilder.fromConstant(new Integer(10000), builder), ExpressionMath.multiply( builder.getParameter(parameterNameForMultiply), builder.getParameter(parameterNameForDivide))))); theTest.setOriginalObjectExpression(whereClause); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE emp.salary > (50000 + 10000 - 10000 / (?1 * ?2))"; theTest.setEjbqlString(ejbqlString); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); myArgumentNames.add("2"); theTest.setArgumentNames(myArgumentNames); theTest.setArguments(new Vector()); theTest.getArguments().addElement(new Integer(2)); theTest.getArguments().addElement(new Integer(5)); return theTest; }
public static BinaryOperatorWithParameterTest getNumericParameterLessThanEqualTest() { BinaryOperatorWithParameterTest theTest = new BinaryOperatorWithParameterTest(); theTest.setName("Less than equal to with parameter test"); String parameterName = "id"; theTest.setExpressionParameters(new Vector()); theTest.getExpressionParameters().add(parameterName); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = builder.get("id").lessThanEqual(builder.getParameter(parameterName)); theTest.setOriginalObjectExpression(whereClause); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE "; ejbqlString = ejbqlString + "emp.id <= ?1 "; theTest.setEjbqlString(ejbqlString); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); theTest.setArgumentNames(myArgumentNames); return theTest; }
public static BinaryOperatorWithParameterTest getNumericTwoParameterMultipleOperatorsWithBracketsAroundPlusMinus() { BinaryOperatorWithParameterTest theTest = new BinaryOperatorWithParameterTest(); theTest.setName("Multiple operators with two parameters with brackets around plus/minus"); String parameterNameForDivide = "amountToDivide"; String parameterNameForMultiply = "amountToMultiply"; theTest.setExpressionParameters(new Vector()); theTest.getExpressionParameters().add(parameterNameForDivide); theTest.getExpressionParameters().add(parameterNameForMultiply); ExpressionBuilder builder = new ExpressionBuilder(); Expression whereClause = ExpressionMath.multiply( ExpressionMath.divide( ExpressionMath.subtract(ExpressionMath.add(builder.get("salary"), 10), 20), builder.getParameter(parameterNameForDivide)), builder.getParameter(parameterNameForMultiply)) .greaterThanEqual(70000); theTest.setOriginalObjectExpression(whereClause); String ejbqlString = "SELECT OBJECT(emp) FROM Employee emp WHERE (emp.salary + 10 - 20) / ?1 * ?2 >= 70000"; theTest.setEjbqlString(ejbqlString); Vector myArgumentNames = new Vector(); myArgumentNames.add("1"); myArgumentNames.add("2"); theTest.setArgumentNames(myArgumentNames); theTest.setArguments(new Vector()); theTest.getArguments().addElement(new Integer(2)); theTest.getArguments().addElement(new Integer(3)); return theTest; }
/** * INTERNAL: For CR#2456 if this is part of an objExp.equal(objExp), do not need to add additional * expressions to normalizer both times, and the foreign key join replaces the equal expression. */ public Expression normalize(ExpressionNormalizer normalizer, Vector foreignKeyJoinPointer) { if (hasBeenNormalized()) { return this; } super.normalize(normalizer); setHasBeenNormalized(true); if ((getMapping() != null) && getMapping().isDirectToXMLTypeMapping()) { normalizer.getStatement().setRequiresAliases(true); } // Check if any joins need to be added. if (isAttribute()) { return this; } // If the mapping is 'ref' or 'structure', no join needed. if ((getMapping() != null) && (getMapping().isReferenceMapping() || getMapping().isStructureMapping())) { normalizer.getStatement().setRequiresAliases(true); return this; } // Compute if a distinct is required during normalization. if (shouldQueryToManyRelationship() && (!normalizer.getStatement().isDistinctComputed()) && (!normalizer.getStatement().isAggregateSelect())) { normalizer.getStatement().useDistinct(); } // Turn off DISTINCT if nestedTableMapping is used (not supported by Oracle 8.1.5). if ((getMapping() != null) && getMapping().isNestedTableMapping()) { // There are two types of nested tables, one used by clients, one used by mappings, do nothing // in the mapping case. if (!shouldQueryToManyRelationship()) { return this; } normalizer.getStatement().dontUseDistinct(); } Expression mappingExpression = mappingCriteria(); if (mappingExpression != null) { mappingExpression = mappingExpression.normalize(normalizer); } if (mappingExpression != null) { // If the join was an outer join we must not add the join criteria to the where clause, // if the platform prints the join in the from clause. if (shouldUseOuterJoin() && (getSession().getPlatform().isInformixOuterJoin())) { normalizer.getStatement().getOuterJoinExpressions().addElement(this); normalizer.getStatement().getOuterJoinedMappingCriteria().addElement(mappingExpression); normalizer.addAdditionalExpression(mappingExpression.and(additionalExpressionCriteria())); return this; } else if ((shouldUseOuterJoin() || isUsingOuterJoinForMultitableInheritance()) && (!getSession().getPlatform().shouldPrintOuterJoinInWhereClause())) { if (shouldUseOuterJoin()) { normalizer.getStatement().getOuterJoinExpressions().addElement(this); normalizer.getStatement().getOuterJoinedMappingCriteria().addElement(mappingExpression); normalizer .getStatement() .getOuterJoinedAdditionalJoinCriteria() .addElement(additionalExpressionCriteriaMap()); normalizer.getStatement().getDescriptorsForMultitableInheritanceOnly().add(null); return this; } else { if (isUsingOuterJoinForMultitableInheritance()) { normalizer.getStatement().getOuterJoinExpressions().addElement(null); normalizer.getStatement().getOuterJoinedMappingCriteria().addElement(null); normalizer .getStatement() .getOuterJoinedAdditionalJoinCriteria() .addElement(additionalExpressionCriteriaMap()); normalizer .getStatement() .getDescriptorsForMultitableInheritanceOnly() .add(getMapping().getReferenceDescriptor()); // fall through to the main case } } } // This must be added even if outer. Actually it should be converted to use a right outer // join, but that gets complex // so we do not support this current which is a limitation in some cases. if (foreignKeyJoinPointer != null) { // If this expression is right side of an objExp.equal(objExp), one // need not add additionalExpressionCriteria twice. // Also the join will replace the original objExp.equal(objExp). // For CR#2456. foreignKeyJoinPointer.add(mappingExpression); } else { normalizer.addAdditionalExpression(mappingExpression.and(additionalExpressionCriteria())); } } // For bug 2900974 special code for DirectCollectionMappings moved to printSQL. return this; }