This fixes a problem created by SapDB.  The quoteAndEscape method in
SqlExpression.java always escapes backslashes, but SapDB reads the escaped
characters literally.  So I added a method to DB.java and DBSapDB.java that
returns a boolean for the escape rules.  The changes were made to the
quoteAndEscape method, so;
public static String quoteAndEscapeText(String rawText) became
public static String quoteAndEscapeText(String rawText, DB db).

And public static String quoteAndEscapeText(String rawText) is marked
deprecated.  I will post a separate message as stated in the deprecation
policy document.

Dave Polito


Index: SqlExpression.java
===================================================================
RCS file:
/home/cvspublic/jakarta-turbine/src/java/org/apache/turbine/util/db/SqlExpre
ssion.java,v
retrieving revision 1.18.4.1
diff -u -r1.18.4.1 SqlExpression.java
--- SqlExpression.java  2001/05/07 03:06:41     1.18.4.1
+++ SqlExpression.java  2001/05/30 00:01:42
@@ -287,7 +287,7 @@
                criteria instanceof StringKey ||
                criteria instanceof DateKey)
            {
-               criteria = quoteAndEscapeText(criteria.toString());
+               criteria = quoteAndEscapeText(criteria.toString(), db);
            }
            else if( criteria instanceof Boolean )
            {
@@ -550,7 +550,7 @@
         String ret = null;
         if (value instanceof String)
         {
-            ret = quoteAndEscapeText((String)value);
+            ret = quoteAndEscapeText((String)value, db);
         }
         else
         {
@@ -571,10 +571,21 @@
      * @param rawText The <i>unquoted</i>, <i>unescaped</i> text to
process.
      * @return Quoted and escaped text.
      */
-    public static String quoteAndEscapeText(String rawText)
+    public static String quoteAndEscapeText(String rawText, DB db)
     {
         StringBuffer buf = new StringBuffer( (int)(rawText.length() * 1.1)
);
 
+        /* Some databases do not need escaping.  */
+        String escapeString = new String();
+        if (db != null && db.escapeText() == false)
+        {
+            escapeString = String.valueOf(BACKSLASH);
+        }
+        else
+        {
+            escapeString = String.valueOf(BACKSLASH) +
String.valueOf(BACKSLASH);
+        }
+
         char[] data = rawText.toCharArray();
         buf.append(SINGLE_QUOTE);
         for (int i = 0; i < data.length; i++)
@@ -594,5 +605,15 @@
         buf.append(SINGLE_QUOTE);
 
         return buf.toString();
+    }
+
+    /**
+     *
+     * @deprecated Use quoteAndEscapeText(String rawText, DB db) instead.
+     * the quoteAndEscapeText rules depend on the database.
+     */
+    public static String quoteAndEscapeText(String rawText)
+    {
+        return quoteAndEscapeText(rawText, null);
     }
 }


Index: DB.java
===================================================================
RCS file:
/home/cvspublic/jakarta-turbine/src/java/org/apache/turbine/util/db/adapter/
DB.java,v
retrieving revision 1.4.10.1
diff -u -r1.4.10.1 DB.java
--- DB.java     2001/05/07 03:06:43     1.4.10.1
+++ DB.java     2001/05/30 00:02:23
@@ -359,4 +359,17 @@
     {
         return LIMIT_STYLE_NONE;
     }
+
+   /**
+    * This method is for the SqlExpression.quoteAndEscape rules.  The rule
is,
+    * any string in a SqlExpression with a BACKSLASH will either be changed
to
+    * "\\" or left as "\".  SapDB does not need the escape character.
+    *
+    * @return true if the database needs to escape text in SqlExpressions.
+    */
+    
+    public boolean escapeText()
+    {
+        return true;
+    }
 }

Index: DBSapDB.java
===================================================================
RCS file:
/home/cvspublic/jakarta-turbine/src/java/org/apache/turbine/util/db/adapter/
DBSapDB.java,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 DBSapDB.java
--- DBSapDB.java        2001/05/28 15:04:06     1.1.2.1
+++ DBSapDB.java        2001/05/30 00:02:47
@@ -180,4 +180,17 @@
         // user may have issued a commit but do it here to be sure.
         con.commit();
     }
+
+   /**
+    * This method is for the SqlExpression.quoteAndEscape rules.  The rule
is,
+    * any string in a SqlExpression with a BACKSLASH will either be changed
to
+    * "\\" or left as "\".  SapDB does not need the escape character.
+    *
+    * @return false.
+    */
+    
+    public boolean escapeText()
+    {
+        return false;
+    }
 }

Reply via email to