Skip to content

Commit 62a9a2e

Browse files
authored
Null parameter (#10)
* Add person with no birthday * Allow null parameter values on conditions * Add tests for empty and blank parameters * Handle greater than and less than conditions
1 parent fd28076 commit 62a9a2e

3 files changed

Lines changed: 144 additions & 29 deletions

File tree

src/main/java/com/github/jonathanhds/sqlbuilder/select/Condition.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.jonathanhds.sqlbuilder.select;
22

33
import com.github.jonathanhds.sqlbuilder.Context;
4+
import com.github.jonathanhds.sqlbuilder.IllegalQueryException;
45
import org.apache.commons.lang.ArrayUtils;
56
import org.apache.commons.lang.StringUtils;
67

@@ -18,12 +19,12 @@ void add(Object condition) {
1819
void add(Object condition, Object parameter) {
1920
if (parameter != null) {
2021
add(condition, new Object[] { parameter });
21-
}
22-
}
23-
24-
void add(String condition, String parameter) {
25-
if (StringUtils.isNotBlank(parameter)) {
26-
add(condition, new Object[] { parameter });
22+
} else {
23+
if (isEqualCondition(condition.toString())) {
24+
add(extractColumnName(condition.toString()) + " IS NULL");
25+
} else {
26+
throw new IllegalQueryException("Could not solve '" + condition + "' condition with a null parameter");
27+
}
2728
}
2829
}
2930

@@ -48,5 +49,30 @@ void between(String columnName, Object start, Object end) {
4849
}
4950
}
5051

52+
private Boolean isEqualCondition(String condition) {
53+
return StringUtils.contains(condition, " =");
54+
}
55+
56+
private String extractColumnName(String condition) {
57+
String[] conditions = new String[] {
58+
" =",
59+
" >",
60+
" >=",
61+
" <",
62+
" <=",
63+
" <>",
64+
" IN",
65+
" IS",
66+
" BETWEEN"
67+
};
68+
String result = condition.toString();
69+
70+
for (String c : conditions) {
71+
result = StringUtils.substringBefore(result, c);
72+
}
73+
74+
return result;
75+
}
76+
5177
protected abstract String getPrefix();
5278
}

src/test/java/com/github/jonathanhds/sqlbuilder/select/SelectTest.java

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.github.jonathanhds.sqlbuilder.builder.QueryBuilderHSQLDB;
99
import com.github.jonathanhds.sqlbuilder.builder.QueryBuilderOracle;
10+
import com.github.jonathanhds.sqlbuilder.IllegalQueryException;
1011
import com.github.jonathanhds.sqlbuilder.support.*;
1112
import java.sql.Connection;
1213
import java.sql.SQLException;
@@ -46,13 +47,14 @@ public void selectAllFromTable() throws Exception {
4647
.table("PERSON p")
4748
.list(new PersonRowMapper());
4849

49-
assertThat(persons, hasSize(3));
50+
assertThat(persons, hasSize(4));
5051
assertThat(
5152
persons,
5253
containsInAnyOrder(
5354
jonathan().setCountry(null),
5455
steveJobs().setCountry(null),
55-
domPedro().setCountry(null)
56+
domPedro().setCountry(null),
57+
martinLutherKing().setCountry(null)
5658
)
5759
);
5860
}
@@ -93,13 +95,69 @@ public void selectAllFromTableWhereConditionWithParameter() throws Exception {
9395
.table("PERSON p")
9496
.where()
9597
.and("p.name = ?", "Steve Jobs")
96-
.and("p.birthday = ?", getNullValue())
9798
.list(new PersonRowMapper());
9899

99100
assertThat(persons, hasSize(1));
100101
assertThat(persons, containsInAnyOrder(steveJobs().setCountry(null)));
101102
}
102103

