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;
11 import java.util.ArrayList;
12 import java.util.List;
14 import net.jaekl.cfb.db.Column;
15 import net.jaekl.cfb.db.Column.Type;
16 import net.jaekl.cfb.db.Condition;
17 import net.jaekl.cfb.db.Row;
18 import net.jaekl.cfb.db.Sequence;
19 import net.jaekl.cfb.db.Table;
21 public abstract class DbDriver {
26 // Load the JDBC driver
27 public abstract void load() throws ClassNotFoundException;
29 public abstract Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException;
31 public boolean createTable(Connection con, Table table) throws SQLException {
32 String sql = createTableSql(table);
33 try (PreparedStatement ps = con.prepareStatement(sql)) {
36 catch (SQLException exc) {
37 throw new SQLException("Failed to executeUpdate: " + sql, exc);
43 public boolean createSequence(Connection con, Sequence seq) throws SQLException
45 String sql = createSequenceSql(seq);
46 try (PreparedStatement ps = con.prepareStatement(sql)) {
49 catch (SQLException exc) {
50 throw new SQLException("Failed to executeUpdate: " + sql, exc);
56 public List<Row> select(Connection con, Column[] columns, Table[] tables, Condition[] conditions)
59 String sql = selectSql(columns, tables, conditions);
60 ArrayList<Row> result = new ArrayList<Row>();
62 try (PreparedStatement ps = con.prepareStatement(sql)) {
64 for (Condition condition : conditions) {
65 if (condition.getOperation().hasParam()) {
67 ps.setObject(index, condition.getValue());
71 try (ResultSet rs = ps.executeQuery()) {
73 Object[] values = new Object[columns.length];
74 for (index = 0; index < columns.length; ++index) {
75 values[index] = rs.getObject(index);
77 Row row = new Row(columns, values);
86 protected String selectSql(Column[] columns, Table[] tables, Condition[] conditions)
88 StringBuilder sb = new StringBuilder("SELECT ");
90 boolean firstColumn = true;
91 for (Column column : columns) {
98 sb.append(column.getName());
103 boolean firstTable = true;
104 for (Table table : tables) {
111 sb.append(table.getName());
114 if (null != conditions && conditions.length > 0) {
115 sb.append(" WHERE ");
117 boolean firstCondition = true;
119 for (Condition condition : conditions) {
120 if (firstCondition) {
121 firstCondition = false;
127 sb.append(condition.getColumn().getName())
128 .append(condition.getOperation().getSql());
132 return sb.toString();
135 protected String typeName(Type type) {
136 return type.toString();
139 protected String createColumnSql(Column column)
141 String result = column.getName() + " " + typeName(column.getType());
142 if (column.getWidth() > 0) {
143 result += "(" + column.getWidth() + ")";
146 if (NOT_NULL == column.getNull()) {
147 result += " NOT NULL";
156 protected String createTableSql(Table table)
158 assert(null != table);
159 assert(null != table.getName());
160 assert(table.getNumColumns() > 0);
162 StringBuilder sb = new StringBuilder();
164 sb.append("CREATE TABLE ")
165 .append(table.getName())
168 for (int idx = 0; idx < table.getNumColumns(); ++idx) {
172 sb.append(createColumnSql(table.getColumn(idx)));
177 return sb.toString();
180 protected String createSequenceSql(Sequence seq) {
182 assert(null != seq.getName());
184 return "CREATE SEQUENCE " + seq.getName();