* @throws PersistenceException if the table exists, or cannot be created
*/
public void create(Table table) throws PersistenceException {
String name = table.getName();
if (_browser.getTableExists(name)) {
throw new PersistenceException(
"An object already exists in the database named " + name);
}
StringBuffer sql = new StringBuffer("create table ");
sql.append(name);
sql.append(" (");
_log.debug("Creating table: " + name);
Attribute[] attributes = table.getAttribute();
for (int i = 0; i < attributes.length; ++i) {
if (i > 0) {
sql.append(", ");
}
Attribute attribute = attributes[i];
sql.append(attribute.getName());
sql.append(" ");
sql.append(getSQLType(attribute));
if (attribute.getNotNull()) {
sql.append(" not null");
}
if (attribute.getPrimaryKey()) {
sql.append(" primary key");
}
if (attribute.getUnique()) {
sql.append(" unique");
}
}
PrimaryKey key = table.getPrimaryKey();
if (key != null) {
sql.append(", primary key (");
Column[] columns = key.getColumn();
for (int i = 0; i < columns.length; ++i) {
if (i > 0) {
sql.append(", ");
}
sql.append(columns[i].getName());
}
sql.append(")");
}
sql.append(")");
_log.debug("SQL=" + sql);
Statement statement = null;
try {
statement = _connection.createStatement();
statement.executeUpdate(sql.toString());
} catch (SQLException exception) {
throw new PersistenceException("Failed to create table=" + name,
exception);
} finally {
SQLHelper.close(statement);
}
createIndexes(table);