001    /**
002     * Copyright (c) 2000-2012 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.io.unsync.UnsyncBufferedReader;
019    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.IOException;
025    
026    /**
027     * @author Neil Griffin
028     * @author Sandeep Soni
029     * @author Ganesh Ram
030     */
031    public class InformixDB extends BaseDB {
032    
033            public static DB getInstance() {
034                    return _instance;
035            }
036    
037            @Override
038            public String buildSQL(String template) throws IOException {
039                    template = convertTimestamp(template);
040                    template = replaceTemplate(template, getTemplate());
041    
042                    template = reword(template);
043                    template = removeNull(template);
044    
045                    return template;
046            }
047    
048            protected InformixDB() {
049                    super(TYPE_INFORMIX);
050            }
051    
052            @Override
053            protected String buildCreateFileContent(
054                            String sqlDir, String databaseName, int population)
055                    throws IOException {
056    
057                    String suffix = getSuffix(population);
058    
059                    StringBundler sb = new StringBundler(22);
060    
061                    sb.append("database sysmaster;\n");
062                    sb.append("drop database ");
063                    sb.append(databaseName);
064                    sb.append(";\n");
065                    sb.append("create database ");
066                    sb.append(databaseName);
067                    sb.append(" WITH LOG;\n");
068                    sb.append("\n");
069                    sb.append("create procedure 'lportal'.isnull(test_string varchar)\n");
070                    sb.append("returning boolean;\n");
071                    sb.append("IF test_string IS NULL THEN\n");
072                    sb.append("\tRETURN 't';\n");
073                    sb.append("ELSE\n");
074                    sb.append("\tRETURN 'f';\n");
075                    sb.append("END IF\n");
076                    sb.append("end procedure;\n");
077                    sb.append("\n\n");
078                    sb.append(getCreateTablesContent(sqlDir, suffix));
079                    sb.append("\n\n");
080                    sb.append(readFile(sqlDir + "/indexes/indexes-informix.sql"));
081                    sb.append("\n\n");
082                    sb.append(readFile(sqlDir + "/sequences/sequences-informix.sql"));
083    
084                    return sb.toString();
085            }
086    
087            @Override
088            protected String getServerName() {
089                    return "informix";
090            }
091    
092            @Override
093            protected String[] getTemplate() {
094                    return _INFORMIX_TEMPLATE;
095            }
096    
097            @Override
098            protected String reword(String data) throws IOException {
099                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
100                            new UnsyncStringReader(data));
101    
102                    StringBundler sb = new StringBundler();
103    
104                    String line = null;
105    
106                    boolean createTable = false;
107    
108                    while ((line = unsyncBufferedReader.readLine()) != null) {
109                            if (line.startsWith(ALTER_COLUMN_NAME)) {
110                                    String[] template = buildColumnNameTokens(line);
111    
112                                    line = StringUtil.replace(
113                                            "rename column @table@.@old-column@ TO @new-column@;",
114                                            REWORD_TEMPLATE, template);
115                            }
116                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
117                                    String[] template = buildColumnTypeTokens(line);
118    
119                                    line = StringUtil.replace(
120                                            "alter table @table@ modify (@old-column@ @type@);",
121                                            REWORD_TEMPLATE, template);
122                            }
123                            else if (line.startsWith(ALTER_TABLE_NAME)) {
124                                    String[] template = buildTableNameTokens(line);
125    
126                                    line = StringUtil.replace(
127                                            "rename table @old-table@ to @new-table@;",
128                                            RENAME_TABLE_TEMPLATE, template);
129                            }
130                            else if (line.contains(DROP_INDEX)) {
131                                    String[] tokens = StringUtil.split(line, ' ');
132    
133                                    line = StringUtil.replace(
134                                            "drop index @index@;", "@index@", tokens[2]);
135                            }
136                            else if (line.indexOf("typeSettings text") > 0) {
137                                    line = StringUtil.replace(
138                                            line, "typeSettings text", "typeSettings lvarchar(4096)");
139                            }
140                            else if (line.indexOf("varchar(300)") > 0) {
141                                    line = StringUtil.replace(
142                                            line, "varchar(300)", "lvarchar(300)");
143                            }
144                            else if (line.indexOf("varchar(500)") > 0) {
145                                    line = StringUtil.replace(
146                                            line, "varchar(500)", "lvarchar(500)");
147                            }
148                            else if (line.indexOf("varchar(1000)") > 0) {
149                                    line = StringUtil.replace(
150                                            line, "varchar(1000)", "lvarchar(1000)");
151                            }
152                            else if (line.indexOf("varchar(1024)") > 0) {
153                                    line = StringUtil.replace(
154                                            line, "varchar(1024)", "lvarchar(1024)");
155                            }
156                            else if (line.indexOf("1970-01-01") > 0) {
157                                    line = StringUtil.replace(
158                                            line, "1970-01-01", "1970-01-01 00:00:00.0");
159                            }
160                            else if (line.contains("create table")) {
161                                    createTable = true;
162                            }
163                            else if (line.contains(");") && createTable) {
164                                    line = StringUtil.replace(
165                                            line, ");",
166                                            ")\nextent size 16 next size 16\nlock mode row;");
167    
168                                    createTable = false;
169                            }
170                            else if (line.contains("commit;")) {
171                                    line = StringPool.BLANK;
172                            }
173    
174                            sb.append(line);
175                            sb.append("\n");
176                    }
177    
178                    unsyncBufferedReader.close();
179    
180                    return sb.toString();
181            }
182    
183            private static final String[] _INFORMIX_TEMPLATE = {
184                    "--", "'T'", "'F'", "'1970-01-01'", "CURRENT YEAR TO FRACTION", " blob",
185                    " blob", " boolean", " datetime YEAR TO FRACTION", " float", " int",
186                    " int8", " lvarchar", " text", " varchar", "", "commit"
187            };
188    
189            private static InformixDB _instance = new InformixDB();
190    
191    }