1 21 22 package org.opensubsystems.core.persist.db.sybase; 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.OSSDatabaseAccessException; 37 import org.opensubsystems.core.error.OSSException; 38 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 39 import org.opensubsystems.core.persist.db.DatabaseImpl; 40 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 41 import org.opensubsystems.core.util.DatabaseUtils; 42 import org.opensubsystems.core.util.Log; 43 44 52 public class SybaseDatabaseImpl extends DatabaseImpl 53 { 54 56 59 private static Logger s_logger = Log.getInstance(SybaseDatabaseImpl.class); 60 61 63 68 public SybaseDatabaseImpl( 69 ) throws OSSException 70 { 71 super(); 72 } 73 74 76 79 protected void createUser( 80 Connection cntAdminDBConnection 81 ) throws OSSException 82 { 83 PreparedStatement pstmQuery = null; 85 try 86 { 87 StringBuffer buffer = new StringBuffer (); 88 String strDatabaseURL = DatabaseConnectionFactoryImpl.getInstance().getDatabaseURL(); 89 String strDatabaseName = strDatabaseURL.substring( 90 strDatabaseURL.lastIndexOf("/") + 1, strDatabaseURL.length()); 91 92 buffer.append("USE "); 93 buffer.append(strDatabaseName); 94 buffer.append(" EXEC sp_addlogin '"); 95 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 96 buffer.append("', '"); 97 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword()); 98 buffer.append("', @defdb='"); 99 buffer.append(strDatabaseName); 100 buffer.append("', @deflanguage='us_english', @auth_mech = 'ASE'"); 101 buffer.append(" EXEC sp_locklogin '"); 102 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 103 buffer.append("', 'unlock'"); 104 buffer.append(" EXEC sp_adduser '"); 105 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 106 buffer.append("', '"); 107 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 108 buffer.append("', 'public'"); 109 buffer.append(" GRANT ALL to "); 110 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 111 112 DatabaseTransactionFactoryImpl.getInstance().commitTransaction( 117 cntAdminDBConnection); 118 119 boolean bOriginalAutoCommit = cntAdminDBConnection.getAutoCommit(); 122 123 try 124 { 125 if (!bOriginalAutoCommit) 126 { 127 cntAdminDBConnection.setAutoCommit(true); 128 } 129 130 pstmQuery = cntAdminDBConnection.prepareStatement(buffer.toString()); 131 132 if (pstmQuery.execute()) 133 { 134 pstmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 136 } 137 } 138 finally 139 { 140 if (!bOriginalAutoCommit) 141 { 142 cntAdminDBConnection.setAutoCommit(bOriginalAutoCommit); 143 } 144 } 145 146 s_logger.log(Level.FINER, "Database user " 147 + DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser() 148 + " with password " 149 + DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword() 150 + " created."); 151 } 152 catch (SQLException sqleExc) 153 { 154 try 155 { 156 DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction( 161 cntAdminDBConnection); 162 } 163 catch (SQLException sqleExc2) 164 { 165 s_logger.log(Level.WARNING, 167 "Failed to rollback changes for creation of user.", 168 sqleExc2); 169 } 170 s_logger.log(Level.SEVERE, 171 "Unable to create default database user.", 172 sqleExc); 173 throw new OSSDatabaseAccessException("Unable to create default database user.", 174 sqleExc); 175 } 176 finally 177 { 178 DatabaseUtils.closeStatement(pstmQuery); 179 } 180 } 181 182 185 public void startDatabaseServer() throws OSSException 186 { 187 } 189 190 193 public void createDatabaseInstance() throws OSSException 194 { 195 } 197 198 201 public void stop( 202 ) throws OSSException 203 { 204 s_logger.entering(this.getClass().getName(), "stop"); 205 206 super.stop(); 209 210 s_logger.entering(this.getClass().getName(), "stop"); 211 } 212 213 216 public int getDatabaseType() 217 { 218 return DatabaseImpl.SYBASE_DATABASE_TYPE; 219 } 220 221 224 public String getDatabaseTypeIdentifier() 225 { 226 return DatabaseImpl.SYBASE_DATABASE_TYPE_IDENTIFIER; 227 } 228 229 232 public String getCurrentTimestampFunctionCall() 233 { 234 return "GETDATE()"; 235 } 236 237 240 public String getSQLCountFunctionCall() 241 { 242 return "count(*)"; 244 } 245 246 249 public Object [] getSQLAnalyzeFunctionCall( 250 Map mapTableNames 251 ) 252 { 253 256 String [] arrReturn = new String [mapTableNames.size()]; 257 StringBuffer buffer = new StringBuffer (); 258 Iterator itItem; 259 int iIndex = 0; 260 261 itItem = mapTableNames.values().iterator(); 262 while (itItem.hasNext()) 263 { 264 buffer.append("update statistics "); 266 buffer.append((String )itItem.next()); 267 arrReturn[iIndex++] = buffer.toString(); 269 buffer.delete(0, buffer.length()); 271 } 272 273 return new Object [] {arrReturn, Boolean.TRUE}; 274 } 275 276 279 public boolean preferCountToLast( 280 ) 281 { 282 return true; 286 } 287 288 291 public boolean hasSelectListRangeSupport( 292 ) 293 { 294 return true; 297 } 298 299 302 public boolean isCallableStatement( 303 String strQuery 304 ) 305 { 306 return strQuery.indexOf("{call ") != -1; 307 } 308 309 312 public void insertAndFetchGeneratedValues( 313 Connection dbConnection, 314 PreparedStatement insertStatement, 315 boolean bIsInDomain, 316 String strTableName, 317 int iIndex, 318 BasicDataObject data 319 ) throws SQLException , 320 OSSException 321 { 322 SybaseDataUtils.insertAndFetchGeneratedValues((CallableStatement )insertStatement, 323 iIndex, data); 324 } 325 326 329 public void updatedAndFetchGeneratedValues( 330 String strDataName, 331 Connection dbConnection, 332 PreparedStatement updateStatement, 333 boolean bIsInDomain, 334 String strTableName, 335 int iIndex, 336 ModifiableDataObject data 337 ) throws SQLException , 338 OSSException 339 { 340 SybaseDataUtils.updateAndFetchGeneratedValues((CallableStatement )updateStatement, 341 iIndex, data); 342 } 343 } 344 | Popular Tags |