1
22
23 package com.liferay.portal.dao.db;
24
25 import com.liferay.portal.kernel.dao.db.DB;
26 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
27 import com.liferay.portal.kernel.util.FileUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.StringReader;
34
35 import java.sql.CallableStatement;
36 import java.sql.Connection;
37 import java.sql.SQLException;
38
39 import java.util.HashSet;
40 import java.util.Set;
41
42
50 public class DB2DB extends BaseDB {
51
52 public static DB getInstance() {
53 return _instance;
54 }
55
56 public String buildSQL(String template) throws IOException {
57 template = convertTimestamp(template);
58 template = replaceTemplate(template, getTemplate());
59
60 template = reword(template);
61 template = removeLongInserts(template);
62 template = removeNull(template);
63 template = StringUtil.replace(template, "\\'", "''");
64
65 return template;
66 }
67
68 public boolean isSupportsAlterColumnType() {
69 return _SUPPORTS_ALTER_COLUMN_TYPE;
70 }
71
72 public boolean isSupportsScrollableResults() {
73 return _SUPPORTS_SCROLLABLE_RESULTS;
74 }
75
76 public void runSQL(String template) throws IOException, SQLException {
77 if (template.startsWith(ALTER_COLUMN_NAME) ||
78 template.startsWith(ALTER_COLUMN_TYPE)) {
79
80 String sql = buildSQL(template);
81
82 String[] alterSqls = StringUtil.split(sql, StringPool.SEMICOLON);
83
84 for (String alterSql : alterSqls) {
85 if (!alterSql.startsWith("-- ")) {
86 runSQL(alterSql);
87 }
88 }
89 }
90 else {
91 super.runSQL(template);
92 }
93 }
94
95 public void runSQL(String[] templates) throws IOException, SQLException {
96 super.runSQL(templates);
97
98 _reorgTables(templates);
99 }
100
101 protected DB2DB() {
102 super(TYPE_DB2);
103 }
104
105 protected String buildCreateFileContent(String databaseName, int population)
106 throws IOException {
107
108 String suffix = getSuffix(population);
109
110 StringBuilder sb = new StringBuilder();
111
112 sb.append("drop database " + databaseName + ";\n");
113 sb.append("create database " + databaseName + ";\n");
114 sb.append("connect to " + databaseName + ";\n");
115 sb.append(
116 FileUtil.read("../sql/portal" + suffix + "/portal" + suffix +
117 "-db2.sql"));
118 sb.append("\n\n");
119 sb.append(FileUtil.read("../sql/indexes/indexes-db2.sql"));
120 sb.append("\n\n");
121 sb.append(FileUtil.read("../sql/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 BufferedReader br = new BufferedReader(new StringReader(data));
136
137 StringBuilder sb = new StringBuilder();
138
139 String line = null;
140
141 while ((line = br.readLine()) != null) {
142 if (line.startsWith(ALTER_COLUMN_NAME)) {
143 String[] template = buildColumnNameTokens(line);
144
145 line = StringUtil.replace(
146 "alter table @table@ add column @new-column@ @type@;\n",
147 REWORD_TEMPLATE, template);
148
149 line = line + StringUtil.replace(
150 "update @table@ set @new-column@ = @old-column@;\n",
151 REWORD_TEMPLATE, template);
152
153 line = line + StringUtil.replace(
154 "alter table @table@ drop column @old-column@",
155 REWORD_TEMPLATE, template);
156 }
157 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
158 line = "-- " + line;
159 }
160
161 sb.append(line);
162 sb.append("\n");
163 }
164
165 br.close();
166
167 return sb.toString();
168 }
169
170 private void _reorgTables(String[] templates) throws SQLException {
171 Set<String> tableNames = new HashSet<String>();
172
173 for (String template : templates) {
174 if (template.startsWith("alter table")) {
175 tableNames.add(template.split(" ")[2]);
176 }
177 }
178
179 if (tableNames.size() == 0) {
180 return;
181 }
182
183 Connection con = null;
184 CallableStatement callStmt = null;
185
186 try {
187 con = DataAccess.getConnection();
188
189 for (String tableName : tableNames) {
190 String sql = "call sysproc.admin_cmd(?)";
191
192 callStmt = con.prepareCall(sql);
193
194 String param = "reorg table " + tableName;
195
196 callStmt.setString(1, param);
197
198 callStmt.execute();
199 }
200 }
201 finally {
202 DataAccess.cleanUp(con, callStmt);
203 }
204 }
205
206 private static String[] _DB2 = {
207 "--", "1", "0",
208 "'1970-01-01-00.00.00.000000'", "current timestamp",
209 " blob(2000)", " smallint", " timestamp",
210 " double", " integer", " bigint",
211 " varchar(500)", " clob", " varchar",
212 " generated always as identity", "commit"
213 };
214
215 private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
216
217 private static boolean _SUPPORTS_SCROLLABLE_RESULTS;
218
219 private static DB2DB _instance = new DB2DB();
220
221 }