1 21 22 package org.opensubsystems.patterns.mappeddata.persist.db.postgresql; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import org.opensubsystems.core.error.OSSException; 32 import org.opensubsystems.core.persist.db.postgresql.PostgreSQLDataUtils; 33 import org.opensubsystems.core.util.DatabaseUtils; 34 import org.opensubsystems.core.util.Log; 35 import org.opensubsystems.patterns.mappeddata.data.MappedData; 36 import org.opensubsystems.patterns.mappeddata.persist.db.MappingDatabaseSchema; 37 import org.opensubsystems.patterns.mappeddata.persist.db.db2.DB2MappingDatabaseSchema; 38 39 48 public class PostgreSQLMappingDatabaseSchema extends MappingDatabaseSchema 49 { 50 132 133 135 138 private static Logger s_logger = Log.getInstance(DB2MappingDatabaseSchema.class); 139 140 142 154 public PostgreSQLMappingDatabaseSchema( 155 String strMapTableName, 156 Class schema1, 157 String strTableName1, 158 String strColumnName1, 159 Class schema2, 160 String strTableName2, 161 String strColumnName2 162 ) throws OSSException 163 { 164 super(strMapTableName, schema1, strTableName1, strColumnName1, 165 schema2, strTableName2, strColumnName2); 166 } 167 168 170 173 public void create( 174 Connection cntDBConnection, 175 String strUserName 176 ) throws SQLException , OSSException 177 { 178 Statement stmQuery = null; 179 try 180 { 181 stmQuery = cntDBConnection.createStatement(); 182 183 if (stmQuery.execute(constructSQL( 184 "ID SERIAL,", "INTEGER", 185 "VARCHAR(" + MAPPING_CUSTOM_DATA_MAXLENGTH + ")" + NL, 186 "TIMESTAMP WITH TIME ZONE", m_strConstraintBody, true, true))) 187 { 188 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 190 } 191 s_logger.log(Level.FINEST, "Table " + getSchemaPrefix() + 192 m_strMapTableName + " created."); 193 194 196 if (stmQuery.execute(constructInsertStoredProc(strUserName, m_strMapTableName))) 197 { 198 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 200 } 201 s_logger.log(Level.FINEST, "Procedure INSERT_" + m_strMapTableName + " created."); 202 203 205 if (stmQuery.execute(constructUpdateStoredProc(strUserName, m_strMapTableName))) 206 { 207 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 209 } 210 s_logger.log(Level.FINEST, "Procedure UPDATE_" + m_strMapTableName + " created."); 211 } 212 catch (SQLException sqleExc) 213 { 214 s_logger.log(Level.WARNING, "Failed to create schema " + MAPPING_SCHEMA_NAME, sqleExc); 215 throw sqleExc; 216 } 217 finally 218 { 219 DatabaseUtils.closeStatement(stmQuery); 220 } 221 } 222 223 226 public MappedData insertMappedData( 227 Connection dbConnection, 228 MappedData data 229 ) throws OSSException 230 { 231 PreparedStatement insertStatement = null; 232 233 try 234 { 235 StringBuffer buffer = new StringBuffer (); 236 int iIndex = 1; 237 238 buffer.append("select INTGR, TMSTP from INSERT_"); 239 buffer.append(getSchemaPrefix()); 240 buffer.append(m_strMapTableName); 241 buffer.append(" (?, ?, ?, ?)"); 242 243 insertStatement = dbConnection.prepareStatement(buffer.toString()); 244 245 insertStatement.setInt(iIndex++, data.getMappedId1()); 246 insertStatement.setInt(iIndex++, data.getMappedId2()); 247 insertStatement.setInt(iIndex++, data.getMappingType()); 248 insertStatement.setString(iIndex++, data.getCustomData()); 249 250 PostgreSQLDataUtils.insertAndFetchGeneratedValues(insertStatement, data); 251 } 252 catch (SQLException eExc) 253 { 254 handleInsertMappedDataException(eExc); 255 } 256 finally 257 { 258 DatabaseUtils.closeStatement(insertStatement); 259 } 260 261 return data; 262 } 263 264 267 public MappedData updateMappedData( 268 Connection dbConnection, 269 MappedData data 270 ) throws OSSException 271 { 272 PreparedStatement updateStatement = null; 273 274 try 275 { 276 StringBuffer buffer = new StringBuffer (); 277 int iIndex = 1; 278 279 buffer.append("select INTGR, TMSTP from UPDATE_"); 280 buffer.append(getSchemaPrefix()); 281 buffer.append(m_strMapTableName); 282 buffer.append(" (?, ?, ?, ?, ?, ?)"); 283 284 updateStatement = dbConnection.prepareStatement(buffer.toString()); 285 286 updateStatement.setInt(iIndex++, data.getId()); 287 updateStatement.setInt(iIndex++, data.getMappedId1()); 288 updateStatement.setInt(iIndex++, data.getMappedId2()); 289 updateStatement.setInt(iIndex++, data.getMappingType()); 290 updateStatement.setString(iIndex++, data.getCustomData()); 291 updateStatement.setTimestamp(iIndex++, data.getModificationTimestamp()); 292 293 PostgreSQLDataUtils.updateAndFetchGeneratedValues(updateStatement, data); 294 } 295 catch (SQLException eExc) 296 { 297 handleUpdateMappedDataException(eExc, dbConnection, data); 298 } 299 finally 300 { 301 DatabaseUtils.closeStatement(updateStatement); 302 } 303 304 return data; 305 } 306 307 309 316 public String constructInsertStoredProc( 317 String strUserName, 318 String strMapTableName 319 ) 320 { 321 StringBuffer buffer = new StringBuffer (); 322 323 buffer.append("CREATE OR REPLACE FUNCTION INSERT_"); 324 buffer.append(getSchemaPrefix()); 325 buffer.append(strMapTableName); 326 buffer.append(NL + "(" + NL + 327 " INTEGER, " + NL + 328 " INTEGER, " + NL + 329 " INTEGER, " + NL + 330 " VARCHAR(" + MAPPING_CUSTOM_DATA_MAXLENGTH + ") " + NL + 331 ") RETURNS type_int_timestamp AS '" + NL + 332 "DECLARE " + NL + 333 " in_id1 ALIAS FOR $1; " + NL + 334 " in_id2 ALIAS FOR $2; " + NL + 335 " in_mapping_type ALIAS FOR $3; " + NL + 336 " in_custom_data ALIAS FOR $4; " + NL + 337 " out_key INTEGER; " + NL + 338 " out_timestamp TIMESTAMP WITH TIME ZONE; " + NL + 339 " output_result type_int_timestamp; " + NL + 340 "BEGIN " + NL + 341 " SELECT INTO out_timestamp now(); " + NL + 342 " INSERT INTO "); 343 buffer.append(strUserName); 344 buffer.append("."); 345 buffer.append(getSchemaPrefix()); 346 buffer.append(strMapTableName); 347 buffer.append("(ID1, ID2, " + NL + 348 " MAPPING_TYPE, CUSTOM_DATA, CREATION_DATE, MODIFICATION_DATE) " + NL + 349 " VALUES (in_id1, in_id2, in_mapping_type, in_custom_data, " + NL + 350 " out_timestamp, out_timestamp);" + NL + 351 " out_key := CURRVAL(''"); 352 buffer.append(getSchemaPrefix().toLowerCase()); 353 buffer.append(strMapTableName.toLowerCase()); 354 buffer.append("_id_seq''); " + NL + 355 " output_result.intgr := out_key; " + NL + 356 " output_result.tmstp := out_timestamp; " + NL + 357 " RETURN output_result; " + NL + 358 "END;" + NL + 359 "' LANGUAGE 'plpgsql';"); 360 361 return buffer.toString(); 362 } 363 364 371 public static String constructUpdateStoredProc( 372 String strUserName, 373 String strMapTableName 374 ) 375 { 376 StringBuffer buffer = new StringBuffer (); 377 378 buffer.append("CREATE OR REPLACE FUNCTION UPDATE_"); 379 buffer.append(getSchemaPrefix()); 380 buffer.append(strMapTableName); 381 buffer.append(NL + "(" + NL + 382 " INTEGER, " + NL + 383 " INTEGER, " + NL + 384 " INTEGER, " + NL + 385 " INTEGER, " + NL + 386 " VARCHAR(" + MAPPING_CUSTOM_DATA_MAXLENGTH + "), " + 387 " TIMESTAMP WITH TIME ZONE " + NL + 388 ") RETURNS type_int_timestamp AS '" + NL + 389 "DECLARE " + NL + 390 " in_id ALIAS FOR $1; " + NL + 391 " in_id1 ALIAS FOR $2; " + NL + 392 " in_id2 ALIAS FOR $3; " + NL + 393 " in_mapping_type ALIAS FOR $4; " + NL + 394 " in_custom_data ALIAS FOR $5; " + NL + 395 " in_modification_date ALIAS FOR $6;" + NL + 396 " out_updated_count INTEGER; " + NL + 397 " out_timestamp TIMESTAMP WITH TIME ZONE; " + NL + 398 " output_result type_int_timestamp; " + NL + 399 "BEGIN " + NL + 400 " SELECT INTO out_timestamp now(); " + NL + 401 " UPDATE "); 402 buffer.append(strUserName); 403 buffer.append("."); 404 buffer.append(getSchemaPrefix()); 405 buffer.append(strMapTableName); 406 buffer.append(" SET ID1 = in_id1, ID2 = in_id2, " + NL + 407 " MAPPING_TYPE = in_mapping_type, CUSTOM_DATA = in_custom_data, " + NL + 408 " MODIFICATION_DATE = out_timestamp WHERE ID = in_id " + NL + 409 " AND MODIFICATION_DATE = in_modification_date; " + NL + 410 " GET DIAGNOSTICS out_updated_count = ROW_COUNT; " + NL + 411 " output_result.intgr := out_updated_count;" + NL + 412 " output_result.tmstp := out_timestamp; " + NL + 413 " RETURN output_result; " + NL + 414 "END;" + NL + 415 "' LANGUAGE 'plpgsql';"); 416 417 return buffer.toString(); 418 } 419 } 420 | Popular Tags |