001    /**
002     * Copyright (c) 2000-present 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                    try (UnsyncBufferedReader unsyncBufferedReader =
103                                    new UnsyncBufferedReader(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",
142                                                    "typeSettings lvarchar(4096)");
143                                    }
144                                    else if (line.indexOf("varchar(300)") > 0) {
145                                            line = StringUtil.replace(
146                                                    line, "varchar(300)", "lvarchar(300)");
147                                    }
148                                    else if (line.indexOf("varchar(500)") > 0) {
149                                            line = StringUtil.replace(
150                                                    line, "varchar(500)", "lvarchar(500)");
151                                    }
152                                    else if (line.indexOf("varchar(1000)") > 0) {
153                                            line = StringUtil.replace(
154                                                    line, "varchar(1000)", "lvarchar(1000)");
155                                    }
156                                    else if (line.indexOf("varchar(1024)") > 0) {
157                                            line = StringUtil.replace(
158                                                    line, "varchar(1024)", "lvarchar(1024)");
159                                    }
160                                    else if (line.indexOf("1970-01-01") > 0) {
161                                            line = StringUtil.replace(
162                                                    line, "1970-01-01", "1970-01-01 00:00:00.0");
163                                    }
164                                    else if (line.contains("create table")) {
165                                            createTable = true;
166                                    }
167                                    else if (line.contains(");") && createTable) {
168                                            line = StringUtil.replace(
169                                                    line, ");",
170                                                    ")\nextent size 16 next size 16\nlock mode row;");
171    
172                                            createTable = false;
173                                    }
174                                    else if (line.contains("commit;")) {
175                                            line = StringPool.BLANK;
176                                    }
177    
178                                    sb.append(line);
179                                    sb.append("\n");
180                            }
181    
182                            return sb.toString();
183                    }
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 final InformixDB _instance = new InformixDB();
193    
194    }