Commits

tbrugz  committed 1f73baf

queryon: refactoring QueryOn / SQL

  • Participants
  • Parent commits 5cc6b4e

Comments (0)

Files changed (2)

File src/tbrugz/queryon/QueryOn.java

 import tbrugz.sqldump.dbmodel.ExecutableObject;
 import tbrugz.sqldump.dbmodel.ExecutableParameter;
 import tbrugz.sqldump.dbmodel.FK;
-import tbrugz.sqldump.dbmodel.Query;
 import tbrugz.sqldump.dbmodel.Relation;
 import tbrugz.sqldump.dbmodel.SchemaModel;
 import tbrugz.sqldump.dbmodel.Table;
 		
 		//boolean isSQLWrapped = false;
 		
-		SQL sql = null;
-		if(relation instanceof Table) {
-			sql = SQL.createSQL(relation, reqspec);
-		}
-		else if(relation instanceof View) {
-			sql = SQL.createSQL(relation, reqspec);
-		}
-		else if(relation instanceof Query) {
-			//XXX: other query builder strategy besides [where-clause]? contains 'cursor'?
-			sql = new SQL( ((View)relation).query );
-		}
-		else {
-			throw new ServletException("unknown relation type: "+relation.getClass().getName());
-		}
+		SQL sql = SQL.createSQL(relation, reqspec);
 		
 		Constraint pk = null;
 		List<Constraint> conss = relation.getConstraints();
 				}
 			}
 		}
-		sql.addFilter(filter, relation);
+		sql.addFilter(filter);
 
+		//limit-offset
 		//XXX: how to decide strategy? default is LimitOffsetStrategy.RESULTSET_CONTROL
 		//query type (table, view, query), resultsetType? (not avaiable at this point), database type
 		LimitOffsetStrategy loStrategy = LimitOffsetStrategy.getDefaultStrategy(model.getSqlDialect());
-		
 		if(loStrategy!=LimitOffsetStrategy.RESULTSET_CONTROL) {
 			log.info("pre-sql:\n"+sql.getSql());
 		}

File src/tbrugz/queryon/SQL.java

 import tbrugz.queryon.QueryOn.LimitOffsetStrategy;
 import tbrugz.sqldump.dbmodel.Query;
 import tbrugz.sqldump.dbmodel.Relation;
+import tbrugz.sqldump.dbmodel.Table;
+import tbrugz.sqldump.dbmodel.View;
 import tbrugz.sqldump.util.Utils;
 
 public class SQL {
 	//XXX add order-clause? limit/offset-clause?
 
 	String sql;
-	//XXX add Relation relation; to constructor?
+	final Relation relation;
 	
-	public SQL(String sql) {
+	public SQL(String sql, Relation relation) {
 		this.sql = sql;
+		this.relation = relation;
 	}
 	
 	public String getSql() {
 		return sql.replace(PARAM_WHERE_CLAUSE, "").replace(PARAM_FILTER_CLAUSE, "");
 	}
 	
-	static String createSQLstr(Relation table, RequestSpec reqspec) {
+	private static String createSQLstr(Relation table, RequestSpec reqspec) {
 		String columns = "*";
 		if(reqspec.columns.size()>0) {
 			columns = Utils.join(reqspec.columns, ", ");
 		return sql;
 	}
 
-	public static SQL createSQL(Relation table, RequestSpec reqspec) {
-		return new SQL(createSQLstr(table, reqspec));
+	public static SQL createSQL(Relation relation, RequestSpec reqspec) {
+		if(relation instanceof Query) { //class Query is subclass of View, so this test must come first
+			//XXX: other query builder strategy besides [where-clause]? contains 'cursor'?
+			return new SQL( ((View)relation).query , relation);
+		}
+		else if(relation instanceof Table) {
+			return new SQL(createSQLstr(relation, reqspec), relation);
+		}
+		else if(relation instanceof View) {
+			return new SQL(createSQLstr(relation, reqspec), relation);
+		}
+		throw new IllegalArgumentException("unknown relation type: "+relation.getClass().getName());
+		
+		//return new SQL(createSQLstr(relation, reqspec), relation);
 	}
 	
-	public void addFilter(String filter, Relation relation) {
+	public void addFilter(String filter) {
 		if(filter==null || filter.length()==0) { return; }
 		
 		if(sql.contains(PARAM_WHERE_CLAUSE)) {
 					+"where rnum > "+reqspec.offset;
 			}
 			else if(reqspec.limit>0) {
-				addFilter("rownum <= "+reqspec.limit, null);
+				addFilter("rownum <= "+reqspec.limit);
 				//sql = "select * from (\n"+sql+"\n) where rownum <= "+reqspec.limit; 
 			}
 			else {