package com.pictc.jdbc;

import org.springframework.util.Assert;

public class SqlBuilder {
	
	public static final String MysqlSeparator="`";
	
	public static final String KBaseSeparator="\"";
	
	public static final String OracleSeparator="\"";
	
	public static final String strSeparator="'";
	
	public static final String space=" ";
	
	public static final String dot=".";
	
	public static final String comma=",";
	
	public static final String enter="\n\t\t";
	
	public static final String leftBracket="(";
	
	public static final String rightBracket=")";
	
	public static final String placeholder="?";

	public static final String as=" AS ";
	
	public static final String select="SELECT ";
	
	public static final String distinct=" DISTINCT";
	
	public static final String from=" FROM ";
	
	public static final String leftJoin=" LEFT JOIN ";
	
	public static final String rightJoin=" RIGHT JOIN ";
	
	public static final String innerJoin=" INNER JOIN ";
	
	public static final String on=" ON ";
	
	public static final String where=" WHERE ";
	
	public static final String AND =" AND ";
	
	public static final String OR =" OR ";
	
	public static final String not=" NOT ";
	
	public static final String eq=" = ";
	
	public static final String notEq=" <> ";
	
	public static final String between=" BETWEEN ";
	
	public static final String exists=" EXISTS ";
	
	public static final String in=" IN(";
	
	public static final String gt=" > ";
	
	public static final String gtEq=" >= ";
	
	public static final String lt=" < ";
	
	public static final String ltEq=" <= ";
	
	public static final String like=" LIKE ";
	
	public static final String isNull=" IS NULL ";
	
	public static final String isNotNull=" IS NOT NULL ";
	
	public static final String groupBy=" GROUP BY";
	
	public static final String having=" HAVING ";
	
	public static final String orderBy=" ORDER BY ";
	
	public static final String orderAsc=" ASC ";
	
	public static final String orderDesc=" DESC ";
	
	public static final String avg=" AVG";
	
	public static final String min=" MIN";
	
	public static final String max=" MAX";
	
	public static final String count=" COUNT";
	
	public static final String sum=" SUM";

	public static final String insert="INSERT INTO ";
	
	public static final String values=" VALUES";
	
	public static final String update="UPDATE ";
	
	public static final String set=" SET ";
	
	public static final String delete="DELETE ";
	
	public static final String limit=" LIMIT ";
	
	public static final String offset=" OFFSET ";
	
	private StringBuilder builder = new StringBuilder();
	
	private String separator = KBaseSeparator;

    public SqlBuilder() {
    	
	}
    
    public SqlBuilder(String separator) {
    	this.separator = separator;
    }
    
    public static SqlBuilder mysql() {
    	return new SqlBuilder(MysqlSeparator);
    }
    
    public static SqlBuilder oracle() {
    	return new SqlBuilder(OracleSeparator);
    }
    
    public static SqlBuilder kbase() {
    	return new SqlBuilder(KBaseSeparator);
    }
    
	public String getSeparator() {
		return separator;
	}

	public SqlBuilder space() {
    	builder.append(space);
    	return this;
    }
    
    public SqlBuilder dot() {
    	builder.append(dot);
    	return this;
    }
    
    public SqlBuilder comma() {
    	builder.append(comma);
    	return this;
    }
    
    public SqlBuilder enter() {
    	builder.append(enter);
    	return this;
    }
    
    public SqlBuilder leftBracket() {
    	builder.append(leftBracket);
    	return this;
    }
    
    public SqlBuilder rightBracket() {
    	builder.append(rightBracket);
    	return this;
    }
    
    public SqlBuilder leftJoin() {
    	builder.append(leftJoin);
    	return this;
    }
    
    public SqlBuilder rightJoin() {
    	builder.append(rightJoin);
    	return this;
    }
    
    public SqlBuilder innerJoin() {
    	builder.append(innerJoin);
    	return this;
    }
    
    public SqlBuilder on() {
    	builder.append(on);
    	return this;
    }

	public SqlBuilder append(Object obj) {
    	builder.append(obj);
        return this;
    }
	
