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