001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.acting.modular;
019:
020: import java.sql.Connection;
021: import java.sql.PreparedStatement;
022: import java.sql.SQLException;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.service.ServiceException;
028:
029: /**
030: * Updates a record in a database. The action can update one or more
031: * tables, and can update more than one row to a table at a time.
032: *
033: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
034: * @version CVS $Id: DatabaseDeleteAction.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class DatabaseDeleteAction extends DatabaseAction {
037:
038: /**
039: * determine which mode to use as default mode
040: * here: DELETE
041: * highly specific to operation INSERT / UPDATE / DELETE / SELECT
042: */
043: protected String selectMode(boolean isAutoIncrement, Map modes) {
044:
045: return (String) modes.get(MODE_OTHERS);
046: }
047:
048: /**
049: * determine whether autoincrement columns should be honoured by
050: * this operation. This is usually snsible only for INSERTs.
051: */
052: protected boolean honourAutoIncrement() {
053: return false;
054: }
055:
056: /**
057: * Fetch all values for all key columns that are needed to do the
058: * database operation.
059: */
060: protected Object[][] getColumnValues(Configuration tableConf,
061: CacheHelper queryData, Map objectModel)
062: throws ConfigurationException, ServiceException {
063:
064: Object[][] columnValues = new Object[queryData.columns.length][];
065: for (int i = 0; i < queryData.columns.length; i++) {
066: if (queryData.columns[i].isKey) {
067: columnValues[i] = this .getColumnValue(tableConf,
068: queryData.columns[i], objectModel);
069: } else {
070: // columnValues[i] = new Object[1]; // this should not be needed
071: }
072: }
073: return columnValues;
074: }
075:
076: /**
077: * Get the String representation of the PreparedStatement. This is
078: * mapped to the Configuration object itself, so if it doesn't exist,
079: * it will be created.
080: *
081: * @param table the table's configuration object
082: * @return the insert query as a string
083: */
084: protected CacheHelper getQuery(Configuration table, Map modeTypes,
085: Map defaultModeNames) throws ConfigurationException,
086: ServiceException {
087:
088: LookUpKey lookUpKey = new LookUpKey(table, modeTypes);
089: CacheHelper queryData = null;
090: synchronized (this .cachedQueryData) {
091: queryData = (CacheHelper) this .cachedQueryData
092: .get(lookUpKey);
093: if (queryData == null) {
094: Configuration[] keys = table.getChild("keys")
095: .getChildren("key");
096:
097: queryData = new CacheHelper(keys.length, keys.length);
098: fillModes(keys, true, defaultModeNames, modeTypes,
099: queryData);
100:
101: StringBuffer queryBuffer = new StringBuffer(
102: "DELETE FROM ");
103: queryBuffer.append(table.getAttribute("name")).append(
104: " WHERE ");
105: for (int i = 0; i < queryData.columns.length; i++) {
106: if (i > 0) {
107: queryBuffer.append(" AND ");
108: }
109: queryBuffer.append(
110: queryData.columns[i].columnConf
111: .getAttribute("name"))
112: .append("= ?");
113: }
114:
115: queryData.queryString = queryBuffer.toString();
116:
117: this .cachedQueryData.put(lookUpKey, queryData);
118: }
119: }
120:
121: return queryData;
122: }
123:
124: /**
125: * set all necessary ?s and execute the query
126: */
127: protected int processRow(Map objectModel, Connection conn,
128: PreparedStatement statement, String outputMode,
129: Configuration table, CacheHelper queryData,
130: Object[][] columnValues, int rowIndex, Map results)
131: throws SQLException, ConfigurationException, Exception {
132:
133: int currentIndex = 1;
134:
135: // ordering is different for DELETE just needs keys
136: for (int i = 0; i < queryData.columns.length; i++) {
137: Column col = queryData.columns[i];
138: if (col.isKey) {
139: this .setColumn(objectModel, outputMode, results, table,
140: col.columnConf, rowIndex,
141: columnValues[i][(col.isSet ? rowIndex : 0)],
142: statement, currentIndex);
143: currentIndex++;
144: }
145: }
146: int rowCount = statement.executeUpdate();
147: return rowCount;
148: }
149:
150: }
|