001    /**
002     * Copyright (c) 2000-2013 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.CharPool;
022    import com.liferay.portal.kernel.util.StringBundler;
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            @Override
047            public String buildSQL(String template) throws IOException {
048                    template = convertTimestamp(template);
049                    template = replaceTemplate(template, getTemplate());
050    
051                    template = reword(template);
052                    template = removeLongInserts(template);
053                    template = removeNull(template);
054                    template = StringUtil.replace(template, "\\'", "''");
055                    template = StringUtil.replace(template, "\\n", "'||CHR(10)||'");
056    
057                    return template;
058            }
059    
060            @Override
061            public boolean isSupportsAlterColumnType() {
062                    return _SUPPORTS_ALTER_COLUMN_TYPE;
063            }
064    
065            @Override
066            public boolean isSupportsInlineDistinct() {
067                    return _SUPPORTS_INLINE_DISTINCT;
068            }
069    
070            @Override
071            public boolean isSupportsScrollableResults() {
072                    return _SUPPORTS_SCROLLABLE_RESULTS;
073            }
074    
075            @Override
076            public void runSQL(String template) throws IOException, SQLException {
077                    if (template.startsWith(ALTER_COLUMN_NAME)) {
078                            String sql = buildSQL(template);
079    
080                            String[] alterSqls = StringUtil.split(sql, CharPool.SEMICOLON);
081    
082                            for (String alterSql : alterSqls) {
083                                    runSQL(alterSql);
084                            }
085                    }
086                    else {
087                            super.runSQL(template);
088                    }
089            }
090    
091            @Override
092            public void runSQL(String[] templates) throws IOException, SQLException {
093                    super.runSQL(templates);
094    
095                    _reorgTables(templates);
096            }
097    
098            protected DB2DB() {
099                    super(TYPE_DB2);
100            }
101    
102            @Override
103            protected String buildCreateFileContent(
104                            String sqlDir, String databaseName, int population)
105                    throws IOException {
106    
107                    String suffix = getSuffix(population);
108    
109                    StringBundler sb = new StringBundler(14);
110    
111                    sb.append("drop database ");
112                    sb.append(databaseName);
113                    sb.append(";\n");
114                    sb.append("create database ");
115                    sb.append(databaseName);
116                    sb.append(" pagesize 8192;\n");
117                    sb.append("connect to ");
118                    sb.append(databaseName);
119                    sb.append(";\n");
120                    sb.append(getCreateTablesContent(sqlDir, suffix));
121                    sb.append("\n\n");
122                    sb.append(readFile(sqlDir + "/indexes/indexes-db2.sql"));
123                    sb.append("\n\n");
124                    sb.append(readFile(sqlDir + "/sequences/sequences-db2.sql"));
125    
126                    return sb.toString();
127            }
128    
129            @Override
130            protected String getServerName() {
131                    return "db2";
132            }
133    
134            @Override
135            protected String[] getTemplate() {
136                    return _DB2;
137            }
138    
139            @Override
140            protected String reword(String data) throws IOException {
141                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
142                            new UnsyncStringReader(data));
143    
144                    StringBundler sb = new StringBundler();
145    
146                    String line = null;
147    
148                    while ((line = unsyncBufferedReader.readLine()) != null) {
149                            if (line.startsWith(ALTER_COLUMN_NAME)) {
150                                    String[] template = buildColumnNameTokens(line);
151    
152                                    line = StringUtil.replace(
153                                            "alter table @table@ add column @new-column@ @type@;\n",
154                                            REWORD_TEMPLATE, template);
155    
156                                    line = line + StringUtil.replace(
157                                            "update @table@ set @new-column@ = @old-column@;\n",
158                                            REWORD_TEMPLATE, template);
159    
160                                    line = line + StringUtil.replace(
161                                            "alter table @table@ drop column @old-column@",
162                                            REWORD_TEMPLATE, template);
163                            }
164                            else if (line.startsWith(ALTER_TABLE_NAME)) {
165                                    String[] template = buildTableNameTokens(line);
166    
167                                    line = StringUtil.replace(
168                                            "alter table @old-table@ to @new-table@;",
169                                            RENAME_TABLE_TEMPLATE, template);
170                            }
171                            else if (line.contains(DROP_INDEX)) {
172                                    String[] tokens = StringUtil.split(line, ' ');
173    
174                                    line = StringUtil.replace(
175                                            "drop index @index@;", "@index@", tokens[2]);
176                            }
177    
178                            sb.append(line);
179                            sb.append("\n");
180                    }
181    
182                    unsyncBufferedReader.close();
183    
184                    return sb.toString();
185            }
186    
187            private void _reorgTables(String[] templates) throws SQLException {
188                    Set<String> tableNames = new HashSet<String>();
189    
190                    for (String template : templates) {
191                            if (template.startsWith("alter table")) {
192                                    tableNames.add(template.split(" ")[2]);
193                            }
194                    }
195    
196                    if (tableNames.size() == 0) {
197                            return;
198                    }
199    
200                    Connection con = null;
201                    CallableStatement callStmt = null;
202    
203                    try {
204                            con = DataAccess.getConnection();
205    
206                            for (String tableName : tableNames) {
207                                    String sql = "call sysproc.admin_cmd(?)";
208    
209                                    callStmt = con.prepareCall(sql);
210    
211                                    String param = "reorg table " + tableName;
212    
213                                    callStmt.setString(1, param);
214    
215                                    callStmt.execute();
216                            }
217                    }
218                    finally {
219                            DataAccess.cleanUp(con, callStmt);
220                    }
221            }
222    
223            private static final String[] _DB2 = {
224                    "--", "1", "0", "'1970-01-01-00.00.00.000000'", "current timestamp",
225                    " blob", " blob", " smallint", " timestamp", " double", " integer",
226                    " bigint", " varchar(750)", " clob", " varchar",
227                    " generated always as identity", "commit"
228            };
229    
230            private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;
231    
232            private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
233    
234            private static final boolean _SUPPORTS_SCROLLABLE_RESULTS = false;
235    
236            private static DB2DB _instance = new DB2DB();
237    
238    }