You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by tc...@hockeyresource.com on 2004/07/08 18:18:12 UTC
[Patch:new] Oracle auto. increment impl. for Modual Database Action
My first contribution, please review!
regards,
TC
=============== OraIncrementModule.java
package org.apache.cocoon.components.modules.database;
import java.lang.Integer;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Map;
import org.apache.avalon.framework.configuration.*;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.components.modules.database.*;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
/**
* This is an impl. on behalf of an ORACLE sequence defined in your schema
for getting an automatic incremental ID.
* A total of 2 queries will be sent to the DB.
*<ol>
* <li>The 1st one is for getting a snapshot of the latest sequence id,
which will be availabe in the sitemap for other usage.</li>
* <li>The 2nd one is for inserting record(s) into the target table.</li>
* </ol>
* NOTE:: If the sequence-id is NOT needed in sitemap, please use
<i>org.apache.cocoon.components.modules.database.OraShortIncrementModule</i>
to save the effort of the first query.
*<P/>
* In cocoon.xconf, one could declare such an auto module as:
*<pre>
* <autoincrement-modules>
* <component-instance
class="org.apache.cocoon.components.modules.database.OraIncrementModule"
logger="steLog" name="auto-ora"/>
*</pre>
* In the mdb configuartion file definition,
*<pre>
* <table ...> ...
* ...
* <key ...>
* <mode name="auto-ora" type="autoincr"
* sequence="u're-sequence-name" reqAttr="seqid-in-sitemap" />
*</pre>
* where mode/@sequence is u're Oracle sequence name and <br>
* mode/@reqAttr is the name of a req. attribute containing a value of the
sequence which is also available in sitemap via
{request-attr:seqid-in-sitemap}.
**/
public class OraSeqIncrementModule extends AbstractAutoIncrementModule
implements ThreadSafe {
/**
* @return: true, key value is needed in the final query.
**/
public boolean includeInQuery() {
return true;
}
/**
* @return true: key appears as a value; 2 queries will be sent to the
DB.
**/
public boolean includeAsValue() {
return true;
}
/**
* a query for getting the NEXTVAL out of a sequence.
* Such a NEXTVAL value will be set as a String into a request
attribute, whose name=<mod>/@reqAttr. Therefore, such a sequence
number is available in the sitemap thru
{request-attr:<va...@reqAttr-defined-in-mdb-file>}
**/
public Object getPreValue( Configuration tableConf, Configuration
columnConf, Configuration modeConf, Connection conn, Map objectModel )
throws SQLException, ConfigurationException {
String sequence = modeConf.getAttribute("sequence", null);
if( null==sequence )
throw new ConfigurationException("ERROR!!! Please define your
sequence name in mdb file, under
table[name="+tableConf.getAttribute("name")+"]/key[@name="+columnConf.getAttribute("name")+"]/mode/@sequence"
);
String reqAttr = modeConf.getAttribute("reqAttr", null);
PreparedStatement pstmt= conn.prepareStatement( "select
"+sequence+".NEXTVAL AS nextid from dual" );
ResultSet rs= pstmt.executeQuery();
rs.next();
Integer nextid= new Integer(rs.getInt("nextid"));
pstmt.close();
if (getLogger().isDebugEnabled())
getLogger().debug("ORA sequence:"+sequence+".NextVal= "+nextid );
if( null!=reqAttr )
org.apache.cocoon.environment.ObjectModelHelper.getRequest(objectModel).setAttribute(reqAttr,
nextid.toString() );
return nextid;
}
public Object getPostValue( Configuration tableConf, Configuration
columnConf, Configuration modeConf, Connection conn, Statement stmt,
Map objectModel ) throws SQLException, ConfigurationException {
return null;
}
public String getSubquery( Configuration tableConf, Configuration
columnConf, Configuration modeConf)
throws ConfigurationException {
return null;
}
}
================= OraShortIncrementModule.java
package org.apache.cocoon.components.modules.database;
import java.lang.Integer;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Map;
import org.apache.avalon.framework.configuration.*;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.components.modules.database.*;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
/**
* This is an impl. on behalf of an ORACLE sequence defined in your schema
for getting an automatic incremental ID.
* Only one insert query will be sent to the DB-table with a snapshot
value out of a sequence.
* NOTE:: If the sequence-id is needed in sitemap, please use
<i>org.apache.cocoon.components.modules.database.OraIncrementModule</i>;
which costs U one more query for getting the sequence and make it
available to the sitemap as a request attributre.
*<P/>
* In cocoon.xconf, one could declare such an auto module as:
*<pre>
* <autoincrement-modules>
* <component-instance
class="org.apache.cocoon.components.modules.database.OraShortIncrementModule"
logger="steLog" name="auto-ora-short"/>
*</pre>
* In the mdb configuartion file definition,
*<pre>
* <table ...> ...
* ...
* <key ...>
* <mode name="auto-ora-short" type="autoincr"
* sequence="u're-sequence-name" />
*</pre>
* where mode/@sequence is u're Oracle sequence name.
**/
public class OraSeqShortIncrementModule extends OraSeqIncrementModule
implements ThreadSafe {
/**
* @return false: key appears as a subquery, <sequence>.NEXTVAL, in
the final query. Only one query will be sent to the DB.
**/
public boolean includeAsValue() {
return false;
}
public String getSubquery( Configuration tableConf, Configuration
columnConf, Configuration modeConf) throws ConfigurationException {
String sequence = modeConf.getAttribute("sequence", null);
if( null==sequence )
throw new ConfigurationException("ERROR!!! Please define your
sequence name in mdb file, under
table[name="+tableConf.getAttribute("name")+"]/key[@name="+columnConf.getAttribute("name")+"]/mode/@sequence"
);
if (getLogger().isDebugEnabled() )
getLogger().debug("Get NEXTVAL from ORA-sequence:"+sequence );
return " "+sequence+".NEXTVAL";
}
}
Re: [Patch:new] Oracle auto. increment impl. for Modual Database
Action
Posted by Torsten Curdt <tc...@vafer.org>.
please make a diff -u patch and file it to bugzilla
thanks
--
Torsten