001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.db;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
020    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.io.IOException;
026    
027    import java.sql.CallableStatement;
028    import java.sql.Connection;
029    import java.sql.SQLException;
030    
031    import java.util.HashSet;
032    import java.util.Set;
033    
034    /**
035     * @author Alexander Chow
036     * @author Bruno Farache
037     * @author Sandeep Soni
038     * @author Ganesh Ram
039     */
040    public class DB2DB extends BaseDB {
041    
042            public static DB getInstance() {
043                    return _instance;
044            }
045    
046            public String buildSQL(String template) throws IOException {
047                    template = convertTimestamp(template);
048                    template = replaceTemplate(template, getTemplate());
049    
050                    template = reword(template);
051                    template = removeLongInserts(template);
052                    template = removeNull(template);
053                    template = StringUtil.replace(template, "\\'", "''");
054    
055                    return template;
056            }
057    
058            public boolean isSupportsAlterColumnType() {
059                    return _SUPPORTS_ALTER_COLUMN_TYPE;
060            }
061    
062            public boolean isSupportsScrollableResults() {
063                    return _SUPPORTS_SCROLLABLE_RESULTS;
064            }
065    
066            public void runSQL(String template) throws IOException, SQLException {
067                    if (template.startsWith(ALTER_COLUMN_NAME) ||
068                            template.startsWith(ALTER_COLUMN_TYPE)) {
069    
070                            String sql = buildSQL(template);
071    
072                            String[] alterSqls = StringUtil.split(sql, StringPool.SEMICOLON);
073    
074                            for (String alterSql : alterSqls) {
075                                    if (!alterSql.startsWith("-- ")) {
076                                            runSQL(alterSql);
077                                    }
078                            }
079                    }
080                    else {
081                            super.runSQL(template);
082                    }
083            }
084    
085            public void runSQL(String[] templates) throws IOException, SQLException {
086                    super.runSQL(templates);
087    
088                    _reorgTables(templates);
089            }
090    
091            protected DB2DB() {
092                    super(TYPE_DB2);
093            }
094    
095            protected String buildCreateFileContent(
096                            String sqlDir, String databaseName, int population)
097                    throws IOException {
098    
099                    String suffix = getSuffix(population);
100    
101                    StringBundler sb = new StringBundler(14);
102    
103                    sb.append("drop database ");
104                    sb.append(databaseName);
105                    sb.append(";\n");
106                    sb.append("create database ");
107                    sb.append(databaseName);
108                    sb.append(";\n");
109                    sb.append("connect to ");
110                    sb.append(databaseName);
111                    sb.append(";\n");
112                    sb.append(
113                            readFile(
114                                    sqlDir + "/portal" + suffix + "/portal" + suffix + "-db2.sql"));
115                    sb.append("\n\n");
116                    sb.append(readFile(sqlDir + "/indexes/indexes-db2.sql"));
117                    sb.append("\n\n");
118                    sb.append(readFile(sqlDir + "/sequences/sequences-db2.sql"));
119    
120                    return sb.toString();
121            }
122    
123            protected String getServerName() {
124                    return "db2";
125            }
126    
127            protected String[] getTemplate() {
128                    return _DB2;
129            }
130    
131            protected String reword(String data) throws IOException {
132                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
133                            new UnsyncStringReader(data));
134    
135                    StringBundler sb = new StringBundler();
136    
137                    String line = null;
138    
139                    while ((line = unsyncBufferedReader.readLine()) != null) {
140                            if (line.startsWith(ALTER_COLUMN_NAME)) {
141                                    String[] template = buildColumnNameTokens(line);
142    
143                                    line = StringUtil.replace(
144                                            "alter table @table@ add column @new-column@ @type@;\n",
145                                            REWORD_TEMPLATE, template);
146    
147                                    line = line + StringUtil.replace(
148                                            "update @table@ set @new-column@ = @old-column@;\n",
149                                            REWORD_TEMPLATE, template);
150    
151                                    line = line + StringUtil.replace(
152                                            "alter table @table@ drop column @old-column@",
153                                            REWORD_TEMPLATE, template);
154                            }
155                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
156                                    line = "-- " + line;
157                            }
158                            else if (line.indexOf(DROP_INDEX) != -1) {
159                                    String[] tokens = StringUtil.split(line, " ");
160    
161                                    line = StringUtil.replace(
162                                            "drop index @index@;", "@index@", tokens[2]);
163                            }
164    
165                            sb.append(line);
166                            sb.append("\n");
167                    }
168    
169                    unsyncBufferedReader.close();
170    
171                    return sb.toString();
172            }
173    
174            private void _reorgTables(String[] templates) throws SQLException {
175                    Set<String> tableNames = new HashSet<String>();
176    
177                    for (String template : templates) {
178                            if (template.startsWith("alter table")) {
179                                    tableNames.add(template.split(" ")[2]);
180                            }
181                    }
182    
183                    if (tableNames.size() == 0) {
184                            return;
185                    }
186    
187                    Connection con = null;
188                    CallableStatement callStmt = null;
189    
190                    try {
191                            con = DataAccess.getConnection();
192    
193                            for (String tableName : tableNames) {
194                                    String sql = "call sysproc.admin_cmd(?)";
195    
196                                    callStmt = con.prepareCall(sql);
197    
198                                    String param = "reorg table " + tableName;
199    
200                                    callStmt.setString(1, param);
201    
202                                    callStmt.execute();
203                            }
204                    }
205                    finally {
206                            DataAccess.cleanUp(con, callStmt);
207                    }
208            }
209    
210            private static String[] _DB2 = {
211                    "--", "1", "0",
212                    "'1970-01-01-00.00.00.000000'", "current timestamp",
213                    " blob(2000)", " smallint", " timestamp",
214                    " double", " integer", " bigint",
215                    " varchar(500)", " clob", " varchar",
216                    " generated always as identity", "commit"
217            };
218    
219            private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
220    
221            private static boolean _SUPPORTS_SCROLLABLE_RESULTS;
222    
223            private static DB2DB _instance = new DB2DB();
224    
225    }