1 package net.jaekl.cfb.db;
3 import java.sql.Connection;
4 import java.sql.DatabaseMetaData;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.HashSet;
10 import net.jaekl.cfb.db.driver.DbDriver;
15 ArrayList<Table> m_tables;
17 public Schema(String name, DbDriver driver) {
20 m_tables = new ArrayList<Table>();
23 public boolean ensureDbInitialized(Connection con) throws SQLException {
25 boolean result = true;
27 if (allTablesPresent(con)) {
31 result = createAllTables(con);
36 boolean allTablesPresent(Connection con) throws SQLException
40 DatabaseMetaData dbmd = con.getMetaData();
41 HashSet<String> extantTables = new HashSet<String>();
43 try (ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"})) {
45 extantTables.add(rs.getString(3));
49 for (Table table : m_tables) {
50 if ( ! extantTables.contains(table.getName()) ) {
51 // One or more tables missing
56 // We could be more thorough here, and check that the expected columns are in place.
61 boolean createAllTables(Connection con) throws SQLException {
62 for (Table table : m_tables) {
63 if (!m_driver.createTable(con, table)) {
70 void addTable(Table table) {
74 // Add a list of tables.
75 // Define each table in the list as follows:
78 // { column_name, type, width (-1 for default), null/not_null }
80 void addTables(Object[][][] tables)
82 for (Object[][] table : tables) {
83 addTable(Table.construct(table));