77
88import com .github .jonathanhds .sqlbuilder .builder .QueryBuilderHSQLDB ;
99import com .github .jonathanhds .sqlbuilder .builder .QueryBuilderOracle ;
10+ import com .github .jonathanhds .sqlbuilder .IllegalQueryException ;
1011import com .github .jonathanhds .sqlbuilder .support .*;
1112import java .sql .Connection ;
1213import 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 *\n FROM\n PERSON p\n \n \n WHERE 1 = 1\n AND 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}
0 commit comments