cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [cayenne] 01/04: Do not use TRIM() on Sybase to keep 4.1 compatibility
Date Sat, 04 May 2019 15:01:03 GMT
This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit d72999e4b78cf2db70dfe4d79fd7953e21bca34b
Author: Nikita Timofeev <stariy95@gmail.com>
AuthorDate: Sat May 4 17:13:21 2019 +0300

    Do not use TRIM() on Sybase to keep 4.1 compatibility
---
 .../cayenne/dba/sqlserver/SQLServerAdapter.java    |  4 +
 .../dba/sqlserver/SQLServerTreeProcessor.java      | 96 +---------------------
 .../apache/cayenne/dba/sybase/SybaseAdapter.java   |  7 +-
 .../SybaseSQLTreeProcessor.java}                   | 21 +++--
 4 files changed, 21 insertions(+), 107 deletions(-)

diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerAdapter.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerAdapter.java
index 6a99dcb..ed0619b 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerAdapter.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerAdapter.java
@@ -74,6 +74,10 @@ import org.apache.cayenne.resource.ResourceLocator;
  */
 public class SQLServerAdapter extends SybaseAdapter {
 
+	/**
+	 * @deprecated since 4.2 unused
+	 */
+	@Deprecated
 	public static final String TRIM_FUNCTION = "RTRIM";
 
 	public SQLServerAdapter(@Inject RuntimeProperties runtimeProperties,
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java
index 2681a53..2d697cb 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java
@@ -20,109 +20,17 @@
 package org.apache.cayenne.dba.sqlserver;
 
 import org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode;
-import org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode;
-import org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode;
-import org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode;
 import org.apache.cayenne.access.sqlbuilder.sqltree.Node;
-import org.apache.cayenne.access.sqlbuilder.sqltree.NodeType;
-import org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode;
-import org.apache.cayenne.access.sqlbuilder.sqltree.TextNode;
-import org.apache.cayenne.access.sqlbuilder.sqltree.TopNode;
-import org.apache.cayenne.access.translator.select.BaseSQLTreeProcessor;
 import org.apache.cayenne.dba.sqlserver.sqltree.SQLServerColumnNode;
+import org.apache.cayenne.dba.sybase.SybaseSQLTreeProcessor;
 
 /**
  * @since 4.2
  */
-public class SQLServerTreeProcessor extends BaseSQLTreeProcessor {
-
-    @Override
-    protected void onLimitOffsetNode(Node parent, LimitOffsetNode child, int index) {
-        // SQLServer uses "SELECT DISTINCT TOP N" or "SELECT TOP N" instead of LIMIT
-        // Offset will be calculated in-memory
-        replaceChild(parent, index, new EmptyNode(), false);
-        if(child.getLimit() > 0) {
-            int limit = child.getLimit() + child.getOffset();
-            // we have root actually as input for processor, but it's better to keep processor
stateless
-            // root shouldn't be far from limit's parent (likely it will be parent itself)
-            Node root = getRoot(parent);
-            int idx = 0;
-            if(root.getChild(0).getType() == NodeType.DISTINCT) {
-                idx = 1;
-            }
-            root.addChild(idx, new TopNode(limit));
-        }
-    }
-
-    private Node getRoot(Node node) {
-        while(node.getParent() != null) {
-            node = node.getParent();
-        }
-        return node;
-    }
+public class SQLServerTreeProcessor extends SybaseSQLTreeProcessor {
 
     @Override
     protected void onColumnNode(Node parent, ColumnNode child, int index) {
         replaceChild(parent, index,  new SQLServerColumnNode(child));
     }
-
-    @Override
-    protected void onFunctionNode(Node parent, FunctionNode child, int index) {
-        String functionName = child.getFunctionName();
-        Node replacement = null;
-        switch (functionName) {
-            case "LENGTH":
-                replacement = new FunctionNode("LEN", child.getAlias(), true);
-                break;
-            case "LOCATE":
-                replacement = new FunctionNode("CHARINDEX", child.getAlias(), true);
-                break;
-            case "MOD":
-                replacement = new OpExpressionNode("%");
-                break;
-            case "TRIM":
-                Node rtrim = new FunctionNode("RTRIM", null, true);
-                replacement = new FunctionNode("LTRIM", child.getAlias(), true);
-                for(int i=0; i<child.getChildrenCount(); i++) {
-                    rtrim.addChild(child.getChild(i));
-                }
-                replacement.addChild(rtrim);
-                parent.replaceChild(index, replacement);
-                return;
-            case "CURRENT_DATE":
-                replacement = new FunctionNode("{fn CURDATE()}", child.getAlias(), false);
-                break;
-            case "CURRENT_TIME":
-                replacement = new FunctionNode("{fn CURTIME()}", child.getAlias(), false);
-                break;
-            case "CURRENT_TIMESTAMP":
-                replacement = new FunctionNode("CURRENT_TIMESTAMP", child.getAlias(), false);
-                break;
-
-            case "YEAR":
-            case "MONTH":
-            case "WEEK":
-            case "DAY_OF_YEAR":
-            case "DAY":
-            case "DAY_OF_MONTH":
-            case "DAY_OF_WEEK":
-            case "HOUR":
-            case "MINUTE":
-            case "SECOND":
-                replacement = new FunctionNode("DATEPART", child.getAlias(), true);
-                if("DAY_OF_MONTH".equals(functionName)) {
-                    functionName = "DAY";
-                } else if("DAY_OF_WEEK".equals(functionName)) {
-                    functionName = "WEEKDAY";
-                } else if("DAY_OF_YEAR".equals(functionName)) {
-                    functionName = "DAYOFYEAR";
-                }
-                replacement.addChild(new TextNode(functionName));
-                break;
-        }
-
-        if(replacement != null) {
-            replaceChild(parent, index, replacement);
-        }
-    }
 }
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseAdapter.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseAdapter.java
index 2aaff9d..6226f2c 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseAdapter.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseAdapter.java
@@ -20,7 +20,6 @@
 package org.apache.cayenne.dba.sybase;
 
 import java.sql.PreparedStatement;
-import java.sql.SQLException;
 import java.sql.Types;
 import java.util.List;
 import java.util.function.Function;
@@ -42,7 +41,6 @@ import org.apache.cayenne.dba.DefaultQuotingStrategy;
 import org.apache.cayenne.dba.JdbcAdapter;
 import org.apache.cayenne.dba.PkGenerator;
 import org.apache.cayenne.dba.QuotingStrategy;
-import org.apache.cayenne.dba.sqlserver.SQLServerTreeProcessor;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.resource.ResourceLocator;
@@ -81,7 +79,7 @@ public class SybaseAdapter extends JdbcAdapter {
      */
     @Override
     public Function<Node, Node> getSqlTreeProcessor() {
-        return new SQLServerTreeProcessor();
+        return new SybaseSQLTreeProcessor();
     }
 
     /**
@@ -124,8 +122,7 @@ public class SybaseAdapter extends JdbcAdapter {
     }
 
     @Override
-    public void bindParameter(PreparedStatement statement, ParameterBinding binding)
-            throws SQLException, Exception {
+    public void bindParameter(PreparedStatement statement, ParameterBinding binding) throws
Exception {
 
         // Sybase driver doesn't like CLOBs and BLOBs as parameters
         if (binding.getValue() == null) {
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseSQLTreeProcessor.java
similarity index 90%
copy from cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java
copy to cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseSQLTreeProcessor.java
index 2681a53..8393298 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseSQLTreeProcessor.java
@@ -17,7 +17,7 @@
  *  under the License.
  ****************************************************************/
 
-package org.apache.cayenne.dba.sqlserver;
+package org.apache.cayenne.dba.sybase;
 
 import org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode;
 import org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode;
@@ -34,7 +34,7 @@ import org.apache.cayenne.dba.sqlserver.sqltree.SQLServerColumnNode;
 /**
  * @since 4.2
  */
-public class SQLServerTreeProcessor extends BaseSQLTreeProcessor {
+public class SybaseSQLTreeProcessor extends BaseSQLTreeProcessor {
 
     @Override
     protected void onLimitOffsetNode(Node parent, LimitOffsetNode child, int index) {
@@ -110,12 +110,16 @@ public class SQLServerTreeProcessor extends BaseSQLTreeProcessor {
             case "MINUTE":
             case "SECOND":
                 replacement = new FunctionNode("DATEPART", child.getAlias(), true);
-                if("DAY_OF_MONTH".equals(functionName)) {
-                    functionName = "DAY";
-                } else if("DAY_OF_WEEK".equals(functionName)) {
-                    functionName = "WEEKDAY";
-                } else if("DAY_OF_YEAR".equals(functionName)) {
-                    functionName = "DAYOFYEAR";
+                switch (functionName) {
+                    case "DAY_OF_MONTH":
+                        functionName = "DAY";
+                        break;
+                    case "DAY_OF_WEEK":
+                        functionName = "WEEKDAY";
+                        break;
+                    case "DAY_OF_YEAR":
+                        functionName = "DAYOFYEAR";
+                        break;
                 }
                 replacement.addChild(new TextNode(functionName));
                 break;
@@ -125,4 +129,5 @@ public class SQLServerTreeProcessor extends BaseSQLTreeProcessor {
             replaceChild(parent, index, replacement);
         }
     }
+
 }


Mime
View raw message