openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r828135 - in /openjpa/branches/1.2.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/...
Date Wed, 21 Oct 2009 18:35:28 GMT
Author: mikedd
Date: Wed Oct 21 18:35:27 2009
New Revision: 828135

URL: http://svn.apache.org/viewvc?rev=828135&view=rev
Log:
OPENJPA-1308:
Remove extraneous cast of VARCHAR to VARCHAR for DB2.
Submitted By: B.J. Reed

Modified:
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestEJBQLCondExpression.java
    openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestEJBQLFunction.java

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToLowerCase.java
Wed Oct 21 18:35:27 2009
@@ -43,7 +43,11 @@
         DBDictionary dict = ctx.store.getDBDictionary();
         String func = dict.toLowerCaseFunction;
         dict.assertSupport(func != null, "ToLowerCaseFunction");
-        func = dict.getCastFunction(getValue(), func);
+        if (getValue() instanceof PCPath) {
+            func = dict.getCastFunction(getValue(), func, ((PCPath) getValue()).getFieldMapping(state).getColumns()[0]);
+        } else {
+            func = dict.getCastFunction(getValue(), func);
+        }
 
         int idx = func.indexOf("{0}");
         buf.append(func.substring(0, idx));

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ToUpperCase.java
Wed Oct 21 18:35:27 2009
@@ -43,7 +43,11 @@
         DBDictionary dict = ctx.store.getDBDictionary();
         String func = dict.toUpperCaseFunction;
         dict.assertSupport(func != null, "ToUpperCaseFunction");
-        func = dict.getCastFunction(getValue(), func);
+        if (getValue() instanceof PCPath) {
+            func = dict.getCastFunction(getValue(), func, ((PCPath) getValue()).getFieldMapping(state).getColumns()[0]);
+        } else {
+            func = dict.getCastFunction(getValue(), func);
+        }
 
         int idx = func.indexOf("{0}");
         buf.append(func.substring(0, idx));

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java
Wed Oct 21 18:35:27 2009
@@ -38,13 +38,9 @@
 
         // DB2-based databases have restrictions on having uncast parameters
         // in string functions
-        toUpperCaseFunction = "UPPER(CAST({0} AS VARCHAR(" + varcharCastLength
-            + ")))";
-        toLowerCaseFunction = "LOWER(CAST({0} AS VARCHAR(" + varcharCastLength
-            + ")))";
         stringLengthFunction = "LENGTH({0})";
         concatenateFunction = "(CAST({0} AS VARCHAR(" + varcharCastLength
-            + ")))||(CAST({1} AS VARCHAR(1000)))";
+            + ")))||(CAST({1} AS VARCHAR(" + varcharCastLength + ")))";
 
         trimLeadingFunction = "LTRIM({0})";
         trimTrailingFunction = "RTRIM({0})";
@@ -60,6 +56,7 @@
         supportsLockingWithOuterJoin = false;
         supportsLockingWithInnerJoin = false;
         supportsLockingWithSelectRange = true;
+        supportsCaseConversionForLob = true;
 
         requiresAutoCommitForMetaData = true;
         requiresAliasForSubselect = true;

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java
Wed Oct 21 18:35:27 2009
@@ -729,9 +729,36 @@
      * @return a String with the correct CAST function syntax
      */
     public String getCastFunction(Val val, String func) {
-        if (val instanceof Lit || val instanceof Param)
-            if (func.indexOf("VARCHAR") == -1)
-                func = addCastAsString(func, "{0}", " AS VARCHAR(1000)");
+        if (val instanceof Lit || val instanceof Param) {
+            if (func.indexOf("VARCHAR") == -1) {
+                func = addCastAsString(func, "{0}", " AS VARCHAR(" + varcharCastLength +
")");
+            }
+        }
+        return func;
+    }
+    
+    /**
+     * Return the correct CAST function syntax
+     * 
+     * @param val operand of cast
+     * @param func original string
+     * @param col database column
+     * @return a String with the correct CAST function syntax
+     */
+    public String getCastFunction(Val val, String func, Column col) {
+        boolean doCast = false;
+        if (val instanceof Lit || val instanceof Param) {
+        	doCast = true;
+        }
+        // cast anything not already a VARCHAR to VARCHAR
+        if (col.getType() != Types.VARCHAR) {
+            doCast = true;
+        }
+        if (doCast == true) {
+            if (func.indexOf("VARCHAR") == -1) {
+                func = addCastAsString(func, "{0}", " AS VARCHAR(" + varcharCastLength +
")");
+            }
+        }
         return func;
     }
 

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Wed Oct 21 18:35:27 2009
@@ -239,6 +239,7 @@
     public boolean supportsModOperator = false;
     public boolean supportsXMLColumn = false;
     public boolean reportsSuccessNoInfoOnBatchUpdates = false;
+    public boolean supportsCaseConversionForLob = false;
     
     /**
      * Some Databases append whitespace after the schema name 
@@ -4400,6 +4401,19 @@
     public String getCastFunction(Val val, String func) {
         return func;
     }
+
+    /**
+     * Return the correct CAST function syntax.  This should be overriden by subclasses
+     * that need access to the Column information.
+     * 
+     * @param val operand of cast
+     * @param func original string
+     * @param col database column
+     * @return a String with the correct CAST function syntax
+     */
+    public String getCastFunction(Val val, String func, Column col) {
+    	return getCastFunction (val, func);
+    }
     
     /**
      * Create an index if necessary for some database tables

Modified: openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
(original)
+++ openjpa/branches/1.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DerbyDictionary.java
Wed Oct 21 18:35:27 2009
@@ -45,6 +45,8 @@
         validationSQL = "VALUES(1)";
         stringLengthFunction = "LENGTH({0})";
         substringFunctionName = "SUBSTR";
+        toUpperCaseFunction = "UPPER(CAST({0} AS VARCHAR(" + varcharCastLength + ")))";
+        toLowerCaseFunction = "LOWER(CAST({0} AS VARCHAR(" + varcharCastLength + ")))";
 
         maxConstraintNameLength = 18;
         maxIndexNameLength = 128;

Modified: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
(original)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/apps/CompUser.java
Wed Oct 21 18:35:27 2009
@@ -18,10 +18,20 @@
  */
 package org.apache.openjpa.persistence.common.apps;
 
