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)) {
33 public abstract ResultSet selectColumnsFromWhere(Column[] columns, Table[] tables, String where);
35 protected String typeName(Type type) {
36 return type.toString();
39 protected String createColumnSql(Column column)
41 String result = column.getName() + " " + typeName(column.getType());
42 if (column.getWidth() > 0) {
43 result += "(" + column.getWidth() + ")";
46 if (NOT_NULL == column.getNull()) {
47 result += " NOT NULL";
56 protected String createTableSql(Table table)
58 assert(null != table);
59 assert(null != table.getName());
60 assert(table.getNumColumns() > 0);
62 StringBuilder sb = new StringBuilder();
64 sb.append("CREATE TABLE ")
65 .append(table.getName())
68 for (int idx = 0; idx < table.getNumColumns(); ++idx) {
72 sb.append(createColumnSql(table.getColumn(idx)));