1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.util.Iterator ; 27 import javax.ejb.EJBException ; 28 import javax.sql.DataSource ; 29 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge; 30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge; 31 import org.jboss.logging.Logger; 32 33 40 public final class JDBCDeleteRelationsCommand 41 { 42 private final Logger log; 43 private int maxKeysInDelete; 44 private String maxKeysStatement; 45 46 public JDBCDeleteRelationsCommand(JDBCStoreManager manager) 47 { 48 log = Logger.getLogger(this.getClass().getName() + 50 "." + 51 manager.getMetaData().getName() 52 ); 53 54 maxKeysInDelete = manager.getJDBCTypeFactory().getTypeMapping().getMaxKeysInDelete(); 55 } 56 57 public void execute(RelationData relationData) 62 { 63 if(relationData.removedRelations.size() == 0) 64 { 65 return; 66 } 67 68 Iterator pairs = relationData.removedRelations.iterator(); 69 int i = 0; 70 while(i < relationData.removedRelations.size()) 71 { 72 String sql = getSQL(relationData, relationData.removedRelations.size() - i); 73 74 Connection con = null; 75 PreparedStatement ps = null; 76 JDBCCMRFieldBridge cmrField = relationData.getLeftCMRField(); 77 try 78 { 79 if(log.isDebugEnabled()) 81 { 82 log.debug("Executing SQL: " + sql); 83 } 84 85 DataSource dataSource = cmrField.getDataSource(); 87 con = dataSource.getConnection(); 88 ps = con.prepareStatement(sql); 89 90 setParameters(ps, relationData, pairs); 92 93 int rowsAffected = ps.executeUpdate(); 95 if(log.isDebugEnabled()) 96 { 97 log.debug("Rows affected = " + rowsAffected); 98 } 99 100 i += (maxKeysInDelete > 0 ? maxKeysInDelete : relationData.removedRelations.size()); 101 } 102 catch(Exception e) 103 { 104 throw new EJBException ("Could not delete relations from " + 105 cmrField.getQualifiedTableName(), e 106 ); 107 } 108 finally 109 { 110 JDBCUtil.safeClose(ps); 111 JDBCUtil.safeClose(con); 112 } 113 } 114 } 115 116 private String getSQL(RelationData relationData, int keys) 117 { 118 if(maxKeysInDelete > 0 && keys >= maxKeysInDelete) 119 { 120 if(maxKeysStatement == null) 121 { 122 maxKeysStatement = createSQL(relationData, maxKeysInDelete); 123 } 124 return maxKeysStatement; 125 } 126 return createSQL(relationData, keys); 127 } 128 129 private static String createSQL(RelationData relationData, int keysInDelete) 130 { 131 JDBCCMRFieldBridge left = relationData.getLeftCMRField(); 132 JDBCCMRFieldBridge right = relationData.getRightCMRField(); 133 134 StringBuffer sql = new StringBuffer (300); 135 sql.append(SQLUtil.DELETE_FROM) 136 .append(left.getQualifiedTableName()) 137 .append(SQLUtil.WHERE); 138 139 StringBuffer whereClause = new StringBuffer (20); 140 whereClause.append('('); 141 SQLUtil.getWhereClause(left.getTableKeyFields(), whereClause) 143 .append(SQLUtil.AND); 144 SQLUtil.getWhereClause(right.getTableKeyFields(), whereClause) 146 .append(')'); 147 String whereClauseStr = whereClause.toString(); 148 sql.append(whereClauseStr); 149 for(int i = 1; i < keysInDelete; ++i) 150 { 151 sql.append(SQLUtil.OR).append(whereClauseStr); 152 } 153 154 return sql.toString(); 155 } 156 157 private void setParameters(PreparedStatement ps, RelationData relationData, Iterator pairs) 158 throws Exception 159 { 160 int index = 1; 161 JDBCCMPFieldBridge[] leftFields = (JDBCCMPFieldBridge[])relationData.getLeftCMRField().getTableKeyFields(); 162 JDBCCMPFieldBridge[] rightFields = (JDBCCMPFieldBridge[])relationData.getRightCMRField().getTableKeyFields(); 163 int keyIndex = 0; 164 while(pairs.hasNext()) 165 { 166 RelationPair pair = (RelationPair)pairs.next(); 167 168 Object leftId = pair.getLeftId(); 170 for(int i = 0; i < leftFields.length; ++i) 171 { 172 index = leftFields[i].setPrimaryKeyParameters(ps, index, leftId); 173 } 174 175 Object rightId = pair.getRightId(); 177 for(int i = 0; i < rightFields.length; ++i) 178 { 179 index = rightFields[i].setPrimaryKeyParameters(ps, index, rightId); 180 } 181 182 if(maxKeysInDelete > 0) 183 { 184 ++keyIndex; 185 if(keyIndex == maxKeysInDelete) 186 { 187 break; 188 } 189 } 190 } 191 } 192 } 193 | Popular Tags |