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

@ -63,7 +63,7 @@ public final class DbMapping implements Updatable {
private HashMap prop2db;
// Map of db columns to Relations objects.
// Case insensitive, keys are stored in upper case so
// Case insensitive, keys are stored in upper case so
// lookups must do a toUpperCase().
private HashMap db2prop;
@ -103,7 +103,7 @@ public final class DbMapping implements Updatable {
private long lastID;
// timestamp of last modification of the mapping (type.properties)
// init value is -1 so we know we have to run update once even if
// init value is -1 so we know we have to run update once even if
// the underlying properties file is non-existent
long lastTypeChange = -1;
@ -1030,25 +1030,45 @@ public final class DbMapping implements Updatable {
*
* @return ...
*/
public StringBuffer getInsert() {
public String getInsert() throws ClassNotFoundException, SQLException {
String ins = insertString;
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());
s.append(" ( ");
s.append(getIDField());
StringBuffer b2 = new StringBuffer(" ) VALUES ( ?");
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.
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");
}
String insertString = dbm.getInsert();
PreparedStatement stmt = con.prepareStatement(insertString);
// app.logEvent ("inserting relational node: "+node.getID ());
DbColumn[] columns = dbm.getColumns();
StringBuffer b1 = dbm.getInsert();
StringBuffer b2 = new StringBuffer(" ) VALUES ( ?");
String nameField = dbm.getNameField();
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) {
app.logEvent("### insertNode: " + b1.toString());
app.logEvent("### insertNode: " + insertString);
}
try {