Search in sources :

Example 1 with ExpressionSQLPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter in project eclipselink by eclipse-ee4j.

the class DerbyPlatform method derbyPowerOperator.

// Emulate POWER(:a,:b) as EXP((:b)*LN(:a))
private static ExpressionOperator derbyPowerOperator() {
    ExpressionOperator exOperator = new ExpressionOperator() {

        @Override
        public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[1]);
            first.printSQL(printer);
            printer.printString(getDatabaseStrings()[2]);
        }

        @Override
        public void printCollection(List items, ExpressionSQLPrinter printer) {
            if (printer.getPlatform().isDynamicSQLRequiredForFunctions() && !isBindingSupported()) {
                printer.getCall().setUsesBinding(false);
            }
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }

        @Override
        public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[1]);
            first.printJava(printer);
            printer.printString(getDatabaseStrings()[2]);
        }

        @Override
        public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printJavaDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }
    };
    exOperator.setType(ExpressionOperator.FunctionOperator);
    exOperator.setSelector(ExpressionOperator.Power);
    exOperator.setName("POWER");
    List<String> v = new ArrayList<>(4);
    v.add("EXP((");
    v.add(")*LN(");
    v.add("))");
    exOperator.printsAs(v);
    exOperator.bePrefix();
    exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
    return exOperator;
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) Expression(org.eclipse.persistence.expressions.Expression) ExpressionSQLPrinter(org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Example 2 with ExpressionSQLPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter in project eclipselink by eclipse-ee4j.

the class DerbyPlatform method derbyRoundOperator.

// Emulate ROUND as FLOOR((:x)*1e:n+0.5)/1e:n
private static ExpressionOperator derbyRoundOperator() {
    ExpressionOperator exOperator = new ExpressionOperator() {

        @Override
        public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            first.printSQL(printer);
            printer.printString(getDatabaseStrings()[1]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[2]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[3]);
        }

        @Override
        public void printCollection(List items, ExpressionSQLPrinter printer) {
            if (printer.getPlatform().isDynamicSQLRequiredForFunctions() && !isBindingSupported()) {
                printer.getCall().setUsesBinding(false);
            }
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }

        @Override
        public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            first.printJava(printer);
            printer.printString(getDatabaseStrings()[1]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[2]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[3]);
        }

        @Override
        public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printJavaDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }
    };
    exOperator.setType(ExpressionOperator.FunctionOperator);
    exOperator.setSelector(ExpressionOperator.Round);
    exOperator.setName("ROUND");
    List<String> v = new ArrayList<>(4);
    v.add("FLOOR((");
    v.add(")*1e");
    v.add("+0.5)/1e");
    v.add("");
    exOperator.printsAs(v);
    exOperator.bePrefix();
    exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
    return exOperator;
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) Expression(org.eclipse.persistence.expressions.Expression) ExpressionSQLPrinter(org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Example 3 with ExpressionSQLPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter in project eclipselink by eclipse-ee4j.

the class DerbyPlatform method derbyExtractOperator.

/**
 * INTERNAL:
 * Derby does not support EXTRACT, but does have YEAR, MONTH, DAY, etc.
 */
public static ExpressionOperator derbyExtractOperator() {
    ExpressionOperator exOperator = new ExpressionOperator() {

        // QUARTER emulation: ((MONTH(:first)+2)/3)
        private final String[] QUARTER_STRINGS = new String[] { "((MONTH(", ")+2)/3)" };

        private void printQuarterSQL(final Expression first, final ExpressionSQLPrinter printer) {
            printer.printString(QUARTER_STRINGS[0]);
            first.printSQL(printer);
            printer.printString(QUARTER_STRINGS[1]);
        }

        private void printQuarterJava(final Expression first, final ExpressionJavaPrinter printer) {
            printer.printString(QUARTER_STRINGS[0]);
            first.printJava(printer);
            printer.printString(QUARTER_STRINGS[1]);
        }

        @Override
        public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
            if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                printQuarterSQL(first, printer);
            } else {
                super.printDuo(first, second, printer);
            }
        }

        @Override
        public void printCollection(List items, ExpressionSQLPrinter printer) {
            if (items.size() == 2) {
                Expression first = (Expression) items.get(0);
                Expression second = (Expression) items.get(1);
                if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                    printQuarterSQL(first, printer);
                    return;
                }
            }
            super.printCollection(items, printer);
        }

        @Override
        public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
            if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                printQuarterJava(first, printer);
            } else {
                super.printJavaDuo(first, second, printer);
            }
        }

        @Override
        public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
            if (items.size() == 2) {
                Expression first = (Expression) items.get(0);
                Expression second = (Expression) items.get(1);
                if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                    printQuarterJava(first, printer);
                    return;
                }
            }
            super.printJavaCollection(items, printer);
        }
    };
    exOperator.setType(ExpressionOperator.FunctionOperator);
    exOperator.setSelector(ExpressionOperator.Extract);
    exOperator.setName("EXTRACT");
    List<String> v = new ArrayList<>(3);
    v.add("");
    v.add("(");
    v.add(")");
    exOperator.printsAs(v);
    int[] indices = new int[2];
    indices[0] = 1;
    indices[1] = 0;
    exOperator.setArgumentIndices(indices);
    exOperator.bePrefix();
    exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
    return exOperator;
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) Expression(org.eclipse.persistence.expressions.Expression) ExpressionSQLPrinter(org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Example 4 with ExpressionSQLPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter in project eclipselink by eclipse-ee4j.

the class PostgreSQLPlatform method pgsqlRoundOperator.

// Emulate ROUND(:x,:n) as FLOOR((:x)*10^(:n)+0.5)/10^(:n)
private static ExpressionOperator pgsqlRoundOperator() {
    ExpressionOperator exOperator = new ExpressionOperator() {

        @Override
        public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            first.printSQL(printer);
            printer.printString(getDatabaseStrings()[1]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[2]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[3]);
        }

        @Override
        public void printCollection(List items, ExpressionSQLPrinter printer) {
            if (printer.getPlatform().isDynamicSQLRequiredForFunctions() && !isBindingSupported()) {
                printer.getCall().setUsesBinding(false);
            }
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }

        @Override
        public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            first.printJava(printer);
            printer.printString(getDatabaseStrings()[1]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[2]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[3]);
        }

        @Override
        public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printJavaDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }
    };
    exOperator.setType(ExpressionOperator.FunctionOperator);
    exOperator.setSelector(ExpressionOperator.Round);
    exOperator.setName("ROUND");
    List<String> v = new ArrayList<>(4);
    v.add("FLOOR((");
    v.add(")*10^(");
    v.add(")+0.5)/10^(");
    v.add(")");
    exOperator.printsAs(v);
    exOperator.bePrefix();
    exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
    return exOperator;
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) RelationExpression(org.eclipse.persistence.internal.expressions.RelationExpression) Expression(org.eclipse.persistence.expressions.Expression) ExpressionSQLPrinter(org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Aggregations

ArrayList (java.util.ArrayList)4 List (java.util.List)4 Expression (org.eclipse.persistence.expressions.Expression)4 ExpressionOperator (org.eclipse.persistence.expressions.ExpressionOperator)4 ExpressionJavaPrinter (org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter)4 ExpressionSQLPrinter (org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter)4 LiteralExpression (org.eclipse.persistence.internal.expressions.LiteralExpression)3 RelationExpression (org.eclipse.persistence.internal.expressions.RelationExpression)1