-import javax.persistence.*;
-import org.apache.openjpa.persistence.*;
+import javax.persistence.Basic;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+import javax.persistence.JoinColumn;
+import javax.persistence.Lob;
+import javax.persistence.OneToOne;
 
-import java.util.*;
+import org.apache.openjpa.persistence.PersistentCollection;
 
 @Entity
 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@@ -37,7 +47,11 @@
 	@Basic
 	@Column(name="compName", length=50)
 	private String computerName;
-
+    
+	@Basic
+    @Lob
+    private String nameAsLob;
+	
 	@PersistentCollection
 	private String[] nicknames = new String[0];
 
@@ -57,6 +71,7 @@
 		this.computerName = cName;
 		this.address = address;
 		this.age = age;
+		this.nameAsLob = name;
 	}
 
 	public String getComputerName() {
@@ -83,7 +98,15 @@
 		return address;
 	}
 
-	public void setAddress(Address address) {
+	public String[] getNicknames() {
+        return nicknames;
+    }
+
+    public void setNicknames(String[] nicknames) {
+        this.nicknames = nicknames;
+    }
+
+    public void setAddress(Address address) {
 		this.address = address;
 	}
 
@@ -95,4 +118,12 @@
 		this.age = age;
 	}
 
+	public String getNameAsLob() {
+		return nameAsLob;
+	}
+
+	public void setNameAsLob(String nameAsLob) {
+		this.nameAsLob = nameAsLob;
+	}
+
 }

Modified: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestEJBQLCondExpression.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestEJBQLCondExpression.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestEJBQLCondExpression.java
(original)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestEJBQLCondExpression.java
Wed Oct 21 18:35:27 2009
@@ -311,12 +311,14 @@
             user.setComputerName(cName);
             user.setAddress(add);
             user.setAge(age);
+            user.setNameAsLob(name);
         } else {
             user = new FemaleUser();
             user.setName(name);
             user.setComputerName(cName);
             user.setAddress(add);
             user.setAge(age);
+            user.setNameAsLob(name);
         }
         return user;
 	}

Modified: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestEJBQLFunction.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestEJBQLFunction.java?rev=828135&r1=828134&r2=828135&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestEJBQLFunction.java
(original)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/functions/TestEJBQLFunction.java
Wed Oct 21 18:35:27 2009
@@ -21,6 +21,9 @@
 import java.util.List;
 import javax.persistence.EntityManager;
 
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
 import org.apache.openjpa.persistence.common.apps.Address;
 import org.apache.openjpa.persistence.common.apps.CompUser;
 import org.apache.openjpa.persistence.common.apps.FemaleUser;
@@ -166,6 +169,35 @@
         endEm(em);
     }
 
+    public void testLowerClobFunc() {
+        OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) currentEntityManager();
+        // some databases do not support case conversion on LOBs,
+        // just skip this test case
+        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
+            .getDBDictionaryInstance();
+        if (!dict.supportsCaseConversionForLob) {
+            return;
+        }
+        startTx(em);
+
+        CompUser user = em.find(CompUser.class, userid5);
+        assertNotNull(user);
+        assertEquals("Famzy", user.getName());
+
+        String query = "UPDATE CompUser e SET " +
+                "e.name = LOWER(e.name) WHERE LOWER(e.nameAsLob)='famzy'";
+
+        int result = em.createQuery(query).executeUpdate();
+
+        user = em.find(CompUser.class, userid5);
+        em.refresh(user);
+        assertNotNull(user);
+        assertEquals("famzy", user.getName());
+
+        endTx(em);
+        endEm(em);
+    }
+
     public void testUpperFunc() {
         EntityManager em = currentEntityManager();
         startTx(em);
@@ -188,6 +220,35 @@
         endEm(em);
     }
 
+    public void testUpperClobFunc() {
+        OpenJPAEntityManagerSPI em = (OpenJPAEntityManagerSPI) currentEntityManager();
+        // some databases do not support case conversion on LOBs,
+        // just skip this test case
+        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
+            .getDBDictionaryInstance();
+        if (!dict.supportsCaseConversionForLob) {
+            return;
+        }
+        startTx(em);
+
+        CompUser user = em.find(CompUser.class, userid5);
+        assertNotNull(user);
+        assertEquals("Famzy", user.getName());
+
+        String query = "UPDATE CompUser e SET " +
+                "e.name = UPPER(e.name) WHERE UPPER(e.nameAsLob)='FAMZY'";
+
+        int result = em.createQuery(query).executeUpdate();
+
+        user = em.find(CompUser.class, userid5);
+        em.refresh(user);
+        assertNotNull(user);
+        assertEquals("FAMZY", user.getName());
+
+        endTx(em);
+        endEm(em);
+    }
+
     public void testLengthFunc() {
         EntityManager em = currentEntityManager();
 
@@ -385,12 +446,14 @@
             user.setComputerName(cName);
             user.setAddress(add);
             user.setAge(age);
+            user.setNameAsLob(name);
         } else {
             user = new FemaleUser();
             user.setName(name);
             user.setComputerName(cName);
             user.setAddress(add);
             user.setAge(age);
+            user.setNameAsLob(name);
         }
         return user;
     }



Mime
View raw message