X-Git-Url: http://jaekl.net/gitweb/?p=cfb.git;a=blobdiff_plain;f=prod%2Fnet%2Fjaekl%2Fcfb%2Fdb%2Fdriver%2FDbDriver.java;h=525ca7049d3444545a717a9788e51595b29697f3;hp=c8649579685d12a9d38f80950e35fb55fb0c595f;hb=3c10b6100c6035a65ce37dea846b027135289f67;hpb=88c635ae6a039a873cba2d794c55d726dcdc616d diff --git a/prod/net/jaekl/cfb/db/driver/DbDriver.java b/prod/net/jaekl/cfb/db/driver/DbDriver.java index c864957..525ca70 100644 --- a/prod/net/jaekl/cfb/db/driver/DbDriver.java +++ b/prod/net/jaekl/cfb/db/driver/DbDriver.java @@ -8,9 +8,14 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import net.jaekl.cfb.db.Column; import net.jaekl.cfb.db.Column.Type; +import net.jaekl.cfb.db.Condition; +import net.jaekl.cfb.db.Row; +import net.jaekl.cfb.db.Sequence; import net.jaekl.cfb.db.Table; public abstract class DbDriver { @@ -35,7 +40,97 @@ public abstract class DbDriver { return true; } - public abstract ResultSet selectColumnsFromWhere(Column[] columns, Table[] tables, String where); + public boolean createSequence(Connection con, Sequence seq) throws SQLException + { + String sql = createSequenceSql(seq); + try (PreparedStatement ps = con.prepareStatement(sql)) { + ps.executeUpdate(); + } + catch (SQLException exc) { + throw new SQLException("Failed to executeUpdate: " + sql, exc); + } + + return true; + } + + public List select(Connection con, Column[] columns, Table[] tables, Condition[] conditions) + throws SQLException + { + String sql = selectSql(columns, tables, conditions); + ArrayList result = new ArrayList(); + + try (PreparedStatement ps = con.prepareStatement(sql)) { + int index = 0; + for (Condition condition : conditions) { + if (condition.getOperation().hasParam()) { + index++; + ps.setObject(index, condition.getValue()); + } + } + + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + Object[] values = new Object[columns.length]; + for (index = 0; index < columns.length; ++index) { + values[index] = rs.getObject(index); + } + Row row = new Row(columns, values); + result.add(row); + } + } + } + + return result; + } + + protected String selectSql(Column[] columns, Table[] tables, Condition[] conditions) + { + StringBuilder sb = new StringBuilder("SELECT "); + + boolean firstColumn = true; + for (Column column : columns) { + if (firstColumn) { + firstColumn = false; + } + else { + sb.append(", "); + } + sb.append(column.getName()); + } + + sb.append(" FROM "); + + boolean firstTable = true; + for (Table table : tables) { + if (firstTable) { + firstTable = false; + } + else { + sb.append(", "); + } + sb.append(table.getName()); + } + + if (null != conditions && conditions.length > 0) { + sb.append(" WHERE "); + + boolean firstCondition = true; + + for (Condition condition : conditions) { + if (firstCondition) { + firstCondition = false; + } + else { + sb.append(" AND "); + } + + sb.append(condition.getColumn().getName()) + .append(condition.getOperation().getSql()); + } + } + + return sb.toString(); + } protected String typeName(Type type) { return type.toString(); @@ -81,4 +176,11 @@ public abstract class DbDriver { return sb.toString(); } + + protected String createSequenceSql(Sequence seq) { + assert(null != seq); + assert(null != seq.getName()); + + return "CREATE SEQUENCE " + seq.getName(); + } }