(Finally) reach the point where we have some useful, if basic, functionality.
[cfb.git] / prod / net / jaekl / cfb / db / driver / DbDriver.java
index eea3e22c44c8d89f8fa79d21a52089b87e05555b..bbe38f8cc0d9f23c4b3a24430bd747c1027a1354 100644 (file)
@@ -9,7 +9,6 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.List;
 
 import net.jaekl.cfb.db.Column;
@@ -80,6 +79,22 @@ public abstract class DbDriver {
                }
        }
        
+       public Row selectExactlyOne(Connection con, Column[] columns, Table[] tables, Condition[] conditions) 
+               throws SQLException
+       {
+               Sort[] sorts = new Sort[0];
+               int limit = 2;
+               List<Row> rows = select(con, columns, tables, conditions, sorts, limit);
+               if (rows.size() < 1) {
+                       throw new SQLException("Expected one result, but found none:  ", selectSql(columns, tables, conditions, sorts, limit));
+               }
+               if (rows.size() > 1) {
+                       throw new SQLException("Expected one result, but found more than one:  " + selectSql(columns, tables, conditions, sorts, limit));
+               }
+               
+               return rows.get(0);
+       }
+       
        public List<Row> select(Connection con, Column[] columns, Table[] tables, Condition[] conditions)
                throws SQLException
        {
@@ -99,8 +114,9 @@ public abstract class DbDriver {
                        int index = 0;
                        for (Condition condition : conditions) {
                                if (condition.getOperation().hasParam()) {
+                                       Column column = condition.getColumn();
                                        index++;
-                                       ps.setObject(index, condition.getValue());
+                                       column.setObject(ps, index, condition.getValue());
                                }
                        }
                        
@@ -121,6 +137,9 @@ public abstract class DbDriver {
                                }
                        }
                }
+               catch (SQLException se) {
+                       throw new SQLException("Error with SQL:  " + sql, se);
+               }
                
                return result;
        }
@@ -131,6 +150,8 @@ public abstract class DbDriver {
                int count = 0;
                int pendingValues = 0;
                
+               assert( isValidInsert(table, values));
+               
                String sql = insertSql(table);
                
                try (PreparedStatement ps = con.prepareStatement(sql))
@@ -141,19 +162,10 @@ public abstract class DbDriver {
                                assert(null != data);
                                assert(data.length == table.getNumColumns());
                                
-                               for (int col = 0; col < data.length; ++col) {
-                                       Object obj = data[col];
-                                       Column column = table.getColumn(col);
-                                       if (column.getType().equals(Type.TIMESTAMPTZ)) {
-                                               // Special case:  because there's no good way to read a TIMESTAMPTZ from 
-                                               // the database using JDBC, we store it as an integer (milliseconds since
-                                               // the epoch, 01.01.1970 00:00:00.000 UTC).
-                                               Date date = (Date)obj;
-                                               ps.setLong(col + 1, date.getTime());
-                                       }
-                                       else {
-                                               ps.setObject(col + 1, data[col]);
-                                       }
+                               for (int idx = 0; idx < data.length; ++idx) {
+                                       Object obj = data[idx];
+                                       Column column = table.getColumn(idx);
+                                       column.setObject(ps, idx + 1, obj);
                                        pendingValues++;
                                }
                                ps.addBatch();
@@ -282,10 +294,10 @@ public abstract class DbDriver {
                                sb.append(sort.getColumn().getName());
                                
                                if (sort.getDirection().equals(Sort.Direction.ASCENDING)) {
-                                       sb.append(" ASCENDING ");
+                                       sb.append(" ASC ");
                                }
                                else {
-                                       sb.append(" DESCENDING ");
+                                       sb.append(" DESC ");
                                }
                        }
                }
@@ -372,4 +384,21 @@ public abstract class DbDriver {
        }
        
        abstract protected String nextValSql(Sequence seq);
+       
+       boolean isValidInsert(Table table, Object[][] values)
+       {
+               if (null == table) return false;
+               if (null == values) return false;
+               
+               for (Object[] rowValues : values) {
+                       if (rowValues.length != table.getNumColumns()) {
+                               return false;
+                       }
+                       for (int idx = 0; idx < rowValues.length; ++idx) {
+                               
+                       }
+               }
+               
+               return true;
+       }
 }