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
This commit is contained in:
@@ -44,14 +44,13 @@ public class Cursor {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (qbeObject != null) {
|
if (qbeObject != null) {
|
||||||
String sql = "select " + table.listOfFields
|
|
||||||
+ " from " + table.name + " " + query;
|
|
||||||
PreparedStatement qbeStmt;
|
PreparedStatement qbeStmt;
|
||||||
synchronized(session.preparedStmtHash) {
|
synchronized(session.preparedStmtHash) {
|
||||||
Object s = session.preparedStmtHash.get(sql);
|
Object s = session.preparedStmtHash.get(query);
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
qbeStmt=session.connection.prepareStatement(sql);
|
qbeStmt=
|
||||||
session.preparedStmtHash.put(sql, qbeStmt);
|
session.connection.prepareStatement(query);
|
||||||
|
session.preparedStmtHash.put(query, qbeStmt);
|
||||||
} else {
|
} else {
|
||||||
qbeStmt = (PreparedStatement)s;
|
qbeStmt = (PreparedStatement)s;
|
||||||
}
|
}
|
||||||
@@ -65,9 +64,7 @@ public class Cursor {
|
|||||||
if (stmt == null) {
|
if (stmt == null) {
|
||||||
stmt = session.connection.createStatement();
|
stmt = session.connection.createStatement();
|
||||||
}
|
}
|
||||||
String sql = "select " + table.listOfFields
|
result = stmt.executeQuery(query);
|
||||||
+ " from " + table.name + " " + query;
|
|
||||||
result = stmt.executeQuery(sql);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (result.next()) {
|
if (result.next()) {
|
||||||
|
|||||||
@@ -88,7 +88,6 @@ public class Table {
|
|||||||
s, null);
|
s, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Constructor of table with "key" and "session" parameters inherited
|
/** Constructor of table with "key" and "session" parameters inherited
|
||||||
* from base table.
|
* from base table.
|
||||||
*/
|
*/
|
||||||
@@ -97,14 +96,29 @@ public class Table {
|
|||||||
null, null);
|
null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Select records from database table according to search condition
|
/** Select records from database table according to search condition
|
||||||
*
|
*
|
||||||
* @param condition valid SQL condition expression started with WHERE
|
* @param condition valid SQL condition expression started with WHERE
|
||||||
* or empty string if all records should be fetched.
|
* or empty string if all records should be fetched.
|
||||||
*/
|
*/
|
||||||
public final Cursor select(String condition) {
|
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
|
/** Select records from database table according to search condition
|
||||||
@@ -114,7 +128,9 @@ public class Table {
|
|||||||
* @param session user database session
|
* @param session user database session
|
||||||
*/
|
*/
|
||||||
public final Cursor select(String condition, Session 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
|
/** Select records from specified and derived database tables
|
||||||
@@ -136,7 +152,6 @@ public class Table {
|
|||||||
return new Cursor(this, session, nDerived+1, condition);
|
return new Cursor(this, session, nDerived+1, condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Select records from database table using <I>obj</I> object as
|
/** Select records from database table using <I>obj</I> object as
|
||||||
* template for selection. All non-builtin fields of this object,
|
* template for selection. All non-builtin fields of this object,
|
||||||
* which are not null, are compared with correspondent table values.
|
* which are not null, are compared with correspondent table values.
|
||||||
@@ -519,6 +534,7 @@ public class Table {
|
|||||||
|
|
||||||
protected String name;
|
protected String name;
|
||||||
protected String listOfFields;
|
protected String listOfFields;
|
||||||
|
protected String qualifiedListOfFields;
|
||||||
protected String listOfAssignments;
|
protected String listOfAssignments;
|
||||||
protected Class cls;
|
protected Class cls;
|
||||||
protected Session session;
|
protected Session session;
|
||||||
@@ -566,6 +582,7 @@ public class Table {
|
|||||||
session = s;
|
session = s;
|
||||||
primaryKeys = keys;
|
primaryKeys = keys;
|
||||||
listOfFields = "";
|
listOfFields = "";
|
||||||
|
qualifiedListOfFields = "";
|
||||||
listOfAssignments = "";
|
listOfAssignments = "";
|
||||||
connectionID = 0;
|
connectionID = 0;
|
||||||
Vector fieldsVector = new Vector();
|
Vector fieldsVector = new Vector();
|
||||||
@@ -760,9 +777,11 @@ public class Table {
|
|||||||
}
|
}
|
||||||
if (listOfFields.length() != 0) {
|
if (listOfFields.length() != 0) {
|
||||||
listOfFields += ",";
|
listOfFields += ",";
|
||||||
|
qualifiedListOfFields += ",";
|
||||||
listOfAssignments += ",";
|
listOfAssignments += ",";
|
||||||
}
|
}
|
||||||
listOfFields += fullName;
|
listOfFields += fullName;
|
||||||
|
qualifiedListOfFields += this.name + "." + fullName;
|
||||||
listOfAssignments += fullName + "=?";
|
listOfAssignments += fullName + "=?";
|
||||||
|
|
||||||
fd.inType = fd.outType = type;
|
fd.inType = fd.outType = type;
|
||||||
@@ -840,8 +859,10 @@ public class Table {
|
|||||||
{
|
{
|
||||||
StringBuffer buf = new StringBuffer();
|
StringBuffer buf = new StringBuffer();
|
||||||
buildQueryList(buf, qbe, 0, nFields);
|
buildQueryList(buf, qbe, 0, nFields);
|
||||||
String condition = buf.toString();
|
if (buf.length() > 0) {
|
||||||
return condition.length() != 0 ? " where " + condition : "";
|
buf.insert(0, " where ");
|
||||||
|
}
|
||||||
|
return "select " + listOfFields + " from " + name + buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -968,10 +989,4 @@ public class Table {
|
|||||||
} catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
|
} catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
|
||||||
return column;
|
return column;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user