From 9af38179e3c1995d5120fc1372df21142cb20982 Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Fri, 27 Jan 2012 22:17:46 +0000 Subject: [PATCH] Modified sql package to interact with tables via Objects, not Strings. --- .../marcozanon/macaco/sql/MSqlConnection.java | 4 +-- .../macaco/sql/MSqlStatementResults.java | 36 +++++++++---------- .../com/marcozanon/macaco/sql/MSqlTable.java | 30 ++++++++-------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/java/com/marcozanon/macaco/sql/MSqlConnection.java b/src/java/com/marcozanon/macaco/sql/MSqlConnection.java index 4ade2c9..359a6cc 100644 --- a/src/java/com/marcozanon/macaco/sql/MSqlConnection.java +++ b/src/java/com/marcozanon/macaco/sql/MSqlConnection.java @@ -217,7 +217,7 @@ public class MSqlConnection extends MObject { return this.executePreparedStatement(statement, null); } - public MSqlStatementResults executePreparedStatement(String statement, LinkedList parameters) throws MStatementSqlException { + public MSqlStatementResults executePreparedStatement(String statement, LinkedList 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(); diff --git a/src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java b/src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java index fe67dae..8433bee 100644 --- a/src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java +++ b/src/java/com/marcozanon/macaco/sql/MSqlStatementResults.java @@ -40,9 +40,9 @@ public class MSqlStatementResults extends MObject { protected PreparedStatement preparedStatement = null; protected ResultSet resultSet = null; - protected LinkedList> records = new LinkedList>(); + protected LinkedList> records = new LinkedList>(); - protected LinkedHashSet generatedKeys = new LinkedHashSet(); + protected LinkedHashSet generatedKeys = new LinkedHashSet(); /* */ @@ -60,10 +60,10 @@ public class MSqlStatementResults extends MObject { if (null != this.getResultSetReference()) { while (this.getResultSetReference().next()) { ResultSetMetaData resultSetMetaData = this.getResultSetReference().getMetaData(); - LinkedHashMap record = new LinkedHashMap(); + LinkedHashMap record = new LinkedHashMap(); 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> getRecordsReference() { + protected LinkedList> getRecordsReference() { return this.records; } - public LinkedList> getRecords() { - LinkedList> tmpRecords = new LinkedList>(); - for (LinkedHashMap record: this.getRecordsReference()) { - LinkedHashMap tmpRecord = new LinkedHashMap(); + public LinkedList> getRecords() { + LinkedList> tmpRecords = new LinkedList>(); + for (LinkedHashMap record: this.getRecordsReference()) { + LinkedHashMap tmpRecord = new LinkedHashMap(); for (String key: record.keySet()) { tmpRecord.put(key, record.get(key)); } @@ -133,14 +133,14 @@ public class MSqlStatementResults extends MObject { return tmpRecords; } - public LinkedList getRecordsByField(String field) { + public LinkedList getRecordsByField(String field) { if ((null == field) || ("".equals(field))) { throw new IllegalArgumentException("Invalid 'field': null or empty."); } // - LinkedList recordsByField = new LinkedList(); - for (LinkedHashMap record: this.getRecordsReference()) { - String r = record.get(field); + LinkedList recordsByField = new LinkedList(); + for (LinkedHashMap 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 getGeneratedKeysReference() { + protected LinkedHashSet getGeneratedKeysReference() { return this.generatedKeys; } - public LinkedHashSet getGeneratedKeys() { - LinkedHashSet tmpGeneratedKeys = new LinkedHashSet(); + public LinkedHashSet getGeneratedKeys() { + LinkedHashSet tmpGeneratedKeys = new LinkedHashSet(); Iterator i = this.getGeneratedKeysReference().iterator(); while (i.hasNext()) { - tmpGeneratedKeys.add((String)i.next()); + tmpGeneratedKeys.add(i.next()); } return tmpGeneratedKeys; } diff --git a/src/java/com/marcozanon/macaco/sql/MSqlTable.java b/src/java/com/marcozanon/macaco/sql/MSqlTable.java index d3910e8..6e7c020 100644 --- a/src/java/com/marcozanon/macaco/sql/MSqlTable.java +++ b/src/java/com/marcozanon/macaco/sql/MSqlTable.java @@ -75,12 +75,12 @@ public class MSqlTable extends MObject { /* Statements */ - protected MSqlStatementResults insertRecord(LinkedHashMap map) throws MStatementSqlException { + protected MSqlStatementResults insertRecord(LinkedHashMap map) throws MStatementSqlException { if (null == map) { throw new IllegalArgumentException("Invalid 'map': null."); } // - LinkedList p = new LinkedList(); + LinkedList p = new LinkedList(); 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 map, String id) throws MStatementSqlException { + protected MSqlStatementResults updateRecord(LinkedHashMap 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 p = new LinkedList(); + LinkedList p = new LinkedList(); 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 map, String id) throws MStatementSqlException { + public MSqlStatementResults setRecord(LinkedHashMap 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 p = new LinkedList(); + LinkedList p = new LinkedList(); 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 p = new LinkedList(); + LinkedList p = new LinkedList(); p.add(id); return this.getConnectionReference().executePreparedStatement(" DELETE FROM \"" + this.getTable() + "\"" + " WHERE (\"" + this.getTable() + "\".\"" + this.getPrimaryKey() + "\" = ?)", -- 2.30.2