1 21 22 package org.opensubsystems.patterns.mappeddata.persist.db.mysql; 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.mysql.MySQLDataUtils; 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 38 47 public class MySQLMappingDatabaseSchema extends MappingDatabaseSchema 48 { 49 79 80 82 85 private static Logger s_logger = Log.getInstance(MySQLMappingDatabaseSchema.class); 86 87 89 101 public MySQLMappingDatabaseSchema( 102 String strMapTableName, 103 Class schema1, 104 String strTableName1, 105 String strColumnName1, 106 Class schema2, 107 String strTableName2, 108 String strColumnName2 109 ) throws OSSException 110 { 111 super(strMapTableName, schema1, strTableName1, strColumnName1, 112 schema2, strTableName2, strColumnName2); 113 } 114 115 117 120 public void create( 121 Connection cntDBConnection, 122 String strUserName 123 ) throws SQLException , OSSException 124 { 125 Statement stmQuery = null; 126 try 127 { 128 StringBuffer bufferTable = new StringBuffer (); 129 stmQuery = cntDBConnection.createStatement(); 130 131 132 bufferTable.append(constructSQL("ID INTEGER NOT NULL AUTO_INCREMENT,", 133 "INTEGER", "TEXT", "TIMESTAMP", 134 m_strConstraintBody, true, true)); 135 bufferTable.deleteCharAt(bufferTable.length() - 1); 138 bufferTable.append(",INDEX IND_"); 140 bufferTable.append(m_strConstraintBody); 141 bufferTable.append("_"); 142 bufferTable.append(MAPPING_SCHEMA_COLUMN1); 143 bufferTable.append(" ("); 144 bufferTable.append(MAPPING_SCHEMA_COLUMN1); 145 bufferTable.append("),"); 146 bufferTable.append(NL); 147 bufferTable.append("INDEX IND_"); 149 bufferTable.append(m_strConstraintBody); 150 bufferTable.append("_"); 151 bufferTable.append(MAPPING_SCHEMA_COLUMN2); 152 bufferTable.append(" ("); 153 bufferTable.append(MAPPING_SCHEMA_COLUMN2); 154 bufferTable.append(")"); 155 bufferTable.append(") TYPE=INNODB"); 156 157 if (stmQuery.execute(bufferTable.toString())) 158 { 159 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 161 } 162 s_logger.log(Level.FINEST, "Table " + getSchemaPrefix() + 163 m_strMapTableName + " created."); 164 175 } 176 catch (SQLException sqleExc) 177 { 178 s_logger.log(Level.WARNING, "Failed to create schema " + MAPPING_SCHEMA_NAME, sqleExc); 179 throw sqleExc; 180 } 181 finally 182 { 183 DatabaseUtils.closeStatement(stmQuery); 184 } 185 } 186 187 190 public MappedData insertMappedData( 191 Connection dbConnection, 192 MappedData data 193 ) throws OSSException 194 { 195 PreparedStatement insertStatement = null; 196 197 try 198 { 199 StringBuffer buffer = new StringBuffer (); 200 int iIndex = 1; 201 202 buffer.append("insert into "); 203 buffer.append(getSchemaPrefix()); 204 buffer.append(m_strMapTableName); 205 buffer.append(" ("); 206 getColumns(false, MappedData.ALL_MAPPEDDATA_COLUMNS, null, null, buffer); 207 buffer.append(") values (null, ?, ?, ?, ?, now(), now())"); 208 209 insertStatement = dbConnection.prepareStatement(buffer.toString()); 210 insertStatement.setInt(iIndex++, data.getMappedId1()); 211 insertStatement.setInt(iIndex++, data.getMappedId2()); 212 insertStatement.setInt(iIndex++, data.getMappingType()); 213 insertStatement.setString(iIndex++, data.getCustomData()); 214 215 MySQLDataUtils.insertAndFetchGeneratedValues(dbConnection, 216 insertStatement, isInDomain(), getSchemaPrefix() + m_strMapTableName, data); 217 } 218 catch (SQLException eExc) 219 { 220 handleInsertMappedDataException(eExc); 221 } 222 finally 223 { 224 DatabaseUtils.closeStatement(insertStatement); 225 } 226 227 return data; 228 } 229 230 233 public MappedData updateMappedData( 234 Connection dbConnection, 235 MappedData data 236 ) throws OSSException 237 { 238 PreparedStatement updateStatement = null; 239 240 try 241 { 242 StringBuffer buffer = new StringBuffer (); 243 int iIndex = 1; 244 245 buffer.append("update "); 246 buffer.append(getSchemaPrefix()); 247 buffer.append(m_strMapTableName); 248 buffer.append(" set ID1 = ?, ID2 = ?, MAPPING_TYPE = ?, " + 249 "CUSTOM_DATA = ?, MODIFICATION_DATE = now() where ID = ? " + 250 "and MODIFICATION_DATE = ?"); 251 252 updateStatement = dbConnection.prepareStatement(buffer.toString()); 253 updateStatement.setInt(iIndex++, data.getMappedId1()); 254 updateStatement.setInt(iIndex++, data.getMappedId2()); 255 updateStatement.setInt(iIndex++, data.getMappingType()); 256 updateStatement.setString(iIndex++, data.getCustomData()); 257 updateStatement.setInt(iIndex++, data.getId()); 258 updateStatement.setTimestamp(iIndex++, data.getModificationTimestamp()); 259 260 MySQLDataUtils.updatedAndFetchGeneratedValues("Mapped Data", dbConnection, 261 updateStatement, isInDomain(), getSchemaPrefix() + m_strMapTableName, data); 262 } 263 catch (SQLException eExc) 264 { 265 handleUpdateMappedDataException(eExc, dbConnection, data); 266 } 267 finally 268 { 269 DatabaseUtils.closeStatement(updateStatement); 270 } 271 272 return data; 273 } 274 } 275 | Popular Tags |