001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
121                            readFile(
122                                    sqlDir + "/portal" + suffix + "/portal" + suffix + "-db2.sql"));
123                    sb.append("\n\n");
124                    sb.append(readFile(sqlDir + "/indexes/indexes-db2.sql"));
125                    sb.append("\n\n");
126                    sb.append(readFile(sqlDir + "/sequences/sequences-db2.sql"));
127    
128                    return sb.toString();
129            }
130    
131            @Override
132            protected String getServerName() {
133                    return "db2";
134            }
135    
136            @Override
137            protected String[] getTemplate() {
138                    return _DB2;
139            }
140    
141            @Override
142            protected String reword(String data) throws IOException {
143                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
144                            new UnsyncStringReader(data));
145    
146                    StringBundler sb = new StringBundler();
147    
148                    String line = null;
149    
150                    while ((line = unsyncBufferedReader.readLine()) != null) {
151                            if (line.startsWith(ALTER_COLUMN_NAME)) {
152                                    String[] template = buildColumnNameTokens(line);
153    
154                                    line = StringUtil.replace(
155                                            "alter table @table@ add column @new-column@ @type@;\n",
156                                            REWORD_TEMPLATE, template);
157    
158                                    line = line + StringUtil.replace(
159                                            "update @table@ set @new-column@ = @old-column@;\n",
160                                            REWORD_TEMPLATE, template);
161    
162                                    line = line + StringUtil.replace(
163                                            "alter table @table@ drop column @old-column@",
164                                            REWORD_TEMPLATE, template);
165                            }
166                            else if (line.indexOf(DROP_INDEX) != -1) {
167                                    String[] tokens = StringUtil.split(line, ' ');
168    
169                                    line = StringUtil.replace(
170                                            "drop index @index@;", "@index@", tokens[2]);
171                            }
172    
173                            sb.append(line);
174                            sb.append("\n");
175                    }
176    
177                    unsyncBufferedReader.close();
178    
179                    return sb.toString();
180            }
181    
182            private void _reorgTables(String[] templates) throws SQLException {
183                    Set<String> tableNames = new HashSet<String>();
184    
185                    for (String template : templates) {
186                            if (template.startsWith("alter table")) {
187                                    tableNames.add(template.split(" ")[2]);
188                            }
189                    }
190    
191                    if (tableNames.size() == 0) {
192                            return;
193                    }
194    
195                    Connection con = null;
196                    CallableStatement callStmt = null;
197    
198                    try {
199                            con = DataAccess.getConnection();
200    
201                            for (String tableName : tableNames) {
202                                    String sql = "call sysproc.admin_cmd(?)";
203    
204                                    callStmt = con.prepareCall(sql);
205    
206                                    String param = "reorg table " + tableName;
207    
208                                    callStmt.setString(1, param);
209    
210                                    callStmt.execute();
211                            }
212                    }
213                    finally {
214                            DataAccess.cleanUp(con, callStmt);
215                    }
216            }
217    
218            private static final String[] _DB2 = {
219                    "--", "1", "0", "'1970-01-01-00.00.00.000000'", "current timestamp",
220                    " blob", " blob", " smallint", " timestamp", " double", " integer",
221                    " bigint", " varchar(750)", " clob", " varchar",
222                    " generated always as identity", "commit"
223            };
224    
225            private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;
226    
227            private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
228    
229            private static final boolean _SUPPORTS_SCROLLABLE_RESULTS = false;
230    
231            private static DB2DB _instance = new DB2DB();
232    
233    }