avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hamm...@apache.org
Subject cvs commit: jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql BasicLXSQLCreateAggregateViewHandler.java BasicLXSQLCreateViewHandler.java BasicLXSQLSelectSimpleHandler.java
Date Thu, 29 Nov 2001 08:46:26 GMT
hammant     01/11/29 00:46:26

  Modified:    apps/db/src/java/org/apache/avalon/db/basic/data
                        BasicAggregateView.java BasicView.java
               apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql
                        BasicLXSQLCreateAggregateViewHandler.java
                        BasicLXSQLCreateViewHandler.java
                        BasicLXSQLSelectSimpleHandler.java
  Added:       apps/db/src/java/org/apache/avalon/db/basic/actions
                        BasicSelectSimple.java
  Removed:     apps/db/src/java/org/apache/avalon/db/basic/actions
                        BasicSelect.java
  Log:
  Migration of XML logic to handler from BasicSelect (also renamed)
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/BasicSelectSimple.java
  
  Index: BasicSelectSimple.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.avalon.db.basic.actions;
  
  import org.apache.avalon.db.actions.impl.AbstractAction;
  import org.apache.avalon.db.actions.Select;
  import org.apache.avalon.db.actions.ParameterAdaptable;
  import org.apache.avalon.db.actions.ActionException;
  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.BasicRowSet;
  import org.apache.avalon.db.basic.results.BasicColumns;
  import org.apache.avalon.db.data.Column;
  import org.apache.avalon.db.data.impl.AbstractQueryable;
  import org.apache.avalon.framework.logger.LogEnabled;
  
  import java.util.Vector;
  import java.util.Iterator;
  
  /**
   * This class represents the BasicSelectSimple
   * @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">g-froehlich@gmx.de</a>
   * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
   *
   * @version $Revision: 1.1 $
   */
  public class BasicSelectSimple extends AbstractAction implements Select,ParameterAdaptable,LogEnabled
{
  
      private RowSet mRowSet;
      private Columns mColumns;
      private boolean mPrepared;
      private boolean mWithParams;
      private int mParamCount;
      private AbstractQueryable mQueryable;
      private Column[] mCols;
      private String[] mTablenames;
      private SimpleWhereClause mSimpleWhereClause;
  
      public BasicSelectSimple(String[] tableNames, Column[] columns, SimpleWhereClause whereClause)
{
          mTablenames = tableNames;
          mCols = columns;
          mSimpleWhereClause = whereClause;
      }
  
      public void initialize() throws ActionException {
          getLogger().debug("BasicSelectSimple.initialize()");
          getLogger().debug("cols.length=" + mCols.length);
          getLogger().debug("tablenames.length=" + mTablenames.length);
      }
  
