sql.append(" (");
boolean addSeparator = false;
Iterator<DBColumn> columns = t.getColumns().iterator();
while (columns.hasNext())
{
DBTableColumn c = (DBTableColumn) columns.next();
sql.append((addSeparator) ? ",\r\n " : "\r\n ");
if (appendColumnDesc(c, sql)==false)
continue; // Ignore and continue;
addSeparator = true;
}
// Primary Key
DBIndex pk = t.getPrimaryKey();
if (pk != null)
{ // add the primary key
sql.append(",\r\n CONSTRAINT ");
appendElementName(sql, pk.getName());
sql.append(" PRIMARY KEY (");
addSeparator = false;
// columns
DBColumn[] keyColumns = pk.getColumns();
for (int i = 0; i < keyColumns.length; i++)
{
sql.append((addSeparator) ? ", " : "");
keyColumns[i].addSQL(sql, DBExpr.CTX_NAME);
addSeparator = true;
}
sql.append(")");
}
sql.append(")");
// Create the table
DBDatabase db = t.getDatabase();
if (script.addStmt(sql) == false)
return false;
// Create other Indexes (except primary key)
Iterator<DBIndex> indexes = t.getIndexes().iterator();
while (indexes.hasNext())
{
DBIndex idx = indexes.next();
if (idx == pk || idx.getType() == DBIndex.PRIMARYKEY)
continue;
// Cretae Index
sql.setLength(0);
sql.append((idx.getType() == DBIndex.UNIQUE) ? "CREATE UNIQUE INDEX " : "CREATE INDEX ");
appendElementName(sql, idx.getName());
sql.append(" ON ");
t.addSQL(sql, DBExpr.CTX_FULLNAME);
sql.append(" (");
addSeparator = false;
// columns
DBColumn[] idxColumns = idx.getColumns();
for (int i = 0; i < idxColumns.length; i++)
{
sql.append((addSeparator) ? ", " : "");
idxColumns[i].addSQL(sql, DBExpr.CTX_NAME);
sql.append("");
addSeparator = true;
}
sql.append(")");
// Create Index
if (script.addStmt(sql) == false)
return false;
}
// add Comments
createComment(db, "TABLE", t, t.getComment(), script);
columns = t.getColumns().iterator();
while (columns.hasNext())
{
DBColumn c = columns.next();
String com = c.getComment();
if (com != null)
createComment(db, "COLUMN", c, com, script);
}
// done
return success();