	public SqlBuilder appendSeparator(String... names) {
		Assert.isTrue(!(names==null || names.length==0),"参数不能为空！");
		boolean flag = false;
		for (int i = 0; i < names.length; i++) {
			if(names[i]!=null) {
				if(!flag) {
					flag = true;
				}else {
					dot();
				}
				builder.append(separator);
				builder.append(names[i]);
				builder.append(separator);
			}
		}
		return this;
	}
	
	public SqlBuilder appendStrSeparator(String... names) {
		Assert.isTrue(!(names==null || names.length==0),"参数不能为空！");
		boolean flag = false;
		for (int i = 0; i < names.length; i++) {
			if(names[i]!=null) {
				if(!flag) {
					flag = true;
				}else {
					dot();
				}
				builder.append(strSeparator);
				builder.append(names[i]);
				builder.append(strSeparator);
			}
		}
		return this;
	}
	
	public StringBuilder stringBuilder() {
		return builder;
	}
	
	@Override
	public String toString() {
		return builder.toString();
	}
	
	public SqlBuilder where() {
    	builder.append(where);
        return this;
    }
	
	public SqlBuilder and() {
		builder.append(AND);
		return this;
	}
	
	public SqlBuilder or() {
		builder.append(OR);
		return this;
	}
	
	public SqlBuilder not() {
		builder.append(not);
		return this;
	}
	
	public SqlBuilder eq() {
		builder.append(eq);
		return this;
	}
	
	public SqlBuilder notEq() {
		builder.append(notEq);
		return this;
	}
	
	public SqlBuilder between() {
		builder.append(between);
		return this;
	}
	
	public SqlBuilder exists() {
		builder.append(exists);
		return this;
	}
	
	public SqlBuilder inStart() {
		builder.append(in);
		return this;
	}
	
	public SqlBuilder inEnd() {
		builder.append(rightBracket);
		return this;
	}
	
	public SqlBuilder gt() {
		builder.append(gt);
		return this;
	}
	
	public SqlBuilder gtEq() {
		builder.append(gtEq);
		return this;
	}
	
	public SqlBuilder lt() {
		builder.append(lt);
		return this;
	}
	
	public SqlBuilder ltEq() {
		builder.append(ltEq);
		return this;
	}
	
	public SqlBuilder like() {
		builder.append(like);
		return this;
	}
	
	public SqlBuilder isNull() {
		builder.append(isNull);
		return this;
	}
	
	public SqlBuilder isNotNull() {
		builder.append(isNotNull);
		return this;
	}
	
	public SqlBuilder groupBy() {
		builder.append(groupBy);
		return this;
	}
	
	public SqlBuilder having() {
		builder.append(having);
		return this;
	}
	
	public SqlBuilder orderBy() {
		builder.append(orderBy);
		return this;
	}
	
	public SqlBuilder asc() {
		builder.append(orderAsc);
		return this;
	}
	
	public SqlBuilder desc() {
		builder.append(orderDesc);
		return this;
	}
	
	public SqlBuilder avg() {
		builder.append(avg);
		return this;
	}
	
	public SqlBuilder min() {
		builder.append(min);
		return this;
	}
	
	public SqlBuilder max() {
		builder.append(max);
		return this;
	}
	
	public SqlBuilder count() {
		builder.append(count);
		return this;
	}
	
	public SqlBuilder sum() {
		builder.append(sum);
		return this;
	}
	
	public SqlBuilder insertInto() {
		builder.append(insert);
		return this;
	}
	
	public SqlBuilder values() {
		builder.append(values);
		return this;
	}
	
	public SqlBuilder update() {
		builder.append(update);
		return this;
	}
	
	public SqlBuilder set() {
		builder.append(set);
		return this;
	}
	
	public SqlBuilder delete() {
		builder.append(delete);
		return this;
	}
	
	public SqlBuilder placeholder() {
		builder.append(placeholder);
		return this;
	}
	
	public SqlBuilder as() {
		builder.append(as);
		return this;
	}
	
	public SqlBuilder select() {
		builder.append(select);
		return this;
	}
	
	public SqlBuilder distinct() {
		builder.append(distinct);
		return this;
	}
	
	public SqlBuilder from() {
		builder.append(from);
		return this;
	}
	
	public SqlBuilder limit() {
		builder.append(limit);
		return this;
	}
	
	public SqlBuilder limitFull() {
		builder.append(limit).append(placeholder).append(offset).append(placeholder);
		return this;
	}
}
