1 64 65 package com.jcorporate.expresso.core.dbobj; 66 67 import com.jcorporate.expresso.core.db.DBConnection; 68 import com.jcorporate.expresso.core.db.DBConnectionPool; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.misc.StringUtil; 71 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 72 import org.apache.log4j.Logger; 73 74 import java.io.InputStream ; 75 import java.sql.PreparedStatement ; 76 import java.sql.SQLException ; 77 78 127 128 public class LOBSupport { 129 static protected LOBSupport theInstance = null; 130 static private Logger log = Logger.getLogger("com.jcorporate" + 131 ".expresso.core.dbobj.LOBSupport"); 132 133 136 protected LOBSupport() { 137 } 138 139 144 public static synchronized LOBSupport getInstance() { 145 if (theInstance == null) { 146 theInstance = new LOBSupport(); 147 } 148 149 return theInstance; 150 } 151 152 153 public String getCLOB(DBObject baseObject, 154 String fieldName) throws DBException { 155 DBConnectionPool aPool = DBConnectionPool.getInstance(baseObject.getDataContext()); 156 DBConnection localConnection = aPool.getConnection(); 157 String returnValue = null; 158 try { 159 returnValue = this.getCLOB(baseObject, fieldName, localConnection); 160 } finally { 161 aPool.release(localConnection); 162 } 163 return returnValue; 164 } 165 166 179 public String getCLOB(DBObject baseObject, 180 String fieldName, 181 DBConnection theConnection) throws DBException { 182 prepSelectResultSet(baseObject, fieldName, theConnection); 183 184 if (theConnection.next()) { 185 return StringUtil.notNull(theConnection.getString(1)); 186 } 187 188 return ""; 189 } 190 191 201 public void setCLOB(DBObject baseObject, 202 String fieldName, 203 String theData, 204 DBConnection theConnection) throws DBException { 205 206 PreparedStatement preparedStatement = prepUpdate(baseObject, fieldName, theConnection); 207 208 try { 209 if ("interbase.interclient.Driver".equals(theConnection.getDBDriver())) { 210 byte[] data = theData.getBytes(); 214 java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (data); 215 preparedStatement.setAsciiStream(1, bis, data.length); 216 } else { 217 java.io.Reader r = new java.io.StringReader (theData); 218 preparedStatement.setCharacterStream(1, r, theData.length()); 219 } 220 } catch (SQLException ex) { 221 throw new DBException("Unable to set CharacterStream to CLOB object.", ex); 222 } 223 224 finalizeUpdate(theConnection); 225 } 226 227 228 241 public byte[] getBLOB(DBObject baseObject, 242 String fieldName, 243 DBConnection theConnection) throws DBException { 244 245 prepSelectResultSet(baseObject, fieldName, theConnection); 246 if (theConnection.next()) { 247 byte retVal[] = theConnection.getBytes(1); 248 if (retVal == null) { 249 return new byte[0]; 250 } else { 251 return retVal; 252 } 253 } 254 255 return new byte[0]; 256 } 257 258 267 public void setBLOB(DBObject baseObject, 268 String fieldName, 269 byte theData[], 270 DBConnection theConnection) throws DBException { 271 272 PreparedStatement preparedStatement = prepUpdate(baseObject, fieldName, theConnection); 273 274 275 try { 276 preparedStatement.setBytes(1, theData); 277 } catch (SQLException ex) { 278 throw new DBException("Error setting BLOB object", ex); 279 } 280 281 finalizeUpdate(theConnection); 282 } 283 284 295 public void setBLOB(DBObject baseObject, 296 String fieldName, 297 InputStream theData, 298 int dataLength, 299 DBConnection theConnection) throws DBException { 300 PreparedStatement preparedStatement = prepUpdate(baseObject, fieldName, theConnection); 301 302 303 try { 304 preparedStatement.setBinaryStream(1, theData, dataLength); 305 } catch (SQLException ex) { 306 throw new DBException("Error setting BLOB object", ex); 307 } 308 309 310 finalizeUpdate(theConnection); 311 } 312 313 314 327 protected void prepSelectResultSet(DBObject baseObject, 328 String fieldName, 329 DBConnection theConnection) throws DBException { 330 FastStringBuffer prepStatement = FastStringBuffer.getInstance(); 331 try { 332 prepStatement.append("SELECT "); 333 prepStatement.append(fieldName); 334 prepStatement.append(" from "); 335 prepStatement.append(baseObject.getJDBCMetaData().getTargetSQLTable(baseObject.getDataContext())); 336 String whereClause = baseObject.buildWhereClause(false); 337 prepStatement.append(whereClause); 338 String thePrepString = prepStatement.toString(); 339 if (log.isDebugEnabled()) { 340 log.debug("Preparing prepared statement: " + thePrepString); 341 } 342 343 PreparedStatement prep = theConnection.createPreparedStatement(thePrepString); 344 if (prep == null) { 345 throw new DBException("Unable to create prepared statement for CLOB retrieval." + 346 " Check DBConnection log for details"); 347 } 348 theConnection.execute(); 349 350 if (log.isDebugEnabled()) { 351 log.debug("Succesfully executed prepared statement"); 352 } 353 } finally { 354 prepStatement.release(); 355 prepStatement = null; 356 } 357 } 358 359 369 protected PreparedStatement prepUpdate(DBObject baseObject, 370 String fieldName, 371 DBConnection theConnection) throws DBException { 372 String whereClause = baseObject.buildWhereClause(false); 373 374 FastStringBuffer prepStatement = FastStringBuffer.getInstance(); 375 String theSQL = null; 376 try { 377 prepStatement.append("UPDATE "); 378 prepStatement.append(baseObject.getJDBCMetaData().getTargetSQLTable(baseObject.getDataContext())); 379 prepStatement.append(" SET "); 380 prepStatement.append(fieldName); 381 prepStatement.append(" = ? "); 382 prepStatement.append(whereClause); 383 theSQL = prepStatement.toString(); 384 } finally { 385 prepStatement.release(); 386 prepStatement = null; 387 } 388 389 return theConnection.createPreparedStatement(theSQL); 390 } 391 392 protected void finalizeUpdate(DBConnection theConnection) throws DBException { 393 394 boolean alreadyInTransaction = !(theConnection.getAutoCommit()); 395 boolean success = false; 396 397 if (!alreadyInTransaction && theConnection.supportsTransactions()) { 403 if (log.isDebugEnabled()) { 404 log.debug("Turning off auto-commit"); 405 } 406 theConnection.setAutoCommit(false); 407 } 408 409 try { 410 theConnection.executeUpdate(null); 411 success = true; 412 } finally { 413 if (success == false) { 414 if (!alreadyInTransaction && theConnection.supportsTransactions()) { 415 if (log.isDebugEnabled()) { 416 log.debug("rolling back"); 417 } 418 theConnection.rollback(); 419 } 420 } 421 if (!alreadyInTransaction && theConnection.supportsTransactions()) { 422 if (log.isDebugEnabled()) { 423 log.debug("Finishing commit and turning auto-commit back to true"); 424 } 425 theConnection.commit(); 426 theConnection.setAutoCommit(true); 427 } 428 } 429 430 } 431 432 } 433 | Popular Tags |