Added Select statements to SQLQuery class

This commit is contained in:
Ziver Koc 2011-03-30 21:58:05 +00:00
parent 89bb6ffbe2
commit d7fdf5b176

View file

@ -1,6 +1,5 @@
package zutil.db; package zutil.db;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
/** /**
@ -9,31 +8,55 @@ import java.util.LinkedList;
* @author Ziver * @author Ziver
*/ */
public class SQLQuery { public class SQLQuery {
protected abstract class SQLQueryItem{ protected static abstract class SQLQueryItem{
public abstract void getString(StringBuilder query); SQLQueryItem root;
protected SQLQueryItem(){}
protected void setRoot(SQLQueryItem root){
this.root = root;
}
protected abstract void build(StringBuilder query);
public String toString(){ public String toString(){
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
this.getString(query); root.build(query);
return query.toString(); return query.toString();
} }
} }
//******************************************* //*******************************************
// Main Types // Main Types
protected class Select extends SQLQueryItem{ /**
* <XMP>
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
* </XMP>
*/
public static class SQLSelect extends SQLQueryItem{
String[] params; String[] params;
SQLFrom from;
/** /**
* @param params is the columns that you want out of the SELECT query, leave empty for all columns * @param params is the columns that you want out of the SELECT query, leave empty for all columns
*/ */
public Select(String ...params ){ protected SQLSelect(String ...params ){
setRoot(this);
this.params = params; this.params = params;
} }
public void getString(StringBuilder query) { protected void build(StringBuilder query) {
query.append("SELECT "); query.append("SELECT ");
if( params == null ) if( params == null || params.length <= 0 )
query.append("*"); query.append("*");
else{ else{
for(int i=0; i<params.length ;i++){ for(int i=0; i<params.length ;i++){
@ -41,63 +64,104 @@ public class SQLQuery {
if( i != params.length-1 ) if( i != params.length-1 )
query.append(","); query.append(",");
} }
query.append(' '); }
if( from != null )
from.build( query );
}
public SQLFrom FROM(String ...tables){
return from = new SQLFrom(this, tables);
} }
} }
public From FROM(String table){ public static class SQLUpdate extends SQLQueryItem{
return new From(table); protected SQLUpdate(){
} setRoot(this);
} }
protected class Update extends SQLQueryItem{ protected void build(StringBuilder query) {
public void getString(StringBuilder query) {
} }
} }
protected class Delete extends SQLQueryItem{ public static class SQLDelete extends SQLQueryItem{
public void getString(StringBuilder query) { protected SQLDelete(){
setRoot(this);
}
protected void build(StringBuilder query) {
} }
} }
//******************************************* //*******************************************
// Sub Types // Sub Types
protected class From extends SQLQueryItem{ public static class SQLFrom extends SQLQueryItem{
ArrayList<String> tables; LinkedList<String> tables = new LinkedList<String>();
SQLQueryItem next;
public From(String ...tables){ protected SQLFrom(SQLQueryItem root, String ...tables){
this.tables = new ArrayList<String>(); setRoot(root);
for( String table : tables ) for( String table : tables )
this.tables.add(table); this.tables.add(table);
} }
public From NATURALJOIN(String ...tables){ public SQLFrom NATURAL_JOIN(String table){
return joinLastTable("NATURAL JOIN", table);
}
public SQLFrom NATURAL_JOIN(String ...tables){
return joinTable("NATURAL JOIN", tables); return joinTable("NATURAL JOIN", tables);
} }
public From JOIN(String ...tables){ public SQLFrom JOIN(String table){
return joinLastTable("JOIN", table);
}
public SQLFrom JOIN(String ...tables){
return joinTable("JOIN", tables); return joinTable("JOIN", tables);
} }
public From UNION(String ...tables){ public SQLFrom UNION(String table){
return joinLastTable("UNION", table);
}
public SQLFrom UNION(String ...tables){
return joinTable("UNION", tables); return joinTable("UNION", tables);
} }
private From joinTable(String type, String[] tables){ private SQLFrom joinLastTable(String type, String table){
String last = tables.getLast();
tables.removeLast();
tables.add(
new StringBuilder(last).append(" ").append(type).append(" ").append(table).toString());
return this;
}
private SQLFrom joinTable(String type, String[] tables){
if( tables.length < 2 )
return this;
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
for(int i=0; i<tables.length ;i++){ for(int i=0; i<tables.length ;i++){
str.append(tables[i]); str.append(tables[i]);
if( i != tables.length-1 ) if( i != tables.length-1 )
str.append(' ').append(type).append(' '); str.append(' ').append(type).append(' ');
} }
str.append(' ');
this.tables.add(str.toString()); this.tables.add(str.toString());
return this; return this;
} }
public void getString(StringBuilder query) { public SQLWhere WHERE(){
return (SQLWhere) (next = new SQLWhere(root));
}
public SQLGroupBy GROUP_BY( String ...cols ){
return (SQLGroupBy) (next = new SQLGroupBy(root, cols));
}
public SQLOrderBy ORDER_BY( String ...cols ){
return (SQLOrderBy) (next = new SQLOrderBy(root, cols));
}
public SQLLimit LIMIT( long count ){
return (SQLLimit) (next = new SQLLimit(root, count));
}
protected void build(StringBuilder query) {
query.append(" FROM "); query.append(" FROM ");
if( tables.isEmpty() ) if( tables.isEmpty() )
throw new RuntimeException("The FROM query item must hav atleast 1 table!"); throw new RuntimeException("The FROM query item must hav atleast 1 table!");
@ -106,35 +170,191 @@ public class SQLQuery {
if( i != tables.size()-1 ) if( i != tables.size()-1 )
query.append(", "); query.append(", ");
} }
query.append(' '); if( next != null ) next.build( query );
} }
} }
//******************************************* //*******************************************
// Condition Types // Condition Types
protected class Where extends SQLQueryItem{ public static class SQLWhere extends SQLQueryItem{
LinkedList<String> conds = new LinkedList<String>();
SQLQueryItem next;
public void getString(StringBuilder query) { protected SQLWhere(SQLQueryItem root){
setRoot(root);
}
/**
* Equals (arg1 = arg2)
*/
public SQLWhere EQ(String arg1, String arg2){
return cond("=", arg1, arg2);
}
/**
* Not Equal (arg1 != arg2)
*/
public SQLWhere NE(String arg1, String arg2){
return cond("!=", arg1, arg2);
}
/**
* Less than (arg1 < arg2)
*/
public SQLWhere LT(String arg1, String arg2){
return cond("<", arg1, arg2);
}
/**
* Greater than (arg1 > arg2)
*/
public SQLWhere GT(String arg1, String arg2){
return cond(">", arg1, arg2);
}
/**
* Less than or equal (arg1 <= arg2)
*/
public SQLWhere LE(String arg1, String arg2){
return cond("<=", arg1, arg2);
}
/**
* Greater than or equal (arg1 >= arg2)
*/
public SQLWhere GE(String arg1, String arg2){
return cond(">=", arg1, arg2);
}
private SQLWhere cond(String cond, String arg1, String arg2){
conds.add(
//new StringBuilder(arg1).append(cond).append('\"').append(arg2).append('\"').toString());
new StringBuilder(arg1).append(cond).append(arg2).toString());
return this;
}
public SQLGroupBy GROUP_BY( String ...cols ){
return (SQLGroupBy) (next = new SQLGroupBy(root, cols));
}
public SQLOrderBy ORDER_BY( String ...cols ){
return (SQLOrderBy) (next = new SQLOrderBy(root, cols));
}
public SQLLimit LIMIT( long count ){
return (SQLLimit) (next = new SQLLimit(root, count));
}
protected void build(StringBuilder query) {
query.append(" WHERE ");
if( conds.isEmpty() )
throw new RuntimeException("The WHERE query item must hav atleast 1 condition!");
for(int i=0; i<conds.size() ;i++){
query.append(conds.get(i));
if( i != conds.size()-1 )
query.append(" AND ");
}
if( next != null ) next.build( query );
} }
} }
//******************************************* //*******************************************
// Sorting Types // Sorting Types
public abstract static class SQLGroupOrderBy<T> extends SQLQueryItem{
protected String[] cols;
protected String end;
SQLQueryItem next;
protected SQLGroupOrderBy(SQLQueryItem root, String ...cols){
setRoot(root);
this.cols = cols;
}
@SuppressWarnings("unchecked")
public T ASC(){
end = "ASC";
return (T)this;
}
@SuppressWarnings("unchecked")
public T DESC(){
end = "DESC";
return (T)this;
}
protected void build(String op, StringBuilder query) {
query.append(' ').append(op).append(' ');
if( cols == null || cols.length <= 0 )
throw new RuntimeException("The "+op+" query item must hav atleast 1 column!");
for(int i=0; i<cols.length ;i++){
query.append( cols[i] );
if( i != cols.length-1 )
query.append(",");
}
if( end != null ) query.append(' ').append( end );
if( next != null ) next.build( query );
}
}
public static class SQLGroupBy extends SQLGroupOrderBy<SQLGroupBy>{
protected SQLGroupBy(SQLQueryItem root, String ...cols){
super( root, cols );
}
public SQLOrderBy ORDER_BY( String ...cols ){
return (SQLOrderBy) (next = new SQLOrderBy(root, cols));
}
public SQLLimit LIMIT( long count ){
return (SQLLimit) (next = new SQLLimit(root, count));
}
protected void build(StringBuilder query) {
build("GROUP BY", query);
}
}
public static class SQLOrderBy extends SQLGroupOrderBy<SQLOrderBy>{
protected SQLOrderBy(SQLQueryItem root, String ...cols){
super( root, cols );
}
public SQLLimit LIMIT( long count ){
return (SQLLimit) (next = new SQLLimit(root, count));
}
protected void build(StringBuilder query) {
build("ORDER BY", query);
}
}
public static class SQLLimit extends SQLQueryItem{
long start;
Long count;
protected SQLLimit(SQLQueryItem root, long start){
setRoot( root );
this.start = start;
}
public SQLLimit TO( long count ){
this.count = count;
return this;
}
protected void build(StringBuilder query) {
query.append(" LIMIT ").append( start );
if( count != null ) query.append(' ').append( count );
}
}
//******************************************* //*******************************************
public Select SELECT(){ public static SQLSelect SELECT( String ...params ){
return new Select(); return new SQLSelect( params );
} }
public Update UPDATE(){ public static SQLUpdate UPDATE(){
return new Update(); return new SQLUpdate();
} }
public Delete DELETE(){ public static SQLDelete DELETE(){
return new Delete(); return new SQLDelete();
} }
} }