104+
@Test
105+
public void selectAllFromTableWhereConditionWithEmptyParameter()
106+
throws Exception {
107+
List<Person> persons = new QueryBuilderHSQLDB(connection)
108+
.select()
109+
.all()
110+
.from()
111+
.table("PERSON p")
112+
.where()
113+
.and("p.name = ?", "")
114+
.list(new PersonRowMapper());
115+
116+
assertThat(persons, hasSize(0));
117+
}
118+
119+
@Test
120+
public void selectAllFromTableWhereConditionWithBlankParameter()
121+
throws Exception {
122+
List<Person> persons = new QueryBuilderHSQLDB(connection)
123+
.select()
124+
.all()
125+
.from()
126+
.table("PERSON p")
127+
.where()
128+
.and("p.name = ?", " ")
129+
.list(new PersonRowMapper());
130+
131+
assertThat(persons, hasSize(0));
132+
}
133+
134+
@Test
135+
public void selectAllFromTableWhereEqualConditionParameterIsNull()
136+
throws Exception {
137+
String sql = new QueryBuilderHSQLDB(connection)
138+
.select()
139+
.all()
140+
.from()
141+
.table("PERSON p")
142+
.where()
143+
.and("p.birthday = ?", getNullValue())
144+
.toString();
145+
146+
assertThat(sql, equalTo("SELECT\n*\nFROM\nPERSON p\n\n\nWHERE 1 = 1\nAND p.birthday IS NULL\n"));
147+
}
148+
149+
@Test(expected = IllegalQueryException.class)
150+
public void selectAllFromTable_shouldThrowExceptionWhenConditionIsDifferentFromEqualAndParmeterIsNull() {
151+
new QueryBuilderHSQLDB(connection)
152+
.select()
153+
.all()
154+
.from()
155+
.table("PERSON p")
156+
.where()
157+
.and("p.birthday >= ?", getNullValue())
158+
.toString();
159+
}
160+
103161
@Test
104162
public void selectAllFromTableWhereBetween() throws Exception {
105163
List<Person> persons = new QueryBuilderHSQLDB(connection)
@@ -129,10 +187,15 @@ public void selectColumnsFromTableJoinAnotherTable() throws Exception {
129187
.innerJoin("COUNTRY c ON c.id = p.country_id")
130188
.list(new PersonRowMapper());
131189

132-
assertThat(persons, hasSize(3));
190+
assertThat(persons, hasSize(4));
133191
assertThat(
134192
persons,
135-
containsInAnyOrder(jonathan(), steveJobs(), domPedro())
193+
containsInAnyOrder(
194+
jonathan(),
195+
steveJobs(),
196+
domPedro(),
197+
martinLutherKing()
198+
)
136199
);
137200

138201
persons =
@@ -144,10 +207,15 @@ public void selectColumnsFromTableJoinAnotherTable() throws Exception {
144207
.innerJoin("COUNTRY c ON c.id = p.country_id")
145208
.list(new PersonRowMapper());
146209

147-
assertThat(persons, hasSize(3));
210+
assertThat(persons, hasSize(4));
148211
assertThat(
149212
persons,
150-
containsInAnyOrder(jonathan(), steveJobs(), domPedro())
213+
containsInAnyOrder(
214+
jonathan(),
215+
steveJobs(),
216+
domPedro(),
217+
martinLutherKing()
218+
)
151219
);
152220

153221
persons =
@@ -160,10 +228,15 @@ public void selectColumnsFromTableJoinAnotherTable() throws Exception {
160228
.where("c.id = p.country_id")
161229
.list(new PersonRowMapper());
162230

163-
assertThat(persons, hasSize(3));
231+
assertThat(persons, hasSize(4));
164232
assertThat(
165233
persons,
166-
containsInAnyOrder(jonathan(), steveJobs(), domPedro())
234+
containsInAnyOrder(
235+
jonathan(),
236+
steveJobs(),
237+
domPedro(),
238+
martinLutherKing()
239+
)
167240
);
168241

169242
persons =
@@ -175,10 +248,15 @@ public void selectColumnsFromTableJoinAnotherTable() throws Exception {
175248
.where("c.id = p.country_id")
176249
.list(new PersonRowMapper());
177250

178-
assertThat(persons, hasSize(3));
251+
assertThat(persons, hasSize(4));
179252
assertThat(
180253
persons,
181-
containsInAnyOrder(jonathan(), steveJobs(), domPedro())
254+
containsInAnyOrder(
255+
jonathan(),
256+
steveJobs(),
257+
domPedro(),
258+
martinLutherKing()
259+
)
182260
);
183261

184262
persons =
@@ -191,10 +269,15 @@ public void selectColumnsFromTableJoinAnotherTable() throws Exception {
191269
.where("c.id = p.country_id")
192270
.list(new PersonRowMapper());
193271

194-
assertThat(persons, hasSize(3));
272+
assertThat(persons, hasSize(4));
195273
assertThat(
196274
persons,
197-
containsInAnyOrder(jonathan(), steveJobs(), domPedro())
275+
containsInAnyOrder(
276+
jonathan(),
277+
steveJobs(),
278+
domPedro(),
279+
martinLutherKing()
280+
)
198281
);
199282
}
200283

@@ -209,11 +292,12 @@ public void selectAllFromTableOrderBy() throws Exception {
209292
.column("p.name", OrderByType.DESC)
210293
.list(new PersonRowMapper());
211294

212-
assertThat(persons, hasSize(3));
295+
assertThat(persons, hasSize(4));
213296
assertThat(
214297
persons,
215298
contains(
216299
steveJobs().setCountry(null),
300+
martinLutherKing().setCountry(null),
217301
jonathan().setCountry(null),
218302
domPedro().setCountry(null)
219303
)
@@ -256,8 +340,8 @@ public void selectAllFromTableGroupBy() throws Exception {
256340
.column("p.birthday")
257341
.list(new CountRowMapper());
258342

259-
assertThat(counts, hasSize(3));
260-
assertThat(counts, contains(1, 1, 1));
343+
assertThat(counts, hasSize(4));
344+
assertThat(counts, contains(1, 1, 1, 1));
261345

262346
counts =
263347
new QueryBuilderHSQLDB(connection)
@@ -268,8 +352,8 @@ public void selectAllFromTableGroupBy() throws Exception {
268352
.groupBy("p.birthday")
269353
.list(new CountRowMapper());
270354

271-
assertThat(counts, hasSize(3));
272-
assertThat(counts, contains(1, 1, 1));
355+
assertThat(counts, hasSize(4));
356+
assertThat(counts, contains(1, 1, 1, 1));
273357

274358
counts =
275359
new QueryBuilderHSQLDB(connection)
@@ -282,7 +366,7 @@ public void selectAllFromTableGroupBy() throws Exception {
282366
.list(new CountRowMapper());
283367

284368
assertThat(counts, hasSize(2));
285-
assertThat(counts, containsInAnyOrder(2, 1));
369+
assertThat(counts, containsInAnyOrder(2, 2));
286370
}
287371

288372
@Test
@@ -297,8 +381,8 @@ public void selectCountFromTableGroupByHaving() throws Exception {
297381
.having("count(c.country_name) > 1")
298382
.list(new CountRowMapper());
299383

300-
assertThat(counts, hasSize(1));
301-
assertThat(counts, contains(2));
384+
assertThat(counts, hasSize(2));
385+
assertThat(counts, contains(2, 2));
302386
}
303387

304388
@Test
@@ -314,8 +398,8 @@ public void selectColumnsFromTableGroupByHavingOrderBy() throws Exception {
314398
.orderBy("c.country_name", DESC)
315399
.list(new CountryRowMapper());
316400

317-
assertThat(countries, hasSize(1));
318-
assertThat(countries, contains(brazil()));
401+
assertThat(countries, hasSize(2));
402+
assertThat(countries, contains(usa(), brazil()));
319403

320404
countries =
321405
new QueryBuilderHSQLDB(connection)
@@ -411,4 +495,8 @@ private Person steveJobs() {
411495
private Person jonathan() {
412496
return new Person("Jonathan", toDate(1988, 11, 8), brazil());
413497
}
498+
499+
private Person martinLutherKing() {
500+
return new Person("Martin Luther King", null, usa());
501+
}
414502
}

src/test/resources/seed.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ INSERT INTO country (id, country_name) VALUES (2, 'United States of America');
44
INSERT INTO person (id, name, birthday, country_id) VALUES (1, 'Jonathan', DATE'1988-11-08', 1);
55
INSERT INTO person (id, name, birthday, country_id) VALUES (2, 'Steve Jobs', DATE'1955-02-24', 2);
66
INSERT INTO person (id, name, birthday, country_id) VALUES (3, 'Dom Pedro II', DATE'1825-12-02', 1);
7+
INSERT INTO person (id, name, birthday, country_id) VALUES (4, 'Martin Luther King', null, 2);

0 commit comments

Comments
 (0)