litesoft
@ 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.sql; import java.util.*; import org.litesoft.db.*; import org.litesoft.orsup.base.*; import org.litesoft.orsup.nonpublic.*; import org.litesoft.orsup.otherattributeaccessors.*; import org.litesoft.orsup.selection.*; import org.litesoft.sql.statements.*; public class SQLTransactionPersistenceHelper extends SQLORPersistenceHelper implements TransactionPersistenceHelper { public SQLTransactionPersistenceHelper( SQLDBconnectionProvider pSQLDBconnectionProvider ) { super( pSQLDBconnectionProvider ); } @Override public void insert( DBconnection pDBconnection, PersistentObjectImpl pPersistentObject, Object pLoggingContext ) throws DBException { MetaDataForPO metaDataForPO = pPersistentObject.getMetaDataForPO(); // Automatically adds the RecordVersion if it exists String cmd = "INSERT INTO " + metaDataForPO.getTableName() + // " (" + SQL_OR_Helper.buildAllTableColumns( metaDataForPO ) + ")" + // " VALUES (" + buildInsertValues( metaDataForPO, pPersistentObject ) + ")"; JDBCexecuteUpdate updater = new JDBCexecuteUpdate( this, pDBconnection, cmd, pLoggingContext ); try { updater.executeUpdate( false ); } finally { updater.dispose(); } } @SuppressWarnings({"unchecked"}) @Override public void update( DBconnection pDBconnection, PersistentObjectImpl pPersistentObject, Object pLoggingContext ) throws ConcurrentPOModificationException, DBException { MetaDataForPO metaDataForPO = pPersistentObject.getMetaDataForPO(); WhereClause wc = createVersionAwareWhereClause( metaDataForPO, pPersistentObject ); StringBuilder zUCbuilder = new StringBuilder(); String zPrefix = ""; if ( metaDataForPO.isRecordVersionable() ) { String rvName = metaDataForPO.getRecordVersionAttributeName(); AttributeAccessorSCD_RecordVersion rvAASCD = (AttributeAccessorSCD_RecordVersion) metaDataForPO.getAccessorSCDrequired( rvName ); Long zNextRecordVersion = rvAASCD.getNextRecordVersion( pPersistentObject ); zUCbuilder.append( rvAASCD.getColumnName() ).append( '=' ); WhereClauseColumnSupport.makeSqlValue( zUCbuilder, rvAASCD, false, zNextRecordVersion ); zPrefix = ", "; } String updateClause = buildUpdated( zUCbuilder, zPrefix, metaDataForPO, pPersistentObject ); if ( updateClause.length() == 0 ) { throw new IllegalStateException( "Changes indicated on '" + pPersistentObject + // "' but did NOT like any of the following attributes: " + // Arrays.asList( pPersistentObject.getChangedAttributeNames() ) ); } String cmd = createUpdateCmd( metaDataForPO.getTableName(), updateClause, wc.toSQL() ); int rowsEffected = update( pDBconnection, cmd, false ); if ( rowsEffected == 0 ) { throw new ConcurrentPOModificationException( cmd, pPersistentObject ); } if ( rowsEffected > 1 ) { throw new MultiRowUpdateException( cmd, rowsEffected ); } } @Override public void delete( DBconnection pDBconnection, PersistentObjectImpl pPersistentObject, Object pLoggingContext ) throws ConcurrentPOModificationException, DBException { MetaDataForPO metaDataForPO = pPersistentObject.getMetaDataForPO(); WhereClause wc = createVersionAwareWhereClause( metaDataForPO, pPersistentObject ); String cmd = "DELETE FROM " + metaDataForPO.getTableName() + " " + SQL_OR_Helper.toSQL( true, metaDataForPO, wc ); JDBCexecuteUpdate updater = new JDBCexecuteUpdate( this, pDBconnection, cmd, pLoggingContext ); int rows; try { rows = updater.executeUpdate( false ); } finally { updater.dispose(); } if ( rows == 0 ) { throw new ConcurrentPOModificationException( "Unable to delete", pPersistentObject ); } } @SuppressWarnings({"unchecked"}) private String buildInsertValues( MetaDataForPO pMetaDataForPO, final PersistentObject<?> pPO ) { return SQL_OR_Helper.processPersistingAccessorSCDs( new StringBuilder(), "", pMetaDataForPO, new SQL_OR_Helper.PersistingAccessorSCDprocessor() { @Override public boolean process( StringBuilder pSB, String pPrefix, AttributeAccessorSCD pSCD ) { Object zPOvalue = pSCD.db_getValueOnPO( pPO ); pSB.append( pPrefix ); WhereClauseColumnSupport.makeSqlValue( pSB, pSCD, false, zPOvalue ); if ( pSCD.hasSearchColumn() ) { pSB.append( ", " ); WhereClauseColumnSupport.makeSqlValue( pSB, pSCD, true, zPOvalue ); } return true; } } ); } @SuppressWarnings({"unchecked"}) private String buildUpdated( StringBuilder pSB, String pPrefix, MetaDataForPO pMetaDataForPO, final PersistentObject<?> pPO ) { return SQL_OR_Helper.processPersistingAccessorSCDs( pSB, pPrefix, pMetaDataForPO, new SQL_OR_Helper.PersistingAccessorSCDprocessor() { @Override public boolean process( StringBuilder pSB, String pPrefix, AttributeAccessorSCD pSCD ) { if ( !pPO.isAttributeChanged( pSCD ) ) { return false; } Object zPOvalue = pSCD.db_getValueOnPO( pPO ); pSB.append( pPrefix ).append( pSCD.getColumnName() ).append( '=' ); WhereClauseColumnSupport.makeSqlValue( pSB, pSCD, false, zPOvalue ); if ( pSCD.hasSearchColumn() ) { pSB.append( ", " ).append( pSCD.getSearchColumnName() ).append( '=' ); WhereClauseColumnSupport.makeSqlValue( pSB, pSCD, true, zPOvalue ); } return true; } } ); } } |