String primaryKeyName,
Map columnMap)
throws ControllerException {
Connection conn = null;
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = null;
String columnName = null;
int i;
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false); // start transaction
// Create primary key in case primary key is null
if (columnMap.get(primaryKeyName) == null) {
int uniqueKey = nz.co.transparent.client.db.SQL.getUniqueKey(tableName, primaryKeyName);
columnMap.put(primaryKeyName, new Integer(uniqueKey));
}
sql = "insert into " + tableName;
String parm = null;
Set columnSet = columnMap.keySet();
Iterator iterator = columnSet.iterator();
List paramList = new ArrayList(columnSet.size());
i = 0;
int j = 0;
while (iterator.hasNext()) {
columnName = (String) iterator.next();
// CURRENT_TIMESTAMP must be set directly into SQL to force
// date created by server
// Alternatively these columns can be left out of the map
if (columnName.equals("date_created")) {
parm = "CURRENT_TIMESTAMP";
} else if (columnName.equals("date_updated")) {
parm = "CURRENT_TIMESTAMP";
} else {
parm = "?";
paramList.add(columnMap.get(columnName));
}
if (j++ == 0) {
sql += " set " + columnName + "=" + parm;
} else {
sql += " ," + columnName + "=" + parm;
}
}
try {
i = queryRunner.update(conn, sql, paramList.toArray());
conn.commit();
return i;
} catch (SQLException se) {
conn.rollback();
throw new ControllerException(se);