Commits

tbrugz committed 4830952

queryon: added xtra filters: 'equals' & 'in'

Comments (0)

Files changed (2)

src/tbrugz/queryon/QueryOn.java

 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Properties;
+import java.util.Set;
 
 import javax.naming.NamingException;
 import javax.servlet.ServletConfig;
 			}
 		}
 		
+		// pk/uk filter
 		int parametersToBind = 0;
 		String filter = "";
 		//TODO: what if parameters already defined in query?
 		}
 		sql.addFilter(filter);
 
+		// xtra filters
+		// TODO parameters: remove reqspec.params in excess of #parametersToBind ?
+		List<String> colNames = relation.getColumnNames();
+		if(colNames!=null) {
+			Set<String> columns = new HashSet<String>();
+			columns.addAll(colNames);
+			for(String col: reqspec.filterEquals.keySet()) {
+				if(columns.contains(col)) {
+					//XXX column type?
+					reqspec.params.add(reqspec.filterEquals.get(col));
+					parametersToBind++;
+					sql.addFilter(col+" = ?");
+				}
+				else {
+					log.warn("unknown column: "+col+" [relation="+relation.getName()+"]");
+				}
+			}
+			for(String col: reqspec.filterIn.keySet()) {
+				if(columns.contains(col)) {
+					//XXX column type?
+					StringBuffer sb = new StringBuffer();
+					sb.append(col+" in (");
+					String[] values = reqspec.filterIn.get(col);
+					for(int i=0;i<values.length;i++) {
+						String value = values[i];
+						sb.append((i>0?", ":"")+"?");
+						reqspec.params.add(value);
+						parametersToBind++;
+					}
+					sb.append(")");
+					sql.addFilter(sb.toString());
+				}
+				else {
+					log.warn("unknown column: "+col+" [relation="+relation.getName()+"]");
+				}
+			}
+		}
+		else {
+			if(reqspec.filterEquals.size()>0) {
+				log.warn("relation '"+relation.getName()+"' has no columns specified");
+			}
+		}
+
 		//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

src/tbrugz/queryon/RequestSpec.java

 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
 import javax.servlet.ServletException;
 	final String outputTypeStr;
 	final DumpSyntax outputSyntax;
 	
+	final Map<String, String> filterEquals = new HashMap<String, String>();
+	final Map<String, String[]> filterIn = new HashMap<String, String[]>();
+	
 	public RequestSpec(DumpSyntaxUtils dsutils, HttpServletRequest req, Properties prop) throws ServletException {
 		String method = req.getParameter("method");
 		//XXX: may method be changed? property?
 			if(value==null) break;
 			params.add(value);
 		}
+		
+		Map<String,String[]> params = req.getParameterMap();
+		for(String param: params.keySet()) {
+			if(param.startsWith("fe:")) {
+				String col = param.substring(3);
+				String value = params.get(param)[0];
+				filterEquals.put(col, value);
+			}
+			else if(param.startsWith("fin:")) {
+				String col = param.substring(4);
+				String[] values = params.get(param);
+				filterIn.put(col, values);
+			}
+			//XXX: warn unknown parameters
+		}
 	}
 }