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.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    
079                    if (population != BARE) {
080                            sb.append(getCreateTablesContent(sqlDir, suffix));
081                            sb.append("\n\n");
082                            sb.append(readFile(sqlDir + "/indexes/indexes-informix.sql"));
083                            sb.append("\n\n");
084                            sb.append(readFile(sqlDir + "/sequences/sequences-informix.sql"));
085                    }
086    
087                    return sb.toString();
088            }
089    
090            @Override
091            protected String getServerName() {
092                    return "informix";
093            }
094    
095            @Override
096            protected String[] getTemplate() {
097                    return _INFORMIX_TEMPLATE;
098            }
099    
100            @Override
101            protected String reword(String data) throws IOException {
102                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
103                            new UnsyncStringReader(data));
104    
105                    StringBundler sb = new StringBundler();
106    
107                    String line = null;
108    
109                    boolean createTable = false;
110    
111                    while ((line = unsyncBufferedReader.readLine()) != null) {
112                            if (line.startsWith(ALTER_COLUMN_NAME)) {
113                                    String[] template = buildColumnNameTokens(line);
114    
115                                    line = StringUtil.replace(
116                                            "rename column @table@.@old-column@ TO @new-column@;",
117                                            REWORD_TEMPLATE, template);
118                            }
119                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
120                                    String[] template = buildColumnTypeTokens(line);
121    
122                                    line = StringUtil.replace(
123                                            "alter table @table@ modify (@old-column@ @type@);",
124                                            REWORD_TEMPLATE, template);
125                            }
126                            else if (line.startsWith(ALTER_TABLE_NAME)) {
127                                    String[] template = buildTableNameTokens(line);
128    
129                                    line = StringUtil.replace(
130                                            "rename table @old-table@ to @new-table@;",
131                                            RENAME_TABLE_TEMPLATE, template);
132                            }
133                            else if (line.contains(DROP_INDEX)) {
134                                    String[] tokens = StringUtil.split(line, ' ');
135    
136                                    line = StringUtil.replace(
137                                            "drop index @index@;", "@index@", tokens[2]);
138                            }
139                            else if (line.indexOf("typeSettings text") > 0) {
140                                    line = StringUtil.replace(
141                                            line, "typeSettings text", "typeSettings lvarchar(4096)");
142                            }
143                            else if (line.indexOf("varchar(300)") > 0) {
144                                    line = StringUtil.replace(
145                                            line, "varchar(300)", "lvarchar(300)");
146                            }
147                            else if (line.indexOf("varchar(500)") > 0) {
148                                    line = StringUtil.replace(
149                                            line, "varchar(500)", "lvarchar(500)");
150                            }
151                            else if (line.indexOf("varchar(1000)") > 0) {
152                                    line = StringUtil.replace(
153                                            line, "varchar(1000)", "lvarchar(1000)");
154                            }
155                            else if (line.indexOf("varchar(1024)") > 0) {
156                                    line = StringUtil.replace(
157                                            line, "varchar(1024)", "lvarchar(1024)");
158                            }
159                            else if (line.indexOf("1970-01-01") > 0) {
160                                    line = StringUtil.replace(
161                                            line, "1970-01-01", "1970-01-01 00:00:00.0");
162                            }
163                            else if (line.contains("create table")) {
164                                    createTable = true;
165                            }
166                            else if (line.contains(");") && createTable) {
167                                    line = StringUtil.replace(
168                                            line, ");",
169                                            ")\nextent size 16 next size 16\nlock mode row;");
170    
171                                    createTable = false;
172                            }
173                            else if (line.contains("commit;")) {
174                                    line = StringPool.BLANK;
175                            }
176    
177                            sb.append(line);
178                            sb.append("\n");
179                    }
180    
181                    unsyncBufferedReader.close();
182    
183                    return sb.toString();
184            }
185    
186            private static final String[] _INFORMIX_TEMPLATE = {
187                    "--", "'T'", "'F'", "'1970-01-01'", "CURRENT YEAR TO FRACTION", " blob",
188                    " blob", " boolean", " datetime YEAR TO FRACTION", " float", " int",
189                    " int8", " lvarchar", " text", " varchar", "", "commit"
190            };
191    
192            private static InformixDB _instance = new InformixDB();
193    
194    }