      /**
       * The execute method for this action
       */
      public void execute() throws ActionException {
          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 */
              mQueryable = (AbstractQueryable) mDatabasePersistor.getQueryable(mTablenames[i]);
              Iterator it = mQueryable.getRows().iterator();
              while(it.hasNext()) {
                  /** iterate rows and select only the needed cols */
                  BasicRow row = (BasicRow)it.next();
                  if (mSimpleWhereClause.testActionRow(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);
                  }
              }
              getLogger().debug("selectedRows.size()=" + selectedRows.size());
              mColumns = new BasicColumns();
              mRowSet = new BasicRowSet(mColumns,selectedRows);
          }
      }
  
      public void execute(Object[] params) throws ActionException {
          getLogger().debug("select execute prepared");
      }
  
      public void setPrepared(boolean b) {
          mPrepared = b;
      }
  
      public boolean getPrepared() {
          return mPrepared;
      }
  
      public void setWithParams(boolean b) {
          mWithParams = b;
      }
  
      public boolean getWithParams() {
          return mWithParams;
      }
  
      public RowSet getRowSet() {
          return mRowSet;
      }
  
      public int getParamCount() {
          return this.mParamCount;
      }
  
      public void setParamCount(int i) {
          this.mParamCount = i;
      }
  }
  
  
  
  1.2       +2 -2      jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/data/BasicAggregateView.java
  
  Index: BasicAggregateView.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/data/BasicAggregateView.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BasicAggregateView.java	2001/11/24 20:26:12	1.1
  +++ BasicAggregateView.java	2001/11/29 08:46:26	1.2
  @@ -14,7 +14,7 @@
   import org.apache.avalon.db.data.RowListener;
   import org.apache.avalon.db.data.Row;
   import org.apache.avalon.db.data.DataException;
  -import org.apache.avalon.db.basic.actions.BasicSelect;
  +import org.apache.avalon.db.basic.actions.BasicSelectSimple;
   import org.apache.avalon.db.basic.actions.SimpleWhereClause;
   import org.apache.avalon.db.basic.actions.SimpleWhereClauseMatchingRowCallback;
   
  @@ -26,7 +26,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.1 $
  + * @version $Revision: 1.2 $
    */
   public class BasicAggregateView extends AbstractAggregateView {
   
  
  
  
  1.8       +2 -2      jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/data/BasicView.java
  
  Index: BasicView.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/data/BasicView.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BasicView.java	2001/11/24 18:37:01	1.7
  +++ BasicView.java	2001/11/29 08:46:26	1.8
  @@ -13,7 +13,7 @@
   import org.apache.avalon.db.data.RowListener;
   import org.apache.avalon.db.data.Row;
   import org.apache.avalon.db.data.DataException;
  -import org.apache.avalon.db.basic.actions.BasicSelect;
  +import org.apache.avalon.db.basic.actions.BasicSelectSimple;
   import org.apache.avalon.db.basic.actions.SimpleWhereClause;
   import org.apache.avalon.db.basic.actions.SimpleWhereClauseMatchingRowCallback;
   
  @@ -25,7 +25,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.7 $
  + * @version $Revision: 1.8 $
    */
   public class BasicView extends AbstractView {
   
  
  
  
  1.3       +2 -2      jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLCreateAggregateViewHandler.java
  
  Index: BasicLXSQLCreateAggregateViewHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLCreateAggregateViewHandler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- BasicLXSQLCreateAggregateViewHandler.java	2001/11/27 21:18:22	1.2
  +++ BasicLXSQLCreateAggregateViewHandler.java	2001/11/29 08:46:26	1.3
  @@ -12,7 +12,7 @@
   
   import org.apache.avalon.db.basic.parser.BasicLXSQLParser;
   import org.apache.avalon.db.basic.actions.SimpleWhereClause;
  -import org.apache.avalon.db.basic.actions.BasicSelect;
  +import org.apache.avalon.db.basic.actions.BasicSelectSimple;
   import org.apache.avalon.db.basic.actions.BasicCreateAggregateView;
   import org.apache.avalon.db.services.DatabasePersistor;
   import org.apache.avalon.db.transport.Reply;
  @@ -34,7 +34,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
   public class BasicLXSQLCreateAggregateViewHandler extends BasicLXSQLAggregateHandler {
   
  
  
  
  1.8       +2 -2      jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLCreateViewHandler.java
  
  Index: BasicLXSQLCreateViewHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLCreateViewHandler.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BasicLXSQLCreateViewHandler.java	2001/11/23 12:40:20	1.7
  +++ BasicLXSQLCreateViewHandler.java	2001/11/29 08:46:26	1.8
  @@ -13,7 +13,7 @@
   import org.apache.avalon.db.basic.parser.BasicLXSQLParser;
   import org.apache.avalon.db.basic.actions.BasicCreateView;
   import org.apache.avalon.db.basic.actions.SimpleWhereClause;
  -import org.apache.avalon.db.basic.actions.BasicSelect;
  +import org.apache.avalon.db.basic.actions.BasicSelectSimple;
   import org.apache.avalon.db.services.DatabasePersistor;
   import org.apache.avalon.db.transport.Reply;
   import org.apache.avalon.db.transport.Request;
  @@ -34,7 +34,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.7 $
  + * @version $Revision: 1.8 $
    */
   public class BasicLXSQLCreateViewHandler extends BasicLXSQLQueryableHandler {
   
  
  
  
  1.4       +84 -3     jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLSelectSimpleHandler.java
  
  Index: BasicLXSQLSelectSimpleHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLSelectSimpleHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BasicLXSQLSelectSimpleHandler.java	2001/11/19 09:03:02	1.3
  +++ BasicLXSQLSelectSimpleHandler.java	2001/11/29 08:46:26	1.4
  @@ -11,7 +11,8 @@
   
   
   import org.apache.avalon.db.basic.parser.BasicLXSQLParser;
  -import org.apache.avalon.db.basic.actions.BasicSelect;
  +import org.apache.avalon.db.basic.actions.BasicSelectSimple;
  +import org.apache.avalon.db.basic.actions.SimpleWhereClause;
   import org.apache.avalon.db.services.DatabasePersistor;
   import org.apache.avalon.db.transport.Reply;
   import org.apache.avalon.db.transport.Request;
  @@ -20,9 +21,13 @@
   import org.apache.avalon.db.actions.ActionException;
   import org.apache.avalon.db.actions.Select;
   import org.apache.avalon.db.server.DBRequestContext;
  +import org.apache.avalon.db.data.Column;
  +import org.apache.avalon.db.data.Queryable;
  +import org.apache.avalon.db.data.impl.AbstractQueryable;
   
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
  +import org.w3c.dom.NodeList;
   
   
   /**
  @@ -30,7 +35,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    */
   public class BasicLXSQLSelectSimpleHandler extends BasicLXSQLHandler {
   
  @@ -63,11 +68,87 @@
           SelectRequest cvr = (SelectRequest) request;
           Document doc = mParser.parseLXSQLString(cvr.getSql());
           Element rootElement = doc.getDocumentElement();
  -        Select action = new BasicSelect(rootElement);
   
  +        String[] tables = parseFrom(rootElement);
  +
  +        AbstractQueryable queryable = (AbstractQueryable) mDatabasePersistor.getQueryable(tables[0]);
  +
  +        Select action = new BasicSelectSimple(tables,
  +        convertColumns(queryable, getColumns(rootElement)),
  +        new SimpleWhereClause(rootElement));
  +
           mParser.initializeAction(action);
           action.execute();
   
           return new ResultSetReply(action.getRowSet());
       }
  +
  +
  +    private String[] parseFrom(Element rootElement) {
  +        NodeList subRootNodes = rootElement.getChildNodes();
  +        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;
  +    }
  +
  +    protected Column[] convertColumns(AbstractQueryable queryable, String[] columnNames)
{
  +
  +        //TODO * could be with other contrived columns
  +        if (columnNames[0].equals("*")) {
  +            return queryable.getColumns();
  +        }
  +
  +        Column[] columns = new Column[columnNames.length];
  +        for ( int i = 0; i < columnNames.length; i++ ) {
  +            columns[i] = queryable.getColumn(columnNames[i], false);
  +        }
  +        return columns;
  +    }
  +
  +    private String[] getColumns(Element rootElement) throws ActionException {
  +        NodeList subRootNodes = rootElement.getChildNodes();
  +        String[] cols = null;
  +
  +        for (int i=0; i < subRootNodes.getLength(); i++) {
  +            if(subRootNodes.item(i).getNodeName().equals("columns")) {
  +                Element columnsElement = (Element)subRootNodes.item(i);
  +                getLogger().debug("columnsElement.getFirstChild()=" + columnsElement.getFirstChild().getNodeName());
  +                //First test if *
  +                if(columnsElement.getFirstChild().getNodeName().equals("#text")) {
  +                   if(columnsElement.getFirstChild().getNodeValue().equals("*")) {
  +                       cols = new String[1];
  +                       cols[0] = "*";
  +                   } else {
  +                       throw new ActionException(columnsElement.getFirstChild().getNodeValue()
  +                                              + " is not valid LXSQL Syntax");
  +                   }
  +                } 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++) {
  +                        //TODO checking columns, function...maybe extern syntax checking!?
  +                        Element columnElement = (Element)subColumnsNodes.item(j);
  +                        cols[j] = columnElement.getAttribute("name");
  +                    }
  +                } else {
  +                    throw new ActionException(columnsElement.getFirstChild().getNodeValue()
  +                                              + " is not valid LXSQL Syntax");
  +                }
  +            }
  +        }
  +        return cols;
  +    }
  +
  +
  +
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message