1 21 22 package org.opensubsystems.core.persist.db.sapdb; 23 24 import java.sql.CallableStatement ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 import java.sql.Statement ; 29 import java.util.Map ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 import org.opensubsystems.core.data.BasicDataObject; 34 import org.opensubsystems.core.data.ModifiableDataObject; 35 import org.opensubsystems.core.error.OSSDatabaseAccessException; 36 import org.opensubsystems.core.error.OSSException; 37 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 38 import org.opensubsystems.core.persist.db.DatabaseImpl; 39 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 40 import org.opensubsystems.core.util.DatabaseUtils; 41 import org.opensubsystems.core.util.Log; 42 43 import com.sap.dbtech.powertoys.DBM; 44 import com.sap.dbtech.powertoys.DBMException; 45 import com.sap.dbtech.rte.comm.RTEException; 46 47 55 public class SapDBDatabaseImpl extends DatabaseImpl 56 { 57 59 62 private static Logger s_logger = Log.getInstance(SapDBDatabaseImpl.class); 63 64 66 71 public SapDBDatabaseImpl( 72 ) throws OSSException 73 { 74 super(); 75 } 76 77 79 82 protected void createUser( 83 Connection cntAdminDBConnection 84 ) throws OSSException 85 { 86 PreparedStatement pstmQuery = null; 88 try 89 { 90 97 StringBuffer buffer = new StringBuffer (); 98 99 buffer.append("CREATE user "); 100 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 101 buffer.append(" PASSWORD "); 102 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword()); 103 buffer.append(" RESOURCE NOT EXCLUSIVE"); 104 105 pstmQuery = cntAdminDBConnection.prepareStatement(buffer.toString()); 106 if (pstmQuery.execute()) 107 { 108 pstmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 110 } 111 112 DatabaseTransactionFactoryImpl.getInstance().commitTransaction( 117 cntAdminDBConnection); 118 119 s_logger.log(Level.FINER, "Database user " 120 + DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser() 121 + " with password " 122 + DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword() 123 + " created."); 124 } 125 catch (SQLException sqleExc) 126 { 127 try 128 { 129 DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction( 134 cntAdminDBConnection); 135 } 136 catch (SQLException sqleExc2) 137 { 138 s_logger.log(Level.WARNING, 140 "Failed to rollback changes for creation of user.", 141 sqleExc2); 142 } 143 s_logger.log(Level.SEVERE, 144 "Unable to create default database user.", 145 sqleExc); 146 throw new OSSDatabaseAccessException("Unable to create default database user.", 147 sqleExc); 148 } 149 finally 150 { 151 DatabaseUtils.closeStatement(pstmQuery); 152 } 153 } 154 155 158 public void startDatabaseServer( 159 ) throws OSSException 160 { 161 String result = null; 162 163 try 164 { 165 String strURL; 166 167 strURL = DatabaseConnectionFactoryImpl.getInstance().getDatabaseURL(); 168 DBM dbSession = DBM.dbDBM(getDatabaseHost(strURL), getDatabaseName(strURL)); 169 dbSession.cmd("user_logon " + 171 DatabaseConnectionFactoryImpl.getInstance().getDatabaseAdminUser() + 172 "," + 173 DatabaseConnectionFactoryImpl.getInstance().getDatabaseAdminPassword()); 174 result = dbSession.cmd("db_state"); 176 if (!(result.indexOf("ONLINE") > 0)) 178 { 179 result = dbSession.cmd("db_warm"); 181 s_logger.log(Level.FINEST, "Starting SAP DB server"); 182 } 183 dbSession.release(); 184 } 185 catch (DBMException eDBMex) 186 { 187 throw new OSSDatabaseAccessException("Unable to connect to the SAP DB server", 188 eDBMex); 189 } 190 catch (RTEException eRTEex) 191 { 192 throw new OSSDatabaseAccessException("Unable to connect to the SAP DB server", 193 eRTEex); 194 } 195 } 196 197 200 public void createDatabaseInstance( 201 ) throws OSSException 202 { 203 262 } 263 264 267 public int getDatabaseType() 268 { 269 return DatabaseImpl.SAPDB_DATABASE_TYPE; 270 } 271 272 275 public String getDatabaseTypeIdentifier() 276 { 277 return DatabaseImpl.SAPDB_DATABASE_TYPE_IDENTIFIER; 278 } 279 280 282 288 protected String getDatabaseHost( 289 String strDatabaseURL 290 ) 291 { 292 String strDatabaseHost = "localhost"; 295 int sIndex = 0; 296 int eIndex = 0; 297 298 sIndex = strDatabaseURL.indexOf("//"); 300 301 if (sIndex > 0) 302 { 303 eIndex = strDatabaseURL.indexOf("/", sIndex + 2); 305 strDatabaseHost = strDatabaseURL.substring(sIndex + 2, eIndex); 306 } 307 308 return strDatabaseHost; 309 } 310 311 317 protected String getDatabaseName( 318 String strDatabaseURL 319 ) 320 { 321 String strDatabaseName = null; 324 int sIndex = 0; 325 int eIndex = 0; 326 327 sIndex = strDatabaseURL.indexOf("//"); 329 330 if (sIndex > 0) 331 { 332 sIndex = strDatabaseURL.indexOf("/", sIndex + "//".length()); 334 } 335 else 336 { 337 sIndex = strDatabaseURL.indexOf(":", "sapdb:".length()); 339 } 340 eIndex = strDatabaseURL.indexOf("?", sIndex + "?".length()); 341 strDatabaseName = strDatabaseURL.substring( 342 sIndex + 1, 343 eIndex > 0 ? eIndex : strDatabaseURL.length() 344 ); 345 return strDatabaseName; 346 } 347 348 351 public void stop( 352 ) throws OSSException 353 { 354 s_logger.entering(this.getClass().getName(), "stop"); 355 356 super.stop(); 359 360 s_logger.entering(this.getClass().getName(), "stop"); 361 } 362 363 366 public boolean preferCountToLast( 367 ) 368 { 369 return true; 373 } 374 375 378 public String getCurrentTimestampFunctionCall() 379 { 380 return "now()"; 382 } 383 384 387 public String getSQLCountFunctionCall() 388 { 389 return "count(*)"; 392 } 393 394 397 public Object [] getSQLAnalyzeFunctionCall( 398 Map mapTableNames 399 ) 400 { 401 return null; 405 } 406 407 410 public boolean isCallableStatement( 411 String strQuery 412 ) 413 { 414 return strQuery.indexOf("call ") != -1; 415 } 416 417 420 public void insertAndFetchGeneratedValues( 421 Connection dbConnection, 422 PreparedStatement insertStatement, 423 boolean bIsInDomain, 424 String strTableName, 425 int iIndex, 426 BasicDataObject data 427 ) throws SQLException , 428 OSSException 429 { 430 SapDBDataUtils.insertAndFetchGeneratedValues((CallableStatement )insertStatement, 431 iIndex, data); 432 } 433 434 437 public void updatedAndFetchGeneratedValues( 438 String strDataName, 439 Connection dbConnection, 440 PreparedStatement updateStatement, 441 boolean bIsInDomain, 442 String strTableName, 443 int iIndex, 444 ModifiableDataObject data 445 ) throws SQLException , 446 OSSException 447 { 448 SapDBDataUtils.updateAndFetchGeneratedValues((CallableStatement )updateStatement, 449 iIndex, data); 450 } 451 } 452 | Popular Tags |