X-Git-Url: http://jaekl.net/gitweb/?a=blobdiff_plain;f=prod%2Fnet%2Fjaekl%2Fcfb%2Fdb%2FSchema.java;h=898687ee4db5fd65112c10d6c39663b1891b4765;hb=a1378c84c773511e4ffe99fb419da67af188aff7;hp=176526d9b649aaadcedd1c4f24a979b9e9eb7e00;hpb=1577da4d5488b4f6f093ef0657c04415326b9bd3;p=cfb.git diff --git a/prod/net/jaekl/cfb/db/Schema.java b/prod/net/jaekl/cfb/db/Schema.java index 176526d..898687e 100644 --- a/prod/net/jaekl/cfb/db/Schema.java +++ b/prod/net/jaekl/cfb/db/Schema.java @@ -1,5 +1,7 @@ package net.jaekl.cfb.db; +// Copyright (C) 2015 Christian Jaekl + import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; @@ -14,11 +16,13 @@ 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 { @@ -32,6 +36,26 @@ public class Schema { return false; } + if (!createAllSequences(con)) { + return false; + } + + if (!postCreationInit(con)) { + return false; + } + + return true; + } + + public void purge(Connection con) throws SQLException { + dropAllTables(con); + dropAllSequences(con); + } + + 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; } @@ -71,6 +95,37 @@ public class Schema { return true; } + void dropAllTables(Connection con) { + for (Table table : m_tables) { + try { + m_driver.dropTable(con, table); + } + catch (SQLException e) { + e.printStackTrace(); + } + } + } + + boolean createAllSequences(Connection con) throws SQLException { + for (Sequence seq : m_sequences) { + if (!m_driver.createSequence(con, seq)) { + return false; + } + } + return true; + } + + void dropAllSequences(Connection con) { + for (Sequence seq : m_sequences) { + try { + m_driver.dropSequence(con, seq); + } + catch (SQLException e) { + e.printStackTrace(); + } + } + } + void addTable(Table table) { m_tables.add(table); } @@ -81,10 +136,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); } - } + } }