001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.model;
018:
019: import java.sql.SQLException;
020: import java.sql.ResultSet;
021: import java.util.List;
022: import java.util.Map;
023:
024: import velosurf.sql.ConnectionWrapper;
025: import velosurf.sql.PooledPreparedStatement;
026: import velosurf.util.StringLists;
027:
028: /** This class is an action that gather several consecutive update/delete/insert queries.
029: *
030: * @author <a href=mailto:claude.brisson@gmail.com>Claude Brisson</a>
031: *
032: */
033: public class Transaction extends Action {
034: /** Builds a new transaction.
035: *
036: * @param name transaction name
037: * @param entity entity
038: */
039: public Transaction(String name, Entity entity) {
040: super (name, entity);
041: }
042:
043: /**
044: * Set the list of queries.
045: * @param queries list of SQL queries
046: */
047: public void setQueries(List<String> queries) {
048: this .queries = queries;
049: }
050:
051: /** Set the list of list of parameters.
052: *
053: * @param paramLists list of list of parameters
054: */
055: public void setParamNamesLists(List<List<String>> paramLists) {
056: paramNamesList = paramLists;
057: }
058:
059: /** Performs this action.
060: *
061: * @param source ReadOnlyMap containing parameter values
062: * @exception SQLException thrown from the database
063: * @return number of affected rows (addition of all the partial counts)
064: */
065: public int perform(Map<String, Object> source) throws SQLException {
066:
067: ConnectionWrapper conn = db.getTransactionConnection();
068: try {
069: int nb = queries.size();
070: int ret = 0;
071: for (int i = 0; i < nb; i++) {
072: // fool the buildArrayList method by using
073: // the super member paramNames
074: paramNames = (List<String>) paramNamesList.get(i);
075: List params = buildArrayList(source);
076: /* TODO: pool transaction statements */
077: PooledPreparedStatement statement = new PooledPreparedStatement(
078: conn, conn.prepareStatement(queries.get(i),
079: ResultSet.TYPE_SCROLL_INSENSITIVE,
080: ResultSet.CONCUR_READ_ONLY));
081: ret += statement.update(params);
082: statement.close();
083: }
084: conn.commit();
085: return ret;
086: } catch (SQLException sqle) {
087: conn.rollback();
088: throw sqle;
089: } finally {
090: conn.leaveBusyState();
091: }
092: }
093:
094: /** Debug method.
095: *
096: * @return the definition string of the transaction
097: */
098: public String toString() {
099: StringBuffer result = new StringBuffer();
100: int nb = queries.size();
101: for (int i = 0; i < nb; i++) {
102: List paramNames = (List) paramNamesList.get(i);
103: if (paramNames.size() > 0) {
104: result.append("(");
105: result.append(StringLists.join(paramNames, ",") + ")");
106: }
107: result.append(":" + queries.get(i));
108: if (i < nb - 1)
109: result.append('\n');
110: }
111: return result.toString();
112:
113: }
114:
115: /** All the queries.
116: */
117: private List<String> queries; // = null; WARNING : this init code is executed AFER Action constructor
118: /** List of lists of parameter names.
119: */
120: private List<List<String>> paramNamesList; // = null;
121:
122: }
|