hammant     01/11/04 15:34:40

  Modified:    apps/db/src/java/org/apache/avalon/db/bcel/data
                        BCELTable.java
               apps/db/src/java/org/apache/avalon/db/bcel/parser
                        LXSQLParser.java
               apps/db/src/java/org/apache/avalon/db/test Tester.java
  Added:       apps/db/src/java/org/apache/avalon/db/bcel/actions
                        BCELInsert.java
  Log:
  Start of InsertInto implementation
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/actions/BCELInsert.java
  
  Index: BCELInsert.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.bcel.actions;
  
  import org.apache.avalon.db.actions.Insert;
  import org.apache.avalon.db.actions.ActionException;
  import org.apache.avalon.db.bcel.data.BCELTable;
  import org.apache.avalon.db.data.impl.AbstractTableRow;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NodeList;
  
  import java.lang.reflect.Method;
  import java.lang.reflect.InvocationTargetException;
  
  
  /**
   * Class BCELInsert
   *
   *
   * @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
   * @version $Revision: 1.1 $
   */
  public class BCELInsert extends AbstractBCELAction implements Insert {
  
      private Document mDocument;
      private BCELTable mTable;
      private int mRecordCount;
  
      /**
       * Constructor BCELInsert
       *
       *
       * @param table
       * @param document
       *
       */
      public BCELInsert(BCELTable table, Document document) {
          mTable = table;
          mDocument = document;
      }
  
  
  
      /**
       * Method execute
       *
       *
       */
      public void execute() throws ActionException {
  
          Class rowClass = mTable.getRowClass();
  
          Element rootElement = mDocument.getDocumentElement();
  
          NodeList subRootNodes = rootElement.getChildNodes();
  
          for (int f = 0 ; f < subRootNodes.getLength(); f++) {
              if (subRootNodes.item(f).getNodeName().equals("rows")) {
                  NodeList subRowsNodes = subRootNodes.item(f).getChildNodes();
                  AbstractTableRow pendingRow;
                  try {
                      pendingRow = (AbstractTableRow) rowClass.newInstance();
                  } catch (Throwable t) {
                      throw new RuntimeException("Some problem instantiating " 
+ rowClass.getName());
                  }
                  for (int x = 0 ; x < subRowsNodes.getLength(); x++) {
                      if (subRowsNodes.item(x).getNodeName().equals("row")) {
                          mRecordCount++;
                          NodeList subRowNodes = 
subRootNodes.item(f).getChildNodes();
                          for (int y = 0 ; y < subRowNodes.getLength(); y++) {
                              if 
(subRowNodes.item(y).getNodeName().equals("value")) {
                                  Element rowNode = (Element) 
subRowNodes.item(y);
                                  String colName = 
rowNode.getAttribute("column");
                                  String valueStr = rowNode.getNodeValue();
                                  try {
                                      Method meth = 
rowClass.getDeclaredMethod("set" + colName, new Class[] {String.class});
                                      meth.invoke(pendingRow, new Object[] 
{valueStr});
                                  } catch (IllegalAccessException iae) {
                                      throw new RuntimeException("TODO");
                                  } catch (NoSuchMethodException nsme) {
                                      throw new RuntimeException("TODO");
                                  } catch (InvocationTargetException ite) {
                                      throw new RuntimeException("TODO");
                                      // TODO this one could contain a 
validation exception.
                                  }
                              }
                          }
                      } else {
                          // TODO
                      }
                  }
                  mTable.addRow(pendingRow);
              } else {
                  //TODO
              }
          }
      }
  
      public int getRecordCount() {
          return mRecordCount;
      }
  }
  
  
  
  1.2       +5 -0      
jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/data/BCELTable.java
  
  Index: BCELTable.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/data/BCELTable.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BCELTable.java    2001/10/31 14:42:14     1.1
  +++ BCELTable.java    2001/11/04 23:34:40     1.2
  @@ -19,4 +19,9 @@
           super(name, columns);
           mRowClass = rowClass;
       }
  +
  +    public Class getRowClass() {
  +        return mRowClass;
  +    }
  +
   }
  
  
  
  1.7       +30 -11    
jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/parser/LXSQLParser.java
  
  Index: LXSQLParser.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/bcel/parser/LXSQLParser.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- LXSQLParser.java  2001/11/04 22:43:04     1.6
  +++ LXSQLParser.java  2001/11/04 23:34:40     1.7
  @@ -22,6 +22,7 @@
   import org.apache.avalon.db.bcel.actions.BCELCreateTable;
   import org.apache.avalon.db.bcel.actions.BCELInsertSingleValue;
   import org.apache.avalon.db.bcel.actions.AbstractBCELAction;
  +import org.apache.avalon.db.bcel.actions.BCELInsert;
   import org.apache.avalon.db.bcel.data.BCELTable;
   import org.apache.avalon.db.data.Table;
   import org.apache.avalon.db.data.Column;
  @@ -74,7 +75,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
  - * @version $Revision: 1.6 $
  + * @version $Revision: 1.7 $
    */
   public class LXSQLParser extends BaseBCELParser {
   
  @@ -134,8 +135,7 @@
           StringReader sr = new StringReader(docStr);
           InputSource is = new InputSource(sr);
   
  -        try
  -        {
  +        try {
               Document doc = mDocumentBuilder.parse(is);
   
               Element rootElement = doc.getDocumentElement();
  @@ -192,13 +192,9 @@
               action.setDatabasePersistor(mDatabasePersistor);
               ((AbstractBCELAction) action).setBCELParser(this);
               return action;
  -        }
  -        catch (SAXException se)
  -        {
  +        } catch (SAXException se) {
               throw new ActionException(se.getMessage());
  -        }
  -        catch (IOException ioe)
  -        {
  +        } catch (IOException ioe) {
               throw new ActionException(ioe.getMessage());
           }
   
  @@ -216,9 +212,32 @@
        */
       public Insert createInsertAction(InsertRequest request) throws 
ActionException {
   
  -        //TODO
  +        String docStr = XMLHDR + request.getSql();
  +        System.out.println("docStr=" + docStr);
   
  -        return null;
  +        StringReader sr = new StringReader(docStr);
  +        InputSource is = new InputSource(sr);
  +
  +        try {
  +            Document doc = mDocumentBuilder.parse(is);
  +
  +            Element rootElement = doc.getDocumentElement();
  +            String tableName = rootElement.getAttribute("table-name");
  +
  +            BCELTable table = (BCELTable) 
mDatabasePersistor.getTable(tableName);
  +
  +            Insert action = new BCELInsert(table, doc);
  +            action.setDatabasePersistor(mDatabasePersistor);
  +            ((AbstractBCELAction) action).setBCELParser(this);
  +
  +            return action;
  +
  +
  +        } catch (SAXException se) {
  +            throw new ActionException(se.getMessage());
  +        } catch (IOException ioe) {
  +            throw new ActionException(ioe.getMessage());
  +        }
       }
   
   }
  
  
  
  1.6       +7 -7      
jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/test/Tester.java
  
  Index: Tester.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/test/Tester.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Tester.java       2001/11/04 22:43:04     1.5
  +++ Tester.java       2001/11/04 23:34:40     1.6
  @@ -37,20 +37,20 @@
           st.close();
   
           st = mCon.createStatement();
  -        String insertInto = "<insert-into name=\"Flintstones\">" +
  +        String insertInto = "<insert-into table-name=\"Flintstones\">" +
                                  "<columns astable\"true\"/>" +
                                  "<rows>" +
                                    "<row>" +
  -                                   "<value>Fred</value>" +
  -                                   "<value>Flintsone</value>" +
  +                                   "<value col=\"forname\">Fred</value>" +
  +                                   "<value 
col=\"surname\">Flintsone</value>" +
                                    "</row>" +
                                    "<row>" +
  -                                   "<value>Wilma</value>" +
  -                                   "<value>Flintsone</value>" +
  +                                   "<value col=\"forname\">Wilma</value>" +
  +                                   "<value 
col=\"surname\">Flintsone</value>" +
                                    "</row>" +
                                    "<row>" +
  -                                   "<value>Barney</value>" +
  -                                   "<value>Rubble</value>" +
  +                                   "<value col=\"forname\">Barney</value>" +
  +                                   "<value col=\"surname\">Rubble</value>" +
                                    "</row>" +
                                  "<rows>" +
                                "</insert-into>";
  
  
  

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

Reply via email to