Skip to content

Commit 90e5563

Browse files
committed
Support the "allowEmptyWhere" setting for select statements
1 parent 29ba159 commit 90e5563

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/context/SelectSettings.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package org.seasar.doma.jdbc.criteria.context;
22

33
public class SelectSettings extends Settings {
4+
private boolean allowEmptyWhere = true;
45
private int fetchSize = 0;
56
private int maxRows = 0;
67

8+
public boolean getAllowEmptyWhere() {
9+
return allowEmptyWhere;
10+
}
11+
12+
public void setAllowEmptyWhere(boolean allowEmptyWhere) {
13+
this.allowEmptyWhere = allowEmptyWhere;
14+
}
15+
716
public int getFetchSize() {
817
return fetchSize;
918
}

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/context/UpdateSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.seasar.doma.jdbc.criteria.context;
22

33
public class UpdateSettings extends Settings {
4-
int batchSize = 0;
5-
boolean allowEmptyWhere;
4+
private int batchSize = 0;
5+
private boolean allowEmptyWhere;
66

77
public int getBatchSize() {
88
return batchSize;

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/statement/EntityqlSelectStatement.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ protected Command<List<ENTITY>> createCommand() {
128128
query.setFetchSize(settings.getFetchSize());
129129
query.setMaxRows(settings.getMaxRows());
130130
query.setQueryTimeout(settings.getQueryTimeout());
131-
return new AssociateCommand<>(context, query);
131+
return new AssociateCommand<ENTITY>(context, query) {
132+
@Override
133+
public List<ENTITY> execute() {
134+
if (!settings.getAllowEmptyWhere()) {
135+
if (context.where.isEmpty()) {
136+
throw new EmptyWhereClauseException(sql);
137+
}
138+
}
139+
return super.execute();
140+
}
141+
};
132142
}
133143
}

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/statement/NativeSqlSelectTerminal.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ protected Command<RESULT> createCommand() {
3737
query.setFetchSize(settings.getQueryTimeout());
3838
query.setMaxRows(settings.getMaxRows());
3939
query.setQueryTimeout(settings.getQueryTimeout());
40-
return new SelectCommand<>(query, resultSetHandler);
40+
return new SelectCommand<RESULT>(query, resultSetHandler) {
41+
@Override
42+
public RESULT execute() {
43+
if (!settings.getAllowEmptyWhere()) {
44+
if (context.where.isEmpty()) {
45+
throw new EmptyWhereClauseException(sql);
46+
}
47+
}
48+
return super.execute();
49+
}
50+
};
4151
}
4252
}

test-criteria/src/test/java/example/EntityqlSelectTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66
import static org.junit.jupiter.api.Assertions.assertNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
89

910
import java.util.Arrays;
@@ -16,6 +17,7 @@
1617
import org.seasar.doma.jdbc.SqlLogType;
1718
import org.seasar.doma.jdbc.criteria.Entityql;
1819
import org.seasar.doma.jdbc.criteria.option.AssociationOption;
20+
import org.seasar.doma.jdbc.criteria.statement.EmptyWhereClauseException;
1921
import org.seasar.doma.jdbc.criteria.statement.Listable;
2022

2123
@ExtendWith(Env.class)
@@ -39,13 +41,23 @@ void settings() {
3941
settings.setComment("all employees");
4042
settings.setSqlLogType(SqlLogType.RAW);
4143
settings.setQueryTimeout(1000);
44+
settings.setAllowEmptyWhere(true);
4245
settings.setFetchSize(100);
4346
settings.setMaxRows(100);
4447
})
4548
.fetch();
4649
assertEquals(14, list.size());
4750
}
4851

52+
@Test
53+
void allowEmptyWhere_disabled() {
54+
Employee_ e = new Employee_();
55+
56+
assertThrows(
57+
EmptyWhereClauseException.class,
58+
() -> entityql.from(e, settings -> settings.setAllowEmptyWhere(false)).fetch());
59+
}
60+
4961
@Test
5062
void fetch() {
5163
Employee_ e = new Employee_();
@@ -262,9 +274,6 @@ void associate() {
262274

263275
@Test
264276
void associate_dynamic() {
265-
Employee_ e = new Employee_();
266-
Department_ d = new Department_();
267-
268277
List<Employee> list = associate_dynamic(true);
269278
assertEquals(14, list.size());
270279
assertTrue(list.stream().allMatch(emp -> emp.getDepartment() != null));

test-criteria/src/test/java/example/NativeSqlSelectTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.jupiter.api.Assertions.assertFalse;
77
import static org.junit.jupiter.api.Assertions.assertNotNull;
88
import static org.junit.jupiter.api.Assertions.assertNull;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
910
import static org.junit.jupiter.api.Assertions.assertTrue;
1011
import static org.seasar.doma.jdbc.criteria.AggregateFunctions.count;
1112
import static org.seasar.doma.jdbc.criteria.AggregateFunctions.min;
@@ -18,8 +19,10 @@
1819
import org.junit.jupiter.api.Test;
1920
import org.junit.jupiter.api.extension.ExtendWith;
2021
import org.seasar.doma.jdbc.Config;
22+
import org.seasar.doma.jdbc.SqlLogType;
2123
import org.seasar.doma.jdbc.criteria.NativeSql;
2224
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel;
25+
import org.seasar.doma.jdbc.criteria.statement.EmptyWhereClauseException;
2326
import org.seasar.doma.jdbc.criteria.tuple.Row;
2427
import org.seasar.doma.jdbc.criteria.tuple.Tuple2;
2528

@@ -32,6 +35,35 @@ public NativeSqlSelectTest(Config config) {
3235
this.nativeSql = new NativeSql(config);
3336
}
3437

38+
@Test
39+
void settings() {
40+
Employee_ e = new Employee_();
41+
42+
List<Employee> list =
43+
nativeSql
44+
.from(
45+
e,
46+
settings -> {
47+
settings.setComment("all employees");
48+
settings.setSqlLogType(SqlLogType.RAW);
49+
settings.setQueryTimeout(1000);
50+
settings.setAllowEmptyWhere(true);
51+
settings.setFetchSize(100);
52+
settings.setMaxRows(100);
53+
})
54+
.fetch();
55+
assertEquals(14, list.size());
56+
}
57+
58+
@Test
59+
void fetch_allowEmptyWhere_disabled() {
60+
Employee_ e = new Employee_();
61+
62+
assertThrows(
63+
EmptyWhereClauseException.class,
64+
() -> nativeSql.from(e, settings -> settings.setAllowEmptyWhere(false)).fetch());
65+
}
66+
3567
@Test
3668
void from() {
3769
Employee_ e = new Employee_();

0 commit comments

Comments
 (0)