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;
9 import java.util.Locale;
11 import net.jaekl.cfb.db.driver.DbDriver;
16 ArrayList<Table> m_tables;
18 public Schema(String name, DbDriver driver) {
21 m_tables = new ArrayList<Table>();
24 public boolean ensureDbInitialized(Connection con) throws SQLException {
26 boolean result = true;
28 if (allTablesPresent(con)) {
32 result = createAllTables(con);
37 boolean allTablesPresent(Connection con) throws SQLException
41 DatabaseMetaData dbmd = con.getMetaData();
42 HashSet<String> extantTables = new HashSet<String>();
44 try (ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"})) {
46 extantTables.add(rs.getString(3).toUpperCase(Locale.CANADA));
50 for (Table table : m_tables) {
51 String name = table.getName().toUpperCase(Locale.CANADA);
52 if ( ! extantTables.contains(name) ) {
53 // One or more tables missing
58 // We could be more thorough here, and check that the expected columns are in place.
63 boolean createAllTables(Connection con) throws SQLException {
64 for (Table table : m_tables) {
65 if (!m_driver.createTable(con, table)) {
72 void addTable(Table table) {
76 // Add a list of tables.
77 // Define each table in the list as follows:
80 // { column_name, type, width (-1 for default), null/not_null }
82 void addTables(Object[][][] tables)
84 for (Object[][] table : tables) {
85 addTable(Table.construct(table));