* Patch from Juerg Lehni: try to autodetect Joins in additionalTables option.
* Some code cleanup: - Don't do string concatenation in arguments to StringBuffer.append() - Remove redundant if clause - Remove throws statement for unthrown exceptions - Remove unneeded variable initialzation
This commit is contained in:
parent
961f755c4f
commit
0f3d15cee5
3 changed files with 50 additions and 62 deletions
|
@ -945,11 +945,8 @@ public final class DbMapping {
|
||||||
* @param rel the Relation we use to select. Currently only used for optimizer hints.
|
* @param rel the Relation we use to select. Currently only used for optimizer hints.
|
||||||
* Is null if selecting by primary key.
|
* Is null if selecting by primary key.
|
||||||
* @return the StringBuffer containing the first part of the select query
|
* @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(Relation rel) throws SQLException, ClassNotFoundException {
|
public StringBuffer getSelect(Relation rel) {
|
||||||
// assign to local variable first so we are thread safe
|
// assign to local variable first so we are thread safe
|
||||||
// (selectString may be reset by other threads)
|
// (selectString may be reset by other threads)
|
||||||
String sel = selectString;
|
String sel = selectString;
|
||||||
|
@ -985,8 +982,8 @@ public final class DbMapping {
|
||||||
|
|
||||||
s.append(table);
|
s.append(table);
|
||||||
|
|
||||||
if (rel != null && rel.additionalTables != null) {
|
if (rel != null) {
|
||||||
s.append(',').append(rel.additionalTables);
|
rel.appendAdditionalTables(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
s.append(" ");
|
s.append(" ");
|
||||||
|
@ -1053,7 +1050,7 @@ public final class DbMapping {
|
||||||
rel.isReference())) ||
|
rel.isReference())) ||
|
||||||
name.equalsIgnoreCase(getNameField()) ||
|
name.equalsIgnoreCase(getNameField()) ||
|
||||||
name.equalsIgnoreCase(getPrototypeField())) {
|
name.equalsIgnoreCase(getPrototypeField())) {
|
||||||
b1.append(", " + cols[i].getName());
|
b1.append(", ").append(cols[i].getName());
|
||||||
b2.append(", ?");
|
b2.append(", ?");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -453,7 +453,7 @@ public final class NodeManager {
|
||||||
|
|
||||||
if (dbm == null) {
|
if (dbm == null) {
|
||||||
throw new IllegalArgumentException("DbMapping can't be null in exportNode");
|
throw new IllegalArgumentException("DbMapping can't be null in exportNode");
|
||||||
} else if ((dbm == null) || !dbm.isRelational()) {
|
} else if (!dbm.isRelational()) {
|
||||||
throw new IllegalArgumentException("Can't export into non-relational database");
|
throw new IllegalArgumentException("Can't export into non-relational database");
|
||||||
} else {
|
} else {
|
||||||
insertRelationalNode(node, dbm, dbm.getConnection());
|
insertRelationalNode(node, dbm, dbm.getConnection());
|
||||||
|
@ -901,7 +901,7 @@ public final class NodeManager {
|
||||||
Statement stmt = null;
|
Statement stmt = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String q = null;
|
String q;
|
||||||
|
|
||||||
StringBuffer b = new StringBuffer("SELECT ");
|
StringBuffer b = new StringBuffer("SELECT ");
|
||||||
|
|
||||||
|
@ -913,9 +913,7 @@ public final class NodeManager {
|
||||||
.append(idfield).append(" FROM ")
|
.append(idfield).append(" FROM ")
|
||||||
.append(table);
|
.append(table);
|
||||||
|
|
||||||
if (rel.additionalTables != null) {
|
rel.appendAdditionalTables(b);
|
||||||
b.append(',').append(rel.additionalTables);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (home.getSubnodeRelation() != null) {
|
if (home.getSubnodeRelation() != null) {
|
||||||
// subnode relation was explicitly set
|
// subnode relation was explicitly set
|
||||||
|
@ -1282,9 +1280,7 @@ public final class NodeManager {
|
||||||
String q = null;
|
String q = null;
|
||||||
StringBuffer tables = new StringBuffer(table);
|
StringBuffer tables = new StringBuffer(table);
|
||||||
|
|
||||||
if (rel.additionalTables != null) {
|
rel.appendAdditionalTables(tables);
|
||||||
tables.append(',').append(rel.additionalTables);
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: we explicitly convert tables StringBuffer to a String
|
// NOTE: we explicitly convert tables StringBuffer to a String
|
||||||
// before appending to be compatible with JDK 1.3
|
// before appending to be compatible with JDK 1.3
|
||||||
|
@ -1360,9 +1356,7 @@ public final class NodeManager {
|
||||||
|
|
||||||
StringBuffer tables = new StringBuffer(table);
|
StringBuffer tables = new StringBuffer(table);
|
||||||
|
|
||||||
if (rel.additionalTables != null) {
|
rel.appendAdditionalTables(tables);
|
||||||
tables.append(',').append(rel.additionalTables);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// NOTE: we explicitly convert tables StringBuffer to a String
|
// NOTE: we explicitly convert tables StringBuffer to a String
|
||||||
|
|
|
@ -92,7 +92,8 @@ public final class Relation {
|
||||||
String prototype;
|
String prototype;
|
||||||
String groupbyPrototype;
|
String groupbyPrototype;
|
||||||
String filter;
|
String filter;
|
||||||
String additionalTables;
|
private String additionalTables;
|
||||||
|
private boolean additionalTablesJoined = false;
|
||||||
String queryHints;
|
String queryHints;
|
||||||
Vector filterFragments;
|
Vector filterFragments;
|
||||||
Vector filterPropertyRefs;
|
Vector filterPropertyRefs;
|
||||||
|
@ -115,6 +116,7 @@ public final class Relation {
|
||||||
this.filterFragments = rel.filterFragments;
|
this.filterFragments = rel.filterFragments;
|
||||||
this.filterPropertyRefs = rel.filterPropertyRefs;
|
this.filterPropertyRefs = rel.filterPropertyRefs;
|
||||||
this.additionalTables = rel.additionalTables;
|
this.additionalTables = rel.additionalTables;
|
||||||
|
this.additionalTablesJoined = rel.additionalTablesJoined;
|
||||||
this.queryHints = rel.queryHints;
|
this.queryHints = rel.queryHints;
|
||||||
this.maxSize = rel.maxSize;
|
this.maxSize = rel.maxSize;
|
||||||
this.constraints = rel.constraints;
|
this.constraints = rel.constraints;
|
||||||
|
@ -290,8 +292,16 @@ public final class Relation {
|
||||||
// get additional tables
|
// get additional tables
|
||||||
additionalTables = props.getProperty(propName + ".filter.additionalTables");
|
additionalTables = props.getProperty(propName + ".filter.additionalTables");
|
||||||
|
|
||||||
if (additionalTables != null && additionalTables.trim().length() == 0) {
|
if (additionalTables != null) {
|
||||||
|
if (additionalTables.trim().length() == 0) {
|
||||||
additionalTables = null;
|
additionalTables = null;
|
||||||
|
} else {
|
||||||
|
String ucTables = additionalTables.toUpperCase();
|
||||||
|
// see wether the JOIN syntax is used. look for " join " with whitespaces on both sides
|
||||||
|
// and for "join " at the beginning:
|
||||||
|
additionalTablesJoined = (ucTables.indexOf(" JOIN ") != -1 ||
|
||||||
|
ucTables.startsWith("STRAIGHT_JOIN ") || ucTables.startsWith("JOIN "));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get query hints
|
// get query hints
|
||||||
|
@ -787,22 +797,29 @@ public final class Relation {
|
||||||
|
|
||||||
// add group and order clauses
|
// add group and order clauses
|
||||||
if (groupby != null) {
|
if (groupby != null) {
|
||||||
q.append(" GROUP BY " + groupby);
|
q.append(" GROUP BY ").append(groupby);
|
||||||
|
|
||||||
if (useOrder && (groupbyOrder != null)) {
|
if (useOrder && (groupbyOrder != null)) {
|
||||||
q.append(" ORDER BY " + groupbyOrder);
|
q.append(" ORDER BY ").append(groupbyOrder);
|
||||||
}
|
}
|
||||||
} else if (useOrder && (order != null)) {
|
} else if (useOrder && (order != null)) {
|
||||||
q.append(" ORDER BY " + order);
|
q.append(" ORDER BY ").append(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
return q.toString();
|
return q.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void appendAdditionalTables(StringBuffer q) {
|
||||||
|
if (additionalTables != null) {
|
||||||
|
q.append(additionalTablesJoined ? ' ' : ',');
|
||||||
|
q.append(additionalTables);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the filter.
|
* Build the filter.
|
||||||
*/
|
*/
|
||||||
protected void appendFilter(StringBuffer q, INode nonvirtual, String prefix) throws SQLException {
|
protected void appendFilter(StringBuffer q, INode nonvirtual, String prefix) {
|
||||||
q.append(prefix);
|
q.append(prefix);
|
||||||
q.append('(');
|
q.append('(');
|
||||||
if (filterFragments == null) {
|
if (filterFragments == null) {
|
||||||
|
@ -813,26 +830,6 @@ public final class Relation {
|
||||||
while (i.hasMoreElements()) {
|
while (i.hasMoreElements()) {
|
||||||
String fragment = (String) i.nextElement();
|
String fragment = (String) i.nextElement();
|
||||||
if (fragment == null) {
|
if (fragment == null) {
|
||||||
/*
|
|
||||||
// begin property version
|
|
||||||
String propertyName = (String) j.nextElement();
|
|
||||||
Object value = null;
|
|
||||||
if (propertyName != null) {
|
|
||||||
if (propertyName.equals("__id__") || propertyName.equals("_id")) {
|
|
||||||
value = nonvirtual.getID();
|
|
||||||
} else if (propertyName.equals("__name__")) {
|
|
||||||
value = nonvirtual.getName();
|
|
||||||
} else if (propertyName.equals("__prototype__") || propertyName.equals("_prototype")) {
|
|
||||||
value = nonvirtual.getPrototype();
|
|
||||||
} else {
|
|
||||||
IProperty property = nonvirtual.get(propertyName);
|
|
||||||
if (property != null) {
|
|
||||||
value = property.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// end property version
|
|
||||||
*/
|
|
||||||
// begin column version
|
// begin column version
|
||||||
String columnName = (String) j.nextElement();
|
String columnName = (String) j.nextElement();
|
||||||
Object value = null;
|
Object value = null;
|
||||||
|
|
Loading…
Add table
Reference in a new issue