/*
 * Copyright (c) 2009-2013 Topi Nieminen
 */
package fi.tnie.db.expr;

import fi.tnie.db.expr.SetOperator.Op;

/**
 * Table expression implementation
 * @author Administrator
 */

public class DefaultTableExpression
	extends QueryExpression
	implements TableExpression {

	private Select select;
	private From from;
	private Where where;
	private GroupBy groupBy;
	private Having having;
	
	private SelectListElement all = null;
	
	public DefaultTableExpression() {
	}
				
//	private static Logger logger = Logger.getLogger(DefaultTableExpression.class);
	
//	public String generate(SimpleQueryContext ctx) {
//		StringBuffer dest = new StringBuffer();		
//		generate(ctx, dest);		
//		return dest.toString();
//	}
					
//	@Override
//	public void generate(SimpleQueryContext qc, StringBuffer dest) {
//				
//		if (getSelect() == null) {
//			throw new NullPointerException("Select -clause must not be null");
//		}
//		
//		if (getFrom() == null) {
//			throw new NullPointerException("From -clause must not be null");
//		}
//		
//		try {
////			for (AbstractTableReference r : refs) {				
////				qc.start(r, r.getCorrelationNamePrefix());
////			}
//						
//			getSelect().generate(qc, dest);		
//			getFrom().generate(qc, dest);
//			generate(getWhere(), qc, dest);			
//			generate(getHaving(), qc, dest);
//			generate(getOrderBy(), qc, dest);
//		}
//		finally {
////			endAll(qc, refs);
//		}
//	}
	
//	private void generate(Element e, SimpleQueryContext qc, StringBuffer dest) {
//		if (e != null) {
//			e.generate(qc, dest);
//		}
//	}
	
	private void traverse(Element e, VisitContext vc, ElementVisitor v) {
		if (e != null) {
			e.traverse(vc, v);
		}
	}

	public void setSelect(Select select) {
		this.select = select;
	}

	@Override
	public Select getSelect() {
		return select;
	}

	public From getFrom() {
		return from;
	}

	public void setFrom(From from) {
		this.from = from;
	}

	public Where getWhere() {
		if (where == null) {
			where = new Where();			
		}

		return where;
	}

	public void setWhere(Where where) {
		this.where = where;
	}

	public Having getHaving() {
		return having;
	}

	public void setHaving(Having having) {
		this.having = having;
	}
	
	@Override
	protected void traverseContent(VisitContext vc, ElementVisitor v) {
		traverse(getSelect(), vc, v);
		traverse(getFrom(), vc, v);
		traverse(getWhere(), vc, v);
		traverse(getGroupBy(), vc, v);
		traverse(getHaving(), vc, v);
	}

	public GroupBy getGroupBy() {
		return groupBy;
	}

	public void setGroupBy(GroupBy groupBy) {
		this.groupBy = groupBy;
	}

	
	@Override
	public String generate() {
		StringBuffer dest = new StringBuffer();
		ElementVisitor v = new QueryGenerator(dest);
		traverse(null, v);		
		return dest.toString();
	}
	
	public SetOperator unionAll(TableExpression rp) {
		return new SetOperator(Op.UNION, true, this, rp);
	}
	
	public SetOperator union(TableExpression rp) {
		return new SetOperator(Op.UNION, false, this, rp);
	}
	
	public SetOperator intersect(TableExpression rp) {
		return new SetOperator(Op.INTERSECT, false, this, rp);
	}
	
	public SetOperator intersectAll(TableExpression rp) {
		return new SetOperator(Op.INTERSECT, true, this, rp);
	}
	
	public SetOperator except(TableExpression rp) {
		return new SetOperator(Op.EXCEPT, false, this, rp);
	}
	
	public SetOperator exceptAll(TableExpression rp) {
		return new SetOperator(Op.EXCEPT, true, this, rp);
	}
	
	public void selectAll() {		 	
		getSelect().getSelectList().set(getAll());
	}
	
	private SelectListElement getAll() {
		if (all == null) {
			all = new AllColumns() {
				@Override
				protected TableRefList getTableRefs() {
					return getFrom().getTableReferenceList();				
				}			
			};			
		}

		return all;
	}
	
	@Override
	public OrderBy getOrderBy() {	
		return null;
	}
	
	@Override
	public TableExpression getTableExpr() {
		return this;
	}
}

@@ -469,1091 +469,8 @@
 %0A%09%7D%0A
-%09%09%09%09%0A//%09private static Logger logger = Logger.getLogger(DefaultTableExpression.class);%0A%09%0A//%09public String generate(SimpleQueryContext ctx) %7B%0A//%09%09StringBuffer dest = new StringBuffer();%09%09%0A//%09%09generate(ctx, dest);%09%09%0A//%09%09return dest.toString();%0A//%09%7D%0A%09%09%09%09%09%0A//%09@Override%0A//%09public void generate(SimpleQueryContext qc, StringBuffer dest) %7B%0A//%09%09%09%09%0A//%09%09if (getSelect() == null) %7B%0A//%09%09%09throw new NullPointerException(%22Select -clause must not be null%22);%0A//%09%09%7D%0A//%09%09%0A//%09%09if (getFrom() == null) %7B%0A//%09%09%09throw new NullPointerException(%22From -clause must not be null%22);%0A//%09%09%7D%0A//%09%09%0A//%09%09try %7B%0A////%09%09%09for (AbstractTableReference r : refs) %7B%09%09%09%09%0A////%09%09%09%09qc.start(r, r.getCorrelationNamePrefix());%0A////%09%09%09%7D%0A//%09%09%09%09%09%09%0A//%09%09%09getSelect().generate(qc, dest);%09%09%0A//%09%09%09getFrom().generate(qc, dest);%0A//%09%09%09generate(getWhere(), qc, dest);%09%09%09%0A//%09%09%09generate(getHaving(), qc, dest);%0A//%09%09%09generate(getOrderBy(), qc, dest);%0A//%09%09%7D%0A//%09%09finally %7B%0A////%09%09%09endAll(qc, refs);%0A//%09%09%7D%0A//%09%7D%0A%09%0A//%09private void generate(Element e, SimpleQueryContext qc, StringBuffer dest) %7B%0A//%09%09if (e != null) %7B%0A//%09%09%09e.generate(qc, dest);%0A//%09%09%7D%0A//%09%7D%0A
 %09%0A%09p

Remove code commented out