(Finally) reach the point where we have some useful, if basic, functionality.
[cfb.git] / prod / net / jaekl / cfb / db / Column.java
index 8fc723ded7b00521bbb8fd103aa9c054437e961e..be9fb65729e7501e23d8fc25932f07700c4096d2 100644 (file)
@@ -1,5 +1,9 @@
 package net.jaekl.cfb.db;
 
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.Date;
+
 import net.jaekl.cfb.util.Util;
 
 // Copyright (C) 2015 Christian Jaekl
@@ -48,6 +52,22 @@ public class Column {
                return new Column(name, type, width.intValue(), canBeNull);
        }
        
+       // Wrapper around PreparedStatement.setObject().
+       // Note that indices start at 1, not at zero.
+       public void setObject(PreparedStatement ps, int idx, Object obj) throws SQLException
+       {
+               if (this.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(idx, date.getTime());
+               }
+               else {
+                       ps.setObject(idx, obj);
+               }
+       }
+       
        @Override 
        public boolean equals(Object obj) 
        {