Modified deleteRecords() to use the IN operator syntax.
authorMarco Zanon <info@marcozanon.com>
Thu, 31 Aug 2017 21:55:00 +0000 (21:55 +0000)
committerMarco Zanon <info@marcozanon.com>
Thu, 31 Aug 2017 21:55:00 +0000 (21:55 +0000)
6.x/src/java/com/marcozanon/macaco/database/MSqlTable.java

index fdc9441b6578e8a93d8d564c28ce98441531c76f..2667dfbac823196682b27cdeae9fe70b639d834d 100644 (file)
@@ -11,6 +11,7 @@ import com.marcozanon.macaco.text.MText;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
+import java.util.StringJoiner;
 
 public class MSqlTable extends MObject {
 
@@ -73,6 +74,7 @@ public class MSqlTable extends MObject {
         }
         fields.delete(fields.length() - 2, fields.length());
         s.delete(s.length() - 2, s.length());
+        //
         return this.getDatabaseConnection().executePreparedStatement(" INSERT INTO \"" + this.getTable() + "\""
                                                                    + "             (" + fields + ")"
                                                                    + " VALUES      (" + s + ")",
@@ -95,6 +97,7 @@ public class MSqlTable extends MObject {
         }
         s.delete(s.length() - 2, s.length());
         p.add(id);
+        //
         return this.getDatabaseConnection().executePreparedStatement(" UPDATE  \"" + this.getTable() + "\""
                                                                    + " SET     " + s
                                                                    + " WHERE   (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)",
@@ -117,6 +120,7 @@ public class MSqlTable extends MObject {
         //
         LinkedList<Object> p = new LinkedList<Object>();
         p.add(id);
+        //
         return this.getDatabaseConnection().executePreparedStatement(" SELECT  *"
                                                                    + " FROM    \"" + this.getTable() + "\""
                                                                    + " WHERE   (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)",
@@ -130,6 +134,7 @@ public class MSqlTable extends MObject {
         //
         LinkedList<Object> p = new LinkedList<Object>();
         p.add(id);
+        //
         return this.getDatabaseConnection().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\""
                                                                    + " WHERE       (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)",
                                                                      p);
@@ -140,12 +145,21 @@ public class MSqlTable extends MObject {
             throw new IllegalArgumentException("Invalid 'idSet': null.");
         }
         //
-        StringBuilder whereClause = new StringBuilder("(0)");
-        LinkedList<Object> p = new LinkedList<Object>();
-        for (Object id: idSet) {
-            whereClause.append(" OR (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)");
-            p.add(id);
+        String whereClause = null;
+        LinkedList<Object> p = null;
+        if (0 == idSet.size()) {
+            whereClause = "0";
+        }
+        else {
+            StringJoiner s = new StringJoiner(", ");
+            p = new LinkedList<Object>();
+            for (Object id: idSet) {
+                s.add("?");
+                p.add(id);
+            }
+            whereClause = "\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" IN (" + s.toString() + ")";
         }
+        //
         return this.getDatabaseConnection().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\""
                                                                    + " WHERE       (" + whereClause + ")",
                                                                      p);