Fetch database records in natural order
This commit is contained in:
parent
79255d217e
commit
b8cf9b2e4c
3 changed files with 45 additions and 36 deletions
|
@ -52,8 +52,8 @@ public final class DbMapping implements Updatable {
|
|||
HashMap prop2db;
|
||||
// Map of db columns to Relations objects
|
||||
HashMap db2prop;
|
||||
// prerendered list of columns to fetch from db
|
||||
String columns = null;
|
||||
// list of columns to fetch from db
|
||||
String[] columns = null;
|
||||
// pre-rendered select statement
|
||||
String select = null;
|
||||
|
||||
|
@ -139,8 +139,6 @@ public final class DbMapping implements Updatable {
|
|||
* for rewire to work, all other db mappings must have been initialized and registered.
|
||||
*/
|
||||
public synchronized void update () {
|
||||
// reset columns
|
||||
columns = select = null;
|
||||
// read in properties
|
||||
table = props.getProperty ("_table");
|
||||
idgen = props.getProperty ("_idgen");
|
||||
|
@ -184,9 +182,13 @@ public final class DbMapping implements Updatable {
|
|||
}
|
||||
|
||||
lastTypeChange = props.lastModified ();
|
||||
// set the cached schema & keydef to null so it's rebuilt the next time around
|
||||
// null the cached schema & keydef so it's rebuilt the next time around
|
||||
schema = null;
|
||||
keydef = null;
|
||||
// same with columns and select string
|
||||
columns = null;
|
||||
select = null;
|
||||
|
||||
|
||||
if (extendsProto != null) {
|
||||
parentMapping = app.getDbMapping (extendsProto);
|
||||
|
@ -574,12 +576,12 @@ public final class DbMapping implements Updatable {
|
|||
/**
|
||||
* Return a Village Schema object for this DbMapping.
|
||||
*/
|
||||
public synchronized String getColumns() throws ClassNotFoundException, SQLException {
|
||||
public synchronized String[] getColumns() throws ClassNotFoundException, SQLException {
|
||||
if (!isRelational ())
|
||||
throw new SQLException ("Can't get Schema for non-relational data mapping");
|
||||
if (source == null && parentMapping != null)
|
||||
return parentMapping.getColumns ();
|
||||
// Use local variable s to avoid synchronization (schema may be nulled elsewhere)
|
||||
// Use local variable cols to avoid synchronization (schema may be nulled elsewhere)
|
||||
if (columns == null) {
|
||||
// we do two things here: set the SQL type on the Relation mappings
|
||||
// and build a string of column names.
|
||||
|
@ -590,18 +592,16 @@ public final class DbMapping implements Updatable {
|
|||
throw new SQLException ("Error retrieving DB scheme for "+this);
|
||||
ResultSetMetaData meta = rs.getMetaData ();
|
||||
// ok, we have the meta data, now loop through mapping...
|
||||
// StringBuffer cbuffer = new StringBuffer (getIDField ());
|
||||
for (Iterator i=getDBPropertyIterator(); i.hasNext(); ) {
|
||||
Relation rel = (Relation) i.next ();
|
||||
if (rel.reftype != Relation.PRIMITIVE && rel.reftype != Relation.REFERENCE)
|
||||
int ncols = meta.getColumnCount ();
|
||||
columns = new String[ncols];
|
||||
for (int i=0; i<ncols; i++) {
|
||||
columns[i] = meta.getColumnName (i+1);
|
||||
Relation rel = columnNameToRelation (columns[i]);
|
||||
if (rel == null || (rel.reftype != Relation.PRIMITIVE &&
|
||||
rel.reftype != Relation.REFERENCE))
|
||||
continue;
|
||||
// cbuffer.append (",");
|
||||
// cbuffer.append (rel.getDbField());
|
||||
int idx = rs.findColumn (rel.getDbField());
|
||||
rel.setColumnType (meta.getColumnType (idx));
|
||||
rel.setColumnType (meta.getColumnType (i+1));
|
||||
}
|
||||
// columns = cbuffer.toString();
|
||||
columns = "*";
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
@ -610,9 +610,14 @@ public final class DbMapping implements Updatable {
|
|||
String sel = select;
|
||||
if (sel != null)
|
||||
return new StringBuffer (sel);
|
||||
StringBuffer s = new StringBuffer ("select ");
|
||||
s.append (getColumns ());
|
||||
s.append (" from ");
|
||||
StringBuffer s = new StringBuffer ("SELECT * FROM ");
|
||||
/* String[] cols = getColumns ();
|
||||
for (int i=0; i<cols.length; i++) {
|
||||
if (i>0)
|
||||
s.append (",");
|
||||
s.append (cols[i]);
|
||||
}
|
||||
s.append (" FROM "); */
|
||||
s.append (getTableName ());
|
||||
s.append (" ");
|
||||
// cache rendered string for later calls.
|
||||
|
|
|
@ -235,7 +235,8 @@ public final class Node implements INode, Serializable {
|
|||
/**
|
||||
* Constructor used for nodes being stored in a relational database table.
|
||||
*/
|
||||
public Node (DbMapping dbm, ResultSet rs, WrappedNodeManager nmgr) throws SQLException {
|
||||
public Node (DbMapping dbm, ResultSet rs, String[] columns, WrappedNodeManager nmgr)
|
||||
throws SQLException {
|
||||
|
||||
this.nmgr = nmgr;
|
||||
// see what prototype/DbMapping this object should use
|
||||
|
@ -268,12 +269,11 @@ public final class Node implements INode, Serializable {
|
|||
|
||||
created = lastmodified = System.currentTimeMillis ();
|
||||
|
||||
for (Iterator i=dbmap.getDBPropertyIterator(); i.hasNext(); ) {
|
||||
for (int i=0; i<columns.length; i++) {
|
||||
|
||||
Relation rel = (Relation) i.next ();
|
||||
// NOTE: this should never be the case, since we're just looping through
|
||||
// mappnigs with a local db column
|
||||
if (rel.reftype != Relation.PRIMITIVE && rel.reftype != Relation.REFERENCE)
|
||||
Relation rel = dbm.columnNameToRelation (columns[i]);
|
||||
if (rel == null || (rel.reftype != Relation.PRIMITIVE &&
|
||||
rel.reftype != Relation.REFERENCE))
|
||||
continue;
|
||||
|
||||
Property newprop = new Property (rel.propName, this);
|
||||
|
|
|
@ -750,6 +750,7 @@ public final class NodeManager {
|
|||
|
||||
Connection con = dbm.getConnection ();
|
||||
Statement stmt = con.createStatement ();
|
||||
String[] columns = dbm.getColumns ();
|
||||
StringBuffer q = dbm.getSelect ();
|
||||
try {
|
||||
if (home.getSubnodeRelation() != null) {
|
||||
|
@ -771,7 +772,7 @@ public final class NodeManager {
|
|||
|
||||
while (rs.next()) {
|
||||
// create new Nodes.
|
||||
Node node = new Node (rel.otherType, rs, safe);
|
||||
Node node = new Node (rel.otherType, rs, columns, safe);
|
||||
Key primKey = node.getKey ();
|
||||
retval.add (new NodeHandle (primKey));
|
||||
// do we need to synchronize on primKey here?
|
||||
|
@ -807,13 +808,14 @@ public final class NodeManager {
|
|||
if (missing > 0) {
|
||||
Connection con = dbm.getConnection ();
|
||||
Statement stmt = con.createStatement ();
|
||||
String[] columns = dbm.getColumns ();
|
||||
StringBuffer q = dbm.getSelect ();
|
||||
try {
|
||||
String idfield = rel.groupby != null ? rel.groupby : dbm.getIDField ();
|
||||
boolean needsQuotes = dbm.needsQuotes (idfield);
|
||||
q.append (" where ");
|
||||
q.append ("WHERE ");
|
||||
q.append (idfield);
|
||||
q.append (" in (");
|
||||
q.append (" IN (");
|
||||
boolean first = true;
|
||||
for (int i=0; i<keys.length; i++) {
|
||||
if (keys[i] != null) {
|
||||
|
@ -855,7 +857,7 @@ public final class NodeManager {
|
|||
|
||||
while (rs.next ()) {
|
||||
// create new Nodes.
|
||||
Node node = new Node (dbm, rs, safe);
|
||||
Node node = new Node (dbm, rs, columns, safe);
|
||||
Key primKey = node.getKey ();
|
||||
|
||||
// for grouped nodes, collect subnode lists for the intermediary
|
||||
|
@ -1035,8 +1037,9 @@ public final class NodeManager {
|
|||
Connection con = dbm.getConnection ();
|
||||
stmt = con.createStatement ();
|
||||
|
||||
String[] columns = dbm.getColumns ();
|
||||
StringBuffer q = dbm.getSelect ();
|
||||
q.append (" where ");
|
||||
q.append ("WHERE ");
|
||||
q.append (idfield);
|
||||
q.append (" = ");
|
||||
q.append (kstr);
|
||||
|
@ -1048,7 +1051,7 @@ public final class NodeManager {
|
|||
|
||||
if (!rs.next ())
|
||||
return null;
|
||||
node = new Node (dbm, rs, safe);
|
||||
node = new Node (dbm, rs, columns, safe);
|
||||
if (rs.next ())
|
||||
throw new RuntimeException ("More than one value returned by query.");
|
||||
|
||||
|
@ -1095,10 +1098,11 @@ public final class NodeManager {
|
|||
DbMapping dbm = rel.otherType;
|
||||
|
||||
Connection con = dbm.getConnection ();
|
||||
String[] columns = dbm.getColumns ();
|
||||
StringBuffer q = dbm.getSelect ();
|
||||
if (home.getSubnodeRelation () != null) {
|
||||
// combine our key with the constraints in the manually set subnode relation
|
||||
q.append (" WHERE ");
|
||||
q.append ("WHERE ");
|
||||
q.append (rel.accessor);
|
||||
q.append (" = '");
|
||||
q.append (escape(kstr));
|
||||
|
@ -1106,7 +1110,7 @@ public final class NodeManager {
|
|||
q.append (" AND ");
|
||||
q.append (home.getSubnodeRelation().trim().substring(5));
|
||||
} else {
|
||||
q.append (rel.buildQuery (home, home.getNonVirtualParent (), kstr, " WHERE ", false));
|
||||
q.append (rel.buildQuery (home, home.getNonVirtualParent (), kstr, "WHERE ", false));
|
||||
}
|
||||
if (logSql)
|
||||
app.logEvent ("### getNodeByRelation: "+q.toString());
|
||||
|
@ -1116,7 +1120,7 @@ public final class NodeManager {
|
|||
|
||||
if (!rs.next ())
|
||||
return null;
|
||||
node = new Node (rel.otherType, rs, safe);
|
||||
node = new Node (rel.otherType, rs, columns, safe);
|
||||
if (rs.next ())
|
||||
throw new RuntimeException ("More than one value returned by query.");
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue