Skip to content

Commit e858fba

Browse files
committed
A much more natural way of doing this
1 parent 43ef1d2 commit e858fba

File tree

3 files changed

+12
-44
lines changed

3 files changed

+12
-44
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@ Usage
3131
.orderBy("pub_year", true)
3232
.create();
3333

34-
// has to be LinkedHashMap to preserve order
35-
LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
36-
order.put("status", true);
37-
order.put("author_id", false);
3834
String query3 = new SQLQuery()
3935
.select('*')
4036
.from("manuscript")
4137
.groupBy("author_id")
42-
.orderBy(order)
38+
.orderBy("author_id", false)
39+
.orderBy("status", true)
4340
.create();
4441
4542
String insert1 = new SQLInsertion()

src/com/tpwang/sql/SQLQuery.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.tpwang.sql;
22

3-
import java.util.LinkedHashMap;
43
import java.util.StringJoiner;
54

65
public class SQLQuery {
76

8-
protected StringBuilder builder;
7+
protected StringBuilder builder = null;
8+
protected StringJoiner orderJoiner = null; // ordering
99

1010
/***
1111
* Constructor
@@ -106,7 +106,9 @@ public SQLQuery where(SQLCondition condition) {
106106
* @return query
107107
*/
108108
public SQLQuery orderBy(String attributeName, boolean ascending) {
109-
builder.append("ORDER BY ").append(attributeName).append(ascending ? " " : " DESC ");
109+
if (orderJoiner == null)
110+
orderJoiner = new StringJoiner(", ");
111+
orderJoiner.add(attributeName + (ascending ? "" : " DESC"));
110112
return this;
111113
}
112114

@@ -117,21 +119,9 @@ public SQLQuery orderBy(String attributeName, boolean ascending) {
117119
* @return query
118120
*/
119121
public SQLQuery orderBy(char attributeName, boolean ascending) {
120-
builder.append("ORDER BY ").append(attributeName).append(ascending ? " " : " DESC ");
121-
return this;
122-
}
123-
124-
/***
125-
* How to order
126-
* @param attributes Selection order criteria
127-
* @return query
128-
*/
129-
public SQLQuery orderBy(LinkedHashMap<String, Boolean> attributes) {
130-
StringJoiner orderJoiner = new StringJoiner(", ");
131-
for (String key : attributes.keySet()) {
132-
orderJoiner.add(key + (attributes.get(key) ? "" : " DESC"));
133-
}
134-
builder.append("ORDER BY ").append(orderJoiner.toString()).append(' ');
122+
if (orderJoiner == null)
123+
orderJoiner = new StringJoiner(", ");
124+
orderJoiner.add(attributeName + (ascending ? "" : " DESC"));;
135125
return this;
136126
}
137127

@@ -177,7 +167,7 @@ public String create() {
177167
*/
178168
@Override
179169
public String toString() {
180-
return builder.toString().trim();
170+
return builder.append(orderJoiner != null ? "ORDER BY " : "").append(orderJoiner != null ? orderJoiner.toString() : "").toString().trim();
181171
}
182172
}
183173

src/com/tpwang/sql/SQLSubQuery.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.tpwang.sql;
22

3-
import java.util.LinkedHashMap;
43
import java.util.StringJoiner;
54

65
public class SQLSubquery extends SQLQuery {
@@ -240,7 +239,6 @@ public SQLSubquery onAttribute(String tableName, String attributeName) {
240239
@Override
241240
public SQLSubquery orderBy(String attributeName, boolean ascending) {
242241
super.orderBy(attributeName, ascending);
243-
joinBuilder.append("ORDER BY ").append(attributeName).append(ascending ? " " : " DESC ");
244242
return this;
245243
}
246244

@@ -253,23 +251,6 @@ public SQLSubquery orderBy(String attributeName, boolean ascending) {
253251
@Override
254252
public SQLSubquery orderBy(char attributeName, boolean ascending) {
255253
super.orderBy(attributeName, ascending);
256-
joinBuilder.append("ORDER BY ").append(attributeName).append(ascending ? " " : " DESC ");
257-
return this;
258-
}
259-
260-
/***
261-
* How to order
262-
* @param attributes Selection order criteria
263-
* @return subquery
264-
*/
265-
@Override
266-
public SQLSubquery orderBy(LinkedHashMap<String, Boolean> attributes) {
267-
super.orderBy(attributes);
268-
StringJoiner orderJoiner = new StringJoiner(", ");
269-
for (String key : attributes.keySet()) {
270-
orderJoiner.add(key + (attributes.get(key) ? "" : " DESC"));
271-
}
272-
joinBuilder.append("ORDER BY ").append(orderJoiner.toString()).append(' ');
273254
return this;
274255
}
275256

@@ -347,7 +328,7 @@ public String create() {
347328
@Override
348329
public String toString() {
349330
return (isJoinStmt
350-
? joinBuilder.toString()
331+
? joinBuilder.append(super.orderJoiner != null ? "ORDER BY " : "").append(super.orderJoiner != null ? orderJoiner.toString() : "").toString()
351332
: super.builder.toString()).trim();
352333
}
353334

0 commit comments

Comments
 (0)