Make DbMapping.getInsert() return and cache the full insert prepared statement.

This commit is contained in:
hns 2004-06-09 15:46:22 +00:00
parent b201bf8f35
commit cf6d9b8330
2 changed files with 34 additions and 31 deletions

View file

@ -1030,25 +1030,45 @@ public final class DbMapping implements Updatable {
* *
* @return ... * @return ...
*/ */
public StringBuffer getInsert() { public String getInsert() throws ClassNotFoundException, SQLException {
String ins = insertString; String ins = insertString;
if (ins != null) { if (ins != null) {
return new StringBuffer(ins); return ins;
} }
StringBuffer s = new StringBuffer("INSERT INTO "); StringBuffer b1 = new StringBuffer("INSERT INTO ");
b1.append(getTableName());
b1.append(" ( ");
b1.append(getIDField());
s.append(getTableName()); StringBuffer b2 = new StringBuffer(" ) VALUES ( ?");
s.append(" ( ");
s.append(getIDField()); DbColumn[] cols = getColumns();
for (int i = 0; i < cols.length; i++) {
Relation rel = cols[i].getRelation();
String name = cols[i].getName();
if (((rel != null) && (rel.isPrimitive() ||
rel.isReference())) ||
name.equalsIgnoreCase(getNameField()) ||
name.equalsIgnoreCase(getPrototypeField())) {
b1.append(", " + cols[i].getName());
b2.append(", ?");
}
}
b1.append(b2.toString());
b1.append(" )");
// cache rendered string for later calls. // cache rendered string for later calls.
insertString = s.toString(); ins = insertString = b1.toString();
return s; return ins;
} }
/** /**
* *
* *

View file

@ -508,34 +508,17 @@ public final class NodeManager {
throw new NullPointerException("Error inserting relational node: Connection is null"); throw new NullPointerException("Error inserting relational node: Connection is null");
} }
String insertString = dbm.getInsert();
PreparedStatement stmt = con.prepareStatement(insertString);
// app.logEvent ("inserting relational node: "+node.getID ()); // app.logEvent ("inserting relational node: "+node.getID ());
DbColumn[] columns = dbm.getColumns(); DbColumn[] columns = dbm.getColumns();
StringBuffer b1 = dbm.getInsert();
StringBuffer b2 = new StringBuffer(" ) VALUES ( ?");
String nameField = dbm.getNameField(); String nameField = dbm.getNameField();
String prototypeField = dbm.getPrototypeField(); String prototypeField = dbm.getPrototypeField();
for (int i = 0; i < columns.length; i++) {
Relation rel = columns[i].getRelation();
String name = columns[i].getName();
if (((rel != null) && (rel.isPrimitive() || rel.isReference())) ||
name.equalsIgnoreCase(nameField) || name.equalsIgnoreCase(prototypeField)) {
b1.append(", " + columns[i].getName());
b2.append(", ?");
}
}
b1.append(b2.toString());
b1.append(" )");
// Connection con = dbm.getConnection();
PreparedStatement stmt = con.prepareStatement(b1.toString());
if (logSql) { if (logSql) {
app.logEvent("### insertNode: " + b1.toString()); app.logEvent("### insertNode: " + insertString);
} }
try { try {