X-Git-Url: http://jaekl.net/gitweb/?p=cfb.git;a=blobdiff_plain;f=prod%2Fnet%2Fjaekl%2Fcfb%2Fdb%2FSchema.java;h=4937fae153eae94dacf8c051c8c49d07b7775892;hp=bfcfdb8abf3bf98d866ad1290a4426573944aea3;hb=083fb5094456d37fa4765400a461625635fbf77d;hpb=a683a5834138300c924274d1cda66a4a359222c5 diff --git a/prod/net/jaekl/cfb/db/Schema.java b/prod/net/jaekl/cfb/db/Schema.java index bfcfdb8..4937fae 100644 --- a/prod/net/jaekl/cfb/db/Schema.java +++ b/prod/net/jaekl/cfb/db/Schema.java @@ -1,11 +1,14 @@ package net.jaekl.cfb.db; +// Copyright (C) 2015 Christian Jaekl + import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashSet; +import java.util.Locale; import net.jaekl.cfb.db.driver.DbDriver; @@ -13,24 +16,42 @@ public class Schema { String m_name; DbDriver m_driver; ArrayList m_tables; + ArrayList m_sequences; public Schema(String name, DbDriver driver) { m_name = name; m_driver = driver; m_tables = new ArrayList
(); + m_sequences = new ArrayList(); } public boolean ensureDbInitialized(Connection con) throws SQLException { assert(null != con); - boolean result = true; if (allTablesPresent(con)) { return true; } - result = createAllTables(con); + if (!createAllTables(con)) { + return false; + } + + if (!createAllSequences(con)) { + return false; + } + + if (!postCreationInit(con)) { + + } - return result; + return true; + } + + boolean postCreationInit(Connection con) throws SQLException { + // no-op + // Override this in a derived class if you need to initialize something + // after the tables and sequences are created. + return true; } boolean allTablesPresent(Connection con) throws SQLException @@ -42,18 +63,20 @@ public class Schema { try (ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"})) { while (rs.next()) { - extantTables.add(rs.getString(3)); + extantTables.add(rs.getString(3).toUpperCase(Locale.CANADA)); } } for (Table table : m_tables) { - if ( ! extantTables.contains(table.getName()) ) { + String name = table.getName().toUpperCase(Locale.CANADA); + if ( ! extantTables.contains(name) ) { // One or more tables missing return false; } } // We could be more thorough here, and check that the expected columns are in place. + // Also, eventually, some sort of DB schema versioning will be needed. return true; } @@ -67,6 +90,15 @@ public class Schema { return true; } + boolean createAllSequences(Connection con) throws SQLException { + for (Sequence seq : m_sequences) { + if (!m_driver.createSequence(con, seq)) { + return false; + } + } + return true; + } + void addTable(Table table) { m_tables.add(table); } @@ -77,10 +109,20 @@ public class Schema { // { table_name }, // { column_name, type, width (-1 for default), null/not_null } // } - void addTables(Object[][][] tables) + void addTables(Table[] tables) { - for (Object[][] table : tables) { - addTable(Table.construct(table)); + for (Table table : tables) { + addTable(table); } - } + } + + void addSequence(Sequence seq) { + m_sequences.add(seq); + } + + void addSequences(Sequence[] sequences) { + for (Sequence sequence : sequences) { + addSequence(sequence); + } + } }