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(
079                            readFile(
080                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
081                                            "-informix.sql"));
082                    sb.append("\n\n");
083                    sb.append(readFile(sqlDir + "/indexes/indexes-informix.sql"));
084                    sb.append("\n\n");
085                    sb.append(readFile(sqlDir + "/sequences/sequences-informix.sql"));
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.indexOf(DROP_INDEX) != -1) {
127                                    String[] tokens = StringUtil.split(line, ' ');
128    
129                                    line = StringUtil.replace(
130                                            "drop index @index@;", "@index@", tokens[2]);
131                            }
132                            else if (line.indexOf("typeSettings text") > 0) {
133                                    line = StringUtil.replace(
134                                            line, "typeSettings text", "typeSettings lvarchar(4096)");
135                            }
136                            else if (line.indexOf("varchar(300)") > 0) {
137                                    line = StringUtil.replace(
138                                            line, "varchar(300)", "lvarchar(300)");
139                            }
140                            else if (line.indexOf("varchar(500)") > 0) {
141                                    line = StringUtil.replace(
142                                            line, "varchar(500)", "lvarchar(500)");
143                            }
144                            else if (line.indexOf("varchar(1000)") > 0) {
145                                    line = StringUtil.replace(
146                                            line, "varchar(1000)", "lvarchar(1000)");
147                            }
148                            else if (line.indexOf("varchar(1024)") > 0) {
149                                    line = StringUtil.replace(
150                                            line, "varchar(1024)", "lvarchar(1024)");
151                            }
152                            else if (line.indexOf("1970-01-01") > 0) {
153                                    line = StringUtil.replace(
154                                            line, "1970-01-01", "1970-01-01 00:00:00.0");
155                            }
156                            else if (line.indexOf("create table") >= 0) {
157                                    createTable = true;
158                            }
159                            else if ((line.indexOf(");") >= 0) && createTable) {
160                                    line = StringUtil.replace(
161                                            line, ");",
162                                            ")\nextent size 16 next size 16\nlock mode row;");
163    
164                                    createTable = false;
165                            }
166                            else if (line.indexOf("commit;") >= 0) {
167                                    line = StringPool.BLANK;
168                            }
169    
170                            sb.append(line);
171                            sb.append("\n");
172                    }
173    
174                    unsyncBufferedReader.close();
175    
176                    return sb.toString();
177            }
178    
179            private static final String[] _INFORMIX_TEMPLATE = {
180                    "--", "'T'", "'F'", "'1970-01-01'", "CURRENT YEAR TO FRACTION", " blob",
181                    " blob", " boolean", " datetime YEAR TO FRACTION", " float", " int",
182                    " int8", " lvarchar", " text", " varchar", "", "commit"
183            };
184    
185            private static InformixDB _instance = new InformixDB();
186    
187    }