1 21 22 package org.opensubsystems.core.persist.db.mssql; 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.Iterator ; 30 import java.util.Map ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 34 import org.opensubsystems.core.data.BasicDataObject; 35 import org.opensubsystems.core.data.ModifiableDataObject; 36 import org.opensubsystems.core.error.OSSConfigException; 37 import org.opensubsystems.core.error.OSSDatabaseAccessException; 38 import org.opensubsystems.core.error.OSSException; 39 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 40 import org.opensubsystems.core.persist.db.DatabaseImpl; 41 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 42 import org.opensubsystems.core.util.DatabaseUtils; 43 import org.opensubsystems.core.util.Log; 44 45 76 public class MSSQLDatabaseImpl extends DatabaseImpl 77 { 78 80 83 private static Logger s_logger = Log.getInstance(MSSQLDatabaseImpl.class); 84 85 87 92 public MSSQLDatabaseImpl( 93 ) throws OSSException 94 { 95 super(); 96 } 97 98 100 103 protected void createUser( 104 Connection cntAdminDBConnection 105 ) throws OSSException 106 { 107 PreparedStatement pstmQuery = null; 109 try 110 { 111 StringBuffer buffer = new StringBuffer (); 112 String strDatabaseURL = DatabaseConnectionFactoryImpl.getInstance().getDatabaseURL(); 113 String strDatabaseName = ""; 114 115 if (strDatabaseURL.indexOf(":jtds:") > 0) 119 { 120 strDatabaseName = strDatabaseURL.substring( 122 strDatabaseURL.lastIndexOf("/") + 1, strDatabaseURL.length()); 123 } 124 else if (strDatabaseURL.indexOf(":microsoft:") > 0) 125 { 126 strDatabaseName = strDatabaseURL.substring( 128 strDatabaseURL.lastIndexOf("DatabaseName=") + 129 "DatabaseName=".length(), strDatabaseURL.indexOf(";", 130 strDatabaseURL.lastIndexOf("DatabaseName="))); 131 } 132 else if (strDatabaseURL.indexOf(":bea:") > 0) 133 { 134 strDatabaseName = strDatabaseURL.substring( 136 strDatabaseURL.lastIndexOf("DatabaseName=") + 137 "DatabaseName=".length(), strDatabaseURL.length()); 138 } 139 else 140 { 141 throw new OSSConfigException("Unsupported URL: " + strDatabaseURL); 142 } 143 144 buffer.append("USE "); 145 buffer.append(strDatabaseName); 146 buffer.append("; EXEC sp_addlogin '"); 147 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 148 buffer.append("', '"); 149 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword()); 150 buffer.append("', '"); 151 buffer.append(strDatabaseName); 152 buffer.append("', 'us_english'"); 153 buffer.append("; EXEC sp_adduser '"); 154 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 155 buffer.append("' ; GRANT ALL TO "); 156 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 157 158 159 pstmQuery = cntAdminDBConnection.prepareStatement(buffer.toString()); 160 161 if (pstmQuery.execute()) 162 { 163 pstmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 165 } 166 167 DatabaseTransactionFactoryImpl.getInstance().commitTransaction( 172 cntAdminDBConnection); 173 174 s_logger.log(Level.FINER, "Database user " 175 + DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser() 176 + " with password " 177 + DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword() 178 + " created."); 179 } 180 catch (SQLException sqleExc) 181 { 182 try 183 { 184 DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction( 189 cntAdminDBConnection); 190 } 191 catch (SQLException sqleExc2) 192 { 193 s_logger.log(Level.WARNING, 195 "Failed to rollback changes for creation of user.", 196 sqleExc2); 197 } 198 s_logger.log(Level.SEVERE, 199 "Unable to create default database user.", 200 sqleExc); 201 throw new OSSDatabaseAccessException("Unable to create default database user.", 202 sqleExc); 203 } 204 finally 205 { 206 DatabaseUtils.closeStatement(pstmQuery); 207 } 208 } 209 210 213 public void startDatabaseServer() throws OSSException 214 { 215 } 217 218 221 public void createDatabaseInstance() throws OSSException 222 { 223 } 225 226 229 public void stop( 230 ) throws OSSException 231 { 232 s_logger.entering(this.getClass().getName(), "stop"); 233 234 super.stop(); 237 238 s_logger.entering(this.getClass().getName(), "stop"); 239 } 240 241 244 public int getDatabaseType() 245 { 246 return DatabaseImpl.MSSQL_DATABASE_TYPE; 247 } 248 249 252 public String getDatabaseTypeIdentifier() 253 { 254 return DatabaseImpl.MSSQL_DATABASE_TYPE_IDENTIFIER; 255 } 256 257 260 public String getCurrentTimestampFunctionCall() 261 { 262 return "CURRENT_TIMESTAMP"; 263 } 264 265 268 public String getSQLCountFunctionCall() 269 { 270 return "count(*)"; 272 } 273 274 277 public boolean preferCountToLast( 278 ) 279 { 280 return false; 283 } 284 285 288 public boolean hasSelectListRangeSupport( 289 ) 290 { 291 return false; 295 } 296 297 300 public Object [] getSQLAnalyzeFunctionCall( 301 Map mapTableNames 302 ) 303 { 304 307 String [] arrReturn = new String [mapTableNames.size()]; 308 StringBuffer buffer = new StringBuffer (); 309 Iterator itItem; 310 int iIndex = 0; 311 312 itItem = mapTableNames.values().iterator(); 313 while (itItem.hasNext()) 314 { 315 buffer.append("update statistics "); 317 buffer.append((String )itItem.next()); 318 buffer.append(" with all; "); 319 arrReturn[iIndex++] = buffer.toString(); 321 buffer.delete(0, buffer.length()); 323 324 } 325 326 return new Object [] {arrReturn, Boolean.TRUE}; 327 } 328 329 332 public boolean isCallableStatement( 333 String strQuery 334 ) 335 { 336 return strQuery.indexOf("EXEC ") != -1; 337 } 338 339 342 public void insertAndFetchGeneratedValues( 343 Connection dbConnection, 344 PreparedStatement insertStatement, 345 boolean bIsInDomain, 346 String strTableName, 347 int iIndex, 348 BasicDataObject data 349 ) throws SQLException , 350 OSSException 351 { 352 MSSQLDataUtils.insertAndFetchGeneratedValues((CallableStatement )insertStatement, 353 iIndex, data); 354 } 355 356 359 public void updatedAndFetchGeneratedValues( 360 String strDataName, 361 Connection dbConnection, 362 PreparedStatement updateStatement, 363 boolean bIsInDomain, 364 String strTableName, 365 int iIndex, 366 ModifiableDataObject data 367 ) throws SQLException , 368 OSSException 369 { 370 MSSQLDataUtils.updateAndFetchGeneratedValues((CallableStatement )updateStatement, 371 iIndex, data); 372 } 373 } 374 | Popular Tags |