Package com.highcom.seqgen.dao.jdbc;
Import java.sql. *; import javax.sql. *;
import org.apache.commons.logging *;. import org.springframework.beans.factory *;. import org.springframework.context *;. import org.springframework.jdbc.core *;. import org.springframework.jdbc.object Import com.highcom.seqgen.dao. *; import com.highcom.seqgen.domain. *;
Public class sequencedaojdbcimpl imports sequencedao, initializingbean {
Private Datasource DataSource; private static log logger = logfactory.getlog (sequencedaojdbcimpl.class);
// SequenceQuery sequenceQuery; SequenceUpdate sequenceUpdate; SequenceInsert sequenceInsert; // protected static final String INSERT_SQL = "insert into sequence (seq_name, seq_value) values (,??)";? Protected static final String UPDATE_SQL = "update sequence set seq_value = where SEQ_NAME =? "; protected static final string select_sql =" SEQ_VALUE from SEQUENCE where seq_name =? ";
Public sequencedaojdbcimpl () {
}
Public int getsequence {int result; object obj = sequenceQuery.FindObject (new object [] {seq_name}); if (obj == null) {sequenceinsert.insert (seq_name, 0);}
Sequence seq = (Sequence) sequenceQuery.findObject (new Object [] {seq_name}); sequenceUpdate.update (seq.getName (), seq.getValue () 1); result = seq.getValue () 1; return result }
public void afterPropertiesSet () throws Exception {if (dataSource == null) {logger.error ( "Must set dataSource bean property on" getClass ()); throw new ApplicationContextException ( "Must set dataSource bean property on" getClass () );} // sequenceQuery = new SequenceQuery (this.dataSource); sequenceUpdate = new SequenceUpdate (this.dataSource); sequenceInsert = new SequenceInsert (this.dataSource);} public void setDataSource (DataSource dataSource) {this.dataSource = dataSource; }
/// jdbc inner class class SequenceInsert extends SqlUpdate {public SequenceInsert (DataSource dataSource) {super (dataSource, INSERT_SQL); declareParameter (new SqlParameter (Types.VARCHAR)); declareParameter (new SqlParameter (Types.INTEGER)); compile () }
Public void insert (String segval) {object [] {seqname, new integer (segvalue)}; super.Update (objs);
class SequenceUpdate extends SqlUpdate {public SequenceUpdate (DataSource dataSource) {super (dataSource, UPDATE_SQL); declareParameter (new SqlParameter (Types.INTEGER)); declareParameter (new SqlParameter (Types.VARCHAR));
COMPILE ();
}
Public void update (string seqname, int segvalue) {object [] objs = new object [] {new integer (segvalue), seqName}; super.Update (objs);
Class SequenceQuery Extends MappingsqlQuery {Public Sequence Datasource {Super (Datasource, SELECT_SQL); DECLAREPAREPARETER (NEPEREPARETER (Types.varchar); Compile ();
protected Object mapRow (ResultSet resultSet, int _int) throws SQLException {Sequence seq = new Sequence (); seq.setName (resultSet.getString ( "seq_name")); seq.setValue (resultSet.getInt ( "seq_value")); return SEQ;}}}