Skip to content

Commit 95b4335

Browse files
committed
Stub out QueryBuilder class and tests.
1 parent d2d1fbf commit 95b4335

File tree

3 files changed

+276
-1
lines changed

3 files changed

+276
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '7.4.3'
17+
version = '7.5'
1818

1919
apply plugin: 'java-library'
2020
apply plugin: 'maven-publish'
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc.sql;
16+
17+
/**
18+
* Class for dynamically constructing a SQL query.
19+
*/
20+
public class QueryBuilder {
21+
private StringBuilder sqlBuilder = new StringBuilder();
22+
23+
private QueryBuilder(String operation) {
24+
sqlBuilder.append(operation);
25+
}
26+
27+
/**
28+
* TODO
29+
* @param columns
30+
* @return
31+
*/
32+
public static QueryBuilder select(String... columns) {
33+
if (columns == null) {
34+
throw new IllegalArgumentException();
35+
}
36+
37+
return new QueryBuilder("select " + String.join(", ", columns));
38+
}
39+
40+
/**
41+
* TODO
42+
* @param table
43+
* @param columns
44+
* @return
45+
*/
46+
public static QueryBuilder insertInto(String table, String... columns) {
47+
if (table == null) {
48+
throw new IllegalArgumentException();
49+
}
50+
51+
if (columns == null) {
52+
throw new IllegalArgumentException();
53+
}
54+
55+
return new QueryBuilder("insert into " + table + " (" + String.join(", ", columns) + ")");
56+
}
57+
58+
/**
59+
* TODO
60+
* @param table
61+
* @return
62+
*/
63+
public static QueryBuilder update(String table) {
64+
if (table == null) {
65+
throw new IllegalArgumentException();
66+
}
67+
68+
return new QueryBuilder("update " + table);
69+
}
70+
71+
/**
72+
* TODO
73+
* @param table
74+
* @return
75+
*/
76+
public static QueryBuilder deleteFrom(String table) {
77+
if (table == null) {
78+
throw new IllegalArgumentException();
79+
}
80+
81+
return new QueryBuilder("delete from " + table);
82+
}
83+
84+
/**
85+
* TODO
86+
* @param tables
87+
* @return
88+
*/
89+
public QueryBuilder from(String... tables) {
90+
if (tables == null) {
91+
throw new IllegalArgumentException();
92+
}
93+
94+
sqlBuilder.append(" from ");
95+
sqlBuilder.append(String.join(", ", tables));
96+
97+
return this;
98+
}
99+
100+
/**
101+
* TODO
102+
* @param table
103+
* @return
104+
*/
105+
public QueryBuilder join(String table) {
106+
if (table == null) {
107+
throw new IllegalArgumentException();
108+
}
109+
110+
sqlBuilder.append(" join ");
111+
sqlBuilder.append(table);
112+
113+
return this;
114+
}
115+
116+
/**
117+
* TODO
118+
* @param predicate
119+
* @return
120+
*/
121+
public QueryBuilder on(String predicate) {
122+
if (predicate == null) {
123+
throw new IllegalArgumentException();
124+
}
125+
126+
sqlBuilder.append(" on ");
127+
sqlBuilder.append(predicate);
128+
129+
return this;
130+
}
131+
132+
/**
133+
* TODO
134+
* @param predicate
135+
* @return
136+
*/
137+
public QueryBuilder where(String predicate) {
138+
if (predicate == null) {
139+
throw new IllegalArgumentException();
140+
}
141+
142+
sqlBuilder.append(" where ");
143+
sqlBuilder.append(predicate);
144+
145+
return this;
146+
}
147+
148+
/**
149+
* TODO
150+
* @param columns
151+
* @return
152+
*/
153+
public QueryBuilder orderBy(String... columns) {
154+
if (columns == null) {
155+
throw new IllegalArgumentException();
156+
}
157+
158+
sqlBuilder.append(" order by ");
159+
sqlBuilder.append(String.join(", ", columns));
160+
161+
return this;
162+
}
163+
164+
/**
165+
* TODO
166+
* @param values
167+
* @return
168+
*/
169+
public QueryBuilder values(Object... values) {
170+
if (values == null) {
171+
throw new IllegalArgumentException();
172+
}
173+
174+
sqlBuilder.append(" values (");
175+
176+
for (int i = 0; i < values.length; i++) {
177+
if (i > 0) {
178+
sqlBuilder.append(", ");
179+
}
180+
181+
sqlBuilder.append(values[i]);
182+
}
183+
184+
sqlBuilder.append(")");
185+
186+
return this;
187+
}
188+
189+
/**
190+
* TODO
191+
* @param column
192+
* @param value
193+
* @return
194+
*/
195+
public QueryBuilder set(String column, Object value) {
196+
sqlBuilder.append(" set ");
197+
sqlBuilder.append(column);
198+
sqlBuilder.append(" = ");
199+
sqlBuilder.append(value);
200+
201+
return this;
202+
}
203+
204+
@Override
205+
public String toString() {
206+
return sqlBuilder.toString();
207+
}
208+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc.sql;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
public class QueryBuilderTest {
22+
@Test
23+
public void testSelect() {
24+
String sql = QueryBuilder.select("a", "b", "c")
25+
.from("A")
26+
.join("B").on("A.id = B.id and x = 50")
27+
.join("C").on("B.id = C.id")
28+
.where("(a > 10 or b < 200) and c = :c")
29+
.orderBy("a", "b").toString();
30+
31+
assertEquals("select a, b, c from A "
32+
+ "join B on A.id = B.id and x = 50 "
33+
+ "join C on B.id = C.id "
34+
+ "where (a > 10 or b < 200) and c = :c "
35+
+ "order by a, b", sql);
36+
}
37+
38+
@Test
39+
public void testInsertInto() {
40+
String sql = QueryBuilder.insertInto("A", "a", "b", "c")
41+
.values(1, true, "hello")
42+
.toString();
43+
44+
System.out.println(sql);
45+
}
46+
47+
@Test
48+
public void testUpdate() {
49+
String sql = QueryBuilder.update("A")
50+
.set("a", 1)
51+
.set("b", true)
52+
.set("c", "hello")
53+
.where("a is not null")
54+
.toString();
55+
56+
System.out.println(sql);
57+
}
58+
59+
@Test
60+
public void testDelete() {
61+
String sql = QueryBuilder.deleteFrom("A")
62+
.where("a < 150")
63+
.toString();
64+
65+
assertEquals("delete from A where a < 150", sql);
66+
}
67+
}

0 commit comments

Comments
 (0)