projects
/
cfb.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Restructure database code.
[cfb.git]
/
prod
/
net
/
jaekl
/
cfb
/
db
/
Schema.java
diff --git
a/prod/net/jaekl/cfb/db/Schema.java
b/prod/net/jaekl/cfb/db/Schema.java
index eef7b8ea15aad2e8272ccfb4d50afbb3850f7262..4937fae153eae94dacf8c051c8c49d07b7775892 100644
(file)
--- 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;
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.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
@@
-14,24
+16,42
@@
public class Schema {
String m_name;
DbDriver m_driver;
ArrayList<Table> m_tables;
String m_name;
DbDriver m_driver;
ArrayList<Table> m_tables;
+ ArrayList<Sequence> m_sequences;
public Schema(String name, DbDriver driver) {
m_name = name;
m_driver = driver;
m_tables = new ArrayList<Table>();
public Schema(String name, DbDriver driver) {
m_name = name;
m_driver = driver;
m_tables = new ArrayList<Table>();
+ m_sequences = new ArrayList<Sequence>();
}
public boolean ensureDbInitialized(Connection con) throws SQLException {
assert(null != con);
}
public boolean ensureDbInitialized(Connection con) throws SQLException {
assert(null != con);
- boolean result = true;
if (allTablesPresent(con)) {
return 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
}
boolean allTablesPresent(Connection con) throws SQLException
@@
-56,6
+76,7
@@
public class Schema {
}
// We could be more thorough here, and check that the expected columns are in place.
}
// 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;
}
return true;
}
@@
-69,6
+90,15
@@
public class Schema {
return true;
}
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);
}
void addTable(Table table) {
m_tables.add(table);
}
@@
-79,10
+109,20
@@
public class Schema {
// { table_name },
// { column_name, type, width (-1 for default), null/not_null }
// }
// { 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);
+ }
+ }
}
}