SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING: Invoke nextval() using...
[cfb.git] / prod / net / jaekl / cfb / db / driver / PostgresqlDriver.java
index 0f6a12d7796d53b8e886eb2975a56f7ebb5b55a2..ce4a802fe39c7cb12dde8db661d8adb8e26796ee 100644 (file)
@@ -1,10 +1,16 @@
 package net.jaekl.cfb.db.driver;
 
+// Copyright (C) 2015 Christian Jaekl
+
 import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Properties;
 
-import net.jaekl.cfb.db.Column;
-import net.jaekl.cfb.db.Table;
+import net.jaekl.cfb.db.Sequence;
+import net.jaekl.cfb.db.Column.Type;
 
 public class PostgresqlDriver extends DbDriver {
 
@@ -17,16 +23,44 @@ public class PostgresqlDriver extends DbDriver {
        }
 
        @Override
-       public Connection connect(String host, int port, String user, String pass) {
-               // TODO Auto-generated method stub
-               return null;
+       public Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException {
+               String url = "jdbc:postgresql://" + host + ":" + port + "/" + dbName;
+               Properties props = new Properties();
+               props.setProperty("user", user);
+               props.setProperty("password", pass);
+               //props.setProperty("ssl", "true");
+               return DriverManager.getConnection(url, props);
        }
-
-       @Override
-       public ResultSet selectColumnsFromWhere(Column[] columns, Table[] tables,
-                       String where) {
-               // TODO Auto-generated method stub
-               return null;
+       
+       @Override 
+       public long nextVal(Connection con, Sequence seq) throws SQLException
+       {
+               String sql = " SELECT NEXTVAL(?) ";
+               
+               try (PreparedStatement ps = con.prepareStatement(sql)) 
+               {
+                       ps.setString(1, seq.getName());
+                       
+                       try (ResultSet rs = ps.executeQuery()) {
+                               if (rs.next()) {
+                                       return rs.getLong(1);
+                               }
+                       }
+               }
+               
+               throw new SQLException("No value returned for sequence:  " + sql);
        }
-
-}
+       
+       
+       @Override
+       protected String typeName(Type type) {
+               // Special case:  TIMESTAMPTZ stored as INTEGER (milliseconds since the epoch)
+               // Reading a TIMESTAMPTZ back from the DB, and converting it to a java.util.Date,
+               // is fraught with peril.  The best way around this is to store the dates in 
+               // milliseconds-since-the-epoch (01.01.1970 00:00:00.000 UTC).
+               if (Type.TIMESTAMPTZ.equals(type)) {
+                       return "BIGINT";
+               }
+               
+               return type.toString();
+       }}