If your database definitions really contain name/type pairs only and no further information like indices, foreign key references or other constraints, then a Map<String, String>
would be sufficient. You could create a class, e.g. TableDef
:
public class TableDef {
private final String tableName;
private final Map<String, String> colDef = new LinkedHashMap<String, String>();
public TableDef(String tableName) {
this.tableName = tableName;
}
public void addColumn(String name, String type) {
if (colDef.containsKey(name))
throw new IllegalArgumentException(name + " column already added");
colDef.put(name, type);
}
public String getCreateTable() {
String ddl = "CREATE TABLE " + tableName + "(";
for (Map.Entry<String, String> col : colDef.entrySet()) {
// add column to ddl
}
return ddl + ")";
}
}
Alternatively, if you can be certain that no duplicate column names exist, you could build the CREATE TABLE
statement dynamically while reading your schema definition. However, it won't make much difference in performance as long as you don't have thousands of columns and tables.
To insert bulk data in an efficient way, take a look at this answer.