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;
import java.util.ArrayList;
import java.util.LinkedList;
/**
@ -9,96 +8,161 @@ import java.util.LinkedList;
* @author Ziver
*/
public class SQLQuery {
protected abstract class SQLQueryItem{
public abstract void getString(StringBuilder query);
protected static abstract class SQLQueryItem{
SQLQueryItem root;
protected SQLQueryItem(){}
protected void setRoot(SQLQueryItem root){
this.root = root;
}
protected abstract void build(StringBuilder query);
public String toString(){
StringBuilder query = new StringBuilder();
this.getString(query);
root.build(query);
return query.toString();
}
}
//*******************************************
// 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;
SQLFrom from;
/**
* @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;
}
public void getString(StringBuilder query) {
protected void build(StringBuilder query) {
query.append("SELECT ");
if( params == null )
query.append("* ");
if( params == null || params.length <= 0 )
query.append("*");
else{
for(int i=0; i<params.length ;i++){
query.append(params[i]);
if( i != params.length-1 )
query.append(", ");
query.append(",");
}
query.append(' ');
}
}
if( from != null )
from.build( query );
}
public From FROM(String table){
return new From(table);
public SQLFrom FROM(String ...tables){
return from = new SQLFrom(this, tables);
}
}
protected class Update extends SQLQueryItem{
public void getString(StringBuilder query) {
public static class SQLUpdate extends SQLQueryItem{
protected SQLUpdate(){
setRoot(this);
}
protected void build(StringBuilder query) {
}
}
protected class Delete extends SQLQueryItem{
public void getString(StringBuilder query) {
public static class SQLDelete extends SQLQueryItem{
protected SQLDelete(){
setRoot(this);
}
protected void build(StringBuilder query) {
}
}
//*******************************************
// Sub Types
protected class From extends SQLQueryItem{
ArrayList<String> tables;
public static class SQLFrom extends SQLQueryItem{
LinkedList<String> tables = new LinkedList<String>();
SQLQueryItem next;
public From(String ...tables){
this.tables = new ArrayList<String>();
protected SQLFrom(SQLQueryItem root, String ...tables){
setRoot(root);
for( String table : tables )
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);
}
public From JOIN(String ...tables){
public SQLFrom JOIN(String table){
return joinLastTable("JOIN", table);
}
public SQLFrom JOIN(String ...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);
}
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();
for(int i=0; i<tables.length ;i++){
str.append(tables[i]);
if( i != tables.length-1 )
str.append(' ').append(type).append(' ');
}
str.append(' ');
this.tables.add(str.toString());
return this;
}
public void getString(StringBuilder query) {
query.append("FROM ");
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 ");
if( tables.isEmpty() )
throw new RuntimeException("The FROM query item must hav atleast 1 table!");
for(int i=0; i<tables.size() ;i++){
@ -106,35 +170,191 @@ public class SQLQuery {
if( i != tables.size()-1 )
query.append(", ");
}
query.append(' ');
if( next != null ) next.build( query );
}
}
//*******************************************
// 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
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(){
return new Select();
public static SQLSelect SELECT( String ...params ){
return new SQLSelect( params );
}
public Update UPDATE(){
return new Update();
public static SQLUpdate UPDATE(){
return new SQLUpdate();
}
public Delete DELETE(){
return new Delete();
public static SQLDelete DELETE(){
return new SQLDelete();
}
}