1 25 package com.mysql.jdbc; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.OutputStream ; 29 30 import java.sql.SQLException ; 31 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 54 public class BlobFromLocator implements java.sql.Blob { 55 private String blobColumnName = null; 56 57 58 private ResultSet creatorResultSet; 59 60 private int numColsInResultSet = 0; 61 62 private int numPrimaryKeys = 0; 63 64 private List primaryKeyColumns = null; 65 66 private List primaryKeyValues = null; 67 68 private String quotedId; 69 70 private String tableName = null; 71 72 75 BlobFromLocator(ResultSet creatorResultSetToSet, int blobColumnIndex) 76 throws SQLException { 77 this.creatorResultSet = creatorResultSetToSet; 78 79 this.numColsInResultSet = this.creatorResultSet.fields.length; 80 this.quotedId = this.creatorResultSet.connection.getMetaData() 81 .getIdentifierQuoteString(); 82 83 if (this.numColsInResultSet > 1) { 84 this.primaryKeyColumns = new ArrayList (); 85 this.primaryKeyValues = new ArrayList (); 86 87 for (int i = 0; i < this.numColsInResultSet; i++) { 88 if (this.creatorResultSet.fields[i].isPrimaryKey()) { 89 StringBuffer keyName = new StringBuffer (); 90 keyName.append(quotedId); 91 92 String originalColumnName = this.creatorResultSet.fields[i] 93 .getOriginalName(); 94 95 if (this.creatorResultSet.connection.getIO() 96 .hasLongColumnInfo() 97 && (originalColumnName != null) 98 && (originalColumnName.length() > 0)) { 99 keyName.append(originalColumnName); 100 } else { 101 keyName.append(this.creatorResultSet.fields[i] 102 .getName()); 103 } 104 105 keyName.append(quotedId); 106 107 this.primaryKeyColumns.add(keyName.toString()); 108 this.primaryKeyValues.add(this.creatorResultSet 109 .getString(i + 1)); 110 } 111 } 112 } else { 113 notEnoughInformationInQuery(); 114 } 115 116 this.numPrimaryKeys = this.primaryKeyColumns.size(); 117 118 if (this.numPrimaryKeys == 0) { 119 notEnoughInformationInQuery(); 120 } 121 122 if (this.creatorResultSet.fields[0].getOriginalTableName() != null) { 123 StringBuffer tableNameBuffer = new StringBuffer (); 124 125 String databaseName = this.creatorResultSet.fields[0] 126 .getDatabaseName(); 127 128 if ((databaseName != null) && (databaseName.length() > 0)) { 129 tableNameBuffer.append(quotedId); 130 tableNameBuffer.append(databaseName); 131 tableNameBuffer.append(quotedId); 132 tableNameBuffer.append('.'); 133 } 134 135 tableNameBuffer.append(quotedId); 136 tableNameBuffer.append(this.creatorResultSet.fields[0] 137 .getOriginalTableName()); 138 tableNameBuffer.append(quotedId); 139 140 this.tableName = tableNameBuffer.toString(); 141 } else { 142 StringBuffer tableNameBuffer = new StringBuffer (); 143 144 tableNameBuffer.append(quotedId); 145 tableNameBuffer.append(this.creatorResultSet.fields[0] 146 .getTableName()); 147 tableNameBuffer.append(quotedId); 148 149 this.tableName = tableNameBuffer.toString(); 150 } 151 152 this.blobColumnName = this.creatorResultSet.getString(blobColumnIndex); 153 } 154 155 163 public java.io.InputStream getBinaryStream() throws SQLException { 164 return new ByteArrayInputStream (getBytes(1L, (int) length())); 165 } 166 167 182 public byte[] getBytes(long pos, int length) throws SQLException { 183 java.sql.ResultSet blobRs = null; 184 java.sql.PreparedStatement pStmt = null; 185 186 StringBuffer query = new StringBuffer ("SELECT SUBSTRING("); 188 query.append(this.blobColumnName); 189 query.append(", "); 190 query.append(pos); 191 query.append(", "); 192 query.append(length); 193 query.append(") FROM "); 194 query.append(this.tableName); 195 query.append(" WHERE "); 196 197 query.append((String ) this.primaryKeyColumns.get(0)); 198 query.append(" = ?"); 199 200 for (int i = 1; i < this.numPrimaryKeys; i++) { 201 query.append(" AND "); 202 query.append((String ) this.primaryKeyColumns.get(i)); 203 query.append(" = ?"); 204 } 205 206 try { 207 pStmt = this.creatorResultSet.connection.prepareStatement(query 209 .toString()); 210 211 for (int i = 0; i < this.numPrimaryKeys; i++) { 212 pStmt.setString(i + 1, (String ) this.primaryKeyValues.get(i)); 213 } 214 215 blobRs = pStmt.executeQuery(); 216 217 if (blobRs.next()) { 218 return ((com.mysql.jdbc.ResultSet) blobRs).getBytes(1, true); 219 } 220 221 throw new SQLException ( 222 "BLOB data not found! Did primary keys change?", 223 SQLError.SQL_STATE_GENERAL_ERROR); 224 } finally { 225 if (blobRs != null) { 226 try { 227 blobRs.close(); 228 } catch (SQLException sqlEx) { 229 ; } 231 232 blobRs = null; 233 } 234 235 if (pStmt != null) { 236 try { 237 pStmt.close(); 238 } catch (SQLException sqlEx) { 239 ; } 241 242 pStmt = null; 243 } 244 } 245 } 246 247 256 public long length() throws SQLException { 257 java.sql.ResultSet blobRs = null; 258 java.sql.PreparedStatement pStmt = null; 259 260 StringBuffer query = new StringBuffer ("SELECT LENGTH("); 262 query.append(this.blobColumnName); 263 query.append(") FROM "); 264 query.append(this.tableName); 265 query.append(" WHERE "); 266 267 query.append((String ) this.primaryKeyColumns.get(0)); 268 query.append(" = ?"); 269 270 for (int i = 1; i < this.numPrimaryKeys; i++) { 271 query.append(" AND "); 272 query.append((String ) this.primaryKeyColumns.get(i)); 273 query.append(" = ?"); 274 } 275 276 try { 277 pStmt = this.creatorResultSet.connection.prepareStatement(query 279 .toString()); 280 281 for (int i = 0; i < this.numPrimaryKeys; i++) { 282 pStmt.setString(i + 1, (String ) this.primaryKeyValues.get(i)); 283 } 284 285 blobRs = pStmt.executeQuery(); 286 287 if (blobRs.next()) { 288 return blobRs.getLong(1); 289 } 290 291 throw new SQLException ( 292 "BLOB data not found! Did primary keys change?", 293 SQLError.SQL_STATE_GENERAL_ERROR); 294 } finally { 295 if (blobRs != null) { 296 try { 297 blobRs.close(); 298 } catch (SQLException sqlEx) { 299 ; } 301 302 blobRs = null; 303 } 304 305 if (pStmt != null) { 306 try { 307 pStmt.close(); 308 } catch (SQLException sqlEx) { 309 ; } 311 312 pStmt = null; 313 } 314 } 315 } 316 317 private void notEnoughInformationInQuery() throws SQLException { 318 throw new SQLException ("Emulated BLOB locators must come from " 319 + "a ResultSet with only one table selected, and all primary " 320 + "keys selected", SQLError.SQL_STATE_GENERAL_ERROR); 321 } 322 323 326 public long position(byte[] pattern, long start) throws SQLException { 327 java.sql.ResultSet blobRs = null; 328 java.sql.PreparedStatement pStmt = null; 329 330 StringBuffer query = new StringBuffer ("SELECT LOCATE("); 332 query.append("?, "); 333 query.append(this.blobColumnName); 334 query.append(", "); 335 query.append(start); 336 query.append(") FROM "); 337 query.append(this.tableName); 338 query.append(" WHERE "); 339 340 query.append((String ) this.primaryKeyColumns.get(0)); 341 query.append(" = ?"); 342 343 for (int i = 1; i < this.numPrimaryKeys; i++) { 344 query.append(" AND "); 345 query.append((String ) this.primaryKeyColumns.get(i)); 346 query.append(" = ?"); 347 } 348 349 try { 350 pStmt = this.creatorResultSet.connection.prepareStatement(query 352 .toString()); 353 pStmt.setBytes(1, pattern); 354 355 for (int i = 0; i < this.numPrimaryKeys; i++) { 356 pStmt.setString(i + 2, (String ) this.primaryKeyValues.get(i)); 357 } 358 359 blobRs = pStmt.executeQuery(); 360 361 if (blobRs.next()) { 362 return blobRs.getLong(1); 363 } 364 365 throw new SQLException ( 366 "BLOB data not found! Did primary keys change?", 367 SQLError.SQL_STATE_GENERAL_ERROR); 368 } finally { 369 if (blobRs != null) { 370 try { 371 blobRs.close(); 372 } catch (SQLException sqlEx) { 373 ; } 375 376 blobRs = null; 377 } 378 379 if (pStmt != null) { 380 try { 381 pStmt.close(); 382 } catch (SQLException sqlEx) { 383 ; } 385 386 pStmt = null; 387 } 388 } 389 } 390 391 405 public long position(java.sql.Blob pattern, long start) throws SQLException { 406 return position(pattern.getBytes(0, (int) pattern.length()), start); 407 } 408 409 412 public OutputStream setBinaryStream(long indexToWriteAt) 413 throws SQLException { 414 throw new NotImplemented(); 415 } 416 417 420 public int setBytes(long writeAt, byte[] bytes) throws SQLException { 421 return setBytes(writeAt, bytes, 0, bytes.length); 422 } 423 424 427 public int setBytes(long writeAt, byte[] bytes, int offset, int length) 428 throws SQLException { 429 java.sql.PreparedStatement pStmt = null; 430 431 if ((offset + length) > bytes.length) { 432 length = bytes.length - offset; 433 } 434 435 byte[] bytesToWrite = new byte[length]; 436 System.arraycopy(bytes, offset, bytesToWrite, 0, length); 437 438 StringBuffer query = new StringBuffer ("UPDATE "); 440 query.append(this.tableName); 441 query.append(" SET "); 442 query.append(this.blobColumnName); 443 query.append(" = INSERT("); 444 query.append(this.blobColumnName); 445 query.append(", "); 446 query.append(writeAt); 447 query.append(", "); 448 query.append(length); 449 query.append(", ?) WHERE "); 450 451 query.append((String ) this.primaryKeyColumns.get(0)); 452 query.append(" = ?"); 453 454 for (int i = 1; i < this.numPrimaryKeys; i++) { 455 query.append(" AND "); 456 query.append((String ) this.primaryKeyColumns.get(i)); 457 query.append(" = ?"); 458 } 459 460 try { 461 pStmt = this.creatorResultSet.connection.prepareStatement(query 463 .toString()); 464 465 pStmt.setBytes(1, bytesToWrite); 466 467 for (int i = 0; i < this.numPrimaryKeys; i++) { 468 pStmt.setString(i + 2, (String ) this.primaryKeyValues.get(i)); 469 } 470 471 int rowsUpdated = pStmt.executeUpdate(); 472 473 if (rowsUpdated != 1) { 474 throw new SQLException ( 475 "BLOB data not found! Did primary keys change?", 476 SQLError.SQL_STATE_GENERAL_ERROR); 477 } 478 } finally { 479 if (pStmt != null) { 480 try { 481 pStmt.close(); 482 } catch (SQLException sqlEx) { 483 ; } 485 486 pStmt = null; 487 } 488 } 489 490 return (int) length(); 491 } 492 493 496 public void truncate(long length) throws SQLException { 497 java.sql.PreparedStatement pStmt = null; 498 499 StringBuffer query = new StringBuffer ("UPDATE "); 501 query.append(this.tableName); 502 query.append(" SET "); 503 query.append(this.blobColumnName); 504 query.append(" = LEFT("); 505 query.append(this.blobColumnName); 506 query.append(", "); 507 query.append(length); 508 query.append(") WHERE "); 509 510 query.append((String ) this.primaryKeyColumns.get(0)); 511 query.append(" = ?"); 512 513 for (int i = 1; i < this.numPrimaryKeys; i++) { 514 query.append(" AND "); 515 query.append((String ) this.primaryKeyColumns.get(i)); 516 query.append(" = ?"); 517 } 518 519 try { 520 pStmt = this.creatorResultSet.connection.prepareStatement(query 522 .toString()); 523 524 for (int i = 0; i < this.numPrimaryKeys; i++) { 525 pStmt.setString(i + 1, (String ) this.primaryKeyValues.get(i)); 526 } 527 528 int rowsUpdated = pStmt.executeUpdate(); 529 530 if (rowsUpdated != 1) { 531 throw new SQLException ( 532 "BLOB data not found! Did primary keys change?", 533 SQLError.SQL_STATE_GENERAL_ERROR); 534 } 535 } finally { 536 if (pStmt != null) { 537 try { 538 pStmt.close(); 539 } catch (SQLException sqlEx) { 540 ; } 542 543 pStmt = null; 544 } 545 } 546 } 547 } 548 | Popular Tags |