-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSelect.java
More file actions
152 lines (112 loc) · 3.23 KB
/
Copy pathSelect.java
File metadata and controls
152 lines (112 loc) · 3.23 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package advancedsql.query;
import advancedsql.query.join.*;
import advancedsql.table.ITable;
import java.util.ArrayList;
import java.util.List;
public class Select extends ExecuteQuery<Select> {
private String[] columns = {"*"};
private String distinct = null;
private String order = null;
private final List<IJoin> joins = new ArrayList<>();
public Select(ITable table, String[] columns) {
super(table);
this.columns = columns;
}
public Select(ITable table) {
this(table, new String[]{"*"});
}
/**
* Set how the values will be ordered.
* Example: .orderBy("id DESC")
* @param order Values order
*/
public Select order(String order) {
return this.orderBy(order);
}
/**
* Set how the values will be ordered.
* Example: .orderBy("id DESC")
* @param order Values order
*/
public Select orderBy(String order) {
this.order = order;
return this;
}
/**
* Set the columns that you want to fetch.
* @param columns Columns array.
* @return Query object.
*/
public Select columns(String[] columns) {
this.columns = columns;
return this;
}
/**
* Set DISTINCT attribute.
* @param column Name of the column that you want to be distinct.
*/
public void distinct(String column) {
this.distinct = column;
}
/**
* Join table.
* @param table Table name.
* @return IJoin object.
*/
public IJoin join(String table) {
IJoin join = new Join(this, table);
this.joins.add(join);
return join;
}
/**
* Inner Join table.
* @param table Table name.
* @return IJoin object.
*/
public IJoin innerJoin(String table) {
IJoin join = new InnerJoin(this, table);
this.joins.add(join);
return join;
}
/**
* Left Join table.
* @param table Table name.
* @return IJoin object.
*/
public IJoin leftJoin(String table) {
IJoin join = new LeftJoin(this, table);
this.joins.add(join);
return join;
}
/**
* Right Join table.
* @param table Table name.
* @return IJoin object.
*/
public IJoin rightJoin(String table) {
IJoin join = new RightJoin(this, table);
this.joins.add(join);
return join;
}
/**
* Full Join table.
* @param table Table name.
* @return IJoin object.
*/
public IJoin fullJoin(String table) {
IJoin join = new FullJoin(this, table);
this.joins.add(join);
return join;
}
@Override
public String toQuery() {
StringBuilder query = new StringBuilder("SELECT " + (this.distinct != null ? "DISTINCT " + this.distinct : ""));
query.append(String.join(", ", this.columns));
query.append(this.table != null ? " FROM " + this.table : "");
for (IJoin join: this.joins) query.append(" ").append(join);
query.append(this.where != null ? "WHERE " + this.where : "");
query.append(this.order != null ? " ORDER BY " + this.order : "");
query.append(this.limit > 0 ? " LIMIT " + this.limit : "");
return query.toString().trim();
}
}