1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.jdbc;
17
18 import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
19 import static org.apache.ibatis.jdbc.SqlBuilder.GROUP_BY;
20 import static org.apache.ibatis.jdbc.SqlBuilder.HAVING;
21 import static org.apache.ibatis.jdbc.SqlBuilder.INNER_JOIN;
22 import static org.apache.ibatis.jdbc.SqlBuilder.OR;
23 import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
24 import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
25 import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
26 import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28
29 import org.junit.jupiter.api.Test;
30
31 class SqlBuilderTest {
32
33 @Test
34 void shouldProduceExpectedSimpleSelectStatement() {
35
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
42 assertEquals(expected, example2("a", "b", "c"));
43 }
44
45 @Test
46 void shouldProduceExpectedSimpleSelectStatementMissingFirstParam() {
47
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
54 assertEquals(expected, example2(null, "b", "c"));
55 }
56
57 @Test
58 void shouldProduceExpectedSimpleSelectStatementMissingFirstTwoParams() {
59
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
66 assertEquals(expected, example2(null, null, "c"));
67 }
68
69 @Test
70 void shouldProduceExpectedSimpleSelectStatementMissingAllParams() {
71
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
77 assertEquals(expected, example2(null, null, null));
78 }
79
80 @Test
81 void shouldProduceExpectedComplexSelectStatement() {
82
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
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 }