Bring things to a state where the basic DB schema gets auto-created if it doesn't...
[cfb.git] / prod / net / jaekl / cfb / db / driver / DbDriver.java
1 package net.jaekl.cfb.db.driver;
2
3 import static net.jaekl.cfb.db.Column.Null.*;
4
5 import java.sql.Connection;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9
10 import net.jaekl.cfb.db.Column;
11 import net.jaekl.cfb.db.Column.Type;
12 import net.jaekl.cfb.db.Table;
13
14 public abstract class DbDriver {
15         DbDriver() {
16                 
17         }
18         
19         // Load the JDBC driver
20         public abstract void load() throws ClassNotFoundException;
21         
22         public abstract Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException;
23         
24         public boolean createTable(Connection con, Table table) throws SQLException {
25                 String sql = createTableSql(table);
26                 try (PreparedStatement ps = con.prepareStatement(sql)) {
27                         ps.executeUpdate();
28                 }
29                 catch (SQLException exc) {
30                         throw new SQLException("Failed to executeUpdate:  " + sql, exc);
31                 }
32                 
33                 return true;
34         }
35         
36         public abstract ResultSet selectColumnsFromWhere(Column[] columns, Table[] tables, String where);
37         
38         protected String typeName(Type type) {
39                 return type.toString();
40         }
41         
42         protected String createColumnSql(Column column) 
43         {
44                 String result = column.getName() + " " + typeName(column.getType());
45                 if (column.getWidth() > 0) {
46                         result += "(" + column.getWidth() + ")";
47                 }
48                 
49                 if (NOT_NULL == column.getNull()) {
50                         result += " NOT NULL";
51                 }
52                 else {
53                         result += " NULL";
54                 }
55             
56                 return result;
57         }
58         
59         protected String createTableSql(Table table) 
60         {
61                 assert(null != table);
62                 assert(null != table.getName());
63                 assert(table.getNumColumns() > 0);
64                 
65                 StringBuilder sb = new StringBuilder();
66                 
67                 sb.append("CREATE TABLE ")
68                   .append(table.getName())
69                   .append("(");
70                 
71                 for (int idx = 0; idx < table.getNumColumns(); ++idx) {
72                         if (idx > 0) {
73                                 sb.append(", ");
74                         }
75                         sb.append(createColumnSql(table.getColumn(idx)));
76                 }
77                 
78                 sb.append(")");
79                 
80                 return sb.toString();
81         }       
82 }