Repository navigation
Expand file tree
/
Copy pathQuery.java
More file actions
203 lines (173 loc) · 5.28 KB
/
Copy pathQuery.java
File metadata and controls
203 lines (173 loc) · 5.28 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Query {
private Database database;
private String table;
private String query;
private boolean needsWhere = true;
private boolean where = false;
private boolean needsSet = false;
private boolean set = false;
private boolean needsValues = false;
private boolean values = false;
private boolean needsOn = false;
private boolean on = false;
Query(Database database, String table) {
this.database = database;
this.table = table;
query = "FROM " + "VaorraCore." + table;
}
public Query join(String tableName){
this.needsOn = true;
this.needsWhere = false;
query += " JOIN " + tableName + " ON ";
return this;
}
public Query on(String columnLeft, String comparator, String columnRight){
on = true;
query += " " + columnLeft + comparator + columnRight;
CheckForColumnSpecification(columnLeft);
CheckForColumnSpecification(columnRight);
return this;
}
public Query find(int id){
return where("id", "=", id);
}
public Query where(String column, String comparator, Object value){
if (where){
query += " AND";
}
query += " WHERE " + column + comparator + convertToSQLString(value);
where = true;
return this;
}
public Query set(String column, Object value){
if (set){
query += ",";
}
else{
query += " SET";
}
query += " " + column + "=" + convertToSQLString(value);
set = true;
return this;
}
public Query values(String key, Object value){
if (values){
String left = query.split(" VALUES ")[0];
String right = query.split(" VALUES ")[1];
left += ", " + key;
right += ", " + convertToSQLString(value);
query = left + " VALUES " + right;
}
else{
query += " (" + key +" VALUES (" + convertToSQLString(value);
}
values = true;
return this;
}
public ResultSet get(){
CheckForCondition();
query = "SELECT * " + query;
ResultSet resultSet = database.executeQuery(query);
try {
resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
}
return resultSet;
}
public List<Integer> getAll(){
needsWhere = false;
ResultSet resultSet = get();
List<Integer> ids = new ArrayList<Integer>();
try{
do{
ids.add(resultSet.getInt("id"));
} while (resultSet.next());
} catch (SQLException e){
e.printStackTrace();
}
return ids;
}
public boolean exists(){
CheckForCondition();
query = "SELECT * " + query;
ResultSet resultSet = database.executeQuery(query);
try {
return resultSet.isBeforeFirst();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public int insert(){
this.needsWhere = false;
this.needsValues = true;
CheckForCondition();
String left = query.split(" VALUES ")[0];
String right = query.split(" VALUES ")[1];
left += ")";
right += ")";
query = left + " VALUES " + right;
query = "INSERT INTO " + query.substring(5);
database.execute(query);
ResultSet idResult = database.executeQuery("SELECT id FROM " + table + " ORDER BY id DESC LIMIT 1");
try {
idResult.next();
return idResult.getInt("id");
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public void update(){
this.needsSet = true;
CheckForCondition();
query = "UPDATE " + query.substring(5);
database.execute(query);
}
public void delete(){
CheckForCondition();
query = "DELETE " + query;
database.execute(query);
}
private void CheckForCondition(){
if (needsWhere && !where){
throw new NoConditionException("where");
}
if (needsOn && !on){
throw new NoConditionException("on");
}
if (needsSet && !set){
throw new NoConditionException("set");
}
if (needsValues && !values){
throw new NoConditionException("values");
}
}
private void CheckForColumnSpecification(String column){
if (column.split("\\.").length != 2){
throw new UnspecifiedColumnException(column);
}
}
private String convertToSQLString(Object object){
if (object == null){
return "null";
}
else if (object instanceof String){
return "'" + object.toString() + "'";
}
else if (object instanceof Date){
return "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)object) + "'";
}
else if (object instanceof Boolean){
return (Boolean)object ? "1" : "0";
}
return object.toString();
}
}