1 package net.jaekl.cfb.db.driver;
3 import static net.jaekl.cfb.db.Column.Null.*;
5 import java.sql.Connection;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
10 import net.jaekl.cfb.db.Column;
11 import net.jaekl.cfb.db.Column.Type;
12 import net.jaekl.cfb.db.Table;
14 public abstract class DbDriver {
19 // Load the JDBC driver
20 public abstract void load() throws ClassNotFoundException;
22 public abstract Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException;
24 public boolean createTable(Connection con, Table table) throws SQLException {
25 String sql = createTableSql(table);
26 try (PreparedStatement ps = con.prepareStatement(sql)) {
29 catch (SQLException exc) {
30 throw new SQLException("Failed to executeUpdate: " + sql, exc);
36 public abstract ResultSet selectColumnsFromWhere(Column[] columns, Table[] tables, String where);
38 protected String typeName(Type type) {
39 return type.toString();
42 protected String createColumnSql(Column column)
44 String result = column.getName() + " " + typeName(column.getType());
45 if (column.getWidth() > 0) {
46 result += "(" + column.getWidth() + ")";
49 if (NOT_NULL == column.getNull()) {
50 result += " NOT NULL";
59 protected String createTableSql(Table table)
61 assert(null != table);
62 assert(null != table.getName());
63 assert(table.getNumColumns() > 0);
65 StringBuilder sb = new StringBuilder();
67 sb.append("CREATE TABLE ")
68 .append(table.getName())
71 for (int idx = 0; idx < table.getNumColumns(); ++idx) {
75 sb.append(createColumnSql(table.getColumn(idx)));