litesoft
@ 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.sql.statements; import java.sql.*; import org.litesoft.db.*; import org.litesoft.logger.*; import org.litesoft.logger.nonpublic.*; import org.litesoft.sql.*; public class JDBCexecuteQuery extends JDBCexecutor implements DisposableResultSetIterator { private static final Logger LOGGER = LoggerFactory.getLogger( JDBCexecuteQuery.class ); private ResultSet mResultSet = null; private Boolean mRSnext = null; public JDBCexecuteQuery( SQLPersistenceHelper pPersistenceHelper, DBconnection pDBconnection, String pCmd, Object pLoggingContext ) { super( pPersistenceHelper, pDBconnection, pCmd, pLoggingContext ); } public JDBCexecuteQuery( SQLPersistenceHelper pPersistenceHelper, String pCmd, Object pLoggingContext ) { super( pPersistenceHelper, pCmd, pLoggingContext ); } private void closeResultSet() { if ( mResultSet != null ) { try { mResultSet.close(); } catch ( SQLException eatit ) { // Whatever... logException( mCmd, eatit ); } finally { mResultSet = null; } } } @Override protected void releaseResources() { mRSnext = Boolean.FALSE; closeResultSet(); super.releaseResources(); } public JDBCexecuteQuery executeQuery() throws DBException { logCommand( mCmd ); try { mResultSet = getStatement().executeQuery( mCmd ); } catch ( SQLException e ) { releaseResources(); throw new WrappedSQLException( mCmd, e ); } return this; } private boolean areRSnext() throws SQLException { if ( mRSnext == null ) { if ( mResultSet.next() ) { mRSnext = Boolean.TRUE; } else { releaseResources(); // Sets: mRSnext = false } } return mRSnext; } @Override public boolean hasNext() { try { return areRSnext(); } catch ( SQLException e ) { logException( mCmd, e ); releaseResources(); // Sets: mRSnext = false throw new WrappedSQLException( mCmd, e ); } } @Override public ResultSet next() { if ( !hasNext() ) { return null; } mRSnext = null; return mResultSet; } public ResultSetMetaData getMetaData() { try { return mResultSet.getMetaData(); } catch ( SQLException e ) { logException( mCmd, e ); releaseResources(); // Sets: mRSnext = false throw new WrappedSQLException( mCmd, e ); } } public int getResultSetColumnCount() { try { return getMetaData().getColumnCount(); } catch ( SQLException e ) { logException( mCmd, e ); releaseResources(); // Sets: mRSnext = false throw new WrappedSQLException( mCmd, e ); } } @Override public void remove() { throw new UnsupportedOperationException( "Can not 'remove' from ResultSet Iterator" ); } /** * Logs the pCmd String at "TRACE" or "DEBUG" level. * * @param pCmd */ @SuppressWarnings({"ThrowableInstanceNeverThrown"}) @Override protected void logCommand( String pCmd ) { AbstractLogger zLogger = LOGGER.trace(); if ( zLogger.isEnabled() ) { zLogger.log( new Exception(), pCmd, "; | ", mLoggingContext ); return; } zLogger = LOGGER.debug(); if ( zLogger.isEnabled() ) { zLogger.log( pCmd, "; | ", mLoggingContext ); } else { super.logCommand( pCmd ); } } } |