diff --git a/src/helma/objectmodel/db/DbMapping.java b/src/helma/objectmodel/db/DbMapping.java index 74a612a9..7f3035c4 100644 --- a/src/helma/objectmodel/db/DbMapping.java +++ b/src/helma/objectmodel/db/DbMapping.java @@ -903,20 +903,28 @@ public final class DbMapping implements Updatable { * Get a StringBuffer initialized to the first part of the select statement * for objects defined by this DbMapping * + * @param rel the Relation we use to select. Currently only used for optimizer hints. + * Is null if selecting by primary key. * @return the StringBuffer containing the first part of the select query * * @throws SQLException if the table meta data could not be retrieved * @throws ClassNotFoundException if the JDBC driver class was not found */ - public StringBuffer getSelect() throws SQLException, ClassNotFoundException { + public StringBuffer getSelect(Relation rel) throws SQLException, ClassNotFoundException { + // assign to local variable first so we are thread safe + // (selectString may be reset by other threads) String sel = selectString; - if (sel != null) { + if (rel == null && sel != null) { return new StringBuffer(sel); } StringBuffer s = new StringBuffer("SELECT "); + if (rel != null && rel.queryHints != null) { + s.append(rel.queryHints).append(" "); + } + String table = getTableName(); // all columns from the main table @@ -951,8 +959,11 @@ public final class DbMapping implements Updatable { joins[i].renderJoinConstraints(s); } - // cache rendered string for later calls. - selectString = s.toString(); + // cache rendered string for later calls, but only if it wasn't + // built for a particular Relation + if (rel == null) { + selectString = s.toString(); + } return s; } diff --git a/src/helma/objectmodel/db/NodeManager.java b/src/helma/objectmodel/db/NodeManager.java index a31537db..929a9588 100644 --- a/src/helma/objectmodel/db/NodeManager.java +++ b/src/helma/objectmodel/db/NodeManager.java @@ -994,9 +994,15 @@ public final class NodeManager { tables.append(',').append(rel.additionalTables); } - StringBuffer b = new StringBuffer("SELECT ").append(table).append('.') - .append(idfield).append(" FROM ") - .append(tables); + StringBuffer b = new StringBuffer("SELECT "); + + if (rel != null && rel.queryHints != null) { + b.append(rel.queryHints).append(" "); + } + + b.append(table).append('.') + .append(idfield).append(" FROM ") + .append(tables); if (home.getSubnodeRelation() != null) { // subnode relation was explicitly set @@ -1089,7 +1095,7 @@ public final class NodeManager { Statement stmt = con.createStatement(); DbColumn[] columns = dbm.getColumns(); Relation[] joins = dbm.getJoins(); - StringBuffer q = dbm.getSelect(); + StringBuffer q = dbm.getSelect(rel); if (rel.additionalTables != null) { q.append(',').append(rel.additionalTables); @@ -1171,7 +1177,7 @@ public final class NodeManager { Statement stmt = con.createStatement(); DbColumn[] columns = dbm.getColumns(); Relation[] joins = dbm.getJoins(); - StringBuffer q = dbm.getSelect(); + StringBuffer q = dbm.getSelect(rel); if (rel.additionalTables != null) { q.append(',').append(rel.additionalTables); @@ -1498,7 +1504,7 @@ public final class NodeManager { DbColumn[] columns = dbm.getColumns(); Relation[] joins = dbm.getJoins(); - StringBuffer q = dbm.getSelect().append("WHERE ") + StringBuffer q = dbm.getSelect(null).append("WHERE ") .append(dbm.getTableName()) .append(".") .append(idfield) @@ -1582,7 +1588,7 @@ public final class NodeManager { Connection con = dbm.getConnection(); DbColumn[] columns = dbm.getColumns(); Relation[] joins = dbm.getJoins(); - StringBuffer q = dbm.getSelect(); + StringBuffer q = dbm.getSelect(rel); if (rel.additionalTables != null) { q.append(',').append(rel.additionalTables); diff --git a/src/helma/objectmodel/db/Relation.java b/src/helma/objectmodel/db/Relation.java index f7452ac7..af93fc5e 100644 --- a/src/helma/objectmodel/db/Relation.java +++ b/src/helma/objectmodel/db/Relation.java @@ -93,6 +93,7 @@ public final class Relation { String groupbyPrototype; String filter; String additionalTables; + String queryHints; Vector filterFragments; Vector filterPropertyRefs; int maxSize = 0; @@ -114,6 +115,7 @@ public final class Relation { this.filterFragments = rel.filterFragments; this.filterPropertyRefs = rel.filterPropertyRefs; this.additionalTables = rel.additionalTables; + this.queryHints = rel.queryHints; this.maxSize = rel.maxSize; this.constraints = rel.constraints; this.accessName = rel.accessName; @@ -292,6 +294,9 @@ public final class Relation { additionalTables = null; } + // get query hints + queryHints = props.getProperty(propName + ".hints"); + // get max size of collection String max = props.getProperty(propName + ".maxSize");