001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-setup/org/opencms/setup/update6to7/oracle/CmsUpdateDBDropOldIndexes.java,v $
003: * Date : $Date: 2008-02-27 12:05:48 $
004: * Version: $Revision: 1.2 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.setup.update6to7.oracle;
033:
034: import org.opencms.setup.CmsSetupDBWrapper;
035: import org.opencms.setup.CmsSetupDb;
036:
037: import java.io.IOException;
038: import java.sql.SQLException;
039: import java.util.ArrayList;
040: import java.util.Arrays;
041: import java.util.Collections;
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: /**
047: * Oracle implementation to drop the old indexes from the database.<p>
048: *
049: * @author Roland Metzler
050: * @author Peter Bonrad
051: *
052: * @version $Revision: 1.2 $
053: *
054: * @since 7.0.0
055: */
056: public class CmsUpdateDBDropOldIndexes extends
057: org.opencms.setup.update6to7.generic.CmsUpdateDBDropOldIndexes {
058:
059: /** Constant for the field of the constraint name.<p> */
060: private static final String FIELD_CONSTRAINT_ORACLE = "CONSTRAINT_NAME";
061:
062: /** Constant for the field of the index name.<p> */
063: private static final String FIELD_INDEX_ORACLE = "INDEX_NAME";
064:
065: /** Constant for the sql query to drop a unique index key. */
066: private static final String QUERY_DROP_CONSTRAINT = "Q_DROP_CONSTRAINT";
067:
068: /** Constant for the SQL query properties.<p> */
069: private static final String QUERY_PROPERTY_FILE_ORACLE = "oracle/cms_drop_all_indexes_queries.properties";
070:
071: /** Constant for the sql query to list contraints for a table. */
072: private static final String QUERY_SHOW_CONSTRAINTS = "Q_SHOW_CONSTRAINTS";
073:
074: /** Constant for the replacement of the index name. */
075: private static final String REPLACEMENT_INDEX_ORACLE = "${indexname}";
076:
077: /** Constant array of the temporary indexes. */
078: private static final String[] TEMP_INDEXES = {
079: "CMS_BACKUP_CONTENTS_INDEX_1",
080: "CMS_BACKUP_CONTENTS_INDEX_2",
081: "CMS_BACKUP_PROJECTRESOURCES_INDEX_1",
082: "CMS_BACKUP_PROJECTS_INDEX_1",
083: "CMS_BACKUP_PROJECTS_INDEX_2",
084: "CMS_BACKUP_PROJECTS_INDEX_3",
085: "CMS_BACKUP_PROJECTS_INDEX_4",
086: "CMS_BACKUP_PROJECTS_INDEX_5",
087: "CMS_BACKUP_PROJECTS_INDEX_6",
088: "CMS_BACKUP_PROPERTIES_INDEX_1",
089: "CMS_BACKUP_PROPERTIES_INDEX_2",
090: "CMS_BACKUP_PROPERTIES_INDEX_3",
091: "CMS_BACKUP_PROPERTYDEF_INDEX_1",
092: "CMS_BACKUP_RESOURCES_INDEX_1",
093: "CMS_BACKUP_RESOURCES_INDEX_2",
094: "CMS_BACKUP_RESOURCES_INDEX_3",
095: "CMS_BACKUP_RESOURCES_INDEX_4",
096: "CMS_BACKUP_RESOURCES_INDEX_5",
097: "CMS_BACKUP_STRUCTURE_INDEX_1",
098: "CMS_BACKUP_STRUCTURE_INDEX_2",
099: "CMS_BACKUP_STRUCTURE_INDEX_3",
100: "CMS_BACKUP_STRUCTURE_INDEX_4", "CMS_GROUPS_INDEX_1",
101: "CMS_OFFLINE_CONTENTS_INDEX_1",
102: "CMS_OFFLINE_RESOURCES_INDEX_1",
103: "CMS_OFFLINE_RESOURCES_INDEX_2",
104: "CMS_OFFLINE_RESOURCES_INDEX_3",
105: "CMS_OFFLINE_RESOURCES_INDEX_4",
106: "CMS_OFFLINE_STRUCTURE_INDEX_1",
107: "CMS_OFFLINE_STRUCTURE_INDEX_2",
108: "CMS_OFFLINE_STRUCTURE_INDEX_3",
109: "CMS_ONLINE_CONTENTS_INDEX_1",
110: "CMS_ONLINE_RESOURCES_INDEX_1",
111: "CMS_ONLINE_RESOURCES_INDEX_2",
112: "CMS_ONLINE_RESOURCES_INDEX_3",
113: "CMS_ONLINE_RESOURCES_INDEX_4",
114: "CMS_ONLINE_STRUCTURE_INDEX_1",
115: "CMS_ONLINE_STRUCTURE_INDEX_2",
116: "CMS_ONLINE_STRUCTURE_INDEX_3",
117: "CMS_PROJECTRESOURCES_INDEX_1", "CMS_PROJECTS_INDEX_1",
118: "CMS_PROJECTS_INDEX_2", "CMS_PROJECTS_INDEX_3",
119: "CMS_PROJECTS_INDEX_4", "CMS_PUBLISH_HISTORY_INDEX_1",
120: "CMS_PUBLISH_HISTORY_INDEX_2" };
121:
122: /** Constant Array List of the temporary indexes. */
123: private static final List TEMP_INDEXES_LIST = Collections
124: .unmodifiableList(Arrays.asList(TEMP_INDEXES));
125:
126: /**
127: * Constructor.<p>
128: *
129: * @throws IOException if the sql queries properties file could not be read
130: */
131: public CmsUpdateDBDropOldIndexes() throws IOException {
132:
133: super ();
134: loadQueryProperties(QUERY_PROPERTIES_PREFIX
135: + QUERY_PROPERTY_FILE_ORACLE);
136: }
137:
138: // No implementation yet
139:
140: /**
141: * @see org.opencms.setup.update6to7.A_CmsUpdateDBPart#internalExecute(org.opencms.setup.CmsSetupDb)
142: */
143: protected void internalExecute(CmsSetupDb dbCon) {
144:
145: List tablenames = CMS_TABLES_LIST;
146:
147: // Iterate over all the tables.
148: for (Iterator tableIterator = tablenames.iterator(); tableIterator
149: .hasNext();) {
150: String tablename = (String) tableIterator.next();
151: System.out.println("dropping indexes for table "
152: + tablename);
153: // Check if the table is existing
154: if (dbCon.hasTableOrColumn(tablename, null)) {
155: try {
156:
157: // First drop constraints
158: List constraints = getConstraints(dbCon, tablename);
159: Iterator iter = constraints.iterator();
160: while (iter.hasNext()) {
161: String constraint = (String) iter.next();
162:
163: String dropConstraint = readQuery(QUERY_DROP_CONSTRAINT);
164: HashMap replacer = new HashMap();
165: replacer.put(REPLACEMENT_TABLENAME, tablename);
166: replacer.put(REPLACEMENT_INDEX_ORACLE,
167: constraint);
168: dbCon.updateSqlStatement(dropConstraint,
169: replacer, null);
170: }
171:
172: // Drop the indexes from the table.
173: List indexes = getIndexes(dbCon, tablename);
174: iter = indexes.iterator();
175: while (iter.hasNext()) {
176: String index = (String) iter.next();
177:
178: // Drop the index
179: String dropIndex = readQuery(QUERY_DROP_INDEX);
180: HashMap replacerIndex = new HashMap();
181: replacerIndex.put(REPLACEMENT_INDEX_ORACLE,
182: index);
183: dbCon.updateSqlStatement(dropIndex,
184: replacerIndex, null);
185: }
186: } catch (SQLException e) {
187: e.printStackTrace();
188: }
189: }
190: }
191:
192: // Create the temporary indexes
193: // Each index must be created in its own query
194: for (Iterator tempIndexes = TEMP_INDEXES_LIST.iterator(); tempIndexes
195: .hasNext();) {
196: try {
197: String createIndex = (String) tempIndexes.next();
198: String creationQuery = readQuery(createIndex);
199: dbCon.updateSqlStatement(creationQuery, null, null);
200: } catch (SQLException e) {
201: e.printStackTrace();
202: }
203: }
204: }
205:
206: /**
207: * Gets the constraints for a table.<p>
208: *
209: * @param dbCon the db connection interface
210: * @param tablename the table to get the indexes from
211: *
212: * @return a list of constraints
213: *
214: * @throws SQLException if something goes wrong
215: */
216: private List getConstraints(CmsSetupDb dbCon, String tablename)
217: throws SQLException {
218:
219: ArrayList constraints = new ArrayList();
220: String tableConstraints = readQuery(QUERY_SHOW_CONSTRAINTS);
221: HashMap replacer = new HashMap();
222: replacer.put(REPLACEMENT_TABLENAME, tablename);
223: CmsSetupDBWrapper db = null;
224: try {
225: db = dbCon.executeSqlStatement(tableConstraints, replacer);
226: while (db.getResultSet().next()) {
227: String constraint = db.getResultSet().getString(
228: FIELD_CONSTRAINT_ORACLE);
229: if (!constraints.contains(constraint)) {
230: constraints.add(constraint);
231: }
232:
233: }
234: } finally {
235: if (db != null) {
236: db.close();
237: }
238: }
239:
240: return constraints;
241: }
242:
243: /**
244: * Gets the indexes for a table.<p>
245: *
246: * @param dbCon the db connection interface
247: * @param tablename the table to get the indexes from
248: *
249: * @return a list of indexes
250: *
251: * @throws SQLException if something goes wrong
252: */
253: private List getIndexes(CmsSetupDb dbCon, String tablename)
254: throws SQLException {
255:
256: ArrayList indexes = new ArrayList();
257: String tableIndex = readQuery(QUERY_SHOW_INDEX);
258: HashMap replacer = new HashMap();
259: replacer.put(REPLACEMENT_TABLENAME, tablename);
260: CmsSetupDBWrapper db = null;
261: try {
262: db = dbCon.executeSqlStatement(tableIndex, replacer);
263: while (db.getResultSet().next()) {
264: String index = db.getResultSet().getString(
265: FIELD_INDEX_ORACLE);
266: if (!indexes.contains(index)) {
267: indexes.add(index);
268: }
269:
270: }
271: } finally {
272: if (db != null) {
273: db.close();
274: }
275: }
276: return indexes;
277: }
278: }
|