Add found bugs to database.
[cfb.git] / prod / net / jaekl / cfb / db / driver / DbDriver.java
index ac9f8ccdad2030150d25d0fe06cd15078c8d7fb4..f85db06ca3345f66f7e9732f8fa3c75db1e2ca18 100644 (file)
@@ -8,7 +8,9 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Timestamp;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import net.jaekl.cfb.db.Column;
@@ -30,7 +32,8 @@ public abstract class DbDriver {
        
        public abstract Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException;
        
-       public boolean createTable(Connection con, Table table) throws SQLException {
+       public boolean createTable(Connection con, Table table) throws SQLException 
+       {
                String sql = createTableSql(table);
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                        ps.executeUpdate();
@@ -42,6 +45,17 @@ public abstract class DbDriver {
                return true;
        }
        
+       public void dropTable(Connection con, Table table) throws SQLException
+       {
+               String sql = dropTableSql(table);
+               try (PreparedStatement ps = con.prepareStatement(sql)) {
+                       ps.executeUpdate();
+               }
+               catch (SQLException exc) {
+                       throw new SQLException("Failed to drop table:  " + sql, exc);
+               }
+       }
+       
        public boolean createSequence(Connection con, Sequence seq) throws SQLException 
        {
                String sql = createSequenceSql(seq);
@@ -55,6 +69,17 @@ public abstract class DbDriver {
                return true;
        }
        
+       public void dropSequence(Connection con, Sequence seq) throws SQLException
+       {
+               String sql = dropSequenceSql(seq);
+               try (PreparedStatement ps = con.prepareStatement(sql)) {
+                       ps.executeUpdate();
+               }
+               catch (SQLException exc) {
+                       throw new SQLException("Failed to drop sequence:  " + sql, exc);
+               }
+       }
+       
        public List<Row> select(Connection con, Column[] columns, Table[] tables, Condition[] conditions)
                throws SQLException
        {
@@ -74,7 +99,7 @@ public abstract class DbDriver {
                                while (rs.next()) {
                                        Object[] values = new Object[columns.length];
                                        for (index = 0; index < columns.length; ++index) {
-                                               values[index] = rs.getObject(index);
+                                               values[index] = rs.getObject(index + 1);
                                        }
                                        Row row = new Row(columns, values);
                                        result.add(row);
@@ -102,7 +127,15 @@ public abstract class DbDriver {
                                assert(data.length == table.getNumColumns());
                                
                                for (int col = 0; col < data.length; ++col) {
-                                       ps.setObject(col + 1, data[col]);
+                                       Object obj = data[col];
+                                       if (obj instanceof java.util.Date) {
+                                               Date date = (Date)obj;
+                                               Timestamp ts = new Timestamp(date.getTime());
+                                               ps.setTimestamp(col + 1, ts);
+                                       }
+                                       else {
+                                               ps.setObject(col + 1, data[col]);
+                                       }
                                        pendingValues++;
                                }
                                ps.addBatch();
@@ -120,6 +153,22 @@ public abstract class DbDriver {
                return count;
        }
        
+       public long nextVal(Connection con, Sequence seq) throws SQLException
+       {
+               String sql = nextValSql(seq);
+               
+               try (PreparedStatement ps = con.prepareStatement(sql)) 
+               {
+                       try (ResultSet rs = ps.executeQuery()) {
+                               if (rs.next()) {
+                                       return rs.getLong(1);
+                               }
+                       }
+               }
+               
+               throw new SQLException("No value returned for sequence:  " + sql);
+       }
+       
        int checkFlushBatch(PreparedStatement ps, int pendingValues, boolean forceFlush) throws SQLException
        {
                int count = 0;
@@ -247,10 +296,26 @@ public abstract class DbDriver {
                return sb.toString();
        }       
        
+       protected String dropTableSql(Table table) {
+               assert(null != table);
+               assert(null != table.getName());
+               
+               return "DROP TABLE " + table.getName();
+       }
+       
        protected String createSequenceSql(Sequence seq) {
                assert(null != seq);
                assert(null != seq.getName());
                
                return "CREATE SEQUENCE " + seq.getName();
        }
+       
+       protected String dropSequenceSql(Sequence seq) {
+               assert(null != seq);
+               assert(null != seq.getName());
+               
+               return "DROP SEQUENCE " + seq.getName();
+       }
+       
+       abstract protected String nextValSql(Sequence seq);
 }