Skip to content

Commit 4a61213

Browse files
committed
Initial Commit
0 parents  commit 4a61213

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Project files
2+
bin/
3+
.project
4+
.classpath
5+
Driver.java
6+
7+
# Compiled class file
8+
*.class
9+
10+
# Log file
11+
*.log
12+
13+
# BlueJ files
14+
*.ctxt
15+
16+
# Mobile Tools for Java (J2ME)
17+
.mtj.tmp/
18+
19+
# Package Files #
20+
*.jar
21+
*.war
22+
*.ear
23+
*.zip
24+
*.tar.gz
25+
*.rar
26+
27+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
28+
hs_err_pid*

src/com/tpwang/sql/SQLCondition.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.tpwang.sql;
2+
3+
public class SQLCondition {
4+
5+
private StringBuilder builder;
6+
7+
/***
8+
* Constructor
9+
*/
10+
public SQLCondition() {
11+
builder = new StringBuilder();
12+
}
13+
14+
/***
15+
* Condition first argument
16+
* @param attributeName First argument in condition
17+
* @return (Unfinished) condition
18+
*/
19+
public SQLCondition whereAttribute(String attributeName) {
20+
builder.append('`').append(attributeName).append('`');
21+
return this;
22+
}
23+
24+
/***
25+
* Condition first argument
26+
* @param attributeName First argument in condition
27+
* @return (Unfinished) condition
28+
*/
29+
public SQLCondition whereAttribute(char attributeName) {
30+
builder.append('`').append(attributeName).append('`');
31+
return this;
32+
}
33+
34+
/***
35+
* Condition second argument
36+
* @param attributeValue Second argument/subquery in condition
37+
* @return condition
38+
*/
39+
public SQLCondition equalsTo(String attributeValue) {
40+
builder.append("=\"").append(attributeValue).append("\"");
41+
return this;
42+
}
43+
44+
/***
45+
* Condition second argument
46+
* @param attributeValue Second argument/subquery in condition
47+
* @return condition
48+
*/
49+
public SQLCondition equalsTo(char attributeValue) {
50+
builder.append("=\"").append(attributeValue).append("\"");
51+
return this;
52+
}
53+
54+
/***
55+
* Condition second argument
56+
* @param attributeValue Second argument/subquery in condition
57+
* @return condition
58+
*/
59+
public SQLCondition equalsTo(int attributeValue) {
60+
builder.append("=\"").append(attributeValue).append("\"");
61+
return this;
62+
}
63+
64+
/***
65+
* Return the final condition
66+
* @return condition
67+
*/
68+
public String make() {
69+
return toString();
70+
}
71+
72+
/***
73+
* Return the final condition
74+
*/
75+
public String toString() {
76+
return builder.toString();
77+
}
78+
}

src/com/tpwang/sql/SQLQuery.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.tpwang.sql;
2+
3+
public class SQLQuery {
4+
5+
StringBuilder builder;
6+
7+
/***
8+
* Constructor
9+
*/
10+
public SQLQuery() {
11+
builder = new StringBuilder().append(' ');
12+
}
13+
14+
/***
15+
* What to query
16+
* @param target Selection target from table
17+
* @return builder
18+
*/
19+
public SQLQuery select(String target) {
20+
builder.append("SELECT ").append(target).append(' ').toString();
21+
return this;
22+
}
23+
24+
/***
25+
* What to query
26+
* @param target Selection target from table
27+
* @return builder
28+
*/
29+
public SQLQuery select(char target) {
30+
return select(Character.toString(target));
31+
}
32+
33+
/***
34+
* Where to query
35+
* @param tableName Selection destination/subquery
36+
* @return builder
37+
*/
38+
public SQLQuery from(String tableName) {
39+
builder.append("FROM ").append(tableName).append(' ');
40+
return this;
41+
}
42+
43+
/***
44+
* How to query
45+
* @param condition Selection condition (manual or from Attribute)
46+
* @return builder
47+
*/
48+
public SQLQuery where(String condition) {
49+
builder.append("WHERE ").append(condition).append(' ');
50+
return this;
51+
}
52+
53+
/***
54+
* Return the final query
55+
* @return query
56+
*/
57+
public String make() {
58+
return toString();
59+
}
60+
61+
/***
62+
* Return the final query
63+
*/
64+
public String toString() {
65+
return builder.toString();
66+
}
67+
}
68+

src/com/tpwang/sql/SQLSubQuery.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.tpwang.sql;
2+
3+
import com.tpwang.sql.SQLQuery;
4+
5+
public class SQLSubQuery {
6+
7+
private SQLQuery query;
8+
9+
public SQLSubQuery() {
10+
this.query = new SQLQuery();
11+
}
12+
13+
// TODO: add other commands
14+
15+
/***
16+
* Return the final query
17+
* @return query
18+
*/
19+
public String make() {
20+
return toString();
21+
}
22+
23+
/***
24+
* Return the final subquery
25+
*/
26+
public String toString() {
27+
return (new StringBuilder()).append('(').append(query.make()).append(')').toString();
28+
}
29+
30+
}

0 commit comments

Comments
 (0)