cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Davanum Srinivas <d...@yahoo.com>
Subject Re: [c2] patch for database actions
Date Fri, 18 May 2001 10:49:19 GMT
Donald,

Here's my +1 for the patch.

Thanks,
dims

--- Donald Ball <balld@webslingerZ.com> wrote:
> attached is a patch for the database actions to allow the database add
> action to add multiple rows to multiple tables from one form of values. a
> sample form descriptor is:
> 
> <map>
>   <connection>test</connection>
>   <table name="product_table">
>     <keys>
>       <key param="id" dbcol="id" type="int" mode="form"/>
>     </keys>
>     <values>
>       <value param="name" dbcol="name" type="string"/>
>     </values>
>   </table>
>   <table name="product_meta_element_table">
>     <keys>
>       <key param="id" dbcol="product_id" type="int" mode="form"/>
>       <key param="product_meta_element_name_*_param" dbcol="name" type="string" mode="form"/>
>     </keys>
>     <values>
>       <value param="product_meta_element_value_*_param" dbcol="value" type="string"/>
>     </values>
>   </table>
> </map:
> 
> note that all of a table's keys must be mode="form" if you're adding many
> rows to the same table, or if the same new key value is used in multiple
> tables. i'm gonna be fixing that. anyway, it's a pretty sizable patch so i
> thought i'd flip it out for y'all to peruse before i committed it. if
> berin doesn't want this added to the main action set i'm happy to commit
> it as a new action.
> 
> - donald
> > Index: src/org/apache/cocoon/acting/AbstractDatabaseAction.java
> ===================================================================
> RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/acting/AbstractDatabaseAction.java,v
> retrieving revision 1.1.1.1
> diff -u -3 -p -r1.1.1.1 AbstractDatabaseAction.java
> --- src/org/apache/cocoon/acting/AbstractDatabaseAction.java	2001/05/09 20:50:04	1.1.1.1
> +++ src/org/apache/cocoon/acting/AbstractDatabaseAction.java	2001/05/17 18:51:10
> @@ -311,22 +311,28 @@ public abstract class AbstractDatabaseAc
>       */
>      protected void setColumn(PreparedStatement statement, int position, Request request,
> Configuration entry)
>      throws Exception {
> -        Integer typeObject = (Integer)
> AbstractDatabaseAction.typeConstants.get(entry.getAttribute("type"));
> -
> -        if (typeObject == null) {
> -            throw new SQLException("Can't set column because the type is invalid");
> -        }
> +        setColumn(statement,position,request,entry,entry.getAttribute("param",""));
> +    }
>  
> -        String attribute = entry.getAttribute("param", "");
> +    /**
> +     * Set the Statement column so that the results are mapped correctly.
> +     */
> +    protected void setColumn(PreparedStatement statement, int position, Request request,
> Configuration entry, String attribute)
> +    throws Exception {
>          Object value = request.getParameter(attribute);
> -
>          if (value == null) value = request.getAttribute(attribute);
>          if (value == null) value = request.get(attribute);
> +        setColumn(statement,position,request,entry,attribute,value);
> +    }
>  
> +    protected void setColumn(PreparedStatement statement, int position, Request request,
> Configuration entry, String attribute, Object value) throws Exception {
>          if (value instanceof String) {
>              value = ((String) value).trim();
>          }
> -
> +        Integer typeObject = (Integer)
> AbstractDatabaseAction.typeConstants.get(entry.getAttribute("type"));
> +        if (typeObject == null) {
> +            throw new SQLException("Can't set column because the type is invalid");
> +        }
>          if (value == null || "".equals(value)) {
>              switch (typeObject.intValue()) {
>                  case Types.DISTINCT:
> Index: src/org/apache/cocoon/acting/DatabaseAddAction.java
> ===================================================================
> RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/acting/DatabaseAddAction.java,v
> retrieving revision 1.1.1.1
> diff -u -3 -p -r1.1.1.1 DatabaseAddAction.java
> --- src/org/apache/cocoon/acting/DatabaseAddAction.java	2001/05/09 20:50:05	1.1.1.1
> +++ src/org/apache/cocoon/acting/DatabaseAddAction.java	2001/05/17 18:51:11
> @@ -16,6 +16,7 @@ import java.util.ArrayList;
>  import java.util.HashMap;
>  import java.util.Iterator;
>  import java.util.Map;
> +import java.util.Enumeration;
>  import org.apache.avalon.framework.component.Component;
>  import org.apache.avalon.framework.component.ComponentException;
>  import org.apache.avalon.framework.configuration.Configurable;
> @@ -49,11 +50,9 @@ public class DatabaseAddAction extends A
>      public Map act(EntityResolver resolver, Map objectModel, String source, Parameters
param)
> throws Exception {
>          DataSourceComponent datasource = null;
>          Connection conn = null;
> -        int currentIndex = 0;
>  
>          try {
>              Configuration conf = this.getConfiguration(param.getParameter("form-descriptor",
> null));
> -            String query = this.getAddQuery(conf);
>  
>              datasource = this.getDataSource(conf);
>              conn = datasource.getConnection();
> @@ -63,40 +62,12 @@ public class DatabaseAddAction extends A
>                  conn.setAutoCommit(false);
>              }
>  
> -            PreparedStatement statement = conn.prepareStatement(query);
> -
> -            Configuration[] keys = conf.getChild("table").getChild("keys").getChildren("key");
> -            Configuration[] values =
> conf.getChild("table").getChild("values").getChildren("value");
> -            currentIndex = 1;
> -
> -            for (int i = 0; i < keys.length; i++) {
> -                String mode = keys[i].getAttribute("mode", "automatic");
> -
> -                if ("manual".equals(mode)) {
> -                    String selectQuery = this.getSelectQuery(keys[i]);
> -                    PreparedStatement select_statement = conn.prepareStatement(selectQuery);
> -                    ResultSet set = select_statement.executeQuery();
> -                    set.next();
> -                    int value = set.getInt("maxid") + 1;
> -
> -                    statement.setInt(currentIndex, value);
> -
> -                    set.close();
> -                    select_statement.close();
> -                    currentIndex++;
> -                } else if ("form".equals(mode)) {
> -                    this.setColumn(statement, currentIndex, request, values[i]);
> -                    currentIndex++;
> -                }
> +            Configuration[] tables = conf.getChildren("table");
> +            for (int i=0; i<tables.length; i++) {
> +              Configuration table = tables[i];
> +              processTable(table,conn,request);
>              }
> -
> -            for (int i = 0; i < values.length; i++, currentIndex++) {
> -                this.setColumn(statement, currentIndex, request, values[i]);
> -            }
> -
> -            statement.execute();
>              conn.commit();
> -            statement.close();
>          } catch (Exception e) {
>              if (conn != null) {
>                  try {
> @@ -106,7 +77,8 @@ public class DatabaseAddAction extends A
>                  }
>              }
>  
> -            throw new ProcessingException("Could not add record :position = " + currentIndex,
> e);
> +            //throw new ProcessingException("Could not add record :position = " + currentIndex,
> e);
> +            throw new ProcessingException("Could not add record",e);
>          } finally {
>              if (conn != null) {
>                  try {
> @@ -122,19 +94,114 @@ public class DatabaseAddAction extends A
>          return null;
>      }
>  
> +    void processTable(Configuration table, Connection conn, Request request) throws
> SQLException,ConfigurationException,Exception {
> +      PreparedStatement statement = null;
> +      try {
> +        String query = this.getAddQuery(table);
> +        statement = conn.prepareStatement(query);
> +        Configuration[] keys = table.getChild("keys").getChildren("key");
> +        Configuration[] values = table.getChild("values").getChildren("value");
> +        int currentIndex = 1;
> +        boolean manyrows = false;
> +        for (int i=0; i<keys.length; i++) {
> +          String param = keys[i].getAttribute("param");
> +          int index;
> +          if ((index = param.indexOf('*')) != -1) {
> +            /**
> +             * This table has a column with a wildcard, so we're going
> +             * to be inserting n rows, where 0 <= n
> +             */
> +            manyrows = true;
> +            String prefix = param.substring(0,index);
> +            String suffix = param.substring(index+1);
> +            Enumeration names = request.getParameterNames();
> +            while (names.hasMoreElements()) {
> +              String name = (String)names.nextElement();
> +              if (name.startsWith(prefix) && name.endsWith(suffix)) {
> +                String wildcard = name.substring(prefix.length());
> +                wildcard = wildcard.substring(0,wildcard.length()-suffix.length());
> +                for (int j=0; j<keys.length; j++) {
> +                  String myparam = getActualParam(keys[j].getAttribute("param"),wildcard);
> +                 
> this.setColumn(statement,j+1,request,keys[j],myparam,request.getParameter(myparam));
> +                }
> +                for (int j=0; j<values.length; j++) {
> +                  String myparam = getActualParam(values[j].getAttribute("param"),wildcard);
> +                 
>
this.setColumn(statement,keys.length+j+1,request,values[j],myparam,request.getParameter(myparam));
> +                }
> +                statement.execute();
> +              }
> +            }
> +            return;
> +            /** Done processing table **/
> +          }
> +        }
> +        if (!manyrows) {
> +          /**
> +           * This table has no wildcard columns, so we're going to
> +           * be inserting 1 row.
> +           */
> +          for (int i = 0; i < keys.length; i++) {
> +            String mode = keys[i].getAttribute("mode", "automatic");
> +            if ("manual".equals(mode)) {
> +              /** Set the key value using SELECT MAX(keyname)+1 **/
> +              String selectQuery = this.getSelectQuery(keys[i]);
> +              PreparedStatement select_statement = conn.prepareStatement(selectQuery);
> +              ResultSet set = select_statement.executeQuery();
> +              set.next();
> +              int value = set.getInt("maxid") + 1;
> +              statement.setInt(currentIndex, value);
> +              set.close();
> +              select_statement.close();
> 
=== message truncated ===> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> For additional commands, email: cocoon-dev-help@xml.apache.org


=====
Davanum Srinivas, JNI-FAQ Manager
http://www.jGuru.com/faq/JNI

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


Mime
View raw message