From: Marco Zanon Date: Thu, 31 Aug 2017 21:55:00 +0000 (+0000) Subject: Modified deleteRecords() to use the IN operator syntax. X-Git-Tag: 6.2.1~1 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=ed45ae02384d7888afa3e5b6c4e6329efdf908b0;p=Macaco Modified deleteRecords() to use the IN operator syntax. --- diff --git a/src/java/com/marcozanon/macaco/database/MSqlTable.java b/src/java/com/marcozanon/macaco/database/MSqlTable.java index fdc9441..2667dfb 100644 --- a/src/java/com/marcozanon/macaco/database/MSqlTable.java +++ b/src/java/com/marcozanon/macaco/database/MSqlTable.java @@ -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 p = new LinkedList(); 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 p = new LinkedList(); 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 p = new LinkedList(); - for (Object id: idSet) { - whereClause.append(" OR (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)"); - p.add(id); + String whereClause = null; + LinkedList p = null; + if (0 == idSet.size()) { + whereClause = "0"; + } + else { + StringJoiner s = new StringJoiner(", "); + p = new LinkedList(); + 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);