1 21 22 package org.opensubsystems.core.persist.db.postgresql; 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 PostgreSQLDatabaseImpl extends DatabaseImpl 53 { 54 56 59 private static Logger s_logger = Log.getInstance(PostgreSQLDatabaseImpl.class); 60 61 63 68 public PostgreSQLDatabaseImpl( 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 95 96 StringBuffer buffer = new StringBuffer (); 97 98 buffer.append("CREATE USER "); 99 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 100 buffer.append(" WITH PASSWORD '"); 101 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword()); 102 buffer.append("'"); 103 104 try 105 { 106 pstmQuery = cntAdminDBConnection.prepareStatement(buffer.toString()); 107 108 if (pstmQuery.execute()) 109 { 110 pstmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 112 } 113 } 114 finally 115 { 116 DatabaseUtils.closeStatement(pstmQuery); 117 pstmQuery = null; 118 } 119 120 buffer.delete(0, buffer.length()); 122 buffer.append("CREATE SCHEMA "); 123 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 124 buffer.append(" AUTHORIZATION "); 125 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 126 127 try 128 { 129 pstmQuery = cntAdminDBConnection.prepareStatement(buffer.toString()); 130 131 if (pstmQuery.execute()) 132 { 133 pstmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 135 } 136 } 137 finally 138 { 139 DatabaseUtils.closeStatement(pstmQuery); 140 pstmQuery = null; 141 } 142 143 DatabaseTransactionFactoryImpl.getInstance().commitTransaction( 148 cntAdminDBConnection); 149 150 s_logger.log(Level.FINER, "Database user " 151 + DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser() 152 + " with password " 153 + DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword() 154 + " and database schema " 155 + DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser() 156 + " created."); 157 } 158 catch (SQLException sqleExc) 159 { 160 try 161 { 162 DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction( 167 cntAdminDBConnection); 168 } 169 catch (SQLException sqleExc2) 170 { 171 s_logger.log(Level.WARNING, 173 "Failed to rollback changes for creation of user.", 174 sqleExc2); 175 } 176 s_logger.log(Level.SEVERE, 177 "Unable to create default database user.", 178 sqleExc); 179 throw new OSSDatabaseAccessException("Unable to create default database user.", 180 sqleExc); 181 } 182 finally 183 { 184 DatabaseUtils.closeStatement(pstmQuery); 185 } 186 } 187 188 191 public void startDatabaseServer() throws OSSException 192 { 193 } 198 199 202 public void createDatabaseInstance() throws OSSException 203 { 204 } 209 210 213 public void stop( 214 ) throws OSSException 215 { 216 220 s_logger.entering(this.getClass().getName(), "stop"); 221 222 super.stop(); 225 226 s_logger.entering(this.getClass().getName(), "stop"); 227 } 228 229 232 public boolean preferCountToLast( 233 ) 234 { 235 return true; 239 } 240 241 244 public int getDatabaseType() 245 { 246 return DatabaseImpl.POSTGRESQL_DATABASE_TYPE; 247 } 248 249 252 public String getDatabaseTypeIdentifier() 253 { 254 return DatabaseImpl.POSTGRESQL_DATABASE_TYPE_IDENTIFIER; 255 } 256 257 260 public String getCurrentTimestampFunctionCall() 261 { 262 return "now()"; 264 } 265 266 269 public String getSQLCountFunctionCall() 270 { 271 return "count(*)"; 273 } 274 275 278 public Object [] getSQLAnalyzeFunctionCall( 279 Map mapTableNames 280 ) 281 { 282 287 String [] arrReturn = new String [mapTableNames.size()]; 288 StringBuffer buffer = new StringBuffer (); 289 Iterator itItem; 290 int iIndex = 0; 291 292 itItem = mapTableNames.values().iterator(); 293 while (itItem.hasNext()) 294 { 295 buffer.append("vacuum analyze "); 297 buffer.append((String )itItem.next()); 298 arrReturn[iIndex++] = buffer.toString(); 300 buffer.delete(0, buffer.length()); 302 } 303 304 return new Object [] {arrReturn, Boolean.TRUE}; 305 } 306 307 310 public boolean hasSelectListRangeSupport( 311 ) 312 { 313 return true; 315 } 316 317 320 public boolean isCallableStatement( 321 String strQuery 322 ) 323 { 324 return strQuery.indexOf("select ") != -1; 327 } 328 329 332 public void insertAndFetchGeneratedValues( 333 Connection dbConnection, 334 PreparedStatement insertStatement, 335 boolean bIsInDomain, 336 String strTableName, 337 int iIndex, 338 BasicDataObject data 339 ) throws SQLException , 340 OSSException 341 { 342 PostgreSQLDataUtils.insertAndFetchGeneratedValues(insertStatement, data); 343 } 344 345 348 public void updatedAndFetchGeneratedValues( 349 String strDataName, 350 Connection dbConnection, 351 PreparedStatement updateStatement, 352 boolean bIsInDomain, 353 String strTableName, 354 int iIndex, 355 ModifiableDataObject data 356 ) throws SQLException , 357 OSSException 358 { 359 PostgreSQLDataUtils.updateAndFetchGeneratedValues((CallableStatement )updateStatement, 360 data); 361 } 362 } 363 | Popular Tags |