return statements;
}
public void create(Row row) throws DriverException {
if (readOnly) {
throw new DriverException(I18N.msg("E_read_only_txn", "create"));
}
Column primaryKey = row.getTable().getPrimaryKey();
String statement = getStatements(row.getTable())
.insertStatement;
String nextIDStatement = getStatements(row.getTable())
.nextIDStatement;
Object useID = null;
try {
// If primary key uses a non-autoincremented sequence, get the ID
if (primaryKey != null && primaryKey.getSequence() != null && !primaryKey.isAutoIncremented()) {
logger.info(nextIDStatement);
PreparedStatement ps1 = currentConnection.prepareStatement(nextIDStatement);
ResultSet rs1 = ps1.executeQuery();
useID = null;
if (rs1.next()) {
useID = rs1.getObject(1);
}
rs1.close();
row.setValue(row.getTable().getPrimaryKey(), useID);
}
logger.info(statement);
logger.info(row.toString());
// Now do insert
PreparedStatement ps = currentConnection.prepareStatement(statement);
Set columns = row.getTable().getColumns();
int pos = 1;
for (Iterator i = columns.iterator(); i.hasNext(); ) {
Column c = (Column) i.next();
if ((c == primaryKey && primaryKey.isAutoIncremented())
|| c.isReadOnly()) {
continue;
}
Object value = row.getValue(c);
setObject(ps, pos++, value, c.getType());
}
ps.executeUpdate();
ps.close();
// If autoincremented, read ID now
if (primaryKey != null && primaryKey.isAutoIncremented()) {
logger.info(nextIDStatement);
PreparedStatement ps1 = currentConnection.prepareStatement(nextIDStatement);
ResultSet rs1 = ps1.executeQuery();
useID = null;
if (rs1.next()) {
useID = rs1.getObject(1);
}
rs1.close();
logger.info("using ID " + useID);
// TODO inform other objects about ID?
row.setValue(row.getTable().getPrimaryKey(), useID);
}
} catch (SQLException e) {
e.printStackTrace();
throw new DriverException(e);
}
}