Permalink
Browse files

do not stringify query params

  • Loading branch information...
kazuho committed Jul 9, 2014
1 parent 1f26521 commit 0d42fd7631de1b0ebdf2e303346e7074414193d6
@@ -1,13 +1,19 @@
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> {
+
+ static interface Parameter {
+ public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException;
+ }
+
final TableDefinition<Row> tableDefinition;
String term;
- ArrayList<String> params = new ArrayList<String>();
+ ArrayList<Parameter> params = new ArrayList<Parameter>();
ArrayList<String> orderBy = new ArrayList<String>();
long limitOffset = 0;
long limitCount = -1;
@@ -1,17 +1,35 @@
package net.examp1e.picoorm;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
public class LongPredicate<Row extends AbstractRow> extends Predicate<Row> {
+
+ static class Parameter implements Condition.Parameter {
+ long value;
+ Parameter(long value) {
+ this.value = value;
+ }
+ public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException {
+ ps.setLong(parameterIndex, value);
+ }
+ }
+
public LongPredicate(TableDefinition<Row> tableDefinition, String fieldName) {
super(tableDefinition, fieldName);
}
+
public Condition<Row> is(long x) {
- Condition<Row> c = new Condition<Row>(this.tableDefinition, this.fieldName + "=?");
- c.params.add(Long.toString(x));
- return c;
+ return _buildBinaryOp("=", x);
}
+
public Condition<Row> lessThan(long x) {
- Condition<Row> c = new Condition<Row>(this.tableDefinition, this.fieldName + "<?");
- c.params.add(Long.toString(x));
+ return _buildBinaryOp("<", x);
+ }
+
+ private Condition<Row> _buildBinaryOp(String op, long value) {
+ Condition<Row> c = new Condition<Row>(this.tableDefinition, this.fieldName + op + "?");
+ c.params.add(new Parameter(value));
return c;
}
}
@@ -1,12 +1,27 @@
package net.examp1e.picoorm;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
public class StringPredicate<Row extends AbstractRow> extends Predicate<Row> {
+
+ static class Parameter implements Condition.Parameter {
+ String value;
+ Parameter(String value) {
+ this.value = value;
+ }
+ public void bindTo(PreparedStatement ps, int parameterIndex) throws SQLException {
+ ps.setString(parameterIndex, value);
+ }
+ }
+
public StringPredicate(TableDefinition<Row> tableDefinition, String fieldName) {
super(tableDefinition, fieldName);
}
+
public Condition<Row> is(String x) {
Condition<Row> c = new Condition<Row>(this.tableDefinition, this.fieldName + "=?");
- c.params.add(x);
+ c.params.add(new Parameter(x));
return c;
}
}
@@ -75,8 +75,8 @@ private String _buildWhere(Condition<Row> cond) {
}
private int _bindWhere(Condition<Row> cond, PreparedStatement ps, int index) throws SQLException {
- for (String value : cond.params) {
- ps.setString(index++, value);
+ for (Condition.Parameter p : cond.params) {
+ p.bindTo(ps, index++);
}
return index;
}

0 comments on commit 0d42fd7

Please sign in to comment.