package journal.action.sql; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import journal.reader.DataJournalEntry; import journal.reader.JournalEntry; import journal.reader.Options; import journal.schema.TableVersion; public class SQLAdapter { private Map<TableVersion, SQLTableVersion> cache = new HashMap<TableVersion, SQLTableVersion>(); private Connection cx; private Set<String> dbTables = new HashSet<String>(); private SQLTableVersion activeTableVersion = null; private SupportedDatabase database; private int batchSize = 1; Options options; public SQLAdapter(Connection cx, Options options) throws SQLException { this.cx = cx; this.options = options; DatabaseMetaData md = cx.getMetaData(); analyseMetaData(md); ResultSet rs = md.getTables(null, null, "DB_%", new String[]{"TABLE"}); while (rs.next()) { dbTables.add(rs.getString("TABLE_NAME").toUpperCase()); } } private void analyseMetaData(DatabaseMetaData md) { String databaseProductName = ""; try { databaseProductName = md.getDatabaseProductName(); System.out.println("Product:\t" + md.getDatabaseProductName()); System.out.println("Version:\t" + md.getDatabaseProductVersion()); System.out.println("Driver :\t" + md.getDriverName()); } catch (SQLException e) { System.out.println("Database does not support these meta data calls"); System.out.println(e); System.exit(2); } try { database = SupportedDatabase.valueOf(databaseProductName); } catch (IllegalArgumentException e) { String error = "Database " + databaseProductName + " not supported yet."; error += " Please update " + SupportedDatabase.class.getName(); System.out.println(error); e.printStackTrace(); System.exit(2); } } public SupportedDatabase getDatabase() { return database; } public int getBatchSize() { return batchSize; } public void setBatchSize(int batchSize) { this.batchSize = batchSize; } public void putValue(DataJournalEntry entry) throws Exception { activeTableVersion = getSQLTableVersion(entry); activeTableVersion.putValue(entry); } public void replaceValue(DataJournalEntry entry) throws Exception { activeTableVersion = getSQLTableVersion(entry); activeTableVersion.replaceValue(entry); } public void deleteValue(DataJournalEntry entry ) throws Exception { activeTableVersion = getSQLTableVersion(entry); activeTableVersion.deleteValue(entry); } public void verifyValue(DataJournalEntry entry) throws Exception { activeTableVersion = getSQLTableVersion(entry); activeTableVersion.verifyValue(entry); } public void commitMarker(JournalEntry entry) throws Exception { if (activeTableVersion != null) activeTableVersion.flushBatchUpdate(); cx.commit(); } public void flushMarker(JournalEntry entry) throws Exception { // pass } Connection getConnection() { return cx; } boolean tableExists(String tableName) { return dbTables.contains(tableName.toUpperCase()); } private SQLTableVersion getSQLTableVersion(DataJournalEntry entry) throws SQLException { TableVersion t = entry.getTableVersion(); if (cache.containsKey(t)) { return cache.get(t); } else { SQLTableVersion s = new SQLTableVersion(this, t); cache.put(t, s); return s; } } public void dropTables() throws Exception { Statement stmt = cx.createStatement(); for (String tableName : dbTables) { stmt.executeUpdate("DROP TABLE " + tableName); } dbTables.clear(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 7527 | Sven Erik Knop |
JournalReader, now in its proper place. Documentation to follow. |