solomax commented on code in PR #122:
URL: https://github.com/apache/openjpa/pull/122#discussion_r1938418532


##########
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/utils/DatabaseHelper.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.common.utils;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.jdbc.sql.DerbyDictionary;
+import org.apache.openjpa.kernel.Broker;
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+
+import jakarta.persistence.EntityManager;
+
+/**
+ * Allows augmentation of databases, if they don't have support to some 
+ * necessary functions, such as DerbyDb's lack of POWER and ROUND
+ */
+public class DatabaseHelper {
+
+    private static final String CREATE_DERBYDB_POWER_FUNCTION_SQL = "CREATE 
FUNCTION POWER(a DOUBLE, b DOUBLE) " + 
+            "RETURNS DOUBLE PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA EXTERNAL 
NAME 'java.lang.Math.pow'";
+    
+    private static final String CREATE_DERBYDB_ROUND_FUNCTION_SQL = "CREATE 
FUNCTION ROUND(a DOUBLE, b INTEGER) " + 
+            "RETURNS DOUBLE PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA " + 
+            "EXTERNAL NAME 
'org.apache.openjpa.persistence.common.utils.DatabaseHelper.roundFunction'";
+
+    /**
+     * Creates the POWER function on DerbyDB, ignoring exceptions if it 
already exists.
+     * 
+     */
+    public static void createPowerFunctionIfNecessary(EntityManager em, 
DBDictionary dict) {
+        if (dict instanceof DerbyDictionary) {
+            try {
+                exec(em, true, 10, CREATE_DERBYDB_POWER_FUNCTION_SQL);
+            } catch (Exception ex) {
+                ex.printStackTrace();

Review Comment:
   shall this one be re-thrown instead? (and the one below)



##########
openjpa-kernel/src/main/java/org/apache/openjpa/kernel/exps/InMemoryExpressionFactory.java:
##########
@@ -624,6 +624,42 @@ public Value mod(Value val1, Value val2) {
     public Value abs(Value val) {
         return new Abs((Val) val);
     }
+    
+    @Override
+    public Value ceiling(Value val) {
+       return new Ceiling((Val) val);
+    }
+
+    @Override
+    public Value exp(Value val) {
+        return new Exponential((Val) val);
+    }
+
+    @Override
+    public Value floor(Value val) {
+        return new Floor((Val) val);
+    }
+
+    @Override
+    public Value ln(Value val) {
+        return new NaturalLogarithm(((Val) val));
+    }
+
+    @Override
+    public Value sign(Value val) {
+        return new Sign((Val) val);
+    }
+
+    @Override
+    public Value power(Value base, Value exponent) {
+        return new Power((Val) base, (Val) exponent);
+    }
+
+    @Override
+    public Value round(Value num, Value precision) {
+        // TODO Auto-generated method stub

Review Comment:
   this TODO need to be addressed? :)



##########
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestEJBQLFunction.java:
##########
@@ -491,6 +495,104 @@ public void testSUMAggregFunc() {
 
         endEm(em);
     }
+    
+    public void testCEILINGFunc() {
+        EntityManager em = currentEntityManager();
+
+        String query = "SELECT CEILING(SUM(c.age) + 0.4) FROM CompUser c";
+
+        List result = em.createQuery(query).getResultList();
+
+        assertNotNull(result);
+        assertEquals(1, result.size());
+        assertEquals(154L, ((BigDecimal) result.get(0)).longValue());
+
+        endEm(em);
+    }
+
+    public void testEXPFunc() {
+        EntityManager em = currentEntityManager();
+
+        String query = "SELECT EXP(MIN(c.age)) FROM CompUser c";
+
+        List result = em.createQuery(query).getResultList();
+
+        assertNotNull(result);
+        assertEquals(1, result.size());
+        assertEquals(Math.exp(10), (double) result.get(0));
+
+        endEm(em);
+    }
+
+    public void testFLOORFunc() {
+        EntityManager em = currentEntityManager();
+
+        String query = "SELECT FLOOR(SUM(c.age) - 0.4) FROM CompUser c";
+
+        List result = em.createQuery(query).getResultList();
+
+        assertNotNull(result);
+        assertEquals(1, result.size());
+        assertEquals(152L, ((BigDecimal) result.get(0)).longValue());

Review Comment:
   can we add one test with negative number(s)? :))



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@openjpa.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to