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