1 package net.jaekl.cfb.db.driver;
3 // Copyright (C) 2015 Christian Jaekl
5 import static net.jaekl.cfb.db.Column.Null.*;
7 import java.sql.Connection;
8 import java.sql.PreparedStatement;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
12 import net.jaekl.cfb.db.Column;
13 import net.jaekl.cfb.db.Column.Type;
14 import net.jaekl.cfb.db.Table;
16 public abstract class DbDriver {
21 // Load the JDBC driver
22 public abstract void load() throws ClassNotFoundException;
24 public abstract Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException;
26 public boolean createTable(Connection con, Table table) throws SQLException {
27 String sql = createTableSql(table);
28 try (PreparedStatement ps = con.prepareStatement(sql)) {
31 catch (SQLException exc) {
32 throw new SQLException("Failed to executeUpdate: " + sql, exc);
38 public abstract ResultSet selectColumnsFromWhere(Column[] columns, Table[] tables, String where);
40 protected String typeName(Type type) {
41 return type.toString();
44 protected String createColumnSql(Column column)
46 String result = column.getName() + " " + typeName(column.getType());
47 if (column.getWidth() > 0) {
48 result += "(" + column.getWidth() + ")";
51 if (NOT_NULL == column.getNull()) {
52 result += " NOT NULL";
61 protected String createTableSql(Table table)
63 assert(null != table);
64 assert(null != table.getName());
65 assert(table.getNumColumns() > 0);
67 StringBuilder sb = new StringBuilder();
69 sb.append("CREATE TABLE ")
70 .append(table.getName())
73 for (int idx = 0; idx < table.getNumColumns(); ++idx) {
77 sb.append(createColumnSql(table.getColumn(idx)));