1 21 22 package org.opensubsystems.patterns.mappeddata.persist.db.hsqldb; 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.hsqldb.HsqlDBDataUtils; 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 HsqlDBMappingDatabaseSchema extends MappingDatabaseSchema 48 { 49 75 76 78 81 private static Logger s_logger = Log.getInstance(HsqlDBMappingDatabaseSchema.class); 82 83 85 97 public HsqlDBMappingDatabaseSchema( 98 String strMapTableName, 99 Class schema1, 100 String strTableName1, 101 String strColumnName1, 102 Class schema2, 103 String strTableName2, 104 String strColumnName2 105 ) throws OSSException 106 { 107 super(strMapTableName, schema1, strTableName1, strColumnName1, 108 schema2, strTableName2, strColumnName2); 109 } 110 111 113 116 public void create( 117 Connection cntDBConnection, 118 String strUserName 119 ) throws SQLException , OSSException 120 { 121 Statement stmQuery = null; 122 try 123 { 124 stmQuery = cntDBConnection.createStatement(); 125 126 if (stmQuery.execute(constructSQL( 127 "ID INTEGER IDENTITY,", "INTEGER", 128 "VARCHAR(" + MAPPING_CUSTOM_DATA_MAXLENGTH + ")" + NL, 129 "TIMESTAMP", m_strConstraintBody, true, false))) 130 { 131 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 133 } 134 s_logger.log(Level.FINEST, "Table " + getSchemaPrefix() + 135 m_strMapTableName + " created."); 136 137 if (stmQuery.execute("grant all on " + getSchemaPrefix() + 138 m_strMapTableName + " to " + strUserName)) 139 { 140 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 142 } 143 s_logger.log(Level.FINEST, "Access for table " + getSchemaPrefix() + 144 m_strMapTableName + " set for user " + strUserName); 145 } 146 catch (SQLException sqleExc) 147 { 148 s_logger.log(Level.WARNING, "Failed to create schema " + MAPPING_SCHEMA_NAME, sqleExc); 149 throw sqleExc; 150 } 151 finally 152 { 153 DatabaseUtils.closeStatement(stmQuery); 154 } 155 } 156 157 160 public MappedData insertMappedData( 161 Connection dbConnection, 162 MappedData data 163 ) throws OSSException 164 { 165 PreparedStatement insertStatement = null; 166 167 try 168 { 169 StringBuffer buffer = new StringBuffer (); 170 int iIndex = 1; 171 172 buffer.append("insert into "); 173 buffer.append(getSchemaPrefix()); 174 buffer.append(m_strMapTableName); 175 buffer.append(" ("); 176 getColumns(false, MappedData.ALL_MAPPEDDATA_COLUMNS, null, null, buffer); 177 buffer.append(") values (null, ?, ?, ?, ?, now, now)"); 178 179 insertStatement = dbConnection.prepareStatement(buffer.toString()); 180 insertStatement.setInt(iIndex++, data.getMappedId1()); 181 insertStatement.setInt(iIndex++, data.getMappedId2()); 182 insertStatement.setInt(iIndex++, data.getMappingType()); 183 insertStatement.setString(iIndex++, data.getCustomData()); 184 185 HsqlDBDataUtils.insertAndFetchGeneratedValues(dbConnection, 186 insertStatement, isInDomain(), getSchemaPrefix() + m_strMapTableName, data); 187 } 188 catch (SQLException eExc) 189 { 190 handleInsertMappedDataException(eExc); 191 } 192 finally 193 { 194 DatabaseUtils.closeStatement(insertStatement); 195 } 196 197 return data; 198 } 199 200 203 public MappedData updateMappedData( 204 Connection dbConnection, 205 MappedData data 206 ) throws OSSException 207 { 208 PreparedStatement updateStatement = null; 209 210 try 211 { 212 StringBuffer buffer = new StringBuffer (); 213 int iIndex = 1; 214 215 buffer.append("update "); 216 buffer.append(getSchemaPrefix()); 217 buffer.append(m_strMapTableName); 218 buffer.append(" set ID1 = ?, ID2 = ?, MAPPING_TYPE = ?, " + 219 "CUSTOM_DATA = ?, MODIFICATION_DATE = now where ID = ? " + 220 "and MODIFICATION_DATE = ?"); 221 222 updateStatement = dbConnection.prepareStatement(buffer.toString()); 223 updateStatement.setInt(iIndex++, data.getMappedId1()); 224 updateStatement.setInt(iIndex++, data.getMappedId2()); 225 updateStatement.setInt(iIndex++, data.getMappingType()); 226 updateStatement.setString(iIndex++, data.getCustomData()); 227 updateStatement.setInt(iIndex++, data.getId()); 228 updateStatement.setTimestamp(iIndex++, data.getModificationTimestamp()); 229 230 HsqlDBDataUtils.updatedAndFetchGeneratedValues("Mapped Data", dbConnection, 231 updateStatement, isInDomain(), getSchemaPrefix() + m_strMapTableName, data); 232 } 233 catch (SQLException eExc) 234 { 235 handleUpdateMappedDataException(eExc, dbConnection, data); 236 } 237 finally 238 { 239 DatabaseUtils.closeStatement(updateStatement); 240 } 241 242 return data; 243 } 244 } 245 | Popular Tags |