froehlich 01/11/08 16:26:23 Modified: apps/db/src/java/org/apache/avalon/db/driver AvalonDBPreparedStatement.java Log: prepared statement extension Revision Changes Path 1.5 +40 -28 jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/driver/AvalonDBPreparedStatement.java Index: AvalonDBPreparedStatement.java =================================================================== RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/driver/AvalonDBPreparedStatement.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- AvalonDBPreparedStatement.java 2001/11/06 00:32:34 1.4 +++ AvalonDBPreparedStatement.java 2001/11/09 00:26:23 1.5 @@ -9,7 +9,10 @@ package org.apache.avalon.db.driver; import org.apache.avalon.db.common.FeatureNotImplemented; - +import org.apache.avalon.db.transport.Reply; +import org.apache.avalon.db.transport.SelectReply; +import org.apache.avalon.db.transport.SelectRequest; +import org.apache.avalon.db.transport.ResultSetReply; import java.math.BigDecimal; import java.io.InputStream; import java.io.Reader; @@ -29,26 +32,20 @@ import java.sql.Array; import java.sql.ResultSetMetaData; - /** * Class AvalonDBPreparedStatement * * * @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ */ public class AvalonDBPreparedStatement extends AvalonDBStatement implements PreparedStatement { - - private String mSQL; - public void initialize() { - /* - parse sql string and extract params and store in Array/Vector/Hashtable - Reply reply = mAvalonDBConnection.sendRequest(new SelectRequest(sql,true)); - handleSQLException(reply); - */ - } + private String mSQL = null; + private String[] params = null; + private int mParamterCountPrep = 0; + private int mParamterCountVal = 0; public AvalonDBPreparedStatement(AvalonDBConnection avalonDBConnection, String sql) { super(avalonDBConnection); @@ -59,6 +56,15 @@ mSQL = sql; } + /** + * Initialize the prepared statement. + */ + public void initialize() throws SQLException { + SelectReply reply = (SelectReply)mAvalonDBConnection.sendRequest(new SelectRequest(mSQL,true)); + handleSQLException(reply); + mParamterCountPrep = reply.getParameterCount(); + params = new String[mParamterCountPrep]; + } /** * Executes the SQL query in this <code>PreparedStatement</code> object @@ -71,10 +77,17 @@ * @exception SQLException if a database access error occurs */ public ResultSet executeQuery() throws SQLException { - - debug(); - - return null; + boolean correct = verifyParams(); + if(correct) { + Reply reply = mAvalonDBConnection.sendRequest(new SelectRequest(mSQL,params)); + handleSQLException(reply); + ResultSetReply resultSetReply = (ResultSetReply) reply; + mRowSet = resultSetReply.getRowSet(); + mResultSet = new AvalonDBResultSet(mAvalonDBConnection,mRowSet); + return mResultSet; + } else { + throw new SQLException("Not all variables bound!"); + } } /** @@ -88,17 +101,7 @@ * @exception SQLException if a database access error occurs */ public ResultSet executeQuery(String sql) throws SQLException { - - /* - send request with "prepared" params... - Reply reply = mAvalonDBConnection.sendRequest(new SelectRequest(sql,params)); - handleSQLException(reply); - ResultSetReply resultSetReply = (ResultSetReply) reply; - mRowSet = resultSetReply.getRowSet(); - mResultSet = new AvalonDBResultSet(mAvalonDBConnection,mRowSet); - return mResultSet; - */ - + debug(); return null; } @@ -387,7 +390,8 @@ * @exception SQLException if a database access error occurs */ public void setString(int parameterIndex, String x) throws SQLException { - debug(); + mParamterCountVal++; + params[parameterIndex] = x; } /** @@ -1197,5 +1201,13 @@ */ public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException { debug(); + } + + private boolean verifyParams() { + if(mParamterCountPrep != mParamterCountVal) { + return false; + } else { + return true; + } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>