Add some unit testing of the CfbSchema.
[cfb.git] / prod / net / jaekl / cfb / db / driver / PostgresqlDriver.java
1 package net.jaekl.cfb.db.driver;
2
3 // Copyright (C) 2015 Christian Jaekl
4
5 import java.sql.Connection;
6 import java.sql.DriverManager;
7 import java.sql.SQLException;
8 import java.util.Properties;
9
10 import net.jaekl.cfb.db.Sequence;
11 import net.jaekl.cfb.db.Column.Type;
12
13 public class PostgresqlDriver extends DbDriver {
14
15         @Override
16         public void load() throws ClassNotFoundException {
17                 // This should no longer be necessary, so long as we're using 
18                 // JDBC 4 (which came out with JDK 6) and an updated driver.
19                 // But, it shouldn't hurt, either.
20                 Class.forName("org.postgresql.Driver");
21         }
22
23         @Override
24         public Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException {
25                 String url = "jdbc:postgresql://" + host + ":" + port + "/" + dbName;
26                 Properties props = new Properties();
27                 props.setProperty("user", user);
28                 props.setProperty("password", pass);
29                 //props.setProperty("ssl", "true");
30                 return DriverManager.getConnection(url, props);
31         }
32         
33         @Override 
34         public String nextValSql(Sequence seq) 
35         {
36                 return " SELECT NEXTVAL('" + seq.getName() + "') ";
37         }
38         
39         @Override
40         protected String typeName(Type type) {
41                 // Special case:  TIMESTAMPTZ stored as INTEGER (milliseconds since the epoch)
42                 // Reading a TIMESTAMPTZ back from the DB, and converting it to a java.util.Date,
43                 // is fraught with peril.  The best way around this is to store the dates in 
44                 // milliseconds-since-the-epoch (01.01.1970 00:00:00.000 UTC).
45                 if (Type.TIMESTAMPTZ.equals(type)) {
46                         return "BIGINT";
47                 }
48                 
49                 return type.toString();
50         }}