1 33 package smallsql.database; 34 35 import java.sql.*; 36 import java.util.ArrayList ; 37 38 39 class SSStatement implements Statement { 40 41 final SSConnection con; 42 Command cmd; 43 int rsType; 44 int rsConcurrency; 45 private int fetchDirection; 46 private int fetchSize; 47 private int queryTimeout; 48 private int maxRows; 49 private int maxFieldSize; 50 private ArrayList batches; 51 private boolean needGeneratedKeys; 52 private ResultSet generatedKeys; 53 private int[] generatedKeyIndexes; 54 private String [] generatedKeyNames; 55 56 57 SSStatement( SSConnection con ) throws SQLException{ 58 this( con, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY ); 59 } 60 61 SSStatement( SSConnection con, int rsType, int rsConcurrency ) throws SQLException{ 62 this.con = con; 63 this.rsType = rsType; 64 this.rsConcurrency = rsConcurrency; 65 con.testClosedConnection(); 66 } 67 68 final public ResultSet executeQuery(String sql) throws SQLException { 69 executeImpl(sql); 70 return cmd.getQueryResult(); 71 } 72 final public int executeUpdate(String sql) throws SQLException { 73 executeImpl(sql); 74 return cmd.getUpdateCount(); 75 } 76 final public boolean execute(String sql) throws SQLException { 77 executeImpl(sql); 78 return cmd.getResultSet() != null; 79 } 80 81 final private void executeImpl(String sql) throws SQLException { 82 generatedKeys = null; 83 try{ 84 con.log.println(sql); 85 SQLParser parser = new SQLParser(); 86 cmd = parser.parse( con, sql ); 87 if(maxRows != 0 && (cmd.getMaxRows() == 0 || cmd.getMaxRows() > maxRows)) 88 cmd.setMaxRows(maxRows); 89 cmd.execute( con, this); 90 }catch(Exception e){ 91 throw Utils.createSQLException(e); 92 } 93 needGeneratedKeys = false; 94 generatedKeyIndexes = null; 95 generatedKeyNames = null; 96 } 97 98 99 final public void close() throws SQLException { 100 con.log.println("Statement.close"); 101 cmd = null; 102 } 105 106 107 final public int getMaxFieldSize() throws SQLException { 108 return maxFieldSize; 109 } 110 final public void setMaxFieldSize(int max) throws SQLException { 111 maxFieldSize = max; 112 } 113 final public int getMaxRows(){ 114 return maxRows; 115 } 116 117 118 final public void setMaxRows(int max) throws SQLException{ 119 if(max<0) 120 throw Utils.createSQLException("Wrong max rows value:"+max); 121 maxRows = max; 122 } 123 124 125 final public void setEscapeProcessing(boolean enable) throws SQLException { 126 } 128 129 130 final public int getQueryTimeout() throws SQLException { 131 return queryTimeout; 132 } 133 134 135 final public void setQueryTimeout(int seconds) throws SQLException { 136 queryTimeout = seconds; 137 } 138 139 140 final public void cancel(){ 141 } 143 144 145 final public SQLWarning getWarnings(){ 146 return null; 147 } 148 149 150 final public void clearWarnings(){ 151 } 153 154 155 final public void setCursorName(String name) throws SQLException { 156 157 throw Utils.createUnsupportedException("setCursorName"); 158 } 159 160 161 final public ResultSet getResultSet(){ 162 return cmd.getResultSet(); 163 } 164 165 166 final public int getUpdateCount() throws SQLException { 167 return cmd.getUpdateCount(); 168 } 169 170 171 final public boolean getMoreResults() throws SQLException { 172 return getMoreResults(CLOSE_CURRENT_RESULT); 173 } 174 175 176 final public void setFetchDirection(int direction) throws SQLException { 177 fetchDirection = direction; 178 } 179 final public int getFetchDirection() throws SQLException { 180 return fetchDirection; 181 } 182 final public void setFetchSize(int rows) throws SQLException { 183 fetchSize = rows; 184 } 185 final public int getFetchSize() throws SQLException { 186 return fetchSize; 187 } 188 final public int getResultSetConcurrency() throws SQLException { 189 return rsConcurrency; 190 } 191 final public int getResultSetType() throws SQLException { 192 return rsType; 193 } 194 195 196 final public void addBatch(String sql){ 197 if(batches == null) batches = new ArrayList (); 198 batches.add(sql); 199 } 200 201 202 public void clearBatch(){ 203 if(batches == null) return; 204 batches.clear(); 205 } 206 207 208 public int[] executeBatch() throws BatchUpdateException { 209 if(batches == null) return new int[0]; 210 final int[] result = new int[batches.size()]; 211 BatchUpdateException failed = null; 212 for(int i=0; i<result.length; i++){ 213 try { 214 result[i] = executeUpdate((String )batches.get(i)); 215 } catch (SQLException ex) { 216 result[i] = EXECUTE_FAILED; 217 if(failed == null){ 218 failed = new BatchUpdateException(ex.getMessage(), ex.getSQLState(), ex.getErrorCode(), result); 219 failed.initCause(ex); 220 } 221 failed.setNextException(ex); 222 } 223 } 224 batches.clear(); 225 if(failed != null) 226 throw failed; 227 return result; 228 } 229 230 231 final public Connection getConnection(){ 232 return con; 233 } 234 235 236 final public boolean getMoreResults(int current) throws SQLException { 237 switch(current){ 238 case CLOSE_ALL_RESULTS: 239 case CLOSE_CURRENT_RESULT: 241 ResultSet rs = cmd.getResultSet(); 242 cmd.rs = null; 243 if(rs != null) rs.close(); 244 break; 245 case KEEP_CURRENT_RESULT: 246 break; 247 default: 248 throw Utils.createSQLException("Invalid flag value in method getMoreResults:"+current); 249 } 250 return cmd.getMoreResults(); 251 } 252 253 254 final void setNeedGeneratedKeys(int autoGeneratedKeys) throws SQLException{ 255 switch(autoGeneratedKeys){ 256 case NO_GENERATED_KEYS: 257 break; 258 case RETURN_GENERATED_KEYS: 259 needGeneratedKeys = true; 260 break; 261 default: 262 throw Utils.createSQLException("Invalid argument:"+autoGeneratedKeys); 263 } 264 } 265 266 267 final void setNeedGeneratedKeys(int[] columnIndexes) throws SQLException{ 268 needGeneratedKeys = columnIndexes != null; 269 generatedKeyIndexes = columnIndexes; 270 } 271 272 273 final void setNeedGeneratedKeys(String [] columnNames) throws SQLException{ 274 needGeneratedKeys = columnNames != null; 275 generatedKeyNames = columnNames; 276 } 277 278 279 final boolean needGeneratedKeys(){ 280 return needGeneratedKeys; 281 } 282 283 284 final int[] getGeneratedKeyIndexes(){ 285 return generatedKeyIndexes; 286 } 287 288 289 final String [] getGeneratedKeyNames(){ 290 return generatedKeyNames; 291 } 292 293 294 298 final void setGeneratedKeys(ResultSet rs){ 299 generatedKeys = rs; 300 } 301 302 303 final public ResultSet getGeneratedKeys() throws SQLException { 304 if(generatedKeys == null) 305 throw Utils.createSQLException("GeneratedKeys not requested"); 306 return generatedKeys; 307 } 308 309 310 final public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 311 setNeedGeneratedKeys(autoGeneratedKeys); 312 return executeUpdate(sql); 313 } 314 315 316 final public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 317 setNeedGeneratedKeys(columnIndexes); 318 return executeUpdate(sql); 319 } 320 321 322 final public int executeUpdate(String sql, String [] columnNames) throws SQLException { 323 setNeedGeneratedKeys(columnNames); 324 return executeUpdate(sql); 325 } 326 327 328 final public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 329 setNeedGeneratedKeys(autoGeneratedKeys); 330 return execute(sql); 331 } 332 333 334 final public boolean execute(String sql, int[] columnIndexes) throws SQLException { 335 setNeedGeneratedKeys(columnIndexes); 336 return execute(sql); 337 } 338 339 340 final public boolean execute(String sql, String [] columnNames) throws SQLException { 341 setNeedGeneratedKeys(columnNames); 342 return execute(sql); 343 } 344 345 346 final public int getResultSetHoldability() throws SQLException { 347 348 throw new java.lang.UnsupportedOperationException ("Method getResultSetHoldability() not yet implemented."); 349 } 350 } | Popular Tags |