Modified sql package to interact with tables via Objects, not Strings.
authorMarco Zanon <info@marcozanon.com>
Fri, 27 Jan 2012 22:17:46 +0000 (22:17 +0000)
committerMarco Zanon <info@marcozanon.com>
Fri, 27 Jan 2012 22:17:46 +0000 (22:17 +0000)
src/java/com/marcozanon/macaco/sql/MSqlConnection.java
src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java
src/java/com/marcozanon/macaco/sql/MSqlTable.java

index 4ade2c93fc98b2877322fbc9f2b6a9ef395d36ac..359a6cc9f1d4b280e077be42646218c69660d4c4 100644 (file)
@@ -217,7 +217,7 @@ public class MSqlConnection extends MObject {
         return this.executePreparedStatement(statement, null);
     }
 
-    public MSqlStatementResults executePreparedStatement(String statement, LinkedList<String> parameters) throws MStatementSqlException {
+    public MSqlStatementResults executePreparedStatement(String statement, LinkedList<Object> parameters) throws MStatementSqlException {
         if ((null == statement) || ("".equals(statement))) {
             throw new IllegalArgumentException("Invalid 'statement': null or empty.");
         }
@@ -231,7 +231,7 @@ public class MSqlConnection extends MObject {
             // prepare the statement
             preparedStatement = this.getConnectionReference().prepareStatement(statement, PreparedStatement.RETURN_GENERATED_KEYS);
             for (int p = 0; p < parameters.size(); p++) {
-                preparedStatement.setString(p + 1, parameters.get(p));
+                preparedStatement.setObject(p + 1, parameters.get(p));
             }
             // execute the statement and parse the results
             preparedStatement.execute();
index fe67dae5f257bda93c0857f51674dad7b577574f..8433bee5ff74aeec4ffc91aeb724eefebffe35ac 100644 (file)
@@ -40,9 +40,9 @@ public class MSqlStatementResults extends MObject {
     protected PreparedStatement preparedStatement = null;
     protected ResultSet resultSet = null;
 
-    protected LinkedList<LinkedHashMap<String, String>> records = new LinkedList<LinkedHashMap<String, String>>();
+    protected LinkedList<LinkedHashMap<String, Object>> records = new LinkedList<LinkedHashMap<String, Object>>();
 
-    protected LinkedHashSet<String> generatedKeys = new LinkedHashSet<String>();
+    protected LinkedHashSet<Object> generatedKeys = new LinkedHashSet<Object>();
 
     /* */
 
@@ -60,10 +60,10 @@ public class MSqlStatementResults extends MObject {
             if (null != this.getResultSetReference()) {
                 while (this.getResultSetReference().next()) {
                     ResultSetMetaData resultSetMetaData = this.getResultSetReference().getMetaData();
-                    LinkedHashMap<String, String> record = new LinkedHashMap<String, String>();
+                    LinkedHashMap<String, Object> record = new LinkedHashMap<String, Object>();
                     for (int f = 0; f < resultSetMetaData.getColumnCount(); f++) {
                         String field = resultSetMetaData.getColumnLabel(f + 1);
-                        record.put(field, this.getResultSetReference().getString(field));
+                        record.put(field, this.getResultSetReference().getObject(field));
                     }
                     this.getRecordsReference().add(record);
                 }
@@ -76,7 +76,7 @@ public class MSqlStatementResults extends MObject {
         try {
             ResultSet generatedKeysResultSet = this.getPreparedStatementReference().getGeneratedKeys();
             while (generatedKeysResultSet.next()) {
-                this.getGeneratedKeysReference().add(generatedKeysResultSet.getString(1));
+                this.getGeneratedKeysReference().add(generatedKeysResultSet.getObject(1));
             }
             generatedKeysResultSet.close();
         }
@@ -117,14 +117,14 @@ public class MSqlStatementResults extends MObject {
 
     /* Records */
 
-    protected LinkedList<LinkedHashMap<String, String>> getRecordsReference() {
+    protected LinkedList<LinkedHashMap<String, Object>> getRecordsReference() {
         return this.records;
     }
 
-    public LinkedList<LinkedHashMap<String, String>> getRecords() {
-        LinkedList<LinkedHashMap<String, String>> tmpRecords = new LinkedList<LinkedHashMap<String, String>>();
-        for (LinkedHashMap<String, String> record: this.getRecordsReference()) {
-            LinkedHashMap<String, String> tmpRecord = new LinkedHashMap<String, String>();
+    public LinkedList<LinkedHashMap<String, Object>> getRecords() {
+        LinkedList<LinkedHashMap<String, Object>> tmpRecords = new LinkedList<LinkedHashMap<String, Object>>();
+        for (LinkedHashMap<String, Object> record: this.getRecordsReference()) {
+            LinkedHashMap<String, Object> tmpRecord = new LinkedHashMap<String, Object>();
             for (String key: record.keySet()) {
                 tmpRecord.put(key, record.get(key));
             }
@@ -133,14 +133,14 @@ public class MSqlStatementResults extends MObject {
         return tmpRecords;
     }
 
-    public LinkedList<String> getRecordsByField(String field) {
+    public LinkedList<Object> getRecordsByField(String field) {
         if ((null == field) || ("".equals(field))) {
             throw new IllegalArgumentException("Invalid 'field': null or empty.");
         }
         //
-        LinkedList<String> recordsByField = new LinkedList<String>();
-        for (LinkedHashMap<String, String> record: this.getRecordsReference()) {
-            String r = record.get(field);
+        LinkedList<Object> recordsByField = new LinkedList<Object>();
+        for (LinkedHashMap<String, Object> record: this.getRecordsReference()) {
+            Object r = record.get(field);
             if (null == r) {
                 throw new IllegalArgumentException(String.format("Invalid 'field': %s.", field));
             }
@@ -153,15 +153,15 @@ public class MSqlStatementResults extends MObject {
 
     /* Generated keys */
 
-    protected LinkedHashSet<String> getGeneratedKeysReference() {
+    protected LinkedHashSet<Object> getGeneratedKeysReference() {
         return this.generatedKeys;
     }
 
-    public LinkedHashSet<String> getGeneratedKeys() {
-        LinkedHashSet<String> tmpGeneratedKeys = new LinkedHashSet<String>();
+    public LinkedHashSet<Object> getGeneratedKeys() {
+        LinkedHashSet<Object> tmpGeneratedKeys = new LinkedHashSet<Object>();
         Iterator i = this.getGeneratedKeysReference().iterator();
         while (i.hasNext()) {
-            tmpGeneratedKeys.add((String)i.next());
+            tmpGeneratedKeys.add(i.next());
         }
         return tmpGeneratedKeys;
     }
index d3910e832ae8c669aa5ec1a39e1f183967a5915a..6e7c02091f3bb15f7bec1d81e1f043f798718a03 100644 (file)
@@ -75,12 +75,12 @@ public class MSqlTable extends MObject {
 
     /* Statements */
 
-    protected MSqlStatementResults insertRecord(LinkedHashMap<String, String> map) throws MStatementSqlException {
+    protected MSqlStatementResults insertRecord(LinkedHashMap<String, Object> map) throws MStatementSqlException {
         if (null == map) {
             throw new IllegalArgumentException("Invalid 'map': null.");
         }
         //
-        LinkedList<String> p = new LinkedList<String>();
+        LinkedList<Object> p = new LinkedList<Object>();
         StringBuilder fields = new StringBuilder("");
         StringBuilder s = new StringBuilder("");
         for (String field: map.keySet()) {
@@ -96,15 +96,15 @@ public class MSqlTable extends MObject {
                                                                       p);
     }
 
-    protected MSqlStatementResults updateRecord(LinkedHashMap<String, String> map, String id) throws MStatementSqlException {
+    protected MSqlStatementResults updateRecord(LinkedHashMap<String, Object> map, Object id) throws MStatementSqlException {
         if (null == map) {
             throw new IllegalArgumentException("Invalid 'map': null.");
         }
-        if ((null == id) || ("".equals(id))) {
-            throw new IllegalArgumentException("Invalid 'id': null or empty.");
+        if (null == id) {
+            throw new IllegalArgumentException("Invalid 'id': null.");
         }
         //
-        LinkedList<String> p = new LinkedList<String>();
+        LinkedList<Object> p = new LinkedList<Object>();
         StringBuilder s = new StringBuilder("");
         for (String field: map.keySet()) {
             s.append("\"" + this.getTable() + "\".\"" + field + "\" = ?, ");
@@ -118,7 +118,7 @@ public class MSqlTable extends MObject {
                                                                       p);
     }
 
-    public MSqlStatementResults setRecord(LinkedHashMap<String, String> map, String id) throws MStatementSqlException {
+    public MSqlStatementResults setRecord(LinkedHashMap<String, Object> map, Object id) throws MStatementSqlException {
         if (null == id) {
             return this.insertRecord(map);
         }
@@ -127,12 +127,12 @@ public class MSqlTable extends MObject {
         }
     }
 
-    public MSqlStatementResults getRecord(String id) throws MStatementSqlException {
-        if ((null == id) || ("".equals(id))) {
-            throw new IllegalArgumentException("Invalid 'id': null or empty.");
+    public MSqlStatementResults getRecord(Object id) throws MStatementSqlException {
+        if (null == id) {
+            throw new IllegalArgumentException("Invalid 'id': null.");
         }
         //
-        LinkedList<String> p = new LinkedList<String>();
+        LinkedList<Object> p = new LinkedList<Object>();
         p.add(id);
         return this.getConnectionReference().executePreparedStatement(" SELECT  *"
                                                                     + " FROM    \"" + this.getTable() + "\""
@@ -140,12 +140,12 @@ public class MSqlTable extends MObject {
                                                                       p);
     }
 
-    public MSqlStatementResults deleteRecord(String id) throws MStatementSqlException {
-        if ((null == id) || ("".equals(id))) {
-            throw new IllegalArgumentException("Invalid 'id': null or empty.");
+    public MSqlStatementResults deleteRecord(Object id) throws MStatementSqlException {
+        if (null == id) {
+            throw new IllegalArgumentException("Invalid 'id': null.");
         }
         //
-        LinkedList<String> p = new LinkedList<String>();
+        LinkedList<Object> p = new LinkedList<Object>();
         p.add(id);
         return this.getConnectionReference().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\""
                                                                     + " WHERE       (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)",