1 21 22 package org.opensubsystems.core.persist.db.postgresql; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.util.Map ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import org.opensubsystems.core.error.OSSDatabaseAccessException; 32 import org.opensubsystems.core.error.OSSException; 33 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 34 import org.opensubsystems.core.persist.db.VersionedDatabaseSchema; 35 import org.opensubsystems.core.util.DatabaseUtils; 36 import org.opensubsystems.core.util.Log; 37 38 50 public class PostgreSQLVersionedDatabaseSchema extends VersionedDatabaseSchema 51 { 52 53 55 58 private static Logger s_logger = Log.getInstance(PostgreSQLVersionedDatabaseSchema.class); 59 60 62 65 public PostgreSQLVersionedDatabaseSchema( 66 ) throws OSSException 67 { 68 super(); 69 } 70 71 74 public void loadExistingSchemas( 75 Connection cntDBConnection, 76 Map mpSchemasToAdd, 77 Map mpSchemasToUpgrade 78 ) throws OSSException 79 { 80 super.loadExistingSchemas(cntDBConnection, mpSchemasToAdd, mpSchemasToUpgrade); 81 82 try 83 { 84 DatabaseTransactionFactoryImpl.getInstance().commitTransaction(cntDBConnection); 85 } 86 catch (Throwable thr) 87 { 88 s_logger.log(Level.SEVERE, "Failed to initialize database.", 89 thr); 90 if (cntDBConnection != null) 91 { 92 try 93 { 94 DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction( 99 cntDBConnection); 100 } 101 catch (SQLException sqleExc) 102 { 103 s_logger.log(Level.WARNING, 105 "Failed to rollback changes for creation of database.", 106 sqleExc); 107 } 108 } 109 throw new OSSDatabaseAccessException("Failed to initialize database.", 110 thr); 111 } 112 } 113 114 117 public void create( 118 Connection cntDBConnection, 119 String strUserName 120 ) throws SQLException , 121 OSSException 122 { 123 s_logger.entering(this.getClass().getName(), "create"); 124 125 try 126 { 127 createUserDefinedType(cntDBConnection, strUserName); 130 } 131 finally 132 { 133 s_logger.exiting(this.getClass().getName(), "create"); 134 } 135 136 super.create(cntDBConnection, strUserName); 137 } 138 139 142 public void upgrade( 143 Connection cntDBConnection, 144 String strUserName, 145 int iOriginalVersion 146 ) throws SQLException 147 { 148 s_logger.entering(this.getClass().getName(), "upgrade"); 149 150 try 151 { 152 if (iOriginalVersion == 1) 153 { 154 createUserDefinedType(cntDBConnection, strUserName); 157 } 158 } 159 finally 160 { 161 s_logger.exiting(this.getClass().getName(), "upgrade"); 162 } 163 164 super.upgrade(cntDBConnection, strUserName, iOriginalVersion); 165 } 166 167 169 177 protected void createUserDefinedType( 178 Connection cntDBConnection, 179 String strUserName 180 ) throws SQLException 181 { 182 s_logger.entering(this.getClass().getName(), "createUserDefinedType"); 183 184 try 185 { 186 Statement stmQuery = null; 187 try 188 { 189 190 stmQuery = cntDBConnection.createStatement(); 191 192 if (stmQuery.execute("CREATE TYPE type_int_timestamp AS (intgr INTEGER, " + 193 "tmstp TIMESTAMP WITH TIME ZONE)")) 194 { 195 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 197 } 198 s_logger.log(Level.FINEST, "Type 'type_int_timestamp' created."); 199 } 200 catch (SQLException sqleExc) 201 { 202 s_logger.log(Level.WARNING, "Failed to create int - timestamp type.", 204 sqleExc); 205 throw sqleExc; 206 } 207 finally 208 { 209 DatabaseUtils.closeStatement(stmQuery); 210 } 211 } 212 finally 213 { 214 s_logger.exiting(this.getClass().getName(), "createUserDefinedType"); 215 } 216 } 217 } 218 | Popular Tags |