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