From b8cf9b2e4cdfe65cf007893c2a94758e03e6864a Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 20 Nov 2002 19:31:31 +0000 Subject: [PATCH] Fetch database records in natural order --- src/helma/objectmodel/db/DbMapping.java | 45 +++++++++++++---------- src/helma/objectmodel/db/Node.java | 12 +++--- src/helma/objectmodel/db/NodeManager.java | 24 +++++++----- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/helma/objectmodel/db/DbMapping.java b/src/helma/objectmodel/db/DbMapping.java index 0369f39d..f89347eb 100644 --- a/src/helma/objectmodel/db/DbMapping.java +++ b/src/helma/objectmodel/db/DbMapping.java @@ -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; i0) + s.append (","); + s.append (cols[i]); + } + s.append (" FROM "); */ s.append (getTableName ()); s.append (" "); // cache rendered string for later calls. diff --git a/src/helma/objectmodel/db/Node.java b/src/helma/objectmodel/db/Node.java index 4feaf3d2..dba3415d 100644 --- a/src/helma/objectmodel/db/Node.java +++ b/src/helma/objectmodel/db/Node.java @@ -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 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