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