You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by hu...@apache.org on 2002/10/28 16:18:20 UTC

cvs commit: jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql StorageBeanBase.java StatementUtils.java

husted      2002/10/28 07:18:20

  Modified:    scaffold/src/java/org/apache/commons/scaffold/sql
                        StorageBeanBase.java StatementUtils.java
  Log:
  + StatementUtils, StorageBeanBase: Additional convenience methods.
  
  Revision  Changes    Path
  1.10      +92 -30    jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StorageBeanBase.java
  
  Index: StorageBeanBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StorageBeanBase.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- StorageBeanBase.java	18 Oct 2002 20:44:53 -0000	1.9
  +++ StorageBeanBase.java	28 Oct 2002 15:18:20 -0000	1.10
  @@ -33,7 +33,7 @@
   
   /**
    * Implements StorageBean interface using
  - * <CODE>StatementUtils</CODE> and <CODE>ResultSetUtils</CODE> in
  + * <code>StatementUtils</code> and <code>ResultSetUtils</code> in
    * this package. Specialty operations may be added using the same
    * utilities.
    * <P>
  @@ -42,11 +42,11 @@
    * <LI>Declare a subclass adding the properties to be stored.
    * <LI>Provide in a set of standard SQL queries naming the properties
    * to be stored as columns in the command
  - * <LI>Override <CODE>getPrimaryKey</CODE> to return the property used
  + * <LI>Override <code>getPrimaryKey</code> to return the property used
    * as the primary or unique key in the SQL commands.
  - * <LI>Override <CODE>getParameters</CODE> to return an array of
  + * <LI>Override <code>getParameters</code> to return an array of
    * properties as needed by object's insert and update SQL commands.
  - * <LI>Override <CODE>getPrefix</CODE> to set a unique prefix
  + * <LI>Override <code>getPrefix</code> to set a unique prefix
    * for each StorageBean class.
    * Include the prefix in the resource file, but not in the Java code.
    * <LI>Provide keys.next and keys.inc SQL commands to obtain a new
  @@ -55,14 +55,14 @@
    * <P>
    * <B>To specify a connection pool and load the SQL commands:</B>
    * <UL>
  - * <LI>Load the <CODE>ConnectionAdaptor</CODE> for your database
  + * <LI>Load the <code>ConnectionAdaptor</code> for your database
    * connection pool
  - * <LI>Load the <CODE>Properties</CODE> file with your commands and
  - * pass it to the StorageBeanBase <CODE>init</CODE> method.
  + * <LI>Load the <code>Properties</code> file with your commands and
  + * pass it to the StorageBeanBase <code>init</code> method.
    * </UL>
  - * <P>The <CODE>ConnectionServlet</CODE> in this package is designed
  - * to load <CODE>ConnectionAdapters<CODE> and resource
  - * (<CODE>Properties</CODE>) files.
  + * <P>The <code>ConnectionServlet</code> in this package is designed
  + * to load <code>ConnectionAdapters<code> and resource
  + * (<code>Properties</code>) files.
    * <P>
    * Regarding the queries:
    * <UL>
  @@ -303,7 +303,7 @@
   // ------------------------------------------------------------ Public Methods
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public void populate(Map parameters) throws Exception {
   
           if (parameters!=null) {
  @@ -316,7 +316,7 @@
   // ------------------------------------------------------------ Create Methods
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public void executeUpdate(String command)
               throws ResourceException {
   
  @@ -348,10 +348,54 @@
   
       } // end executeUpdate
   
  -// --------------------------------------------------------------------- Retrieval Methods
  +// ----------------------------------------------------------- Retrieval Methods
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
  +    public int count(
  +            String command,
  +            Object parameter)
  +            throws ResourceException {
  +
  +        Integer result = null;
  +        try {
  +            result = (Integer) StatementUtils.getColumn(
  +                null,
  +                1,
  +                getCommand(command),
  +                parameter
  +            );
  +        }
  +        catch (SQLException e) {
  +            throw new ResourceException(e);
  +        }
  +        return result.intValue();
  +    }
  +
  +
  +        // See interface for JavaDoc
  +    public int countLike(
  +            String command,
  +            String parameter)
  +            throws ResourceException {
  +
  +        Integer result = null;
  +        try {
  +            result = (Integer) StatementUtils.getColumnLike(
  +                null,
  +                1,
  +                getCommand(command),
  +                parameter
  +            );
  +        }
  +        catch (SQLException e) {
  +            throw new ResourceException(e);
  +        }
  +        return result.intValue();
  +    }
  +
  +
  +        // See interface for JavaDoc
       public boolean findElement(
               Object target,
               String command,
  @@ -374,7 +418,7 @@
      } // end findElement
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public Collection findCollection(Object target,
           String command, Object[] parameters) throws ResourceException {
   
  @@ -393,7 +437,7 @@
   
       /**
        * Convenience method that calls
  -     * <CODE>findCollection(Object,String,Object[])</CODE>
  +     * <code>findCollection(Object,String,Object[])</code>
        * with appropriate parameters.
        */
       public Collection findCollection(Object target,
  @@ -406,7 +450,7 @@
   
       /**
        * Convenience method that calls
  -     * <CODE>findCollection(Object,String,Object[])</CODE>
  +     * <code>findCollection(Object,String,Object[])</code>
        * with appropriate parameters.
        */
       protected final Collection findCollection(Object target,
  @@ -422,7 +466,7 @@
   
       /**
        * Convenience method that calls
  -     * <CODE>findCollection(Object,String,Object[])</CODE>
  +     * <code>findCollection(Object,String,Object[])</code>
        * with appropriate parameters.
        */
       protected final Collection findCollection(Object target,
  @@ -434,7 +478,7 @@
   
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public Collection findCollectionLike(Object target,
           String command, String parameter) throws ResourceException {
   
  @@ -452,7 +496,25 @@
   
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
  +    public Collection findCollectionLike(Object target,
  +        String command, Object[] parameters) throws ResourceException {
  +
  +        try {
  +
  +            return StatementUtils.getCollectionLike(null,
  +                target,getCommand(command),parameters, 0);
  +
  +        }
  +        catch (SQLException e) {
  +            throw new ResourceException(e);
  +        }
  +
  +    } // end findCollectionLike
  +
  +
  +
  +        // See interface for JavaDoc
       public Collection findByProperty(
               Object target,
               String property,
  @@ -478,13 +540,13 @@
       private Object primaryKey = null;
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public Object getPrimaryKey() {
           return this.primaryKey;
       }
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public void setPrimaryKey(Object primaryKey) {
           this.primaryKey = primaryKey;
       }
  @@ -597,13 +659,13 @@
       private int resultCode = 0;
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public int getResultCode() {
           return resultCode;
       }
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public void setResultCode(int resultCode) {
           this.resultCode = resultCode;
       }
  @@ -615,13 +677,13 @@
       private Object result = this;
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public Object getResult() {
           return result;
       }
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public void setResult(Object result) {
           this.result = result;
       }
  @@ -641,13 +703,13 @@
       public static String KEYS_INC = "keys.inc";
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public boolean isNew() {
           return (null==getPrimaryKey());
       }
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
           // @todo Refactor to use fixed-length high/low String as key.
       public Object createKey(String keyName)
               throws ResourceException {
  @@ -674,7 +736,7 @@
       } // end createKey()
   
   
  -        // See inteface for JavaDoc
  +        // See interface for JavaDoc
       public void allocateKey() throws Exception {
   
           setPrimaryKey(createKey(TABLE));
  
  
  
  1.3       +57 -1     jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StatementUtils.java
  
  Index: StatementUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StatementUtils.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StatementUtils.java	6 Sep 2002 21:47:16 -0000	1.2
  +++ StatementUtils.java	28 Oct 2002 15:18:20 -0000	1.3
  @@ -472,6 +472,32 @@
       } // end getColumn
   
   
  +    /**
  +     * Convenience method to wrap a string for a command that
  +     * uses the <code>LIKE</code> operator.
  +     * Calls <code>getCollection(String,Object,String,Object[]);</code>
  +     *
  +     * @param resource The database resource key or null for default
  +     * @param target The JavaBean object to return in the collection.
  +     * @param command The SQL statement to prepare and execute.
  +     * @param key The replaceable parameter to use with LIKE.
  +     * @exception SQLException if SQL error occurs
  +     */
  +    public static final Object getColumnLike(
  +            String resource,
  +            int column,
  +            String command,
  +            Object key)
  +        throws SQLException {
  +
  +        Object[] parameters = new Object[1];
  +        parameters[0] = new String("%" + key.toString() + "%");
  +
  +        return getColumn(resource,column,command,parameters);
  +
  +    } // end getColumnLike
  +
  +
   // ------------------------------------------------------ getElement
   
       /**
  @@ -752,7 +778,37 @@
       } // end getCollectionLike
   
   
  +    /**
  +     * Convenience method to wrap a string for a command that
  +     * uses the <code>LIKE</code> operator.
  +     *
  +     * Calls <code>getCollection(String,Object,String,Object[]);</code>
  +     *
  +     * @param resource The database resource key or null for default
  +     * @param target The JavaBean object to return in the collection.
  +     * @param command The SQL statement to prepare and execute.
  +     * @param parameters The replaceable parameters to use with query
  +     * @param index The parameter index that will be used with LIKE (0-based)
  +     * @exception SQLException if SQL error occurs
  +     */
  +    public static final Collection getCollectionLike(
  +            String resource,
  +            Object target,
  +            String command,
  +            Object[] parameters,
  +            int index)
  +        throws SQLException {
  +
  +        parameters[index] = new String("%" + parameters[index].toString() + "%");
  +
  +        return getCollection(resource,target,command,parameters);
  +
  +    } // end getCollectionLike
  +
  +
   } // end StatementUtils
  +
  +
   
   
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>