From 7e723c6ddb70bdfbf283bd327a5d97f2ba9557a4 Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 2 Mar 2001 00:34:04 +0000 Subject: [PATCH] Moved query generation entirely into Table instead of half in Cursor and half in table. Then added a select() method that allows the addition of other tables into the FROM clause so that a join can be done to determine the key of the desired row. For example: select ... FROM users, sessions WHERE authcode = 'foo' AND users.userid = sessions.userid git-svn-id: https://samskivert.googlecode.com/svn/trunk@64 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/jdbc/jora/Cursor.java | 13 +++--- .../java/com/samskivert/jdbc/jora/Table.java | 41 +++++++++++++------ 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/jora/Cursor.java b/projects/samskivert/src/java/com/samskivert/jdbc/jora/Cursor.java index e39f1bf2..7ad22360 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/jora/Cursor.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/jora/Cursor.java @@ -44,14 +44,13 @@ public class Cursor { continue; } if (qbeObject != null) { - String sql = "select " + table.listOfFields - + " from " + table.name + " " + query; PreparedStatement qbeStmt; synchronized(session.preparedStmtHash) { - Object s = session.preparedStmtHash.get(sql); + Object s = session.preparedStmtHash.get(query); if (s == null) { - qbeStmt=session.connection.prepareStatement(sql); - session.preparedStmtHash.put(sql, qbeStmt); + qbeStmt= + session.connection.prepareStatement(query); + session.preparedStmtHash.put(query, qbeStmt); } else { qbeStmt = (PreparedStatement)s; } @@ -65,9 +64,7 @@ public class Cursor { if (stmt == null) { stmt = session.connection.createStatement(); } - String sql = "select " + table.listOfFields - + " from " + table.name + " " + query; - result = stmt.executeQuery(sql); + result = stmt.executeQuery(query); } } if (result.next()) { diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/jora/Table.java b/projects/samskivert/src/java/com/samskivert/jdbc/jora/Table.java index e605a699..d2ead1d7 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/jora/Table.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/jora/Table.java @@ -88,7 +88,6 @@ public class Table { s, null); } - /** Constructor of table with "key" and "session" parameters inherited * from base table. */ @@ -97,14 +96,29 @@ public class Table { null, null); } - /** Select records from database table according to search condition * * @param condition valid SQL condition expression started with WHERE * or empty string if all records should be fetched. */ public final Cursor select(String condition) { - return new Cursor(this, session, 1, condition); + String query = "select " + listOfFields + " from " + name + + " " + condition; + return new Cursor(this, session, 1, query); + } + + /** Select records from database table according to search condition + * including the specified (comma separated) extra tables into the + * SELECT clause to facilitate a join in determining the key. + * + * @param tables the (comma separated) names of extra tables to + * include in the SELECT clause. + * @param condition valid SQL condition expression started with WHERE. + */ + public final Cursor select(String tables, String condition) { + String query = "select " + qualifiedListOfFields + + " from " + name + "," + tables + " " + condition; + return new Cursor(this, session, 1, query); } /** Select records from database table according to search condition @@ -114,7 +128,9 @@ public class Table { * @param session user database session */ public final Cursor select(String condition, Session session) { - return new Cursor(this, session, 1, condition); + String query = "select " + listOfFields + " from " + name + + " " + condition; + return new Cursor(this, session, 1, query); } /** Select records from specified and derived database tables @@ -135,7 +151,6 @@ public class Table { public final Cursor selectAll(String condition, Session session) { return new Cursor(this, session, nDerived+1, condition); } - /** Select records from database table using obj object as * template for selection. All non-builtin fields of this object, @@ -519,6 +534,7 @@ public class Table { protected String name; protected String listOfFields; + protected String qualifiedListOfFields; protected String listOfAssignments; protected Class cls; protected Session session; @@ -566,6 +582,7 @@ public class Table { session = s; primaryKeys = keys; listOfFields = ""; + qualifiedListOfFields = ""; listOfAssignments = ""; connectionID = 0; Vector fieldsVector = new Vector(); @@ -760,9 +777,11 @@ public class Table { } if (listOfFields.length() != 0) { listOfFields += ","; + qualifiedListOfFields += ","; listOfAssignments += ","; } listOfFields += fullName; + qualifiedListOfFields += this.name + "." + fullName; listOfAssignments += fullName + "=?"; fd.inType = fd.outType = type; @@ -840,8 +859,10 @@ public class Table { { StringBuffer buf = new StringBuffer(); buildQueryList(buf, qbe, 0, nFields); - String condition = buf.toString(); - return condition.length() != 0 ? " where " + condition : ""; + if (buf.length() > 0) { + buf.insert(0, " where "); + } + return "select " + listOfFields + " from " + name + buf; } @@ -968,10 +989,4 @@ public class Table { } catch(IllegalAccessException ex) { throw new IllegalAccessError(); } return column; } - } - - - - -