You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/06/01 12:21:14 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql SqlTagSupport.java UpdateTag.java QueryTag.java

jstrachan    2002/06/01 03:21:14

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/sql
                        UpdateTag.java QueryTag.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/sql
                        SqlTagSupport.java
  Log:
  Refactored code in SQL tags a little bit to share code across the query and update tags.
  
  We could now add smartQuery and smartUpdate tags that allow <param> tags to denote where a ? should be, to make it easier to line up params with '?'
  
  e.g.
  
  <sql:smartQuery>
  update foo set a = <sql:param value="${foo}"/> where b = <sql:param value="${bar}"/>
  </sql:smartQuery>
  
  Revision  Changes    Path
  1.5       +8 -120    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/UpdateTag.java
  
  Index: UpdateTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/UpdateTag.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- UpdateTag.java	30 May 2002 14:58:30 -0000	1.4
  +++ UpdateTag.java	1 Jun 2002 10:21:14 -0000	1.5
  @@ -61,7 +61,6 @@
   import javax.naming.InitialContext;
   import javax.naming.Context;
   import javax.naming.NamingException;
  -import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
   
   import org.apache.commons.jelly.JellyContext;
   import org.apache.commons.jelly.JellyException;
  @@ -76,26 +75,12 @@
    * @author Justyna Horwat
    */
   
  -public class UpdateTag extends TagSupport implements SQLExecutionTag {
  -
  -    private String var;
  -    private String scope = "page";
  -
  -    /*
  -     * The following properties take expression values, so the
  -     * setter methods are implemented by the expression type
  -     * specific subclasses.
  -     */
  -    protected Object rawDataSource;
  -    protected boolean dataSourceSpecified;
  -    protected String sql;
  +public class UpdateTag extends SqlTagSupport {
   
       /*
        * Instance variables that are not for attributes
        */
       private Connection conn;
  -    private List parameters;
  -    private boolean isPartOfTransaction;
   
       //*********************************************************************
       // Constructor and initialization
  @@ -104,44 +89,6 @@
       }
   
       //*********************************************************************
  -    // Accessor methods
  -
  -    /**
  -     * Setter method for the name of the variable to hold the
  -     * result.
  -     */
  -    public void setVar(String var) {
  -        this.var = var;
  -    }
  -
  -    /**
  -     * Setter method for the scope of the variable to hold the
  -     * result.
  -     */
  -    public void setScope(String scopeName) {
  -        this.scope = scopeName;
  -    }
  -
  -    /**
  -     * Setter method for the SQL DataSource. DataSource can be
  -     * a String or a DataSource object.
  -     */
  -    public void setDataSource(Object dataSource) {
  -        this.rawDataSource = dataSource;
  -        this.dataSourceSpecified = true;
  -    }
  -
  -    /**
  -     * Setter method for the SQL statement to use for the
  -     * query. The statement may contain parameter markers
  -     * (question marks, ?). If so, the parameter values must
  -     * be set using nested value elements.
  -     */
  -    public void setSql(String sql) {
  -        this.sql = sql;
  -    }    
  -    
  -    //*********************************************************************
       // Tag logic
   
       /**
  @@ -182,15 +129,15 @@
   
           int result = 0;
           try {
  -            if ( parameters == null || parameters.size() == 0 ) {
  -                Statement statement = conn.createStatement();
  -                result = statement.executeUpdate(sqlStatement);
  -            }
  -            else {
  +            if ( hasParameters() ) {
                   PreparedStatement ps = conn.prepareStatement(sqlStatement);
  -                setParameters(ps, parameters);
  +                setParameters(ps);
                   result = ps.executeUpdate();
               }
  +            else {
  +                Statement statement = conn.createStatement();
  +                result = statement.executeUpdate(sqlStatement);
  +            }
               if (var != null) {
                   context.setVariable(var, new Integer(result));
               }
  @@ -207,66 +154,7 @@
                       // Not much we can do
                   }
               }
  -            parameters = null;
  -            conn = null;
  -        }
  -    }
  -
  -    //*********************************************************************
  -    // Public utility methods
  -
  -    /**
  -     * Called by nested parameter elements to add PreparedStatement
  -     * parameter values.
  -     */
  -    public void addSQLParameter(Object o) {
  -        if (parameters == null) {
  -            parameters = new ArrayList();
  -        }
  -        parameters.add(o);
  -    }
  -
  -    //*********************************************************************
  -    // Private utility methods
  -
  -    private Connection getConnection() throws JellyException, SQLException {
  -        // Fix: Add all other mechanisms
  -        Connection conn = null;
  -        isPartOfTransaction = false;
  -
  -        TransactionTag parent =
  -            (TransactionTag) findAncestorWithClass(TransactionTag.class);
  -        if (parent != null) {
  -            if (dataSourceSpecified) {
  -                throw new JellyException(Resources.getMessage("ERROR_NESTED_DATASOURCE"));
  -            }
  -            conn = parent.getSharedConnection();
  -            isPartOfTransaction = true;
  -        }
  -        else {
  -            if ((rawDataSource == null) && dataSourceSpecified) {
  -                throw new JellyException(Resources.getMessage("SQL_DATASOURCE_NULL"));
  -            }
  -            DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, context);
  -            try {
  -                conn = dataSource.getConnection();
  -            }
  -            catch (Exception ex) {
  -                throw new JellyException(
  -                    Resources.getMessage("DATASOURCE_INVALID", ex.getMessage()));
  -            }
  -        }
  -
  -        return conn;
  -    }
  -
  -    private void setParameters(PreparedStatement ps, List parameters)
  -        throws SQLException {
  -        if (parameters != null) {
  -            for (int i = 0; i < parameters.size(); i++) {
  -                // The first parameter has index 1
  -                ps.setObject(i + 1, parameters.get(i));
  -            }
  +            clearParameters();
           }
       }
   }
  
  
  
  1.9       +8 -106    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/QueryTag.java
  
  Index: QueryTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/QueryTag.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- QueryTag.java	30 May 2002 14:58:30 -0000	1.8
  +++ QueryTag.java	1 Jun 2002 10:21:14 -0000	1.9
  @@ -78,22 +78,16 @@
    * @author Justyna Horwat
    */
   
  -public class QueryTag extends TagSupport implements SQLExecutionTag {
  +public class QueryTag extends SqlTagSupport {
   
       /** The Log to which logging calls will be made. */
       private static final Log log = LogFactory.getLog(QueryTag.class);
   
  -    private String var;
  -    private String scope = "page";
  -
       /*
        * The following properties take expression values, so the
        * setter methods are implemented by the expression type
        * specific subclasses.
        */
  -    protected Object rawDataSource;
  -    protected boolean dataSourceSpecified;
  -    protected String sql;
       protected int maxRows = -1;
       protected boolean maxRowsSpecified;
       protected int startRow;
  @@ -102,8 +96,6 @@
        * Instance variables that are not for attributes
        */
       private Connection conn;
  -    private List parameters;
  -    private boolean isPartOfTransaction;
   
       //*********************************************************************
       // Constructor and initialization
  @@ -115,27 +107,6 @@
       // Accessor methods
   
       /**
  -     * Setter method for the name of the variable to hold the
  -     * result.
  -     */
  -    public void setVar(String var) {
  -        this.var = var;
  -    }
  -
  -    /**
  -     * Setter method for the scope of the variable to hold the
  -     * result.
  -     */
  -    public void setScope(String scopeName) {
  -        this.scope = scopeName;
  -    }
  -
  -    public void setDataSource(Object dataSource) {
  -        this.rawDataSource = dataSource;
  -        this.dataSourceSpecified = true;
  -    }
  -
  -    /**
        * The index of the first row returned can be
        * specified using startRow.
        */
  @@ -152,30 +123,6 @@
           this.maxRowsSpecified = true;
       }
   
  -    /**
  -     * Setter method for the SQL statement to use for the
  -     * query. The statement may contain parameter markers
  -     * (question marks, ?). If so, the parameter values must
  -     * be set using nested value elements.
  -     */
  -    public void setSql(String sql) {
  -        this.sql = sql;
  -    }
  -
  -    //*********************************************************************
  -    // Public utility methods
  -
  -    /**
  -     * Called by nested parameter elements to add PreparedStatement
  -     * parameter values.
  -     */
  -    public void addSQLParameter(Object o) {
  -        if (parameters == null) {
  -            parameters = new ArrayList();
  -        }
  -        parameters.add(o);
  -    }
  -
       //*********************************************************************
       // Tag logic
   
  @@ -256,15 +203,15 @@
               }
               
               ResultSet rs = null;
  -            if ( parameters == null || parameters.size() == 0 ) {
  -                Statement statement = conn.createStatement();
  -                rs = statement.executeQuery(sqlStatement);
  -            }
  -            else {
  +            if ( hasParameters() ) {
                   PreparedStatement ps = conn.prepareStatement(sqlStatement);
  -                setParameters(ps, parameters);            
  +                setParameters(ps);            
                   rs = ps.executeQuery();
               }
  +            else {
  +                Statement statement = conn.createStatement();
  +                rs = statement.executeQuery(sqlStatement);
  +            }
               
               result = new ResultImpl(rs, startRow, maxRows);
               context.setVariable(var, result);
  @@ -281,52 +228,7 @@
                   } // Not much we can do
                   conn = null;
               }
  -            parameters = null;
  -        }
  -    }
  -
  -    //*********************************************************************
  -    // Private utility methods
  -
  -    private Connection getConnection() throws JellyException, SQLException {
  -        // Fix: Add all other mechanisms
  -        Connection conn = null;
  -        isPartOfTransaction = false;
  -
  -        TransactionTag parent =
  -            (TransactionTag) findAncestorWithClass(TransactionTag.class);
  -        if (parent != null) {
  -            if (dataSourceSpecified) {
  -                throw new JellyException(Resources.getMessage("ERROR_NESTED_DATASOURCE"));
  -            }
  -            conn = parent.getSharedConnection();
  -            isPartOfTransaction = true;
  -        }
  -        else {
  -            if ((rawDataSource == null) && dataSourceSpecified) {
  -                throw new JellyException(Resources.getMessage("SQL_DATASOURCE_NULL"));
  -            }
  -            DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, context);
  -            try {
  -                conn = dataSource.getConnection();
  -            }
  -            catch (Exception ex) {
  -                log.error( "Caught: " + ex, ex );
  -                throw new JellyException(
  -                    Resources.getMessage("DATASOURCE_INVALID", ex.getMessage()));
  -            }
  -        }
  -
  -        return conn;
  -    }
  -
  -    private void setParameters(PreparedStatement ps, List parameters)
  -        throws SQLException {
  -        if (parameters != null) {
  -            for (int i = 0; i < parameters.size(); i++) {
  -                // The first parameter has index 1
  -                ps.setObject(i + 1, parameters.get(i));
  -            }
  +            clearParameters();
           }
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/sql/SqlTagSupport.java
  
  Index: SqlTagSupport.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.jelly.tags.sql;
  
  import java.sql.*;
  import java.util.*;
  import javax.sql.DataSource;
  import javax.naming.InitialContext;
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
  
  import org.apache.commons.jelly.JellyContext;
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.tags.Resources;
  
  /**
   * <p>Abstract base class for any SQL related tag in JSTL.  
   * 
   * @author Hans Bergsten
   * @author Justyna Horwat
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   */
  
  public abstract class SqlTagSupport extends TagSupport implements SQLExecutionTag {
  
      protected String var;
      protected String scope = "page";
  
      /*
       * The following properties take expression values, so the
       * setter methods are implemented by the expression type
       * specific subclasses.
       */
      protected Object rawDataSource;
      protected boolean dataSourceSpecified;
      protected String sql;
  
      /*
       * Instance variables that are not for attributes
       */
      private List parameters;
      protected boolean isPartOfTransaction;
  
      //*********************************************************************
      // Constructor and initialization
  
      public SqlTagSupport() {
      }
  
      //*********************************************************************
      // Accessor methods
  
      /**
       * Setter method for the name of the variable to hold the
       * result.
       */
      public void setVar(String var) {
          this.var = var;
      }
  
      /**
       * Setter method for the scope of the variable to hold the
       * result.
       */
      public void setScope(String scopeName) {
          this.scope = scopeName;
      }
  
      /**
       * Setter method for the SQL DataSource. DataSource can be
       * a String or a DataSource object.
       */
      public void setDataSource(Object dataSource) {
          this.rawDataSource = dataSource;
          this.dataSourceSpecified = true;
      }
  
      /**
       * Setter method for the SQL statement to use for the
       * query. The statement may contain parameter markers
       * (question marks, ?). If so, the parameter values must
       * be set using nested value elements.
       */
      public void setSql(String sql) {
          this.sql = sql;
      }    
      
  
      //*********************************************************************
      // Public utility methods
  
      /**
       * Called by nested parameter elements to add PreparedStatement
       * parameter values.
       */
      public void addSQLParameter(Object o) {
          if (parameters == null) {
              parameters = new ArrayList();
          }
          parameters.add(o);
      }
  
      //*********************************************************************
      // Protected utility methods
  
      /**
       * @return true if there are SQL parameters
       */
      protected boolean hasParameters() {
          return parameters != null && parameters.size() > 0;
      }
      
      protected void clearParameters() {
          parameters = null;
      }
  
      protected Connection getConnection() throws JellyException, SQLException {
          // Fix: Add all other mechanisms
          Connection conn = null;
          isPartOfTransaction = false;
  
          TransactionTag parent =
              (TransactionTag) findAncestorWithClass(TransactionTag.class);
          if (parent != null) {
              if (dataSourceSpecified) {
                  throw new JellyException(Resources.getMessage("ERROR_NESTED_DATASOURCE"));
              }
              conn = parent.getSharedConnection();
              isPartOfTransaction = true;
          }
          else {
              if ((rawDataSource == null) && dataSourceSpecified) {
                  throw new JellyException(Resources.getMessage("SQL_DATASOURCE_NULL"));
              }
              DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, context);
              try {
                  conn = dataSource.getConnection();
              }
              catch (Exception ex) {
                  throw new JellyException(
                      Resources.getMessage("DATASOURCE_INVALID", ex.getMessage()));
              }
          }
  
          return conn;
      }
  
      protected void setParameters(PreparedStatement ps)
          throws SQLException {
          if (parameters != null) {
              for (int i = 0; i < parameters.size(); i++) {
                  // The first parameter has index 1
                  ps.setObject(i + 1, parameters.get(i));
              }
          }
      }
  }
  
  
  

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