001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.dao.db.BaseDBProcess;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
024    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
025    import com.liferay.portal.kernel.util.StringUtil;
026    
027    import java.sql.Connection;
028    import java.sql.DatabaseMetaData;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    import java.sql.ResultSetMetaData;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Alexander Chow
036     */
037    public abstract class UpgradeProcess extends BaseDBProcess {
038    
039            public UpgradeProcess() {
040            }
041    
042            public int getThreshold() {
043    
044                    // This upgrade process will only run if the build number is larger than
045                    // the returned threshold value. Return 0 to always run this upgrade
046                    // process.
047    
048                    return 0;
049            }
050    
051            public boolean hasTable(String tableName) throws Exception {
052                    if (doHasTable(StringUtil.toLowerCase(tableName)) ||
053                            doHasTable(StringUtil.toUpperCase(tableName)) ||
054                            doHasTable(tableName)) {
055    
056                            return true;
057                    }
058    
059                    return false;
060            }
061    
062            public long increment() {
063                    DB db = DBFactoryUtil.getDB();
064    
065                    return db.increment();
066            }
067    
068            public long increment(String name) {
069                    DB db = DBFactoryUtil.getDB();
070    
071                    return db.increment(name);
072            }
073    
074            public boolean isSupportsAlterColumnName() {
075                    DB db = DBFactoryUtil.getDB();
076    
077                    return db.isSupportsAlterColumnName();
078            }
079    
080            public boolean isSupportsAlterColumnType() {
081                    DB db = DBFactoryUtil.getDB();
082    
083                    return db.isSupportsAlterColumnType();
084            }
085    
086            public boolean isSupportsStringCaseSensitiveQuery() {
087                    DB db = DBFactoryUtil.getDB();
088    
089                    return db.isSupportsStringCaseSensitiveQuery();
090            }
091    
092            public boolean isSupportsUpdateWithInnerJoin() {
093                    DB db = DBFactoryUtil.getDB();
094    
095                    return db.isSupportsUpdateWithInnerJoin();
096            }
097    
098            public boolean tableHasColumn(String tableName, String columnName)
099                    throws Exception {
100    
101                    Connection con = null;
102                    PreparedStatement ps = null;
103                    ResultSet rs = null;
104    
105                    try {
106                            con = DataAccess.getUpgradeOptimizedConnection();
107    
108                            ps = con.prepareStatement("select * from " + tableName);
109    
110                            rs = ps.executeQuery();
111    
112                            ResultSetMetaData rsmd = rs.getMetaData();
113    
114                            for (int i = 0; i < rsmd.getColumnCount(); i++) {
115                                    String curColumnName = rsmd.getColumnName(i + 1);
116    
117                                    if (StringUtil.equalsIgnoreCase(curColumnName, columnName)) {
118                                            return true;
119                                    }
120                            }
121                    }
122                    catch (Exception e) {
123                    }
124                    finally {
125                            DataAccess.cleanUp(con, ps, rs);
126                    }
127    
128                    return false;
129            }
130    
131            public boolean tableHasData(String tableName) throws Exception {
132                    Connection con = null;
133                    PreparedStatement ps = null;
134                    ResultSet rs = null;
135    
136                    try {
137                            con = DataAccess.getUpgradeOptimizedConnection();
138    
139                            ps = con.prepareStatement("select count(*) from " + tableName);
140    
141                            rs = ps.executeQuery();
142    
143                            while (rs.next()) {
144                                    int count = rs.getInt(1);
145    
146                                    if (count > 0) {
147                                            return true;
148                                    }
149                            }
150                    }
151                    catch (Exception e) {
152                    }
153                    finally {
154                            DataAccess.cleanUp(con, ps, rs);
155                    }
156    
157                    return false;
158            }
159    
160            public void upgrade() throws UpgradeException {
161                    try {
162                            if (_log.isInfoEnabled()) {
163                                    _log.info("Upgrading " + getClass().getName());
164                            }
165    
166                            doUpgrade();
167                    }
168                    catch (Exception e) {
169                            throw new UpgradeException(e);
170                    }
171            }
172    
173            public void upgrade(Class<?> upgradeProcessClass) throws UpgradeException {
174                    UpgradeProcess upgradeProcess = null;
175    
176                    try {
177                            upgradeProcess = (UpgradeProcess)upgradeProcessClass.newInstance();
178                    }
179                    catch (Exception e) {
180                            throw new UpgradeException(e);
181                    }
182    
183                    upgradeProcess.upgrade();
184            }
185    
186            public void upgrade(UpgradeProcess upgradeProcess) throws UpgradeException {
187                    upgradeProcess.upgrade();
188            }
189    
190            protected boolean doHasTable(String tableName) throws Exception {
191                    Connection con = null;
192                    PreparedStatement ps = null;
193                    ResultSet rs = null;
194    
195                    try {
196                            con = DataAccess.getUpgradeOptimizedConnection();
197    
198                            DatabaseMetaData metadata = con.getMetaData();
199    
200                            rs = metadata.getTables(null, null, tableName, null);
201    
202                            while (rs.next()) {
203                                    return true;
204                            }
205                    }
206                    finally {
207                            DataAccess.cleanUp(con, ps, rs);
208                    }
209    
210                    return false;
211            }
212    
213            protected void doUpgrade() throws Exception {
214            }
215    
216            protected void upgradeTable(String tableName, Object[][] tableColumns)
217                    throws Exception {
218    
219                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
220                            tableName, tableColumns);
221    
222                    upgradeTable.updateTable();
223            }
224    
225            protected void upgradeTable(
226                            String tableName, Object[][] tableColumns, String sqlCreate,
227                            String[] sqlAddIndexes)
228                    throws Exception {
229    
230                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
231                            tableName, tableColumns);
232    
233                    upgradeTable.setCreateSQL(sqlCreate);
234                    upgradeTable.setIndexesSQL(sqlAddIndexes);
235    
236                    upgradeTable.updateTable();
237            }
238    
239            private static Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
240    
241    }