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