Commits

tbrugz committed 1e7691d

queryon: added 'fields' parameter

Comments (0)

Files changed (2)

src/tbrugz/queryon/RequestSpec.java

 			limit = (int)(long) Utils.getPropLong(prop, QueryOn.PROP_DEFAULT_LIMIT, 1000l);
 		}
 		
-		for(int i=1;;i++) {
-			String value = req.getParameter("c"+i);
-			if(value==null) break;
-			columns.add(value);
+		String fields = req.getParameter("fields");
+		if(fields!=null) {
+			columns.addAll(Arrays.asList(fields.split(",")));
 		}
 
 		for(int i=1;;i++) {

src/tbrugz/queryon/SQL.java

 package tbrugz.queryon;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
 import javax.servlet.ServletException;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import tbrugz.queryon.QueryOn.LimitOffsetStrategy;
 import tbrugz.sqldump.dbmodel.Query;
 import tbrugz.sqldump.dbmodel.Relation;
 import tbrugz.sqldump.util.Utils;
 
 public class SQL {
+
+	static final Log log = LogFactory.getLog(SQL.class);
 	
 	public static final String PARAM_WHERE_CLAUSE = "[where-clause]";
 	public static final String PARAM_FILTER_CLAUSE = "[filter-clause]";
 
 	String sql;
 	final Relation relation;
+	//XXX add 'final String initialSql;'?
 	
 	public SQL(String sql, Relation relation) {
 		this.sql = sql;
 	private static String createSQLstr(Relation table, RequestSpec reqspec) {
 		String columns = "*";
 		if(reqspec.columns.size()>0) {
-			columns = Utils.join(reqspec.columns, ", ");
+			Set<String> tabCols = new HashSet<String>(table.getColumnNames()); 
+			List<String> sqlCols = new ArrayList<String>(); 
+			for(String reqColumn: reqspec.columns) {
+				if(tabCols.contains(reqColumn)) {
+					sqlCols.add(reqColumn);
+				}
+				else {
+					log.warn("column not found: "+reqColumn+" [table:"+table.getName()+"]");
+				}
+			}
+			if(sqlCols.size()>0) {
+				columns = Utils.join(sqlCols, ", ");
+			}
+			else {
+				log.warn("no valid column specified. defaulting to 'all'");
+			}
 		}
 		String sql = "select "+columns+
 			" from " + (table.getSchemaName()!=null?table.getSchemaName()+".":"") + table.getName()+
 		if(filter==null || filter.length()==0) { return; }
 		
 		if(sql.contains(PARAM_WHERE_CLAUSE)) {
-			sql = sql.replace(PARAM_WHERE_CLAUSE, " where "+filter+" "+PARAM_FILTER_CLAUSE);
+			sql = sql.replace(PARAM_WHERE_CLAUSE, "where "+filter+" "+PARAM_FILTER_CLAUSE);
 		}
 		else if(sql.contains(PARAM_FILTER_CLAUSE)) {
-			sql = sql.replace(PARAM_FILTER_CLAUSE, " and "+filter+" "+PARAM_FILTER_CLAUSE);
+			sql = sql.replace(PARAM_FILTER_CLAUSE, "and "+filter+" "+PARAM_FILTER_CLAUSE);
 		}
 		else if(filter.length()>0) {
 			//FIXME: if selecting from Table object, do not need to wrap
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.