Permalink
Browse files

refactor

  • Loading branch information...
kazuho committed Jul 10, 2014
1 parent dec87dd commit 0e479d3bec8ccaa5b06697d0df94a2ac067a4ed1
@@ -1,31 +1,28 @@
package net.examp1e.picoorm;
import java.sql.Connection;
-import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
-public class Condition<Row extends AbstractRow> {
+import net.examp1e.picoorm.types.AnyType;
- public static interface Parameter {
- public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException;
- }
+public class Condition<Row extends AbstractRow> {
public static abstract class OrderBy {
abstract String toOrderBySQL();
}
final TableDefinition<Row> tableDefinition;
String term;
- ArrayList<Parameter> params = new ArrayList<Parameter>();
+ ArrayList<AnyType> params = new ArrayList<AnyType>();
ArrayList<String> orderBy = new ArrayList<String>();
long limitOffset = 0;
long limitCount = -1;
- public Condition(TableDefinition<Row> tableDefinition, String term, Parameter... params) {
+ public Condition(TableDefinition<Row> tableDefinition, String term, AnyType... params) {
this.tableDefinition = tableDefinition;
this.term = term;
- for (Parameter param : params) {
+ for (AnyType param : params) {
this.params.add(param);
}
}
@@ -23,6 +23,7 @@ String toOrderBySQL() {
protected void _init(TableDefinition<Row> tableDefinition, String fieldName) {
this.tableDefinition = tableDefinition;
this.fieldName = fieldName;
+ tableDefinition.addColumnDefinition(this);
}
@Override
@@ -4,69 +4,108 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Types;
import java.util.ArrayList;
+import net.examp1e.picoorm.types.AnyType;
+
public abstract class TableDefinition<Row extends AbstractRow> {
- public abstract String getTableName();
-
- public abstract ArrayList<String> getColumnNames(Row row);
-
- public abstract int bind(Row row, PreparedStatement ps, int index) throws SQLException;
-
- public abstract Row deserialize(ResultSet rs) throws SQLException;
+ public final String TABLE_NAME;
+ private ArrayList<Predicate<Row>> columnDefinitions = new ArrayList<Predicate<Row>>();
+
+ protected TableDefinition(String tableName) {
+ TABLE_NAME = tableName;
+ }
+ public abstract Row createRow();
+
+ public abstract AnyType[] getColumns(Row row);
+
public ArrayList<Row> search(Connection conn, Condition<Row> cond) throws SQLException {
- String sql = "SELECT " + join(getColumnNames(null)) + " FROM " + getTableName() + _buildWhere(cond);
+ String sql = "SELECT " + _join(_getColumnNames(null)) + " FROM " + TABLE_NAME + _buildWhere(cond);
PreparedStatement ps = conn.prepareStatement(sql);
this._bindWhere(cond, ps, 1);
ResultSet rs = ps.executeQuery();
ArrayList<Row> ret = new ArrayList<Row>();
while (rs.next()) {
- ret.add(deserialize(rs));
+ Row row = createRow();
+ AnyType[] columns = getColumns(row);
+ for (int i = 0; i != columns.length; ++i) {
+ columns[i].unbind(rs, i + 1);
+ }
+ ret.add(row);
}
return ret;
}
+ void addColumnDefinition(Predicate<Row> columnDefinition) {
+ this.columnDefinitions.add(columnDefinition);
+ }
+
public void insert(Connection conn, Row row) throws SQLException {
- ArrayList<String> names = getColumnNames(row);
+ ArrayList<String> names = _getColumnNames(row);
- String sql = "INSERT INTO " + getTableName() + " (" + join(names) + ") VALUES (";
+ String sql = "INSERT INTO " + TABLE_NAME + " (" + _join(names) + ") VALUES (";
for (int i = 0; i != names.size(); ++i) {
if (i != 0)
sql += ",";
sql += "?";
}
sql += ")";
PreparedStatement ps = conn.prepareStatement(sql);
- bind(row, ps, 1);
+ _bindColumnsBeingSet(row, ps, 1);
ps.execute();
}
public void update(Connection conn, Condition<Row> cond, Row changes) throws SQLException {
- ArrayList<String> columnsToChange = getColumnNames(changes);
+ ArrayList<String> columnsToChange = _getColumnNames(changes);
if (columnsToChange.size() == 0)
return;
- String sql = "UPDATE " + getTableName() + " SET ";
+ String sql = "UPDATE " + TABLE_NAME + " SET ";
for (String column : columnsToChange) {
sql += column + "=?";
}
sql += _buildWhere(cond);
PreparedStatement ps = conn.prepareStatement(sql);
int parameterIndex = 1;
- parameterIndex = bind(changes, ps, parameterIndex);
+ parameterIndex = _bindColumnsBeingSet(changes, ps, parameterIndex);
parameterIndex = _bindWhere(cond, ps, parameterIndex);
ps.execute();
}
+ private ArrayList<String> _getColumnNames(Row row) {
+ ArrayList<String> columnNames = new ArrayList<String>();
+ if (row == null) {
+ for (Predicate<Row> c : columnDefinitions)
+ columnNames.add(c.fieldName);
+ } else {
+ // only name the columns that are being set
+ AnyType[] columns = getColumns(row);
+ int columnIndex = 0;
+ for (Predicate<Row> c : columnDefinitions) {
+ if (columns[columnIndex++].isSet())
+ columnNames.add(c.fieldName);
+ }
+ }
+ return columnNames;
+ }
+
+ private int _bindColumnsBeingSet(Row row, PreparedStatement ps, int parameterIndex) throws SQLException {
+ AnyType[] columns = getColumns(row);
+ for (AnyType column : columns) {
+ if (column.isSet())
+ column.bind(ps, parameterIndex++);
+ }
+ return parameterIndex;
+ }
+
private String _buildWhere(Condition<Row> cond) {
String clause = " WHERE " + cond.term;
if (cond.orderBy.size() != 0) {
- clause += " ORDER BY " + join(cond.orderBy);
+ clause += " ORDER BY " + _join(cond.orderBy);
}
if (cond.limitCount != -1) {
clause += " LIMIT " + Long.toString(cond.limitOffset) + "," + Long.toString(cond.limitCount);
@@ -75,29 +114,13 @@ private String _buildWhere(Condition<Row> cond) {
}
private int _bindWhere(Condition<Row> cond, PreparedStatement ps, int index) throws SQLException {
- for (Condition.Parameter p : cond.params) {
- p.bindTo(ps, index++);
+ for (AnyType p : cond.params) {
+ p.bind(ps, index++);
}
return index;
}
- public static void setLong(PreparedStatement ps, int parameterIndex, Long value) throws SQLException {
- if (value != null) {
- ps.setLong(parameterIndex, value);
- } else {
- ps.setNull(parameterIndex, Types.BIGINT);
- }
- }
-
- public static void setString(PreparedStatement ps, int parameterIndex, String value) throws SQLException {
- if (value != null) {
- ps.setString(parameterIndex, value);
- } else {
- ps.setNull(parameterIndex, Types.VARCHAR);
- }
- }
-
- public static String join(ArrayList<String> list) {
+ private static String _join(ArrayList<String> list) {
String s = "";
for (String e : list) {
if (s.length() != 0)
@@ -1,64 +1,43 @@
package net.examp1e.picoorm.example;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
import net.examp1e.picoorm.*;
import net.examp1e.picoorm.types.*;
// this class definition should be automatically generated from the DDL
public class Member extends AbstractRow {
// serdes
- public static final String TABLE_NAME = "member";
- static final TableDefinition<Member> TABLE_DEFINITION = new TableDefinition<Member>() {
- public String getTableName() {
- return TABLE_NAME;
+ public static final TableDefinition<Member> TABLE_DEFINITION = new TableDefinition<Member>("member") {
+ @Override
+ public Member createRow() {
+ return new Member();
}
- public ArrayList<String> getColumnNames(Member row) {
- ArrayList<String> names = new ArrayList<String>();
- if (row == null || row._id.isSet())
- names.add("id");
- if (row == null || row._name.isSet())
- names.add("name");
- return names;
- }
- public int bind(Member row, PreparedStatement ps, int index) throws SQLException {
- index = row._id.bindTo(ps, index);
- index = row._name.bindTo(ps, index);
- return index;
- }
- public Member deserialize(ResultSet rs) throws SQLException {
- return new Member()
- .setId(rs.getLong("id"))
- .setName(rs.getString("name"));
+ @Override
+ public AnyType[] getColumns(Member row) {
+ return new AnyType[]{ row._id, row._name };
}
};
protected TableDefinition<Member> _getTableDefinition() {
return TABLE_DEFINITION;
}
- // for query
public final static LongType.Predicate<Member> id = new LongType.Predicate<Member>().init(TABLE_DEFINITION, "id");
- public final static StringType.Predicate<Member> name = new StringType.Predicate<Member>().init(TABLE_DEFINITION, "name");
-
- // for insert,update
- LongType _id = new LongType().init(0L);
- StringType _name = new StringType().init("");
+ private LongType _id = new LongType().init(0L);
public Member setId(long id) {
_id.set(id);
return this;
}
public long getId() {
return _id.get();
}
+
+ public final static StringType.Predicate<Member> name = new StringType.Predicate<Member>().init(TABLE_DEFINITION, "name");
+ private StringType _name = new StringType().init("");
public Member setName(String name) {
_name.set(name);
return this;
}
public String getName() {
return _name.get();
}
-
}
@@ -1,55 +1,44 @@
package net.examp1e.picoorm.types;
import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
import net.examp1e.picoorm.*;
-public abstract class AbstractType<ThisType, ValueType> {
+public abstract class AnyType {
+
+ public abstract boolean isSet();
- public static interface Binder<T> {
- void bind(PreparedStatement ps, int parameterIndex, T value) throws SQLException;
- }
+ public abstract void bind(PreparedStatement ps, int parameterIndex) throws SQLException;
- ValueType value;
- boolean isSet;
+ public abstract void unbind(ResultSet rs, int parameterIndex) throws SQLException;
- public boolean isSet() {
- return isSet;
- }
+}
- public ValueType get() {
- return value;
- }
+abstract class AnyTypeImpl<ThisType, ValueType> extends AnyType {
- public void set(ValueType value) {
- this.value = value;
- this.isSet = true;
- }
+ boolean isSet = false;
+ ValueType value;
@SuppressWarnings("unchecked")
public ThisType init(ValueType defaultValue) {
this.value = defaultValue;
return (ThisType)this;
}
- public int bindTo(PreparedStatement ps, int parameterIndex) throws SQLException {
- if (isSet())
- getBinder().bind(ps, parameterIndex, value);
- return parameterIndex;
+ @Override
+ public boolean isSet() {
+ return isSet;
}
- protected abstract Binder<ValueType> getBinder();
+ public ValueType get() {
+ return value;
+ }
- static abstract class Parameter<ValueType> implements Condition.Parameter {
- ValueType value;
- Parameter(ValueType value) {
- this.value = value;
- }
- public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException {
- getBinder().bind(ps, parameterIndex, value);
- }
- protected abstract Binder<ValueType> getBinder();
+ public void set(ValueType value) {
+ this.value = value;
+ this.isSet = true;
}
public static abstract class Predicate<ThisType, Row extends AbstractRow, ValueType> extends net.examp1e.picoorm.Predicate<Row> {
@@ -79,7 +68,7 @@ public ThisType init(TableDefinition<Row> tableDefinition, String fieldName) {
protected Condition<Row> _buildBinaryOp(String op, ValueType value) {
return new Condition<Row>(this.tableDefinition, this.fieldName + op + "?", createParameter(value));
}
- protected abstract Parameter<ValueType> createParameter(ValueType x);
+ protected abstract AnyType createParameter(ValueType x);
}
}
Oops, something went wrong.

0 comments on commit 0e479d3

Please sign in to comment.