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