View Javadoc
1   /**
2    *    Copyright 2009-2015 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    *       http://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.*;
19  import static org.junit.Assert.assertEquals;
20  import org.junit.Test;
21  
22  public class SelectBuilderTest {
23  
24    @Test
25    public void shouldProduceExpectedSimpleSelectStatement() {
26      String expected =
27          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n" +
28              "FROM PERSON P\n" +
29              "WHERE (P.ID like #id# AND P.FIRST_NAME like #firstName# AND P.LAST_NAME like #lastName#)\n" +
30              "ORDER BY P.LAST_NAME";
31      assertEquals(expected, example2("a", "b", "c"));
32    }
33  
34    @Test
35    public void shouldProduceExpectedSimpleSelectStatementMissingFirstParam() {
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.FIRST_NAME like #firstName# AND P.LAST_NAME like #lastName#)\n" +
40              "ORDER BY P.LAST_NAME";
41      assertEquals(expected, example2(null, "b", "c"));
42    }
43  
44    @Test
45    public void shouldProduceExpectedSimpleSelectStatementMissingFirstTwoParams() {
46      String expected =
47          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n" +
48              "FROM PERSON P\n" +
49              "WHERE (P.LAST_NAME like #lastName#)\n" +
50              "ORDER BY P.LAST_NAME";
51      assertEquals(expected, example2(null, null, "c"));
52    }
53  
54    @Test
55    public void shouldProduceExpectedSimpleSelectStatementMissingAllParams() {
56      String expected =
57          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n" +
58              "FROM PERSON P\n" +
59              "ORDER BY P.LAST_NAME";
60      assertEquals(expected, example2(null, null, null));
61    }
62  
63    @Test
64    public void shouldProduceExpectedComplexSelectStatement() {
65      String expected =
66          "SELECT P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME, P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON\n" +
67              "FROM PERSON P, ACCOUNT A\n" +
68              "INNER JOIN DEPARTMENT D on D.ID = P.DEPARTMENT_ID\n" +
69              "INNER JOIN COMPANY C on D.COMPANY_ID = C.ID\n" +
70              "WHERE (P.ID = A.ID AND P.FIRST_NAME like ?) \n" +
71              "OR (P.LAST_NAME like ?)\n" +
72              "GROUP BY P.ID\n" +
73              "HAVING (P.LAST_NAME like ?) \n" +
74              "OR (P.FIRST_NAME like ?)\n" +
75              "ORDER BY P.ID, P.FULL_NAME";
76      assertEquals(expected, example1());
77    }
78  
79    private static String example1() {
80      SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");
81      SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");
82      FROM("PERSON P");
83      FROM("ACCOUNT A");
84      INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID");
85      INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID");
86      WHERE("P.ID = A.ID");
87      WHERE("P.FIRST_NAME like ?");
88      OR();
89      WHERE("P.LAST_NAME like ?");
90      GROUP_BY("P.ID");
91      HAVING("P.LAST_NAME like ?");
92      OR();
93      HAVING("P.FIRST_NAME like ?");
94      ORDER_BY("P.ID");
95      ORDER_BY("P.FULL_NAME");
96      return SQL();
97    }
98  
99    private static String example2(String id, String firstName, String lastName) {
100     SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME");
101     FROM("PERSON P");
102     if (id != null) {
103       WHERE("P.ID like #id#");
104     }
105     if (firstName != null) {
106       WHERE("P.FIRST_NAME like #firstName#");
107     }
108     if (lastName != null) {
109       WHERE("P.LAST_NAME like #lastName#");
110     }
111     ORDER_BY("P.LAST_NAME");
112     return SQL();
113   }
114 
115 
116 }