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