/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * 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. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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. [NOTE: This license contains NO advertising clause.] */ package xbn.db; import xbn.XBNObject; import xbn.string.UtilString; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.sql.PreparedStatement; import java.sql.ResultSet; /**

The base class for handling a Query, via the Statement contained in a DBConnStatement.

Source code:  QueryBase.java.

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class QueryBase extends XBNObject { private UtilString uStr = new UtilString(); private boolean bThrowOnNoRowUpdate = false; public QueryBase(boolean b_throwOnNoRowUpdate) { bThrowOnNoRowUpdate = b_throwOnNoRowUpdate; } public QueryBase() { this(false); } /**

This closes the ResultSet.

**/ public String getStringFromRS(ResultSet rs) throws SQLException { rs.next(); String s = rs.getString(1); rs.close(); return s; } /**

If the string returned is null, it is assumed to be zero.

**/ public int getIntFromRS(ResultSet rs) throws SQLException { String s_potentialInt = getStringFromRS(rs); if(s_potentialInt == null) { s_potentialInt = "0"; } try { return uStr.getInt(s_potentialInt, true); } catch(NumberFormatException nfx) { throwAX("getInt: The ResultSet element [1, 1] is not a valid int. " + nfx.toString()); } //Never reached. Required for compile. return -1; } /**

If the string returned is null, it is assumed to be zero.

**/ public long getLongFromRS(ResultSet rs) throws SQLException { String sPotentialLong = getStringFromRS(rs); if(sPotentialLong == null) { sPotentialLong = "0"; } try { return uStr.getLong(sPotentialLong, true); } catch(NumberFormatException nfx) { throwAX("getLong: The ResultSet element [1, 1] is not a valid long. " + nfx.toString()); } //Never reached. Required for compile. return -1; } /**

Executes a non-select query, such as update, insert, delete...

@return The number of rows affected by the update. For example, if 10 rows are deleted, then 10 is returned. If no rows are deleted, 0 is returned.

**/ protected int update(DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) throws SQLException { DBCSOfType dbscot = new DBCSOfType("update", dbcs_plain, dbcs_prepared); int iStatus = 0; try { if(dbscot.isPlain()) { dbscot.getDBConnDirect().dbg("update: " + dbscot.getSql()); iStatus = dbscot.getStatement().executeUpdate(dbcs_plain.getSql()); } else { dbscot.getDBConnDirect().dbg("update: " + ((DBCSPrepared)dbscot.getDBConnStatement()).getSqlAndParams()); iStatus = dbscot.getPrepared().executeUpdate(); } } catch(SQLException sqlx) { dbscot.getDBConnDirect().throwSQLX(this.getClass().getName() + ".update: " + sqlx.toString()); } if(doThrowOnNoRowUpdate() && iStatus == 0) { dbscot.getDBConnDirect().throwSQLX(this.getClass().getName() + ".update: Zero rows were affected by this update statement, and doThrowOnNoRowUpdate() is true."); } return iStatus; } /**

Executes DML.

**/ protected boolean execute(DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) throws SQLException { DBCSOfType dbscot = new DBCSOfType("execute", dbcs_plain, dbcs_prepared); boolean bStatus = false; try { if(dbscot.isPlain()) { dbscot.getDBConnDirect().dbg("execute: " + dbscot.getSql()); bStatus = dbscot.getStatement().execute(dbcs_plain.getSql()); } else { dbscot.getDBConnDirect().dbg("execute: " + ((DBCSPrepared)dbscot.getDBConnStatement()).getSqlAndParams()); bStatus = dbscot.getPrepared().execute(); } } catch(SQLException sqlx) { dbscot.getDBConnDirect().throwSQLX(this.getClass().getName() + ".execute: " + sqlx.toString()); } return bStatus; } /** If true, and no rows are affected in an update statement, the throw an SQLException, instead of returning 0.

Normally, an update query returns the number of rows affected by the query. **/ public void setThrowOnNoRowUpdate(boolean b_throw) { bThrowOnNoRowUpdate = b_throw; } /** If true, and no rows are affected in an update statement, then throw an SQLException, instead of returning 0.

Normally, an update query returns the number of rows affected by the query. **/ public boolean doThrowOnNoRowUpdate() { return bThrowOnNoRowUpdate; } protected String getString(DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) throws SQLException { ResultSet rs = getResultSet("getString", dbcs_plain, dbcs_prepared); String s = getStringFromRS(rs); rs.close(); return s; } protected int getInt(DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) throws SQLException { ResultSet rs = getResultSet("getInt", dbcs_plain, dbcs_prepared); int i = getIntFromRS(rs); rs.close(); return i; } protected long getLong(DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) throws SQLException { ResultSet rs = getResultSet("getLong", dbcs_plain, dbcs_prepared); long l = getLongFromRS(rs); rs.close(); return l; } protected ResultSet getResultSet(DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) throws SQLException { return getResultSet("getResultSet", dbcs_plain, dbcs_prepared); } protected ResultSet getResultSet(String s_callingFunc, DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) throws SQLException { DBCSOfType dbscot = new DBCSOfType(s_callingFunc, dbcs_plain, dbcs_prepared); try { if(dbscot.isPlain()) { dbscot.getDBConnDirect().dbg(s_callingFunc + ": " + dbscot.getSql()); return dbscot.getStatement().executeQuery(dbcs_plain.getSql()); } else { dbscot.getDBConnDirect().dbg(s_callingFunc + ": " + ((DBCSPrepared)dbscot.getDBConnStatement()).getSqlAndParams()); return dbscot.getPrepared().executeQuery(); } } catch(SQLException sqlx) { dbscot.getDBConnDirect().throwSQLX(s_callingFunc + ": " + sqlx.toString()); } //Never reached. Required for compile. return null; } } class DBCSOfType extends XBNObject { private DBConnStatement dbcs = null; private boolean bIsPlain = false; public DBCSOfType(String s_callingFunc, DBCSPlain dbcs_plain, DBCSPrepared dbcs_prepared) { final String sBothErr = ": Exactly one dbcs parameter (dbcs_plain or dbcs_prepared) must be non-null, and the other must be null. Both currently "; if(dbcs_plain != null) { if(dbcs_prepared != null) { throwAX(s_callingFunc + sBothErr + "non-null."); } dbcs = dbcs_plain; bIsPlain = true; } else if(dbcs_prepared != null) { dbcs = dbcs_prepared; bIsPlain = false; } else { throwAX(s_callingFunc + sBothErr + "null."); } } public boolean isPlain() { return bIsPlain; } public DBConnStatement getDBConnStatement() { return dbcs; } public String getSql() { return getDBConnStatement().getSql(); } public DBConnDirect getDBConnDirect() { return dbcs.getDBConnDirect(); } public Connection getConnection() { return getDBConnDirect().getConnection(); } public Statement getStatement() { return getDBConnStatement().getStatement(); } public PreparedStatement getPrepared() { if(isPlain()) { throwAX("getPrepared: isPlain() is true. This function may only be called when isPlain() is false."); } return ((DBCSPrepared)getDBConnStatement()).getPrepared(); } }