View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.jdbc;
17  
18  import static org.apache.ibatis.jdbc.SelectBuilder.FROM;
19  import static org.apache.ibatis.jdbc.SelectBuilder.GROUP_BY;
20  import static org.apache.ibatis.jdbc.SelectBuilder.HAVING;
21  import static org.apache.ibatis.jdbc.SelectBuilder.INNER_JOIN;
22  import static org.apache.ibatis.jdbc.SelectBuilder.OR;
23  import static org.apache.ibatis.jdbc.SelectBuilder.ORDER_BY;
24  import static org.apache.ibatis.jdbc.SelectBuilder.SELECT;
25  import static org.apache.ibatis.jdbc.SelectBuilder.SQL;
26  import static org.apache.ibatis.jdbc.SelectBuilder.WHERE;
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  
29  import org.junit.jupiter.api.Test;
30  
31  class SelectBuilderTest {
32  
33    @Test
34    void shouldProduceExpectedSimpleSelectStatement() {
35      // @formatter:off
36      String expected =
37          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n"
38              + "FROM PERSON P\n"
39              + "WHERE (P.ID like #id# AND P.FIRST_NAME like #firstName# AND P.LAST_NAME like #lastName#)\n"
40              + "ORDER BY P.LAST_NAME";
41      // @formatter:on
42      assertEquals(expected, example2("a", "b", "c"));
43    }
44  
45    @Test
46    void shouldProduceExpectedSimpleSelectStatementMissingFirstParam() {
47      // @formatter:off
48      String expected =
49          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n"
50              + "FROM PERSON P\n"
51              + "WHERE (P.FIRST_NAME like #firstName# AND P.LAST_NAME like #lastName#)\n"
52              + "ORDER BY P.LAST_NAME";
53      // @formatter:on
54      assertEquals(expected, example2(null, "b", "c"));
55    }
56  
57    @Test
58    void shouldProduceExpectedSimpleSelectStatementMissingFirstTwoParams() {
59      // @formatter:off
60      String expected =
61          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n"
62              + "FROM PERSON P\n"
63              + "WHERE (P.LAST_NAME like #lastName#)\n"
64              + "ORDER BY P.LAST_NAME";
65      // @formatter:on
66      assertEquals(expected, example2(null, null, "c"));
67    }
68  
69    @Test
70    void shouldProduceExpectedSimpleSelectStatementMissingAllParams() {
71      // @formatter:off
72      String expected =
73          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n"
74              + "FROM PERSON P\n"
75              + "ORDER BY P.LAST_NAME";
76      // @formatter:on
77      assertEquals(expected, example2(null, null, null));
78    }
79  
80    @Test
81    void shouldProduceExpectedComplexSelectStatement() {
82      // @formatter:off
83      String expected =
84          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME, P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON\n"
85              + "FROM PERSON P, ACCOUNT A\n"
86              + "INNER JOIN DEPARTMENT D on D.ID = P.DEPARTMENT_ID\n"
87              + "INNER JOIN COMPANY C on D.COMPANY_ID = C.ID\n"
88              + "WHERE (P.ID = A.ID AND P.FIRST_NAME like ?) \n"
89              + "OR (P.LAST_NAME like ?)\n"
90              + "GROUP BY P.ID\n"
91              + "HAVING (P.LAST_NAME like ?) \n"
92              + "OR (P.FIRST_NAME like ?)\n"
93              + "ORDER BY P.ID, P.FULL_NAME";
94      // @formatter:on
95      assertEquals(expected, example1());
96    }
97  
98    private static String example1() {
99      SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");
100     SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");
101     FROM("PERSON P");
102     FROM("ACCOUNT A");
103     INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID");
104     INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID");
105     WHERE("P.ID = A.ID");
106     WHERE("P.FIRST_NAME like ?");
107     OR();
108     WHERE("P.LAST_NAME like ?");
109     GROUP_BY("P.ID");
110     HAVING("P.LAST_NAME like ?");
111     OR();
112     HAVING("P.FIRST_NAME like ?");
113     ORDER_BY("P.ID");
114     ORDER_BY("P.FULL_NAME");
115     return SQL();
116   }
117 
118   private static String example2(String id, String firstName, String lastName) {
119     SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME");
120     FROM("PERSON P");
121     if (id != null) {
122       WHERE("P.ID like #id#");
123     }
124     if (firstName != null) {
125       WHERE("P.FIRST_NAME like #firstName#");
126     }
127     if (lastName != null) {
128       WHERE("P.LAST_NAME like #lastName#");
129     }
130     ORDER_BY("P.LAST_NAME");
131     return SQL();
132   }
133 
134 }