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 BasicLXSQLUpdateRowsHandler.java
Date Fri, 23 Nov 2001 08:21:54 GMT
hammant     01/11/23 00:21:54

  Modified:    apps/db/src/java/org/apache/avalon/db/basic/actions
                        BasicSelect.java BasicUpdateRows.java
                        SimpleWhereClause.java
               apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql
                        BasicLXSQLUpdateRowsHandler.java
  Log:
  UPDATE works - for basic
  
  Revision  Changes    Path
  1.31      +22 -26    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.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- BasicSelect.java	2001/11/23 07:24:57	1.30
  +++ BasicSelect.java	2001/11/23 08:21:53	1.31
  @@ -45,7 +45,7 @@
       private BasicTable mTable = null;
       private Column[] mCols;
       private String[] mTablenames;
  -    private SimpleWhereClause swc;
  +    private SimpleWhereClause mSimpleWhereClause;
   
       /**
        * The Constructor
  @@ -60,7 +60,7 @@
           getLogger().debug("BasicSelect.initialize()");
           mTablenames = parseFrom(mRootElement);
           mCols = convertColumns(parseSelect(mRootElement));
  -        swc = new SimpleWhereClause(mRootElement);
  +        mSimpleWhereClause = new SimpleWhereClause(mRootElement);
           getLogger().debug("cols.length=" + mCols.length);
           getLogger().debug("tablenames.length=" + mTablenames.length);
       }
  @@ -74,33 +74,29 @@
           Object[] selectedRow = null;
           selectedRows = new Vector();
   
  -//        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.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);
  +        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 (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);
               }
  -  //      } catch(Exception e) {
  -    //        getLogger().error("execute(): Exception:",e);
  -      //  }
  +            getLogger().debug("selectedRows.size()=" + selectedRows.size());
  +            mColumns = new BasicColumns();
  +            mRowSet = new BasicRowSet(mColumns,selectedRows);
  +        }
       }
   
       private boolean checkSelectStructure(Element rootElement) {
  
  
  
  1.4       +10 -4     jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/BasicUpdateRows.java
  
  Index: BasicUpdateRows.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/BasicUpdateRows.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BasicUpdateRows.java	2001/11/18 20:47:20	1.3
  +++ BasicUpdateRows.java	2001/11/23 08:21:53	1.4
  @@ -22,7 +22,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 BasicUpdateRows extends AbstractAction
           implements UpdateRows, LogEnabled, SimpleWhereClauseMatchingRowCallback {
  @@ -54,10 +54,16 @@
        *
        */
       public void execute() throws ActionException {
  -
  +        // TODO use the testActionRows method
           Iterator it = mQueryable.getRows().iterator();
  -        mWhereClause.testActionRows(it, this);
  -
  +        //mWhereClause.testActionRows(it, this);
  +        while(it.hasNext()) {
  +            /** iterate rows and select only the needed cols */
  +            BasicRow row = (BasicRow)it.next();
  +            if (mWhereClause.testActionRow(row)) {
  +                rowMatches(row,true);
  +            }
  +        }
       }
   
       /**
  
  
  
  1.6       +14 -5     jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/SimpleWhereClause.java
  
  Index: SimpleWhereClause.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/SimpleWhereClause.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SimpleWhereClause.java	2001/11/22 17:41:41	1.5
  +++ SimpleWhereClause.java	2001/11/23 08:21:53	1.6
  @@ -31,7 +31,7 @@
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
    * @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">g-froehlich@gmx.de</a>
  - * @version $Revision: 1.5 $
  + * @version $Revision: 1.6 $
    */
   public class SimpleWhereClause extends AbstractLogEnabled {
       private String mWhereClause;
  @@ -39,6 +39,8 @@
       private String[] mTablenames;
       private Element mElement;
       private static Hashtable mOperators;
  +    private String mMatchingInstructions = "callBack.rowMatches(row, true);\n";
  +    private String mUnMatchingInstructions = "callBack.rowMatches(row, false);\n";
       static {
           mOperators = new Hashtable();
           addOperator("=","testEqual");
  @@ -53,6 +55,12 @@
           mOperators.put(operator,method);
       }
   
  +    public SimpleWhereClause(Element rootElement, String matchingInstructions,  String
unMatchingInstructions) {
  +        this(rootElement);
  +        mMatchingInstructions = matchingInstructions;
  +        mUnMatchingInstructions = unMatchingInstructions;
  +    }
  +
       public SimpleWhereClause(Element rootElement) {
           try {
               setupLogger(this);
  @@ -100,9 +108,9 @@
                 "do {\n" +
                   "var row = iterator.next();\n" +
                   "if ( " + mWhereClause + " ) {\n" +
  -                  "callBack.rowMatches(row, true);\n" +
  +                  mMatchingInstructions +
                   "} else {\n" +
  -                  "callBack.rowMatches(row, false);\n" +
  +                  mUnMatchingInstructions +
                   "}\n" +
                 "} while(iterator.hasNext())\n" +
               "}\n";
  @@ -140,9 +148,9 @@
               //getLogger().debug("mWhereClause" + mWhereClause.toString());
               rh.executeScript(
                    "if (" + mWhereClause.toString() + ") { " +
  -                    "callBack.rowMatches(row, true);" +
  +                  mMatchingInstructions +
                    "} else {" +
  -                    "callBack.rowMatches(row, false);" +
  +                  mUnMatchingInstructions +
                    "}");
               return mh.mMatched;
           } else {
  @@ -150,6 +158,7 @@
           }
       }
   
  +    // TODO this needs to move to an abstract handler, or a helper class at that level.
       private void parseLXSQL(Element rootElement) throws ActionException {
           NodeList subRootNodes = this.getSubRootNodes(rootElement);
           boolean whereDone = false;
  
  
  
  1.5       +31 -2     jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLUpdateRowsHandler.java
  
  Index: BasicLXSQLUpdateRowsHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLUpdateRowsHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- BasicLXSQLUpdateRowsHandler.java	2001/11/20 16:44:11	1.4
  +++ BasicLXSQLUpdateRowsHandler.java	2001/11/23 08:21:54	1.5
  @@ -26,6 +26,7 @@
   
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
  +import org.w3c.dom.NodeList;
   
   
   /**
  @@ -33,7 +34,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.4 $
  + * @version $Revision: 1.5 $
    */
   public class BasicLXSQLUpdateRowsHandler extends BasicLXSQLHandler {
   
  @@ -69,7 +70,16 @@
           String queryableStr = rootElement.getAttribute("from");
           Queryable queryable = (Queryable) mDatabasePersistor.getQueryable(queryableStr);
   
  -        SimpleWhereClause swc = new SimpleWhereClause(rootElement);
  +        String matchingInstructions = null;
  +
  +        NodeList subRootNodes = rootElement.getChildNodes();
  +        for (int i=0; i < subRootNodes.getLength(); i++) {
  +            if(subRootNodes.item(i).getNodeName().equals("updates")) {
  +                matchingInstructions = getMatchingInstructions((Element) subRootNodes.item(i));
  +            }
  +        }
  +
  +        SimpleWhereClause swc = new SimpleWhereClause(rootElement, matchingInstructions,
"");
           swc.enableLogging(super.getLogger());
   
           UpdateRows action = new BasicUpdateRows((AbstractQueryable) queryable, swc);
  @@ -79,4 +89,23 @@
   
           return new UpdateReply(action.getRecordCount());
       }
  +
  +   private String getMatchingInstructions(Element updatesElement) throws ActionException
{
  +        NodeList subUpdatesNodes = updatesElement.getChildNodes();
  +        StringBuffer instructionBuffer = new StringBuffer();
  +        for (int i=0; i < subUpdatesNodes.getLength(); i++) {
  +            if(subUpdatesNodes.item(i).getNodeName().equals("set")) {
  +                setSetInstruction((Element) subUpdatesNodes.item(i), instructionBuffer);
  +            } else {
  +                throw new ActionException("Element 'set' is the only element allowed beneath
'updates'");
  +            }
  +        }
  +       return instructionBuffer.toString();
  +   }
  +
  +   private void setSetInstruction(Element setElement, StringBuffer instructionBuffer) throws
ActionException {
  +       String name = setElement.getAttribute("name");
  +       String value = setElement.getAttribute("value");
  +       instructionBuffer.append("row.setValue('"+name+"','" + value + "');\n");
  +   }
   }
  
  
  

--
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