1 24 25 package com.mckoi.database.jdbc; 26 27 import java.sql.*; 28 import java.io.*; 29 import java.util.Vector ; 30 import com.mckoi.database.global.StreamableObject; 31 32 40 41 class MStatement implements Statement { 42 43 46 private MConnection connection; 47 48 51 private MResultSet[] result_set_list; 52 53 54 private int max_field_size; 55 private int max_row_count; 56 private int query_timeout; 57 private int fetch_size; 58 59 private SQLWarning head_warning; 60 61 private boolean escape_processing; 62 63 66 private Vector batch_list; 67 68 72 private Vector streamable_object_list; 73 74 77 private int multi_result_set_index; 78 79 82 MStatement(MConnection connection) { 83 this.connection = connection; 84 this.escape_processing = true; 85 } 86 87 90 final void addSQLWarning(SQLWarning warning) { 91 if (head_warning == null) { 92 head_warning = warning; 93 } 94 else { 95 head_warning.setNextWarning(warning); 96 } 97 } 98 99 104 final MResultSet[] internalResultSetList(int count) { 105 if (count <= 0) { 106 throw new Error ("'count' must be > 0"); 107 } 108 109 if (result_set_list != null && result_set_list.length != count) { 110 for (int i = 0; i < result_set_list.length; ++i) { 112 result_set_list[i].dispose(); 113 } 114 result_set_list = null; 115 } 116 117 if (result_set_list == null) { 118 result_set_list = new MResultSet[count]; 119 for (int i = 0; i < count; ++i) { 120 result_set_list[i] = new MResultSet(connection, this); 121 } 122 } 123 124 return result_set_list; 125 } 126 127 131 final MResultSet internalResultSet() { 132 return internalResultSetList(1)[0]; 133 } 134 135 139 protected StreamableObject createStreamableObject(InputStream x, 140 int length, byte type) { 141 StreamableObject s_ob = connection.createStreamableObject(x, length, type); 142 if (streamable_object_list == null) { 143 streamable_object_list = new Vector (); 144 } 145 streamable_object_list.add(s_ob); 146 return s_ob; 147 } 148 149 152 protected void addBatch(SQLQuery query) { 153 if (batch_list == null) { 154 batch_list = new Vector (); 155 } 156 batch_list.add(query); 157 } 158 159 163 protected MResultSet executeQuery(SQLQuery query) throws SQLException { 164 MResultSet result_set = internalResultSet(); 166 executeQueries(new SQLQuery[] { query }); 168 return result_set; 170 } 171 172 175 protected MResultSet[] executeQueries(SQLQuery[] queries) 176 throws SQLException { 177 178 MResultSet[] results = internalResultSetList(queries.length); 180 181 multi_result_set_index = 0; 183 184 for (int i = 0; i < queries.length; ++i) { 186 queries[i].prepare(escape_processing); 188 results[i].closeCurrentResult(); 190 } 191 192 connection.executeQueries(queries, results); 194 195 for (int i = 0; i < queries.length; ++i) { 197 MResultSet result_set = results[i]; 198 result_set.setFetchSize(fetch_size); 200 result_set.setMaxRowCount(max_row_count); 202 boolean contains_large_objects = result_set.containsLargeObjects(); 205 if (!contains_large_objects && result_set.rowCount() < 40) { 208 result_set.storeResultLocally(); 209 } 210 else { 211 result_set.updateResultPart(0, Math.min(10, result_set.rowCount())); 212 } 213 } 214 215 return results; 216 217 } 218 219 221 public ResultSet executeQuery(String sql) throws SQLException { 222 return executeQuery(new SQLQuery(sql)); 223 } 224 225 public int executeUpdate(String sql) throws SQLException { 226 MResultSet result_set = executeQuery(new SQLQuery(sql)); 227 return result_set.intValue(); } 229 230 public void close() throws SQLException { 231 if (result_set_list != null) { 233 for (int i = 0; i < result_set_list.length; ++i) { 234 result_set_list[i].dispose(); 235 } 236 result_set_list = null; 237 } 238 if (streamable_object_list != null) { 241 int sz = streamable_object_list.size(); 242 for (int i = 0; i < sz; ++i) { 243 StreamableObject s_object = 244 (StreamableObject) streamable_object_list.elementAt(i); 245 connection.removeStreamableObject(s_object); 246 } 247 streamable_object_list = null; 248 } 249 } 250 251 253 public int getMaxFieldSize() throws SQLException { 254 return max_field_size; 256 } 257 258 public void setMaxFieldSize(int max) throws SQLException { 259 if (max >= 0) { 260 max_field_size = max; 261 } 262 else { 263 throw new SQLException("MaxFieldSize negative."); 264 } 265 } 266 267 public int getMaxRows() throws SQLException { 268 return max_row_count; 269 } 270 271 public void setMaxRows(int max) throws SQLException { 272 if (max >= 0) { 273 max_row_count = max; 274 } 275 else { 276 throw new SQLException("MaxRows negative."); 277 } 278 } 279 280 public void setEscapeProcessing(boolean enable) throws SQLException { 281 escape_processing = enable; 282 } 283 284 public int getQueryTimeout() throws SQLException { 285 return query_timeout; 286 } 287 288 public void setQueryTimeout(int seconds) throws SQLException { 289 if (seconds >= 0) { 290 query_timeout = seconds; 291 MDriver.QUERY_TIMEOUT = seconds; 302 } 303 else { 304 throw new SQLException("Negative query timout."); 305 } 306 } 307 308 public void cancel() throws SQLException { 309 if (result_set_list != null) { 310 for (int i = 0; i < result_set_list.length; ++i) { 311 connection.disposeResult(result_set_list[i].getResultID()); 312 } 313 } 314 } 315 316 public SQLWarning getWarnings() throws SQLException { 317 return head_warning; 318 } 319 320 public void clearWarnings() throws SQLException { 321 head_warning = null; 322 } 323 324 public void setCursorName(String name) throws SQLException { 325 } 327 328 330 334 public boolean execute(String sql) throws SQLException { 335 MResultSet result_set = executeQuery(new SQLQuery(sql)); 336 return !result_set.isUpdate(); 337 } 338 339 public ResultSet getResultSet() throws SQLException { 340 if (result_set_list != null) { 341 if (multi_result_set_index < result_set_list.length) { 342 return result_set_list[multi_result_set_index]; 343 } 344 } 345 return null; 346 } 347 348 public int getUpdateCount() throws SQLException { 349 if (result_set_list != null) { 350 if (multi_result_set_index < result_set_list.length) { 351 MResultSet rs = result_set_list[multi_result_set_index]; 352 if (rs.isUpdate()) { 353 return rs.intValue(); 354 } 355 } 356 } 357 return -1; 358 } 359 360 public boolean getMoreResults() throws SQLException { 361 if (result_set_list == null || 363 multi_result_set_index >= result_set_list.length) { 364 return false; 365 } 366 367 ++multi_result_set_index; 369 370 return true; 372 } 373 374 376 378 public void setFetchSize(int rows) throws SQLException { 379 if (rows >= 0) { 380 fetch_size = rows; 381 } 382 else { 383 throw new SQLException("Negative fetch size."); 384 } 385 } 386 387 public int getFetchSize() throws SQLException { 388 return fetch_size; 389 } 390 391 393 public void setFetchDirection(int direction) throws SQLException { 394 } 396 397 public int getFetchDirection() throws SQLException { 398 return ResultSet.FETCH_UNKNOWN; 399 } 400 401 public int getResultSetConcurrency() throws SQLException { 402 return ResultSet.CONCUR_READ_ONLY; 404 } 405 406 public int getResultSetType() throws SQLException { 407 return ResultSet.TYPE_SCROLL_INSENSITIVE; 409 } 410 411 public void addBatch(String sql) throws SQLException { 412 addBatch(new SQLQuery(sql)); 413 } 414 415 public void clearBatch() throws SQLException { 416 batch_list = null; 417 } 418 419 public int[] executeBatch() throws SQLException { 420 if (batch_list == null) { 422 throw new SQLException("Batch list is empty - nothing to do."); 424 } 425 426 int sz = batch_list.size(); 427 SQLQuery[] batch_query_list = new SQLQuery[sz]; 428 for (int i = 0; i < sz; ++i) { 429 batch_query_list[i] = (SQLQuery) batch_list.elementAt(i); 430 } 431 try { 432 MResultSet[] batch_results = executeQueries(batch_query_list); 434 435 int[] update_result = new int[sz]; 437 for (int i = 0; i < sz; ++i) { 438 update_result[i] = batch_results[i].intValue(); 439 batch_results[i].closeCurrentResult(); 440 } 441 442 return update_result; 443 } 444 finally { 445 clearBatch(); 447 } 448 } 449 450 public Connection getConnection() throws SQLException { 451 return connection; 452 } 453 454 456 458 460 public boolean getMoreResults(int current) throws SQLException { 461 return getMoreResults(); 462 } 463 464 public ResultSet getGeneratedKeys() throws SQLException { 465 throw MSQLException.unsupported(); 466 } 467 468 public int executeUpdate(String sql, int autoGeneratedKeys) 469 throws SQLException { 470 throw MSQLException.unsupported(); 471 } 472 473 public int executeUpdate(String sql, int columnIndexes[]) 474 throws SQLException { 475 throw MSQLException.unsupported(); 476 } 477 478 public int executeUpdate(String sql, String columnNames[]) 479 throws SQLException { 480 throw MSQLException.unsupported(); 481 } 482 483 public boolean execute(String sql, int autoGeneratedKeys) 484 throws SQLException { 485 throw MSQLException.unsupported(); 486 } 487 488 public boolean execute(String sql, int columnIndexes[]) 489 throws SQLException { 490 throw MSQLException.unsupported(); 491 } 492 493 public boolean execute(String sql, String columnNames[]) 494 throws SQLException { 495 throw MSQLException.unsupported(); 496 } 497 498 public int getResultSetHoldability() throws SQLException { 499 return ResultSet.HOLD_CURSORS_OVER_COMMIT; 501 } 502 503 505 506 508 511 public void finalize() { 512 try { 513 close(); 514 } 515 catch (SQLException e) { } 516 } 517 518 } 519 | Popular Tags |