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