1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools.sql;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.spring.hibernate.HibernateUtil;
28  import com.liferay.util.FileUtil;
29  import com.liferay.util.dao.DataAccess;
30  
31  import java.io.BufferedReader;
32  import java.io.File;
33  import java.io.IOException;
34  import java.io.StringReader;
35  
36  import java.sql.CallableStatement;
37  import java.sql.Connection;
38  import java.sql.SQLException;
39  
40  import java.util.HashSet;
41  import java.util.Iterator;
42  import java.util.Set;
43  
44  /**
45   * <a href="DB2Util.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Alexander Chow
48   * @author Bruno Farache
49   *
50   */
51  public class DB2Util extends DBUtil {
52  
53      public static DBUtil getInstance() {
54          return _instance;
55      }
56  
57      public String buildSQL(String template) throws IOException {
58          template = convertTimestamp(template);
59          template = StringUtil.replace(template, TEMPLATE, getTemplate());
60  
61          template = reword(template);
62          template = removeLongInserts(template);
63          template = removeNull(template);
64          template = StringUtil.replace(template, "\\'", "''");
65  
66          return template;
67      }
68  
69      public void runSQL(String template) throws IOException, SQLException {
70          if (template.startsWith(ALTER_COLUMN_NAME)) {
71              String sql = buildSQL(template);
72  
73              String[] renameSqls = sql.split(";");
74  
75              runSQL(renameSqls);
76          }
77          else {
78              super.runSQL(template);
79          }
80      }
81  
82      public void runSQL(String[] templates) throws IOException, SQLException {
83          super.runSQL(templates);
84  
85          _reorgTables(templates);
86      }
87  
88      protected DB2Util() {
89      }
90  
91      protected void buildCreateFile(String databaseName, boolean minimal)
92          throws IOException {
93  
94          String minimalSuffix = getMinimalSuffix(minimal);
95  
96          File file = new File(
97              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
98                  "-db2.sql");
99  
100         StringMaker sm = new StringMaker();
101 
102         sm.append("drop database " + databaseName + ";\n");
103         sm.append("create database " + databaseName + ";\n");
104         sm.append("connect to " + databaseName + ";\n");
105         sm.append(
106             FileUtil.read("../sql/portal" + minimalSuffix + "/portal" +
107                 minimalSuffix + "-db2.sql"));
108         sm.append("\n\n");
109         sm.append(FileUtil.read("../sql/indexes/indexes-db2.sql"));
110         sm.append("\n\n");
111         sm.append(FileUtil.read("../sql/sequences/sequences-db2.sql"));
112 
113         FileUtil.write(file, sm.toString());
114     }
115 
116     protected String getServerName() {
117         return "db2";
118     }
119 
120     protected String[] getTemplate() {
121         return _DB2;
122     }
123 
124     protected String reword(String data) throws IOException {
125         BufferedReader br = new BufferedReader(new StringReader(data));
126 
127         StringMaker sm = new StringMaker();
128 
129         String line = null;
130 
131         while ((line = br.readLine()) != null) {
132             if (line.startsWith(ALTER_COLUMN_TYPE)) {
133                 line = "-- " + line;
134             }
135             else if (line.startsWith(ALTER_COLUMN_NAME)) {
136                 String[] template = buildColumnNameTokens(line);
137 
138                 line = StringUtil.replace(
139                     "alter table @table@ add column @new-column@ @type@;\n",
140                     REWORD_TEMPLATE, template);
141 
142                 line = line + StringUtil.replace(
143                     "update @table@ set @new-column@ = @old-column@;\n",
144                     REWORD_TEMPLATE, template);
145 
146                 line = line + StringUtil.replace(
147                     "alter table @table@ drop column @old-column@",
148                     REWORD_TEMPLATE, template);
149             }
150 
151             sm.append(line);
152             sm.append("\n");
153         }
154 
155         br.close();
156 
157         return sm.toString();
158     }
159 
160     private void _reorgTables(String[] templates) throws SQLException {
161         Set tableNames = new HashSet();
162 
163         for (int i = 0; i < templates.length; i++) {
164             if (templates[i].startsWith("alter table")) {
165                 tableNames.add(templates[i].split(" ")[2]);
166             }
167         }
168 
169         if (tableNames.size() == 0) {
170             return;
171         }
172 
173         Connection con = null;
174         CallableStatement callStmt = null;
175 
176         try {
177             con = HibernateUtil.getConnection();
178 
179             Iterator itr = tableNames.iterator();
180 
181             while (itr.hasNext()) {
182                 String tableName = (String)itr.next();
183 
184                 String sql = "call sysproc.admin_cmd(?)";
185 
186                 callStmt = con.prepareCall(sql);
187 
188                 String param = "reorg table " + tableName;
189 
190                 callStmt.setString(1, param);
191 
192                 callStmt.execute();
193             }
194         }
195         finally {
196             DataAccess.cleanUp(con, callStmt);
197         }
198     }
199 
200     private static String[] _DB2 = {
201         "--", "1", "0",
202         "'1970-01-01-00.00.00.000000'", "current timestamp",
203         " smallint", " timestamp", " double",
204         " integer", " bigint",
205         " varchar(500)", " clob", " varchar",
206         " generated always as identity", "commit"
207     };
208 
209     private static DB2Util _instance = new DB2Util();
210 
211 }