001
014
015 package com.liferay.portal.upgrade.util;
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.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.upgrade.UpgradeException;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.Validator;
024
025
029 public abstract class BaseUpgradeTableImpl extends Table {
030
031 public BaseUpgradeTableImpl(String tableName) {
032 super(tableName);
033 }
034
035 public BaseUpgradeTableImpl(String tableName, Object[][] columns) {
036 super(tableName, columns);
037 }
038
039 public void setCreateSQL(String createSQL) throws Exception {
040 if (_calledUpdateTable) {
041 throw new UpgradeException(
042 "setCreateSQL is called after updateTable");
043 }
044
045 super.setCreateSQL(createSQL);
046 }
047
048 public void updateTable() throws Exception {
049 _calledUpdateTable = true;
050
051 String tempFileName = generateTempFile();
052
053 try {
054 DB db = DBFactoryUtil.getDB();
055
056 String createSQL = getCreateSQL();
057
058 if (Validator.isNotNull(createSQL)) {
059 db.runSQL("drop table " + getTableName());
060
061 db.runSQL(createSQL);
062 }
063
064 if (Validator.isNotNull(tempFileName)) {
065 String deleteSQL = getDeleteSQL();
066
067 db.runSQL(deleteSQL);
068
069 populateTable(tempFileName);
070 }
071 }
072 finally {
073 if (Validator.isNotNull(tempFileName)) {
074 FileUtil.delete(tempFileName);
075 }
076 }
077 }
078
079 static Log _log = LogFactoryUtil.getLog(BaseUpgradeTableImpl.class);
080
081 private boolean _calledUpdateTable;
082
083 }