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 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 long start = System.currentTimeMillis();
162
163 try {
164 if (_log.isInfoEnabled()) {
165 _log.info("Upgrading " + ClassUtil.getClassName(this));
166 }
167
168 doUpgrade();
169 }
170 catch (Exception e) {
171 throw new UpgradeException(e);
172 }
173 finally {
174 if (_log.isInfoEnabled()) {
175 _log.info(
176 "Completed upgrade process " +
177 ClassUtil.getClassName(this) + " in " +
178 (System.currentTimeMillis() - start) + "ms");
179 }
180 }
181 }
182
183 public void upgrade(Class<?> upgradeProcessClass) throws UpgradeException {
184 UpgradeProcess upgradeProcess = null;
185
186 try {
187 upgradeProcess = (UpgradeProcess)upgradeProcessClass.newInstance();
188 }
189 catch (Exception e) {
190 throw new UpgradeException(e);
191 }
192
193 upgradeProcess.upgrade();
194 }
195
196 @Override
197 public void upgrade(DBProcessContext dbProcessContext)
198 throws UpgradeException {
199
200 upgrade();
201 }
202
203 public void upgrade(UpgradeProcess upgradeProcess) throws UpgradeException {
204 upgradeProcess.upgrade();
205 }
206
207 protected boolean doHasTable(String tableName) throws Exception {
208 Connection con = null;
209 PreparedStatement ps = null;
210 ResultSet rs = null;
211
212 try {
213 con = DataAccess.getUpgradeOptimizedConnection();
214
215 DatabaseMetaData metadata = con.getMetaData();
216
217 rs = metadata.getTables(null, null, tableName, null);
218
219 while (rs.next()) {
220 return true;
221 }
222 }
223 finally {
224 DataAccess.cleanUp(con, ps, rs);
225 }
226
227 return false;
228 }
229
230 protected void doUpgrade() throws Exception {
231 }
232
233 protected void upgradeTable(String tableName, Object[][] tableColumns)
234 throws Exception {
235
236 UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
237 tableName, tableColumns);
238
239 upgradeTable.updateTable();
240 }
241
242 protected void upgradeTable(
243 String tableName, Object[][] tableColumns, String sqlCreate,
244 String[] sqlAddIndexes)
245 throws Exception {
246
247 UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
248 tableName, tableColumns);
249
250 upgradeTable.setCreateSQL(sqlCreate);
251 upgradeTable.setIndexesSQL(sqlAddIndexes);
252
253 upgradeTable.updateTable();
254 }
255
256 private static final Log _log = LogFactoryUtil.getLog(UpgradeProcess.class);
257
258 }