001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.util.Iterator;
027: import javax.ejb.EJBException;
028: import javax.sql.DataSource;
029:
030: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
031: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge;
032: import org.jboss.logging.Logger;
033:
034: /**
035: * Inserts relations into a relation table.
036: *
037: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
038: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
039: * @version $Revision: 57209 $
040: */
041: public final class JDBCInsertRelationsCommand {
042: private final Logger log;
043:
044: public JDBCInsertRelationsCommand(JDBCStoreManager manager) {
045: this .log = Logger.getLogger(this .getClass().getName() + "."
046: + manager.getMetaData().getName());
047: }
048:
049: public void execute(RelationData relationData) {
050: if (relationData.addedRelations.size() == 0) {
051: return;
052: }
053:
054: Connection con = null;
055: PreparedStatement ps = null;
056:
057: JDBCCMRFieldBridge cmrField = relationData.getLeftCMRField();
058: try {
059: // get the sql
060: String sql = getSQL(relationData);
061: boolean debug = log.isDebugEnabled();
062: if (debug)
063: log.debug("Executing SQL: " + sql);
064:
065: // get the connection
066: DataSource dataSource = cmrField.getDataSource();
067: con = dataSource.getConnection();
068:
069: // get a prepared statement
070: ps = con.prepareStatement(sql);
071:
072: Iterator pairs = relationData.addedRelations.iterator();
073: while (pairs.hasNext()) {
074: RelationPair pair = (RelationPair) pairs.next();
075:
076: // set the parameters
077: setParameters(ps, relationData, pair);
078:
079: int rowsAffected = ps.executeUpdate();
080: }
081: } catch (Exception e) {
082: throw new EJBException("Could insert relations into "
083: + cmrField.getQualifiedTableName(), e);
084: } finally {
085: JDBCUtil.safeClose(ps);
086: JDBCUtil.safeClose(con);
087: }
088: }
089:
090: protected static String getSQL(RelationData relationData) {
091: JDBCCMRFieldBridge left = relationData.getLeftCMRField();
092: JDBCCMRFieldBridge right = relationData.getRightCMRField();
093:
094: StringBuffer sql = new StringBuffer(200);
095: sql.append(SQLUtil.INSERT_INTO).append(
096: left.getQualifiedTableName());
097:
098: sql.append('(');
099: SQLUtil.getColumnNamesClause(left.getTableKeyFields(), sql);
100: sql.append(SQLUtil.COMMA);
101: SQLUtil.getColumnNamesClause(right.getTableKeyFields(), sql);
102: sql.append(')');
103:
104: sql.append(SQLUtil.VALUES).append('(');
105: SQLUtil.getValuesClause(left.getTableKeyFields(), sql);
106: sql.append(SQLUtil.COMMA);
107: SQLUtil.getValuesClause(right.getTableKeyFields(), sql);
108: sql.append(')');
109: return sql.toString();
110: }
111:
112: protected static void setParameters(PreparedStatement ps,
113: RelationData relationData, RelationPair pair) {
114: int index = 1;
115:
116: // left keys
117: Object leftId = pair.getLeftId();
118: JDBCCMPFieldBridge[] leftFields = (JDBCCMPFieldBridge[]) relationData
119: .getLeftCMRField().getTableKeyFields();
120: for (int i = 0; i < leftFields.length; ++i)
121: index = leftFields[i].setPrimaryKeyParameters(ps, index,
122: leftId);
123:
124: // right keys
125: Object rightId = pair.getRightId();
126: JDBCCMPFieldBridge[] rightFields = (JDBCCMPFieldBridge[]) relationData
127: .getRightCMRField().getTableKeyFields();
128: for (int i = 0; i < rightFields.length; ++i)
129: index = rightFields[i].setPrimaryKeyParameters(ps, index,
130: rightId);
131: }
132: }
|