froehlich 01/11/20 08:43:53
Modified: apps/db/src/java/org/apache/avalon/db/basic/actions
BasicSelect.java
Log:
merged "where" logic to SimpleWhereClause
Revision Changes Path
1.29 +30 -219
jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/BasicSelect.java
Index: BasicSelect.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/BasicSelect.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- BasicSelect.java 2001/11/18 20:47:20 1.28
+++ BasicSelect.java 2001/11/20 16:43:53 1.29
@@ -17,61 +17,35 @@
import org.apache.avalon.db.basic.data.BasicRow;
import org.apache.avalon.db.basic.results.BasicColumns;
import org.apache.avalon.db.basic.results.BasicRowSet;
-import org.apache.avalon.db.utils.RhinoHelper;
-import org.apache.avalon.db.utils.StringUtils;
-import org.apache.avalon.db.utils.RhinoException;
-import org.apache.avalon.db.data.ValidationException;
-import org.apache.avalon.db.data.Table;
import org.apache.avalon.db.data.Column;
-import org.apache.avalon.db.data.impl.StringConstantColumn;
import org.apache.avalon.framework.logger.LogEnabled;
-import org.apache.regexp.RE;
-import org.apache.regexp.RESyntaxException;
-
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
-import java.util.Hashtable;
import java.util.Iterator;
-import java.util.Enumeration;
import java.util.Vector;
-import java.util.StringTokenizer;
/**
- * This class represents a the BasicSelect action impl.
+ * This class represents the BasicSelect
* @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]">[EMAIL
PROTECTED]</a>
* @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL
PROTECTED]</a>
- * @version
+ *
+ * @version $Revision: 1.3 $
*/
public class BasicSelect extends AbstractAction implements
Select,ParameterAdaptable,LogEnabled {
- private static Hashtable mOperators;
private RowSet mRowSet;
private Columns mColumns;
private Element mRootElement;
private boolean mPrepared;
private boolean mWithParams;
private int mParamCount;
- private StringBuffer mWhereBuffer = new StringBuffer();
private BasicTable mTable = null;
private Column[] mCols;
private String[] mTablenames;
- static {
- mOperators = new Hashtable();
- addOperator("=","testEqual");
- addOperator(">","testGreaterThan");
- addOperator("<","testSmallerThan");
- addOperator("<>","testNotEqual");
- addOperator("<=","testSmallerThanOrEqual");
- addOperator(">=","testGreaterThanOrEqual");
- addOperator("like","testLike");
- }
- private static void addOperator(String operator, String method) {
- mOperators.put(operator,method);
- }
-
+ private SimpleWhereClause swc;
/**
* The Constructor
@@ -86,7 +60,7 @@
getLogger().debug("BasicSelect.initialize()");
mTablenames = parseFrom(mRootElement);
mCols = convertColumns(parseSelect(mRootElement));
- parseLXSQL(mRootElement);
+ swc = new SimpleWhereClause(mRootElement);
getLogger().debug("cols.length=" + mCols.length);
getLogger().debug("tablenames.length=" + mTablenames.length);
}
@@ -98,81 +72,37 @@
StringBuffer strbuf = new StringBuffer();
Vector selectedRows = new Vector();
Object[] selectedRow = null;
-
selectedRows = new Vector();
- for(int i=0; i < mTablenames.length; i++) {
- /** get the table, should be only one */
- mTable =
(BasicTable)mDatabasePersistor.getQueryable(mTablenames[i]);
- Iterator it = mTable.getRows().iterator();
- while(it.hasNext()) {
- /** iterate rows and select only the needed cols */
- BasicRow row = (BasicRow)it.next();
- if (testRow(row)) {
- getLogger().debug("specific select=" + mCols);
- selectedRow = new Object[mCols.length];
- for (int j=0; j < mCols.length; j++) {
- getLogger().debug("mCols[" + j + "]=" + mCols[j]);
- Object o = mCols[j].getValue(row);
- getLogger().debug("row.getValue(mCols[" + j + "]=" +
o);
- selectedRow[j] = o;
+
+ try {
+ for(int i=0; i < mTablenames.length; i++) {
+ /** get the table, should be only one */
+ mTable =
(BasicTable)mDatabasePersistor.getQueryable(mTablenames[i]);
+ Iterator it = mTable.getRows().iterator();
+ while(it.hasNext()) {
+ /** iterate rows and select only the needed cols */
+ BasicRow row = (BasicRow)it.next();
+ if (swc.testDataRow(row)) {
+ getLogger().debug("specific select=" + mCols);
+ selectedRow = new Object[mCols.length];
+ for (int j=0; j < mCols.length; j++) {
+ getLogger().debug("mCols[" + j + "]=" +
mCols[j]);
+ Object o = mCols[j].getValue(row);
+ getLogger().debug("row.getValue(mCols[" + j +
"]=" + o);
+ selectedRow[j] = o;
+ }
+ selectedRows.add(selectedRow);
}
- selectedRows.add(selectedRow);
}
+ getLogger().debug("selectedRows.size()=" +
selectedRows.size());
+ mColumns = new BasicColumns();
+ mRowSet = new BasicRowSet(mColumns,selectedRows);
}
- getLogger().debug("selectedRows.size()=" + selectedRows.size());
- mColumns = new BasicColumns();
- mRowSet = new BasicRowSet(mColumns,selectedRows);
- }
- }
-
- public boolean testRow(BasicRow row) throws ActionException {
- if(!mWhereBuffer.toString().equals("")) {
- RhinoHelper rh = new RhinoHelper();
- Matched rc = new Matched();
- ColumnCheck cc = new ColumnCheck();
- TestHelper testHelper = new TestHelper();
- rh.addBean("row",row);
- rh.addBean("cc",cc);
- rh.addBean("testHelper",testHelper);
- rh.addBean("rc",rc);
- rh.addBean("table",mTable);
- getLogger().debug("mWhereBuffer.toString()" +
mWhereBuffer.toString());
- try {
- rh.executeScript("if (" + mWhereBuffer.toString() + ") {
rc.itDidMatch(true); } ");
- } catch (RhinoException re) {
- throw new ActionException(re.getMessage());
- }
- return rc.didItMatch();
- } else {
- return true;
- }
- }
-
- public class Matched {
- private boolean boolval = false;
- public void itDidMatch(boolean b) {
- boolval = b;
- }
- boolean didItMatch() {
- return boolval;
+ } catch(Exception e) {
+ getLogger().error("execute(): Exception:",e);
}
}
- public class ColumnCheck {
-
- public Column getColumn(BasicTable table, String name) {
- if(table.getColumn(name,false) == null) {
- return getConstantColumn("constantcolumn",name);
- } else {
- return table.getColumn(name);
- }
- }
-
- private Column getConstantColumn(String name, String value) {
- return new StringConstantColumn(name,value);
- }
- }
-
private boolean checkSelectStructure(Element rootElement) {
NodeList subRootNodes = mRootElement.getChildNodes();
@@ -195,35 +125,7 @@
}
return false;
}
-
- protected void parseLXSQL(Element rootElement) throws ActionException {
- NodeList subRootNodes = this.getSubRootNodes(rootElement);
- boolean whereDone = false;
- StringBuffer whereBuffer = new StringBuffer();
- int indent = 0;
-
- try {
- for (int i=0; i < subRootNodes.getLength(); i++) {
- if(subRootNodes.item(i).getNodeName().equals("where")) {
- if (whereDone) {
- throw new ActionException("<where> term must only be
specified once");
- }
- Element whereElement = (Element)subRootNodes.item(i);
- NodeList elements = whereElement.getChildNodes();
- for (int j=0; j < elements.getLength(); j++) {
- Element element = (Element)elements.item(j);
- parseWhereTerm(element, whereBuffer, indent+1);
- }
- whereDone = true;
- }
- }
- } catch (ValidationException ve) {
- throw new ActionException("Validation Exception: " +
ve.getMessage());
- }
- mWhereBuffer = whereBuffer;
- getLogger().debug("sb=[\n" + whereBuffer + "\n]");
- }
-
+
private String[] parseSelect(Element rootElement) throws ActionException
{
NodeList subRootNodes = this.getSubRootNodes(rootElement);
String[] cols = null;
@@ -273,97 +175,6 @@
}
}
return tablenames;
- }
-
- private void parseWhereTerm(Element element, StringBuffer sb, int
indent) throws ValidationException {
- if(element != null) {
- getLogger().debug("Element != null " + element);
- if(element.getTagName().equals("and")) {
- getLogger().debug("parseWhereTerm(): found and");
- parseWhereAndOrTerm(element.getChildNodes(),sb, indent, "&");
- } else if(element.getTagName().equals("or")) {
- getLogger().debug("parseWhereTerm(): found or");
- parseWhereAndOrTerm(element.getChildNodes(),sb, indent, "|");
- } else if(element.getTagName().equals("condition")) {
- getLogger().debug("parseWhereTerm(): found condition");
- parseWhereConditionTerm(element,sb,indent);
- } else {
- throw new ValidationException("Unknown where term " +
element.getTagName());
- }
- } else {
- getLogger().debug("Element == null " + element);
- getLogger().debug("parseWhereTerm after if");
- throw new ValidationException("Null where element");
- }
- getLogger().debug("parseWhereTerm at the end");
- }
-
- private void parseWhereAndOrTerm(NodeList nodes, StringBuffer sb, int
indent, String andOr) {
- sb.append(getIndent(indent) + "(\n");
- try {
- for(int i=0; i<nodes.getLength(); i++) {
- getLogger().debug("Looping and/or");
- parseWhereTerm((Element)nodes.item(i),sb, indent +1);
- if (i+1 < nodes.getLength()) {
- sb.append( getIndent(indent) + andOr + "\n");
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- sb.append(getIndent(indent) + ")\n");
- }
-
- private void parseWhereConditionTerm(Element element, StringBuffer sb,
int indent) {
- try {
- getLogger().debug("parseWhereConditionTerm(): found condition");
- //Node nextSibling = (Node)element.getNextSibling();
- sb.append(getIndent(indent) +
composeExpr(element.getAttribute("expr")) + "\n");
- //parseWhereTerm((Element)nextSibling,sb);
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
-
- private String getIndent(int indent) {
- String rval = "";
- for (int f = 0; f < indent; f++) {
- rval = rval + " ";
- }
- return rval;
- }
-
- private String composeExpr(String expr) {
- String[] strarr = null;
- String[] result = null;
- String method = null;
- String operator = null;
- getLogger().debug("expr=" + expr);
- try {
- Enumeration enum = mOperators.keys();
- getLogger().debug("mOperators.size()=" + mOperators.size());
- while(enum.hasMoreElements()) {
- operator = (String)enum.nextElement();
- getLogger().debug("matching operator = " + operator);
- getLogger().debug("matching function = " +
mOperators.get(operator));
- RE re = new RE(operator);
- strarr = re.split(expr);
- if(strarr.length > 1) {
- method = (String)mOperators.get(operator);
- getLogger().debug("matched function = " + method);
- result = strarr;
- }
- }
- method = "testHelper." + method + "(cc.getColumn(table,\"" +
result[0]
- + "\"),cc.getColumn(table,\"" + result[1] + "\"),row)";
- getLogger().debug("method=" + method);
-
- } catch (RESyntaxException rse) {
- getLogger().error("composeExpr(): RESyntaxException",rse);
- } catch (Exception e) {
- getLogger().error("composeExpr(): Exception",e);
- }
- return method;
}
private NodeList getSubRootNodes(Element rootElement) {
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>