Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 322602

Summary: Sybase/SqlServer date expressions' parameters are backwards
Product: z_Archived Reporter: Chad Retz <chad.retz>
Component: EclipselinkAssignee: Project Inbox <eclipselink.foundation-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P2 CC: martin.grebac, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: submitted_patch, sybase, sqlserver
Attachments:
Description Flags
Patch for 322602 none

Description Chad Retz CLA 2010-08-12 19:08:20 EDT
Build Identifier: 

Basically in org.eclipse.persistence.expressions.Expression the methods dateDifference (both overloads), dateName, and datePart put the parameters in the wrong order (at least on SQL Server). The datePart should come first, and even though appears in code to do this, the baseExpression of the function is the date.

Reproducible: Always
Comment 1 Chad Retz CLA 2010-08-12 19:10:32 EDT
Created attachment 176512 [details]
Patch for 322602

Patch added that fixed the problem for me. Note, I usually access this w/ a custom FunctionExpressionImpl from the JPA criteria side instead of using the EclipseLink libraries directly. For instance, here is my datePart function:

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Expression<Integer> datePart(EntityManager em, 
        Expression<Date> date, DatePart datePart) {
    org.eclipse.persistence.expressions.Expression dateExpr = 
        ((InternalSelection) date).getCurrentNode();
    ExpressionOperator anOperator = dateExpr.getOperator(ExpressionOperator.DatePart);
    FunctionExpression expression = new FunctionExpression();
    org.eclipse.persistence.expressions.Expression datePartLiteral = 
        dateExpr.literal(datePart.toString());
    expression.setBaseExpression(datePartLiteral);
    expression.addChild(datePartLiteral);
    expression.addChild(dateExpr);
    expression.setOperator(anOperator);
    return new FunctionExpressionImpl(em.getMetamodel(), ClassConstants.INTEGER,
            expression, Arrays.asList(em.getCriteriaBuilder().
                    literal(datePart.toString()), date), "datepart");
}
Comment 2 Tom Ware CLA 2010-08-24 13:30:33 EDT
Setting target and priority see: http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines for more information on the meanings of these fields.
Comment 3 Tom Ware CLA 2010-08-25 11:31:27 EDT
This should probably be patched in SQLServerPlatform and on SybasePlatform to ensure the changes do not break any platforms that are working with the current setup.

You should write methods on those platforms similar to the method written on SymfowarePlatform. e.g.:

    protected static ExpressionOperator addDate() {
        ExpressionOperator exOperator = new ExpressionOperator();
        exOperator.setSelector(ExpressionOperator.AddDate);
        Vector<String> v = NonSynchronizedVector.newInstance(4);
        v.addElement("ADD_DATE(");
        v.addElement(", ");
        v.addElement(", '");
        v.addElement("')");
        exOperator.printsAs(v);
        exOperator.bePrefix();

        int[] indices = new int[3];
        indices[0] = 0;
        indices[1] = 2;
        indices[2] = 1;
        exOperator.setArgumentIndices(indices);
        return exOperator;
    }

The "indices" array is used to reorder the arguments.  In fact, you may be able to use a number of the Symfoware date functions as a guideline.  The new operator switched in the initializePlatformOperators() method of each platform.

The additional advantage of this is that you can subclass SybasePlatform or SQLServerPlatform and take advantages of the changes right away by making use of your subclass.
Comment 4 Chad Retz CLA 2010-08-26 16:25:06 EDT
Thanks for the info, this helps me a lot in not having to edit the original source of EclipseLink in my project to support this. I will provide a new patch in not too long (little busy now). At the least, on Expression, the javadoc should be updated to remove the Sybase reference, remove the SQL example, or correct the SQL example.
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:05:40 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:10:26 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink