1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.db;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
19  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
20  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  
26  import java.io.IOException;
27  
28  import java.sql.CallableStatement;
29  import java.sql.Connection;
30  import java.sql.SQLException;
31  
32  import java.util.HashSet;
33  import java.util.Set;
34  
35  /**
36   * <a href="DB2DB.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Alexander Chow
39   * @author Bruno Farache
40   * @author Sandeep Soni
41   * @author Ganesh Ram
42   */
43  public class DB2DB extends BaseDB {
44  
45      public static DB getInstance() {
46          return _instance;
47      }
48  
49      public String buildSQL(String template) throws IOException {
50          template = convertTimestamp(template);
51          template = replaceTemplate(template, getTemplate());
52  
53          template = reword(template);
54          template = removeLongInserts(template);
55          template = removeNull(template);
56          template = StringUtil.replace(template, "\\'", "''");
57  
58          return template;
59      }
60  
61      public boolean isSupportsAlterColumnType() {
62          return _SUPPORTS_ALTER_COLUMN_TYPE;
63      }
64  
65      public boolean isSupportsScrollableResults() {
66          return _SUPPORTS_SCROLLABLE_RESULTS;
67      }
68  
69      public void runSQL(String template) throws IOException, SQLException {
70          if (template.startsWith(ALTER_COLUMN_NAME) ||
71              template.startsWith(ALTER_COLUMN_TYPE)) {
72  
73              String sql = buildSQL(template);
74  
75              String[] alterSqls = StringUtil.split(sql, StringPool.SEMICOLON);
76  
77              for (String alterSql : alterSqls) {
78                  if (!alterSql.startsWith("-- ")) {
79                      runSQL(alterSql);
80                  }
81              }
82          }
83          else {
84              super.runSQL(template);
85          }
86      }
87  
88      public void runSQL(String[] templates) throws IOException, SQLException {
89          super.runSQL(templates);
90  
91          _reorgTables(templates);
92      }
93  
94      protected DB2DB() {
95          super(TYPE_DB2);
96      }
97  
98      protected String buildCreateFileContent(
99              String sqlDir, String databaseName, int population)
100         throws IOException {
101 
102         String suffix = getSuffix(population);
103 
104         StringBundler sb = new StringBundler(14);
105 
106         sb.append("drop database ");
107         sb.append(databaseName);
108         sb.append(";\n");
109         sb.append("create database ");
110         sb.append(databaseName);
111         sb.append(";\n");
112         sb.append("connect to ");
113         sb.append(databaseName);
114         sb.append(";\n");
115         sb.append(
116             FileUtil.read(sqlDir + "/portal" + suffix + "/portal" + suffix +
117                 "-db2.sql"));
118         sb.append("\n\n");
119         sb.append(FileUtil.read(sqlDir + "/indexes/indexes-db2.sql"));
120         sb.append("\n\n");
121         sb.append(FileUtil.read(sqlDir + "/sequences/sequences-db2.sql"));
122 
123         return sb.toString();
124     }
125 
126     protected String getServerName() {
127         return "db2";
128     }
129 
130     protected String[] getTemplate() {
131         return _DB2;
132     }
133 
134     protected String reword(String data) throws IOException {
135         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
136             new UnsyncStringReader(data));
137 
138         StringBundler sb = new StringBundler();
139 
140         String line = null;
141 
142         while ((line = unsyncBufferedReader.readLine()) != null) {
143             if (line.startsWith(ALTER_COLUMN_NAME)) {
144                 String[] template = buildColumnNameTokens(line);
145 
146                 line = StringUtil.replace(
147                     "alter table @table@ add column @new-column@ @type@;\n",
148                     REWORD_TEMPLATE, template);
149 
150                 line = line + StringUtil.replace(
151                     "update @table@ set @new-column@ = @old-column@;\n",
152                     REWORD_TEMPLATE, template);
153 
154                 line = line + StringUtil.replace(
155                     "alter table @table@ drop column @old-column@",
156                     REWORD_TEMPLATE, template);
157             }
158             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
159                 line = "-- " + line;
160             }
161             else if (line.indexOf(DROP_INDEX) != -1) {
162                 String[] tokens = StringUtil.split(line, " ");
163 
164                 line = StringUtil.replace(
165                     "drop index @index@;", "@index@", tokens[2]);
166             }
167 
168             sb.append(line);
169             sb.append("\n");
170         }
171 
172         unsyncBufferedReader.close();
173 
174         return sb.toString();
175     }
176 
177     private void _reorgTables(String[] templates) throws SQLException {
178         Set<String> tableNames = new HashSet<String>();
179 
180         for (String template : templates) {
181             if (template.startsWith("alter table")) {
182                 tableNames.add(template.split(" ")[2]);
183             }
184         }
185 
186         if (tableNames.size() == 0) {
187             return;
188         }
189 
190         Connection con = null;
191         CallableStatement callStmt = null;
192 
193         try {
194             con = DataAccess.getConnection();
195 
196             for (String tableName : tableNames) {
197                 String sql = "call sysproc.admin_cmd(?)";
198 
199                 callStmt = con.prepareCall(sql);
200 
201                 String param = "reorg table " + tableName;
202 
203                 callStmt.setString(1, param);
204 
205                 callStmt.execute();
206             }
207         }
208         finally {
209             DataAccess.cleanUp(con, callStmt);
210         }
211     }
212 
213     private static String[] _DB2 = {
214         "--", "1", "0",
215         "'1970-01-01-00.00.00.000000'", "current timestamp",
216         " blob(2000)", " smallint", " timestamp",
217         " double", " integer", " bigint",
218         " varchar(500)", " clob", " varchar",
219         " generated always as identity", "commit"
220     };
221 
222     private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
223 
224     private static boolean _SUPPORTS_SCROLLABLE_RESULTS;
225 
226     private static DB2DB _instance = new DB2DB();
227 
228 }