Fetch database records in natural order

This commit is contained in:
hns 2002-11-20 19:31:31 +00:00
parent 79255d217e
commit b8cf9b2e4c
3 changed files with 45 additions and 36 deletions

View file

@ -52,8 +52,8 @@ public final class DbMapping implements Updatable {
HashMap prop2db; HashMap prop2db;
// Map of db columns to Relations objects // Map of db columns to Relations objects
HashMap db2prop; HashMap db2prop;
// prerendered list of columns to fetch from db // list of columns to fetch from db
String columns = null; String[] columns = null;
// pre-rendered select statement // pre-rendered select statement
String select = null; 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. * for rewire to work, all other db mappings must have been initialized and registered.
*/ */
public synchronized void update () { public synchronized void update () {
// reset columns
columns = select = null;
// read in properties // read in properties
table = props.getProperty ("_table"); table = props.getProperty ("_table");
idgen = props.getProperty ("_idgen"); idgen = props.getProperty ("_idgen");
@ -184,9 +182,13 @@ public final class DbMapping implements Updatable {
} }
lastTypeChange = props.lastModified (); 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; schema = null;
keydef = null; keydef = null;
// same with columns and select string
columns = null;
select = null;
if (extendsProto != null) { if (extendsProto != null) {
parentMapping = app.getDbMapping (extendsProto); parentMapping = app.getDbMapping (extendsProto);
@ -574,12 +576,12 @@ public final class DbMapping implements Updatable {
/** /**
* Return a Village Schema object for this DbMapping. * Return a Village Schema object for this DbMapping.
*/ */
public synchronized String getColumns() throws ClassNotFoundException, SQLException { public synchronized String[] getColumns() throws ClassNotFoundException, SQLException {
if (!isRelational ()) if (!isRelational ())
throw new SQLException ("Can't get Schema for non-relational data mapping"); throw new SQLException ("Can't get Schema for non-relational data mapping");
if (source == null && parentMapping != null) if (source == null && parentMapping != null)
return parentMapping.getColumns (); 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) { if (columns == null) {
// we do two things here: set the SQL type on the Relation mappings // we do two things here: set the SQL type on the Relation mappings
// and build a string of column names. // 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); throw new SQLException ("Error retrieving DB scheme for "+this);
ResultSetMetaData meta = rs.getMetaData (); ResultSetMetaData meta = rs.getMetaData ();
// ok, we have the meta data, now loop through mapping... // ok, we have the meta data, now loop through mapping...
// StringBuffer cbuffer = new StringBuffer (getIDField ()); int ncols = meta.getColumnCount ();
for (Iterator i=getDBPropertyIterator(); i.hasNext(); ) { columns = new String[ncols];
Relation rel = (Relation) i.next (); for (int i=0; i<ncols; i++) {
if (rel.reftype != Relation.PRIMITIVE && rel.reftype != Relation.REFERENCE) columns[i] = meta.getColumnName (i+1);
Relation rel = columnNameToRelation (columns[i]);
if (rel == null || (rel.reftype != Relation.PRIMITIVE &&
rel.reftype != Relation.REFERENCE))
continue; continue;
// cbuffer.append (","); rel.setColumnType (meta.getColumnType (i+1));
// cbuffer.append (rel.getDbField());
int idx = rs.findColumn (rel.getDbField());
rel.setColumnType (meta.getColumnType (idx));
} }
// columns = cbuffer.toString();
columns = "*";
} }
return columns; return columns;
} }
@ -610,9 +610,14 @@ public final class DbMapping implements Updatable {
String sel = select; String sel = select;
if (sel != null) if (sel != null)
return new StringBuffer (sel); return new StringBuffer (sel);
StringBuffer s = new StringBuffer ("select "); StringBuffer s = new StringBuffer ("SELECT * FROM ");
s.append (getColumns ()); /* String[] cols = getColumns ();
s.append (" from "); 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 (getTableName ());
s.append (" "); s.append (" ");
// cache rendered string for later calls. // cache rendered string for later calls.

View file

@ -235,7 +235,8 @@ public final class Node implements INode, Serializable {
/** /**
* Constructor used for nodes being stored in a relational database table. * 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; this.nmgr = nmgr;
// see what prototype/DbMapping this object should use // see what prototype/DbMapping this object should use
@ -268,12 +269,11 @@ public final class Node implements INode, Serializable {
created = lastmodified = System.currentTimeMillis (); created = lastmodified = System.currentTimeMillis ();
for (Iterator i=dbmap.getDBPropertyIterator(); i.hasNext(); ) { for (int i=0; i<columns.length; i++) {
Relation rel = (Relation) i.next (); Relation rel = dbm.columnNameToRelation (columns[i]);
// NOTE: this should never be the case, since we're just looping through if (rel == null || (rel.reftype != Relation.PRIMITIVE &&
// mappnigs with a local db column rel.reftype != Relation.REFERENCE))
if (rel.reftype != Relation.PRIMITIVE && rel.reftype != Relation.REFERENCE)
continue; continue;
Property newprop = new Property (rel.propName, this); Property newprop = new Property (rel.propName, this);

View file

@ -750,6 +750,7 @@ public final class NodeManager {
Connection con = dbm.getConnection (); Connection con = dbm.getConnection ();
Statement stmt = con.createStatement (); Statement stmt = con.createStatement ();
String[] columns = dbm.getColumns ();
StringBuffer q = dbm.getSelect (); StringBuffer q = dbm.getSelect ();
try { try {
if (home.getSubnodeRelation() != null) { if (home.getSubnodeRelation() != null) {
@ -771,7 +772,7 @@ public final class NodeManager {
while (rs.next()) { while (rs.next()) {
// create new Nodes. // create new Nodes.
Node node = new Node (rel.otherType, rs, safe); Node node = new Node (rel.otherType, rs, columns, safe);
Key primKey = node.getKey (); Key primKey = node.getKey ();
retval.add (new NodeHandle (primKey)); retval.add (new NodeHandle (primKey));
// do we need to synchronize on primKey here? // do we need to synchronize on primKey here?
@ -807,13 +808,14 @@ public final class NodeManager {
if (missing > 0) { if (missing > 0) {
Connection con = dbm.getConnection (); Connection con = dbm.getConnection ();
Statement stmt = con.createStatement (); Statement stmt = con.createStatement ();
String[] columns = dbm.getColumns ();
StringBuffer q = dbm.getSelect (); StringBuffer q = dbm.getSelect ();
try { try {
String idfield = rel.groupby != null ? rel.groupby : dbm.getIDField (); String idfield = rel.groupby != null ? rel.groupby : dbm.getIDField ();
boolean needsQuotes = dbm.needsQuotes (idfield); boolean needsQuotes = dbm.needsQuotes (idfield);
q.append (" where "); q.append ("WHERE ");
q.append (idfield); q.append (idfield);
q.append (" in ("); q.append (" IN (");
boolean first = true; boolean first = true;
for (int i=0; i<keys.length; i++) { for (int i=0; i<keys.length; i++) {
if (keys[i] != null) { if (keys[i] != null) {
@ -855,7 +857,7 @@ public final class NodeManager {
while (rs.next ()) { while (rs.next ()) {
// create new Nodes. // create new Nodes.
Node node = new Node (dbm, rs, safe); Node node = new Node (dbm, rs, columns, safe);
Key primKey = node.getKey (); Key primKey = node.getKey ();
// for grouped nodes, collect subnode lists for the intermediary // for grouped nodes, collect subnode lists for the intermediary
@ -1035,8 +1037,9 @@ public final class NodeManager {
Connection con = dbm.getConnection (); Connection con = dbm.getConnection ();
stmt = con.createStatement (); stmt = con.createStatement ();
String[] columns = dbm.getColumns ();
StringBuffer q = dbm.getSelect (); StringBuffer q = dbm.getSelect ();
q.append (" where "); q.append ("WHERE ");
q.append (idfield); q.append (idfield);
q.append (" = "); q.append (" = ");
q.append (kstr); q.append (kstr);
@ -1048,7 +1051,7 @@ public final class NodeManager {
if (!rs.next ()) if (!rs.next ())
return null; return null;
node = new Node (dbm, rs, safe); node = new Node (dbm, rs, columns, safe);
if (rs.next ()) if (rs.next ())
throw new RuntimeException ("More than one value returned by query."); throw new RuntimeException ("More than one value returned by query.");
@ -1095,6 +1098,7 @@ public final class NodeManager {
DbMapping dbm = rel.otherType; DbMapping dbm = rel.otherType;
Connection con = dbm.getConnection (); Connection con = dbm.getConnection ();
String[] columns = dbm.getColumns ();
StringBuffer q = dbm.getSelect (); StringBuffer q = dbm.getSelect ();
if (home.getSubnodeRelation () != null) { if (home.getSubnodeRelation () != null) {
// combine our key with the constraints in the manually set subnode relation // combine our key with the constraints in the manually set subnode relation
@ -1116,7 +1120,7 @@ public final class NodeManager {
if (!rs.next ()) if (!rs.next ())
return null; return null;
node = new Node (rel.otherType, rs, safe); node = new Node (rel.otherType, rs, columns, safe);
if (rs.next ()) if (rs.next ())
throw new RuntimeException ("More than one value returned by query."); throw new RuntimeException ("More than one value returned by query.");