KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCDeleteRelationsCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.sql.DataSource JavaDoc;
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 /**
34  * Deletes relations from a relation table.
35  *
36  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
37  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
38  * @version $Revision: 41848 $
39  */

40 public final class JDBCDeleteRelationsCommand
41 {
42    private final Logger log;
43    private int maxKeysInDelete;
44    private String JavaDoc maxKeysStatement;
45
46    public JDBCDeleteRelationsCommand(JDBCStoreManager manager)
47    {
48       // Create the Log
49
log = Logger.getLogger(this.getClass().getName() +
50          "." +
51          manager.getMetaData().getName()
52       );
53
54       maxKeysInDelete = manager.getJDBCTypeFactory().getTypeMapping().getMaxKeysInDelete();
55    }
56
57    //
58
// This command needs to be changed to chunk delete commands, because
59
// some database have a limit on the number of parameters in a statement.
60
//
61
public void execute(RelationData relationData)
62    {
63       if(relationData.removedRelations.size() == 0)
64       {
65          return;
66       }
67
68       Iterator JavaDoc pairs = relationData.removedRelations.iterator();
69       int i = 0;
70       while(i < relationData.removedRelations.size())
71       {
72          String JavaDoc sql = getSQL(relationData, relationData.removedRelations.size() - i);
73
74          Connection JavaDoc con = null;
75          PreparedStatement JavaDoc ps = null;
76          JDBCCMRFieldBridge cmrField = relationData.getLeftCMRField();
77          try
78          {
79             // create the statement
80
if(log.isDebugEnabled())
81             {
82                log.debug("Executing SQL: " + sql);
83             }
84
85             // get the connection
86
DataSource JavaDoc dataSource = cmrField.getDataSource();
87             con = dataSource.getConnection();
88             ps = con.prepareStatement(sql);
89
90             // set the parameters
91
setParameters(ps, relationData, pairs);
92
93             // execute statement
94
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 JavaDoc e)
103          {
104             throw new EJBException JavaDoc("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 JavaDoc 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 JavaDoc createSQL(RelationData relationData, int keysInDelete)
130    {
131       JDBCCMRFieldBridge left = relationData.getLeftCMRField();
132       JDBCCMRFieldBridge right = relationData.getRightCMRField();
133
134       StringBuffer JavaDoc sql = new StringBuffer JavaDoc(300);
135       sql.append(SQLUtil.DELETE_FROM)
136          .append(left.getQualifiedTableName())
137          .append(SQLUtil.WHERE);
138
139       StringBuffer JavaDoc whereClause = new StringBuffer JavaDoc(20);
140       whereClause.append('(');
141       // left keys
142
SQLUtil.getWhereClause(left.getTableKeyFields(), whereClause)
143          .append(SQLUtil.AND);
144       // right keys
145
SQLUtil.getWhereClause(right.getTableKeyFields(), whereClause)
146          .append(')');
147       String JavaDoc 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 JavaDoc ps, RelationData relationData, Iterator JavaDoc pairs)
158       throws Exception JavaDoc
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          // left keys
169
Object JavaDoc leftId = pair.getLeftId();
170          for(int i = 0; i < leftFields.length; ++i)
171          {
172             index = leftFields[i].setPrimaryKeyParameters(ps, index, leftId);
173          }
174
175          // right keys
176
Object JavaDoc 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