Skip to content

Commit 94566be

Browse files
authored
Merge pull request #294 from domaframework/support-array
Support Array for bind variable directives and literal variable direc…
2 parents 4daa0c9 + 9d3fe54 commit 94566be

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/main/java/org/seasar/doma/internal/jdbc/sql/NodePreparedSqlBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,15 @@ protected Void visitValueNode(ValueNode node, Context p, Consumer<Scalar<?, ?>>
253253
openedFragmentNode.accept(this, p);
254254
if (Iterable.class.isAssignableFrom(valueClass)) {
255255
handleIterableValueNode(node, p, (Iterable<?>) value, valueClass, valueHandler);
256+
} else if (valueClass.isArray()) {
257+
handleIterableValueNode(node, p, Arrays.asList((Object[]) value), valueClass, valueHandler);
256258
} else {
257259
throw new JdbcException(
258260
Message.DOMA2112,
259261
location.getSql(),
260262
location.getLineNumber(),
261263
location.getPosition(),
262-
node.getText(),
264+
node.getVariableName(),
263265
valueClass);
264266
}
265267
OtherNode closedFragmentNode = parensNode.getClosedFragmentNode();

src/main/java/org/seasar/doma/message/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public enum Message implements MessageResource {
115115
DOMA2111(
116116
"Failed to build the SQL on line [{1}] at column [{2}]. The cause is as follows: {3}. SQL[{0}]"),
117117
DOMA2112(
118-
"Failed to build the SQL on line [{1}] at column [{2}]. The object type[{4}] that corresponds to the bind variable comment or the literal variable comment[{3}] that is just before the parenthesis is not subtype of java.lang.Iterable. SQL[{0}]"),
118+
"Failed to build the SQL on line [{1}] at column [{2}]. The type of the variable [{3}] must be a subtype of either java.lang.Iterable or Array, but it is [{4}]. SQL[{0}]"),
119119
DOMA2115(
120120
"Failed to build the SQL on line [{1}] at column [{2}]. The [{4}]th element of java.lang.Iterable that corresponds to the bind variable comment or the literal variable comment[{3}] that is just before the parenthesis is null. SQL[{0}]"),
121121
DOMA2116(

src/test/java/org/seasar/doma/internal/jdbc/sql/SqlParserTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testBindVariable_domain() throws Exception {
5757
assertEquals("01-2345-6789", sql.getParameters().get(0).getWrapper().get());
5858
}
5959

60-
public void testBindVariable_in() throws Exception {
60+
public void testBindVariable_in_iterable() throws Exception {
6161
ExpressionEvaluator evaluator = new ExpressionEvaluator();
6262
evaluator.add("name", new Value(List.class, Arrays.asList("hoge", "foo")));
6363
String testSql = "select * from aaa where ename in /*name*/('aaa', 'bbb')";
@@ -74,6 +74,23 @@ public void testBindVariable_in() throws Exception {
7474
assertEquals("foo", sql.getParameters().get(1).getWrapper().get());
7575
}
7676

77+
public void testBindVariable_in_array() throws Exception {
78+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
79+
evaluator.add("name", new Value(String[].class, new String[] {"hoge", "foo"}));
80+
String testSql = "select * from aaa where ename in /*name*/('aaa', 'bbb')";
81+
SqlParser parser = new SqlParser(testSql);
82+
SqlNode sqlNode = parser.parse();
83+
PreparedSql sql =
84+
new NodePreparedSqlBuilder(
85+
config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED)
86+
.build(sqlNode, Function.identity());
87+
assertEquals("select * from aaa where ename in (?, ?)", sql.getRawSql());
88+
assertEquals("select * from aaa where ename in ('hoge', 'foo')", sql.getFormattedSql());
89+
assertEquals(2, sql.getParameters().size());
90+
assertEquals("hoge", sql.getParameters().get(0).getWrapper().get());
91+
assertEquals("foo", sql.getParameters().get(1).getWrapper().get());
92+
}
93+
7794
public void testBindVariable_in_empty_iterable() throws Exception {
7895
ExpressionEvaluator evaluator = new ExpressionEvaluator();
7996
evaluator.add("name", new Value(List.class, Collections.emptyList()));

0 commit comments

Comments
 (0)