-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathQueryBuilder.java
More file actions
128 lines (100 loc) · 2.97 KB
/
Copy pathQueryBuilder.java
File metadata and controls
128 lines (100 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package designpatterns;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class QueryBuilder {
public static void main(String[] args) {
String query = new QueryBuilderUtil().select("select").column(new String[] {"age","name"}).from("Employee").where("age",30).where("name", "XYZ").build();
System.out.println(query);
}
}
//select * from table_name
class Query {
String select;
String[] column;
String from;
LinkedHashMap<String,Object> where = new LinkedHashMap<>();
public void setSelect(String select) {
this.select = select;
}
public void setColumn(String[] column) {
this.column = Arrays.copyOf(column, column.length);
}
public void setTable(String from) {
this.from = from;
}
public void setTWhere(String key,Object value) {
this.where.put(key, value);
}
public String getSelect() {
return this.select;
}
public String[] getColumn() {
return this.column;
}
public String getFrom() {
return this.from;
}
public LinkedHashMap<String,Object> getWhere() {
return this.where;
}
}
class QueryBuilderUtil {
private Query query;
public QueryBuilderUtil() {
this.query = new Query();
}
public QueryBuilderUtil select(String select) {
this.query.select = select;
return this;
}
public QueryBuilderUtil column(String[] columns) {
this.query.column = Arrays.copyOf(columns, columns.length);
return this;
}
public QueryBuilderUtil from(String from) {
this.query.from = from;
return this;
}
public QueryBuilderUtil where(String key,Object value) {
this.query.where.put(key, value);
return this;
}
public String build() {
StringBuilder sb = new StringBuilder();
if(this.query.getSelect()!= null && !this.query.getSelect().isEmpty() && this.query.getSelect().equalsIgnoreCase("SELECT")) {
sb.append(this.query.getSelect()+" ");
}else {
throw new RuntimeException("Query should contain select statement");
}
if(this.query.getColumn() != null && this.query.getColumn().length >=1 ) {
int len = this.query.getColumn().length;
if(len == 1 && this.query.getColumn()[0].equals("*")) {
sb.append(this.query.getColumn()[0]+" ");
}else {
sb.append(String.join(",", this.query.getColumn())).append(" ");
}
}
if(this.query.getFrom() != null && !this.query.getFrom().isEmpty()) {
sb.append("from"+" "+this.query.getFrom()+" ");
}
if(!this.query.where.isEmpty()) {
sb.append("where"+" ");
int count = 0;
for(Map.Entry<String,Object> entry :this.query.where.entrySet()) {
if(count > 0) {
sb.append("AND"+" ");
}
sb.append(entry.getKey()).append("=");
if(entry.getValue() instanceof String) {
sb.append("'").append(entry.getValue()).append("'");
}else {
sb.append(entry.getValue()).append(" ");
}
count++;
}
}
return sb.toString();
}
}