Repository: empire-db
Updated Branches:
  refs/heads/master 0d65af5a3 -> b0eb672d5


EMPIREDB-184

Project: http://git-wip-us.apache.org/repos/asf/empire-db/repo
Commit: http://git-wip-us.apache.org/repos/asf/empire-db/commit/b0eb672d
Tree: http://git-wip-us.apache.org/repos/asf/empire-db/tree/b0eb672d
Diff: http://git-wip-us.apache.org/repos/asf/empire-db/diff/b0eb672d

Branch: refs/heads/master
Commit: b0eb672d542629aec03e7444607f0b6ecba41a0a
Parents: 0d65af5
Author: Jan Glaubitz <[email protected]>
Authored: Mon Dec 12 14:42:06 2016 +0100
Committer: Jan Glaubitz <[email protected]>
Committed: Mon Dec 12 14:42:06 2016 +0100

----------------------------------------------------------------------
 .../empire/db/mysql/DBDatabaseDriverMySQL.java  | 35 +++++------
 .../db/mysql/DBDatabaseDriverMySQLTest.java     | 64 ++++++++++++++++++++
 2 files changed, 78 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/empire-db/blob/b0eb672d/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
----------------------------------------------------------------------
diff --git 
a/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java 
b/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
index 81f9ee4..49f19eb 100644
--- 
a/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
+++ 
b/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
@@ -725,32 +725,25 @@ public class DBDatabaseDriverMySQL extends 
DBDatabaseDriver
     @Override
     protected void appendSQLTextValue(StringBuilder buf, String value)
     {
-       boolean escape = false;
-        if (value.indexOf('\'') >= 0)
-        { // a routine to double up single quotes for SQL
-               escape = true;
-            int len = value.length();
+        if (value.indexOf('\'') >= 0 || value.indexOf('\\') >= 0)
+        {
+               int len = value.length();
             for (int i = 0; i < len; i++)
             {
                 if (value.charAt(i) == '\'')
+                { // a routine to double up single quotes for SQL
                     buf.append("''");
-                else
-                    buf.append(value.charAt(i));
-            }
-        }
-        if (value.indexOf('\\') >= 0)
-        { // a routine to double up backslashes for MySQL
-               escape = true;
-            int len = value.length();
-            for (int i = 0; i < len; i++)
-            {
-                if (value.charAt(i) == '\\')
-                    buf.append("\\\\");
-                else
-                    buf.append(value.charAt(i));
+                }
+                else if (value.charAt(i) == '\\')
+                { // a routine to double up backslashes for MySQL
+                       buf.append("\\\\");
+                } else
+                {
+                       buf.append(value.charAt(i));
+                }
             }
-        }
-        if (!escape) {
+        } else 
+        {
             buf.append(value);
         }
     }

http://git-wip-us.apache.org/repos/asf/empire-db/blob/b0eb672d/empire-db/src/test/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQLTest.java
----------------------------------------------------------------------
diff --git 
a/empire-db/src/test/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQLTest.java
 
b/empire-db/src/test/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQLTest.java
new file mode 100644
index 0000000..563ddbc
--- /dev/null
+++ 
b/empire-db/src/test/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQLTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.empire.db.mysql;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.empire.db.CompanyDB;
+import org.apache.empire.db.CompanyDB.Departments;
+import org.apache.empire.db.DBCommand;
+import org.apache.empire.db.DBDatabaseDriver;
+import org.junit.Test;
+
+public class DBDatabaseDriverMySQLTest {
+       
+       @Test
+       public void appendSQLTextValue()
+       {
+
+        CompanyDB db = new CompanyDB();
+        DBDatabaseDriver driver = new DBDatabaseDriverMySQL();
+        db.open(driver, null);
+
+        Departments TD = db.DEPARTMENT;
+        
+               DBCommand cmd = db.createCommand();
+
+               // Test 1
+               cmd.select(TD.count());
+               cmd.where(TD.NAME.is("\\LCI\\"));
+               assertTrue(cmd.getSelect().endsWith(("NAME='\\\\LCI\\\\'"))); 
// Must be double escaped
+               
+               cmd = db.createCommand();
+
+               // Test 2
+               cmd.select(TD.count());
+               cmd.where(TD.NAME.is("'"));
+               assertTrue(cmd.getSelect().contains("NAME=''''"));
+
+               cmd = db.createCommand();
+               
+               // \ and '
+               cmd.select(TD.count());
+               cmd.where(TD.NAME.is("Tarkk\\'ampujankatu"));
+               
assertTrue(cmd.getSelect().contains("NAME='Tarkk\\\\''ampujankatu'"));
+               
+       }
+       
+}

Reply via email to