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/scripts/lxsql Test2.script
Date Tue, 18 Dec 2001 19:25:24 GMT
hammant     01/12/18 11:25:24

  Modified:    apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql
                        BasicLXSQLSelectSimpleHandler.java
               apps/db/src/java/org/apache/avalon/db/functions
                        NumericFunction.java
               apps/db/src/scripts/lxsql Test2.script
  Added:       apps/db/src/java/org/apache/avalon/db/data/impl
                        DefaultContrivedIntegerFunctionColumn.java
               apps/db/src/java/org/apache/avalon/db/functions/impl
                        AbstractIntegerFunction.java PositionFunction.java
  Log:
  Position function.  First of numerics.
  
  Revision  Changes    Path
  1.11      +45 -10    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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- BasicLXSQLSelectSimpleHandler.java	2001/12/18 14:24:53	1.10
  +++ BasicLXSQLSelectSimpleHandler.java	2001/12/18 19:25:24	1.11
  @@ -23,12 +23,15 @@
   import org.apache.avalon.db.data.impl.AbstractQueryable;
   import org.apache.avalon.db.data.impl.DefaultContrivedStringFunctionColumn;
   import org.apache.avalon.db.data.impl.DefaultStringConstantColumn;
  +import org.apache.avalon.db.data.impl.DefaultContrivedIntegerFunctionColumn;
   import org.apache.avalon.db.functions.impl.ConcatFunction;
   import org.apache.avalon.db.functions.impl.TrimStringFunction;
   import org.apache.avalon.db.functions.impl.LowerCaseStringFunction;
   import org.apache.avalon.db.functions.impl.UpperCaseStringFunction;
  +import org.apache.avalon.db.functions.impl.PositionFunction;
   import org.apache.avalon.db.functions.Function;
   import org.apache.avalon.db.functions.StringFunction;
  +import org.apache.avalon.db.functions.NumericFunction;
   
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
  @@ -44,7 +47,7 @@
    *
   * @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.10 $
  + * @version $Revision: 1.11 $
    */
   public class BasicLXSQLSelectSimpleHandler extends BasicLXSQLHandler {
   
  @@ -182,11 +185,13 @@
           } else if (nodeName.equals("const")) {
               return getSelectColumnConst(queryable, elem);
           } else if (nodeName.equals("trim")) {
  -            return getSelectColumnFromStringFunction(queryable, elem, new TrimStringFunction());
  +            return getStringColumnFromStringFunction(queryable, elem, new TrimStringFunction());
           } else if (nodeName.equals("lowercase")) {
  -            return getSelectColumnFromStringFunction(queryable, elem, new LowerCaseStringFunction());
  +            return getStringColumnFromStringFunction(queryable, elem, new LowerCaseStringFunction());
           } else if (nodeName.equals("uppercase")) {
  -            return getSelectColumnFromStringFunction(queryable, elem, new UpperCaseStringFunction());
  +            return getStringColumnFromStringFunction(queryable, elem, new UpperCaseStringFunction());
  +        } else if (nodeName.equals("position")) {
  +            return getIntegerColumnFromIntegerFunction(queryable, elem, new PositionFunction());
           } else {
               throw new ActionException("Unknown node type '"+nodeName+"' under 'columns'");
           }
  @@ -230,13 +235,13 @@
           return new DefaultStringConstantColumn("TODO",value);
       }
   
  -    private Column getSelectColumnFromStringFunction(AbstractQueryable queryable, Element
trimElem, StringFunction function) throws ActionException {
  -        String as = trimElem.getAttribute("as");
  +    private Column getStringColumnFromStringFunction(AbstractQueryable queryable, Element
aElem, StringFunction function) throws ActionException {
  +        String as = aElem.getAttribute("as");
           // TODO as might be missing
           //return queryable.getColumn(name, true);
           int count = 0;
  -        if (trimElem.hasChildNodes()) {
  -            NodeList subConcatNodes = trimElem.getChildNodes();
  +        if (aElem.hasChildNodes()) {
  +            NodeList subConcatNodes = aElem.getChildNodes();
               Vector colVec = new Vector();
               for (int j=0; j < subConcatNodes.getLength(); j++) {
                   Node node = subConcatNodes.item(j);
  @@ -249,12 +254,42 @@
                   }
               }
               if (colVec.size() > 1) {
  -                throw new ActionException("trim can only have one child column");
  +                throw new ActionException("String function "+aElem.getNodeName()+" can
only have one child column");
               }
               function.initialize((Column) colVec.elementAt(0));
               return new DefaultContrivedStringFunctionColumn(as,function);
           } else {
  -            throw new ActionException("trim must have child column");
  +            throw new ActionException("String function "+aElem.getNodeName()+" must have
child column");
  +        }
  +    }
  +
  +    private Column getIntegerColumnFromIntegerFunction(AbstractQueryable queryable, Element
aElem, NumericFunction function) throws ActionException {
  +        String as = aElem.getAttribute("as");
  +        // TODO as might be missing
  +        //return queryable.getColumn(name, true);
  +        int count = 0;
  +        if (aElem.hasChildNodes()) {
  +            NodeList subConcatNodes = aElem.getChildNodes();
  +            Vector colVec = new Vector();
  +            for (int j=0; j < subConcatNodes.getLength(); j++) {
  +                Node node = subConcatNodes.item(j);
  +                String nodeName =  node.getNodeName();
  +                if (nodeName.equals("#text")) {
  +                    // do nothing
  +                } else {
  +                    count++;
  +                    colVec.add(getSelectColumn(queryable,(Element) node));
  +                }
  +            }
  +            if (colVec.size() > function.getMaxCols() | colVec.size() < function.getMinCols()
) {
  +                throw new ActionException("Numeric function "+aElem.getNodeName()+" must
have between "+function.getMinCols()+" and "+function.getMaxCols()+" ");
  +            }
  +            Column[] cols = new Column[colVec.size()];
  +            colVec.copyInto(cols);
  +            function.initialize(cols);
  +            return new DefaultContrivedIntegerFunctionColumn(as,function);
  +        } else {
  +            throw new ActionException("Numeric function "+aElem.getNodeName()+" must have
child column");
           }
       }
   
  
  
  
  1.1                  jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/data/impl/DefaultContrivedIntegerFunctionColumn.java
  
  Index: DefaultContrivedIntegerFunctionColumn.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.data.impl;
  
  import org.apache.avalon.db.data.types.NumericColumn;
  import org.apache.avalon.db.data.ValidationException;
  import org.apache.avalon.db.data.Row;
  import org.apache.avalon.db.data.Column;
  import org.apache.avalon.db.functions.NumericFunction;
  import org.apache.avalon.db.actions.ActionException;
  
  import java.math.BigDecimal;
  
  
  /**
   * Class DefaultContrivedIntegerFunctionColumn
   *
   *
   * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
   * @version $Revision: 1.1 $
   */
  public class DefaultContrivedIntegerFunctionColumn extends AbstractContrivedColumn implements
NumericColumn {
  
      NumericFunction mNumericFunction;
  
      /**
       * Constructor DefaultContrivedIntegerFunctionColumn
       *
       *
       * @param name
       * @param stringFunction
       *
       */
      public DefaultContrivedIntegerFunctionColumn(String name, NumericFunction numericFunction)
throws ActionException {
          super(getNameOfColumn(name),"integer",Integer.class.getName());
          mNumericFunction = numericFunction;
      }
  
      public Object getValue(Row row) {
          return mNumericFunction.getValue(row);
      }
  
      public String toString(Row row) {
          return (String)mNumericFunction.getValue(row);
      }
  
      public int getPrecesion() {
          return Column.PREC_STRING;
      }
  
      public Integer toInteger(Row row) {
          return new Integer((String)mNumericFunction.getValue(row));
      }
  
      public Long toLong(Row row) {
          return new Long((String)mNumericFunction.getValue(row));
      }
  
      public BigDecimal toBigDecimal(Row row) {
          return new BigDecimal((String)mNumericFunction.getValue(row));
      }
  
      public Float toFloat(Row row) {
          return new Float((String)mNumericFunction.getValue(row));
      }
  
      public Short toShort(Row row) {
          return new Short((String)mNumericFunction.getValue(row));
      }
  
  }
  
  
  
  1.2       +4 -1      jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/functions/NumericFunction.java
  
  Index: NumericFunction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/functions/NumericFunction.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NumericFunction.java	2001/11/17 08:52:56	1.1
  +++ NumericFunction.java	2001/12/18 19:25:24	1.2
  @@ -15,11 +15,14 @@
    * Interface NumericFunction
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version * $Revision: 1.1 $
  + * @version * $Revision: 1.2 $
    */
   public interface NumericFunction extends Function {
       int getIntValue(Row row);
       long getLongValue(Row row);
       float getFloatValue(Row row);
       double getDoubleValue(Row row);
  +
  +    int getMinCols();
  +    int getMaxCols();
   }
  
  
  
  1.1                  jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/functions/impl/AbstractIntegerFunction.java
  
  Index: AbstractIntegerFunction.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.functions.impl;
  
  import org.apache.avalon.db.functions.StringFunction;
  import org.apache.avalon.db.functions.NumericFunction;
  import org.apache.avalon.db.data.Row;
  
  
  /**
   * Class AbstractIntegerFunction
   *
   * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
   * @version $Revision: 1.1 $
   */
  public abstract class AbstractIntegerFunction extends AbstractFunction implements NumericFunction
{
  
      public final Object getValue(Row row) {
          return getIntegerValue(row);
      }
  
      protected abstract Integer getIntegerValue(Row row);
  
      public int getIntValue(Row row) {
          return getIntegerValue(row).intValue();
      }
      public long getLongValue(Row row) {
          return getIntegerValue(row).longValue();
      }
      public float getFloatValue(Row row) {
          return getIntegerValue(row).floatValue();
      }
      public double getDoubleValue(Row row) {
          return getIntegerValue(row).doubleValue();
      }
  
  }
  
  
  
  1.1                  jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/functions/impl/PositionFunction.java
  
  Index: PositionFunction.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.functions.impl;
  
  import org.apache.avalon.db.data.Row;
  import org.apache.avalon.db.data.types.StringColumn;
  import org.apache.avalon.db.functions.StringFunction;
  
  
  /**
   * Class PositionFunction
   *
   * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
   * @version $Revision: 1.1 $
   */
  public class PositionFunction extends AbstractIntegerFunction {
  
      protected Integer getIntegerValue(Row row) {
          String c0 = mColumns[0].toString(row);
          String c1 = mColumns[1].toString(row);
          int ix = c1.indexOf(c0) + 1;
          return new Integer(ix);
      }
  
      public int getMinCols() {
          return 2;
      }
      public int getMaxCols() {
          return 2;
      }
  
  
  }
  
  
  
  1.3       +18 -1     jakarta-avalon-cornerstone/apps/db/src/scripts/lxsql/Test2.script
  
  Index: Test2.script
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/scripts/lxsql/Test2.script,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Test2.script	2001/12/18 14:24:53	1.2
  +++ Test2.script	2001/12/18 19:25:24	1.3
  @@ -1,5 +1,5 @@
   
  -### SELECT 0 (function concat cols) ##!
  +### SELECT 0 (function concat/trim/upper/lower cols) ##!
   
   <simple-select>
      <columns>
  @@ -24,5 +24,22 @@
        <table name="Flintstones"/>
      </from>
   </simple-select>
  +
  +### SELECT 1 (function postion cols) ##!
  +
  +<simple-select>
  +   <columns>
  +     <const value="Calc Position of 'a' in field"/>
  +     <column name="forename"/>
  +     <position>
  +       <const value="a"/>
  +       <column name="forename"/>
  +     </position>
  +   </columns>
  +   <from>
  +     <table name="Flintstones"/>
  +   </from>
  +</simple-select>
  +
   
   ###
  
  
  

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