Browse files

do not stringify query params

  • Loading branch information...
1 parent 1f26521 commit 24849eb97966b2569e43342c8256224f8cfc00d2 @kazuho committed Jul 9, 2014
View
8 net/examp1e/picoorm/Condition.java
@@ -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;
View
28 net/examp1e/picoorm/LongPredicate.java
@@ -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;
}
}
View
17 net/examp1e/picoorm/StringPredicate.java
@@ -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;
}
}
View
6 net/examp1e/picoorm/TableDefinition.java
@@ -7,6 +7,8 @@
import java.sql.Types;
import java.util.ArrayList;
+import net.examp1e.picoorm.LongPredicate.Parameter;
+
public abstract class TableDefinition<Row extends AbstractRow> {
public abstract String getTableName();
@@ -75,8 +77,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 24849eb

Please sign in to comment.