001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools.sql;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.spring.hibernate.HibernateUtil;
026: import com.liferay.util.FileUtil;
027: import com.liferay.util.dao.DataAccess;
028:
029: import java.io.BufferedReader;
030: import java.io.File;
031: import java.io.IOException;
032: import java.io.StringReader;
033:
034: import java.sql.CallableStatement;
035: import java.sql.Connection;
036: import java.sql.SQLException;
037:
038: import java.util.HashSet;
039: import java.util.Iterator;
040: import java.util.Set;
041:
042: /**
043: * <a href="DB2Util.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Alexander Chow
046: * @author Bruno Farache
047: *
048: */
049: public class DB2Util extends DBUtil {
050:
051: public static DBUtil getInstance() {
052: return _instance;
053: }
054:
055: public String buildSQL(String template) throws IOException {
056: template = convertTimestamp(template);
057: template = StringUtil
058: .replace(template, TEMPLATE, getTemplate());
059:
060: template = reword(template);
061: template = removeLongInserts(template);
062: template = removeNull(template);
063: template = StringUtil.replace(template, "\\'", "''");
064:
065: return template;
066: }
067:
068: public void runSQL(String template) throws IOException,
069: SQLException {
070: if (template.startsWith(ALTER_COLUMN_NAME)) {
071: String sql = buildSQL(template);
072:
073: String[] renameSqls = sql.split(";");
074:
075: runSQL(renameSqls);
076: } else {
077: super .runSQL(template);
078: }
079: }
080:
081: public void runSQL(String[] templates) throws IOException,
082: SQLException {
083: super .runSQL(templates);
084:
085: _reorgTables(templates);
086: }
087:
088: protected DB2Util() {
089: }
090:
091: protected void buildCreateFile(String databaseName, boolean minimal)
092: throws IOException {
093:
094: String minimalSuffix = getMinimalSuffix(minimal);
095:
096: File file = new File("../sql/create" + minimalSuffix
097: + "/create" + minimalSuffix + "-db2.sql");
098:
099: StringMaker sm = new StringMaker();
100:
101: sm.append("drop database " + databaseName + ";\n");
102: sm.append("create database " + databaseName + ";\n");
103: sm.append("connect to " + databaseName + ";\n");
104: sm.append(FileUtil.read("../sql/portal" + minimalSuffix
105: + "/portal" + minimalSuffix + "-db2.sql"));
106: sm.append("\n\n");
107: sm.append(FileUtil.read("../sql/indexes/indexes-db2.sql"));
108: sm.append("\n\n");
109: sm.append(FileUtil.read("../sql/sequences/sequences-db2.sql"));
110:
111: FileUtil.write(file, sm.toString());
112: }
113:
114: protected String getServerName() {
115: return "db2";
116: }
117:
118: protected String[] getTemplate() {
119: return _DB2;
120: }
121:
122: protected String reword(String data) throws IOException {
123: BufferedReader br = new BufferedReader(new StringReader(data));
124:
125: StringMaker sm = new StringMaker();
126:
127: String line = null;
128:
129: while ((line = br.readLine()) != null) {
130: if (line.startsWith(ALTER_COLUMN_TYPE)) {
131: line = "-- " + line;
132: } else if (line.startsWith(ALTER_COLUMN_NAME)) {
133: String[] template = buildColumnNameTokens(line);
134:
135: line = StringUtil
136: .replace(
137: "alter table @table@ add column @new-column@ @type@;\n",
138: REWORD_TEMPLATE, template);
139:
140: line = line
141: + StringUtil
142: .replace(
143: "update @table@ set @new-column@ = @old-column@;\n",
144: REWORD_TEMPLATE, template);
145:
146: line = line
147: + StringUtil
148: .replace(
149: "alter table @table@ drop column @old-column@",
150: REWORD_TEMPLATE, template);
151: }
152:
153: sm.append(line);
154: sm.append("\n");
155: }
156:
157: br.close();
158:
159: return sm.toString();
160: }
161:
162: private void _reorgTables(String[] templates) throws SQLException {
163: Set tableNames = new HashSet();
164:
165: for (int i = 0; i < templates.length; i++) {
166: if (templates[i].startsWith("alter table")) {
167: tableNames.add(templates[i].split(" ")[2]);
168: }
169: }
170:
171: if (tableNames.size() == 0) {
172: return;
173: }
174:
175: Connection con = null;
176: CallableStatement callStmt = null;
177:
178: try {
179: con = HibernateUtil.getConnection();
180:
181: Iterator itr = tableNames.iterator();
182:
183: while (itr.hasNext()) {
184: String tableName = (String) itr.next();
185:
186: String sql = "call sysproc.admin_cmd(?)";
187:
188: callStmt = con.prepareCall(sql);
189:
190: String param = "reorg table " + tableName;
191:
192: callStmt.setString(1, param);
193:
194: callStmt.execute();
195: }
196: } finally {
197: DataAccess.cleanUp(con, callStmt);
198: }
199: }
200:
201: private static String[] _DB2 = { "--", "1", "0",
202: "'1970-01-01-00.00.00.000000'", "current timestamp",
203: " smallint", " timestamp", " double", " integer",
204: " bigint", " varchar(500)", " clob", " varchar",
205: " generated always as identity", "commit" };
206:
207: private static DB2Util _instance = new DB2Util();
208:
209: }
|