{
databaseNameFromInsertValues = getDatabaseName();
}
Database database = Torque.getDatabase(databaseNameFromInsertValues);
Object keyInfo = getIdMethodInfo();
IdGenerator keyGen = database.getIdGenerator(
getTableMap().getPrimaryKeyMethod());
SimpleKey id = null;
// can currently generate only single column pks, therefore a single
// columnMap is ok
ColumnMap primaryKey = null;
if (keyGen != null)
{
// fail on multiple pks
primaryKey = getTableMap().getPrimaryKey();
// primaryKey will be null if there is no primary key
// defined for the table we're inserting into.
if (keyGen.isPriorToInsert() && primaryKey != null
&& !insertValues.containsKey(
primaryKey))
{
id = getId(primaryKey, keyGen, connection, keyInfo);
insertValues.put(
primaryKey,
new JdbcTypedValue(id.getValue(), id.getJdbcType()));
}
}
List<String> columnNames = new ArrayList<String>();
List<JdbcTypedValue> replacementObjects
= new ArrayList<JdbcTypedValue>();
for (Map.Entry<Column, JdbcTypedValue> columnValue
: insertValues.entrySet())
{
Column column = columnValue.getKey();
columnNames.add(column.getColumnName());
JdbcTypedValue value = columnValue.getValue();
replacementObjects.add(value);
}
String fullTableName = SqlBuilder.getFullTableName(
getTableMap().getFullyQualifiedTableName(),
databaseNameFromInsertValues);
StringBuilder query = new StringBuilder("INSERT INTO ")
.append(fullTableName)
.append("(")
.append(StringUtils.join(columnNames, ","))
.append(") VALUES (");
for (int i = 0; i < columnNames.size(); ++i)
{
if (i != 0)
{
query.append(",");
}
query.append("?");
}
query.append(")");
PreparedStatement preparedStatement = null;
try
{
preparedStatement = connection.prepareStatement(query.toString());
int position = 1;
for (JdbcTypedValue replacementObject : replacementObjects)
{
Object value = replacementObject.getValue();
if (value != null)
{
if (replacementObject.getJdbcType() != Types.BLOB
&& replacementObject.getJdbcType() != Types.CLOB)
{
preparedStatement.setObject(
position,
value,
replacementObject.getJdbcType());
}
else
{
preparedStatement.setObject(
position,
value);
}
}
else
{
preparedStatement.setNull(
position,
replacementObject.getJdbcType());
}
position++;
}
long startTime = System.currentTimeMillis();
log.debug("Executing insert " + query.toString()
+ " using parameters " + replacementObjects);
preparedStatement.executeUpdate();
long queryEndTime = System.currentTimeMillis();
log.trace("insert took " + (queryEndTime - startTime)
+ " milliseconds");
preparedStatement.close();
preparedStatement = null;
}
catch (SQLException e)
{
throw ExceptionMapper.getInstance().toTorqueException(e);
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
log.warn("error closing prepared statement", e);
}
}
}
// If the primary key column is auto-incremented, get the id
// now.
if (keyGen != null && keyGen.isPostInsert()
&& primaryKey != null
&& !insertValues.containsKey(
primaryKey))
{
id = getId(primaryKey, keyGen, connection, keyInfo);