bfcfdb8abf3bf98d866ad1290a4426573944aea3
[cfb.git] / prod / net / jaekl / cfb / db / Schema.java
1 package net.jaekl.cfb.db;
2
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
10 import net.jaekl.cfb.db.driver.DbDriver;
11
12 public class Schema {
13         String m_name;
14         DbDriver m_driver;
15         ArrayList<Table> m_tables;
16         
17         public Schema(String name, DbDriver driver) {
18                 m_name = name;
19                 m_driver = driver;
20                 m_tables = new ArrayList<Table>();
21         }
22         
23         public boolean ensureDbInitialized(Connection con) throws SQLException {
24                 assert(null != con);
25                 boolean result = true;
26                 
27                 if (allTablesPresent(con)) {
28                         return true;
29                 }
30                 
31                 result = createAllTables(con);
32                 
33                 return result;
34         }
35         
36         boolean allTablesPresent(Connection con) throws SQLException 
37         {
38                 assert(null != con);
39                 
40                 DatabaseMetaData dbmd = con.getMetaData();
41                 HashSet<String> extantTables = new HashSet<String>();
42                 
43                 try (ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"})) {
44                         while (rs.next()) {
45                                 extantTables.add(rs.getString(3));
46                         }
47                 }
48                 
49                 for (Table table : m_tables) {
50                         if ( ! extantTables.contains(table.getName()) ) {
51                                 // One or more tables missing
52                                 return false;
53                         }
54                 }
55                 
56                 // We could be more thorough here, and check that the expected columns are in place.
57                 
58                 return true;            
59         }
60         
61         boolean createAllTables(Connection con) throws SQLException {
62                 for (Table table : m_tables) {
63                         if (!m_driver.createTable(con, table)) {
64                                 return false;
65                         }
66                 }
67                 return true;
68         }
69         
70         void addTable(Table table) {
71                 m_tables.add(table);
72         }
73         
74         // Add a list of tables.
75         // Define each table in the list as follows:
76         // {
77         //   { table_name },
78         //   { column_name, type, width (-1 for default), null/not_null }
79         // }
80         void addTables(Object[][][] tables) 
81         {
82                 for (Object[][] table : tables) {
83                         addTable(Table.construct(table));
84                 }
85         }       
86 }