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.io.unsync.UnsyncBufferedReader;
28 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
29 import com.liferay.portal.kernel.util.FileUtil;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32
33 import java.io.IOException;
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 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
136 new UnsyncStringReader(data));
137
138 StringBuilder sb = new StringBuilder();
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
162 sb.append(line);
163 sb.append("\n");
164 }
165
166 unsyncBufferedReader.close();
167
168 return sb.toString();
169 }
170
171 private void _reorgTables(String[] templates) throws SQLException {
172 Set<String> tableNames = new HashSet<String>();
173
174 for (String template : templates) {
175 if (template.startsWith("alter table")) {
176 tableNames.add(template.split(" ")[2]);
177 }
178 }
179
180 if (tableNames.size() == 0) {
181 return;
182 }
183
184 Connection con = null;
185 CallableStatement callStmt = null;
186
187 try {
188 con = DataAccess.getConnection();
189
190 for (String tableName : tableNames) {
191 String sql = "call sysproc.admin_cmd(?)";
192
193 callStmt = con.prepareCall(sql);
194
195 String param = "reorg table " + tableName;
196
197 callStmt.setString(1, param);
198
199 callStmt.execute();
200 }
201 }
202 finally {
203 DataAccess.cleanUp(con, callStmt);
204 }
205 }
206
207 private static String[] _DB2 = {
208 "--", "1", "0",
209 "'1970-01-01-00.00.00.000000'", "current timestamp",
210 " blob(2000)", " smallint", " timestamp",
211 " double", " integer", " bigint",
212 " varchar(500)", " clob", " varchar",
213 " generated always as identity", "commit"
214 };
215
216 private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
217
218 private static boolean _SUPPORTS_SCROLLABLE_RESULTS;
219
220 private static DB2DB _instance = new DB2DB();
221
222 }