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 30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge; 31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge; 32 import org.jboss.logging.Logger; 33 34 41 public final class JDBCInsertRelationsCommand { 42 private final Logger log; 43 44 public JDBCInsertRelationsCommand(JDBCStoreManager manager) { 45 this.log = Logger.getLogger( 46 this.getClass().getName() + 47 "." + 48 manager.getMetaData().getName()); 49 } 50 51 public void execute(RelationData relationData) { 52 if(relationData.addedRelations.size() == 0) { 53 return; 54 } 55 56 Connection con = null; 57 PreparedStatement ps = null; 58 59 JDBCCMRFieldBridge cmrField = relationData.getLeftCMRField(); 60 try { 61 String sql = getSQL(relationData); 63 boolean debug = log.isDebugEnabled(); 64 if(debug) 65 log.debug("Executing SQL: " + sql); 66 67 DataSource dataSource = cmrField.getDataSource(); 69 con = dataSource.getConnection(); 70 71 ps = con.prepareStatement(sql); 73 74 Iterator pairs = relationData.addedRelations.iterator(); 75 while(pairs.hasNext()) { 76 RelationPair pair = (RelationPair)pairs.next(); 77 78 setParameters(ps, relationData, pair); 80 81 int rowsAffected = ps.executeUpdate(); 82 } 83 } catch(Exception e) { 84 throw new EJBException ("Could insert relations into " + 85 cmrField.getQualifiedTableName(), e); 86 } finally { 87 JDBCUtil.safeClose(ps); 88 JDBCUtil.safeClose(con); 89 } 90 } 91 92 protected static String getSQL(RelationData relationData) { 93 JDBCCMRFieldBridge left = relationData.getLeftCMRField(); 94 JDBCCMRFieldBridge right = relationData.getRightCMRField(); 95 96 StringBuffer sql = new StringBuffer (200); 97 sql.append(SQLUtil.INSERT_INTO).append(left.getQualifiedTableName()); 98 99 sql.append('('); 100 SQLUtil.getColumnNamesClause(left.getTableKeyFields(), sql); 101 sql.append(SQLUtil.COMMA); 102 SQLUtil.getColumnNamesClause(right.getTableKeyFields(), sql); 103 sql.append(')'); 104 105 sql.append(SQLUtil.VALUES).append('('); 106 SQLUtil.getValuesClause(left.getTableKeyFields(), sql); 107 sql.append(SQLUtil.COMMA); 108 SQLUtil.getValuesClause(right.getTableKeyFields(), sql); 109 sql.append(')'); 110 return sql.toString(); 111 } 112 113 protected static void setParameters(PreparedStatement ps, 114 RelationData relationData, 115 RelationPair pair) 116 { 117 int index = 1; 118 119 Object leftId = pair.getLeftId(); 121 JDBCCMPFieldBridge[] leftFields = (JDBCCMPFieldBridge[]) relationData.getLeftCMRField().getTableKeyFields(); 122 for(int i = 0; i < leftFields.length; ++i) 123 index = leftFields[i].setPrimaryKeyParameters(ps, index, leftId); 124 125 Object rightId = pair.getRightId(); 127 JDBCCMPFieldBridge[] rightFields = (JDBCCMPFieldBridge[]) relationData.getRightCMRField().getTableKeyFields(); 128 for(int i = 0; i < rightFields.length; ++i) 129 index = rightFields[i].setPrimaryKeyParameters(ps, index, rightId); 130 } 131 } 132 | Popular Tags |