String dbName = criteria.getDbName();
Database database = Torque.getDatabase(dbName);
DatabaseMap dbMap = database.getDatabaseMap();
TableMap tableMap = dbMap.getTable(table);
Object keyInfo = tableMap.getPrimaryKeyMethodInfo();
IdGenerator keyGen
= database.getIdGenerator(tableMap.getPrimaryKeyMethod());
ColumnMap pk = getPrimaryKey(criteria);
// If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id
// before the insert.
if (keyGen != null && keyGen.isPriorToInsert())
{
// pk will be null if there is no primary key defined for the table
// we're inserting into.
if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
{
id = getId(pk, keyGen, con, keyInfo);
criteria.add(pk.getFullyQualifiedName(), id);
}
}
// Use Village to perform the insert.
TableDataSet tds = null;
try
{
String tableName = SQLBuilder.getFullTableName(table, dbName);
tds = new TableDataSet(con, tableName);
Record rec = tds.addRecord();
// not the fully qualified name, insertOrUpdateRecord wants to use table as an index...
BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);
}
catch (DataSetException e)
{
throwTorqueException(e);
}
catch (SQLException e)
{
throwTorqueException(e);
}
catch (TorqueException e)
{
throwTorqueException(e);
}
finally
{
VillageUtils.close(tds);
}
// If the primary key column is auto-incremented, get the id
// now.
if (keyGen != null && keyGen.isPostInsert())
{
id = getId(pk, keyGen, con, keyInfo);
}
return id;