froehlich    01/11/12 05:28:09

  Modified:    apps/db/src/java/org/apache/avalon/db/basic/actions
                        BasicSelect.java
  Log:
  where hack
  
  Revision  Changes    Path
  1.8       +184 -40   
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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BasicSelect.java  2001/11/11 21:44:47     1.7
  +++ BasicSelect.java  2001/11/12 13:28:08     1.8
  @@ -14,11 +14,13 @@
   import org.apache.avalon.db.results.RowSet;
   import org.apache.avalon.db.results.Columns;
   import org.apache.avalon.db.basic.data.BasicTable;
  +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.w3c.dom.Element;
   import org.w3c.dom.NodeList;
  +import org.w3c.dom.Node;
   
   import java.util.Vector;
   import java.util.Iterator;
  @@ -54,55 +56,197 @@
       public void execute() throws ActionException {
           StringBuffer strbuf = new StringBuffer();
           Vector selectedRows = new Vector();
  -        //Rhino makes problems...causes EOFException...
           BasicTable table = null;
  -        String tablename = "";
  -        try {
  -            NodeList subRootNodes = mRootElement.getChildNodes();
  -            /** traverse the dom tree */
  -            System.out.println("traverse the dom tree");
  -            boolean where = false;
  -            String whereExpr = "";
  -            for (int i = 0 ; i < subRootNodes.getLength(); i++) {
  -                if (subRootNodes.item(i).getNodeName().equals("columns")) {
  -                    //TODO process colums and store them temporary for
  -                    //further usage...
  -                } else if 
(subRootNodes.item(i).getNodeName().equals("from")) {
  -                    NodeList subFromNodes = 
subRootNodes.item(i).getChildNodes();
  -                    for(int j = 0; j < subFromNodes.getLength(); j++) {
  -                        if 
(subFromNodes.item(j).getNodeName().equals("table")) {
  -                            //ToDo store the tablename for further lookups
  -                            Element rowNode = (Element)subFromNodes.item(j);
  -                            tablename = rowNode.getAttribute("name");
  -                            System.out.println("tablename=" + tablename);
  -                            table = (BasicTable) 
mDatabasePersistor.getQueryable(tablename);
  +        RhinoHelper rh = new RhinoHelper();
  +        String[] cols;
  +        String[] tablenames;
  +        Object[] selectedrow = null;
  +
  +        System.out.println("execute(): select");
  +        if(true) {
  +            cols = parseSelect(mRootElement);
  +            tablenames = parseFrom(mRootElement);
  +            parseWhereRoot(mRootElement);
  +            System.out.println("cols.length=" + cols.length);
  +            System.out.println("tablenames.length=" + tablenames.length);
  +        } else {
  +            throw new ActionException("invalid select syntax");
  +        }
  +
  +        selectedRows = new Vector();
  +        if(true) {
  +            /** without where all rows */
  +            for(int i=0; i < tablenames.length; i++) {
  +                /** get the table, should be only one */
  +                table = 
(BasicTable)mDatabasePersistor.getQueryable(tablenames[i]);
  +                Iterator it = table.getRows().iterator();
  +                while(it.hasNext()) {
  +                    /** iterate rows and select only the needed cols */
  +                    BasicRow row = (BasicRow)it.next();
  +                    if(((String)cols[0]).equals("*")) {
  +                        selectedrow = new Object[row.getColumsLength()];
  +                        for (int j=0; j < cols.length; j++) {
  +                           selectedrow[j] = row.getValue(j);
  +                        }
  +                    } else {
  +                        selectedrow = new Object[cols.length];
  +                        for (int j=0; j < cols.length; j++) {
  +                           selectedrow[j] = row.getValue(j);
                           }
                       }
  -                } else if 
(subRootNodes.item(i).getNodeName().equals("where")) {
  -                    where = true;
  -                } //implement where clause
  -            }
  -            if (!where) {
  -                whereExpr = "(true)";
  -            }
  -
  -            Iterator it = table.getRows().iterator();
  -            while (it.hasNext()) {
  -                System.out.println("Row test");
  -                RhinoHelper rh = new RhinoHelper();
  -                rh.addBean("row", it.next());
  -                rh.addBean("selectedRows", selectedRows);
  -                String jsString = "if " + whereExpr + " { 
selectedRows.addElement(row); }";
  -                System.out.println("jsString " + jsString);
  -                rh.executeAction(jsString);
  +                    selectedRows.add(selectedrow);
  +                }
               }
  -            System.out.println("selectedRows size " + selectedRows.size());
  -
               mColumns = new BasicColumns();
               mRowSet = new BasicRowSet(mColumns,selectedRows);
  +        } else {
  +
  +        }
  +    }
  +
  +    private boolean checkSelectStructure(Element rootElement) {
  +        NodeList subRootNodes = mRootElement.getChildNodes();
  +
  +        /** Every select must have one from and a least one table */
  +        for (int i=0 ; i < subRootNodes.getLength(); i++) {
  +            if (subRootNodes.item(i).getNodeName().equals("from")) {
  +                NodeList subFromNodes = subRootNodes.item(i).getChildNodes();
  +                for(int j=0; j < subFromNodes.getLength(); j++) {
  +                    if (subFromNodes.item(j).getNodeName().equals("table") ||
  +                        subFromNodes.item(j).getNodeName().equals("view")
  +                       ) {
  +                        return true;
  +                    } else {
  +                        return false;
  +                    }
  +                }
  +            } else {
  +                return false;
  +            }
  +        }
  +        return false;
  +    }
  +
  +    private String[] parseSelect(Element rootElement) throws ActionException 
{
  +        NodeList subRootNodes = this.getSubRootNodes(rootElement);
  +        String[] cols = null;
  +
  +        for (int i=0; i < subRootNodes.getLength(); i++) {
  +            if(subRootNodes.item(i).getNodeName().equals("columns")) {
  +                Element columnsElement = (Element)subRootNodes.item(i);
  +                
if(columnsElement.getFirstChild().getNodeValue().equals("*")) {
  +                   cols = new String[1];
  +                   cols[0] = "*";
  +                } else if(subRootNodes.item(i).hasChildNodes()) {
  +                    NodeList subColumnsNodes = 
subRootNodes.item(i).getChildNodes();
  +                    cols = new String[subColumnsNodes.getLength()];
  +                    for (int j=0; j < subColumnsNodes.getLength(); j++) {
  +                      Element columnElement = 
(Element)subColumnsNodes.item(j);
  +                      cols[j] = columnElement.getAttribute("name");
  +                    }
  +                } else {
  +                    throw new 
ActionException(columnsElement.getFirstChild().getNodeValue()
  +                                              + " is not valid SQL Syntax");
  +                }
  +            }
  +        }
  +        return cols;
  +    }
  +
  +    private String[] parseFrom(Element rootElement) {
  +        NodeList subRootNodes = this.getSubRootNodes(rootElement);
  +        String[] tablenames = null;
  +
  +        for (int i=0; i < subRootNodes.getLength(); i++) {
  +            if(subRootNodes.item(i).getNodeName().equals("from")) {
  +                NodeList subFromNodes = subRootNodes.item(i).getChildNodes();
  +                tablenames = new String[subFromNodes.getLength()];
  +                for(int j=0; j < subFromNodes.getLength(); j++) {
  +                    Element fromElement = (Element)subFromNodes.item(j);
  +                    tablenames[j] = fromElement.getAttribute("name");
  +                }
  +            }
  +        }
  +        return tablenames;
  +    }
  +
  +    private StringBuffer parseWhereRoot(Element rootElement) {
  +        StringBuffer sb = new StringBuffer();
  +        NodeList subRootNodes = this.getSubRootNodes(rootElement);
  +        for (int i=0; i < subRootNodes.getLength(); i++) {
  +            if(subRootNodes.item(i).getNodeName().equals("where")) {
  +                System.out.println("parseWhereRoot(): found where");
  +                
this.parseWhereTerm((Element)subRootNodes.item(i).getFirstChild(),sb);
  +            }
  +        }
  +        return sb;
  +    }
  +
  +    private StringBuffer parseWhereTerm(Element element, StringBuffer sb) {
  +        try {
  +            System.out.println("Enter parseWhereTerm()");
  +            if(element != null) {
  +                sb.append("{\n");
  +                if(element.getTagName().equals("and")) {
  +                    System.out.println("parseWhereTerm(): found and");
  +                    parseWhereAndTerm(element.getChildNodes(),sb);
  +                } else if(element.getTagName().equals("or")) {
  +                    System.out.println("parseWhereTerm(): found or");
  +                    parseWhereOrTerm(element.getChildNodes(),sb);
  +                } else if(element.getTagName().equals("condition")) {
  +                    System.out.println("parseWhereTerm(): found condition");
  +                    //parseWhereConditionTerm(element,sb);
  +                }
  +                sb.append("}\n");
  +            }
  +            System.out.println("sb=" + sb.toString());
           } catch (Exception e) {
               e.printStackTrace();
           }
  +        return sb;
  +    }
  +
  +    private void parseWhereAndTerm(NodeList nodes, StringBuffer sb) {
  +        try {
  +            System.out.println("parseWhereAndTerm() nodes=" + nodes);
  +            System.out.println("nodes.getLength()=" + nodes.getLength());
  +            for(int i=-1;i<nodes.getLength();i++) {
  +                System.out.println("parseWhereAndTerm(): found and");
  +                sb.append("&\n");
  +                parseWhereTerm((Element)nodes.item(i),sb);
  +            }
  +        } catch(Exception e) {
  +            e.printStackTrace();
  +        }
  +    }
  +
  +    private void parseWhereOrTerm(NodeList nodes, StringBuffer sb) {
  +        try {
  +            System.out.println("parseWhereOrTerm() nodes=" + nodes);
  +            for(int i=0;i<nodes.getLength();i++) {
  +                System.out.println("parseWhereOrTerm(): found or");
  +                sb.append("|\n");
  +                parseWhereTerm((Element)nodes.item(i),sb);
  +            }
  +        } catch(Exception e) {
  +            e.printStackTrace();
  +        }
  +    }
  +
  +    private void parseWhereConditionTerm(Element element, StringBuffer sb) {
  +        try {
  +            /** H
  +            System.out.println("parseWhereConditionTerm(): found condition");
  +            Node nextSibling = (Node)element.getNextSibling();
  +            parseWhereTerm((Element)nextSibling,sb);
  +            sb.append(element.getAttribute("expr") + "\n");
  +        } catch(Exception e) {
  +            e.printStackTrace();
  +        }
  +    }
  +
  +    private NodeList getSubRootNodes(Element rootElement) {
  +        return rootElement.getChildNodes();
       }
   
       public void execute(Object[] params) throws ActionException {
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to