001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public String buildSQL(String template) throws IOException {
038                    template = convertTimestamp(template);
039                    template = replaceTemplate(template, getTemplate());
040    
041                    template = reword(template);
042                    template = removeNull(template);
043    
044                    return template;
045            }
046    
047            protected InformixDB() {
048                    super(TYPE_INFORMIX);
049            }
050    
051            protected String buildCreateFileContent(
052                            String sqlDir, String databaseName, int population)
053                    throws IOException {
054    
055                    String suffix = getSuffix(population);
056    
057                    StringBundler sb = new StringBundler(22);
058    
059                    sb.append("database sysmaster;\n");
060                    sb.append("drop database ");
061                    sb.append(databaseName);
062                    sb.append(";\n");
063                    sb.append("create database ");
064                    sb.append(databaseName);
065                    sb.append(" WITH LOG;\n");
066                    sb.append("\n");
067                    sb.append("create procedure 'lportal'.isnull(test_string varchar)\n");
068                    sb.append("returning boolean;\n");
069                    sb.append("IF test_string IS NULL THEN\n");
070                    sb.append("\tRETURN 't';\n");
071                    sb.append("ELSE\n");
072                    sb.append("\tRETURN 'f';\n");
073                    sb.append("END IF\n");
074                    sb.append("end procedure;\n");
075                    sb.append("\n\n");
076                    sb.append(
077                            readFile(
078                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
079                                            "-informix.sql"));
080                    sb.append("\n\n");
081                    sb.append(readFile(sqlDir + "/indexes/indexes-informix.sql"));
082                    sb.append("\n\n");
083                    sb.append(readFile(sqlDir + "/sequences/sequences-informix.sql"));
084    
085                    return sb.toString();
086            }
087    
088            protected String getServerName() {
089                    return "informix";
090            }
091    
092            protected String[] getTemplate() {
093                    return _INFORMIX_TEMPLATE;
094            }
095    
096            protected String reword(String data) throws IOException {
097                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
098                            new UnsyncStringReader(data));
099    
100                    StringBundler sb = new StringBundler();
101    
102                    String line = null;
103    
104                    boolean createTable = false;
105    
106                    while ((line = unsyncBufferedReader.readLine()) != null) {
107                            if (line.startsWith(ALTER_COLUMN_NAME)) {
108                                    String[] template = buildColumnNameTokens(line);
109    
110                                    line = StringUtil.replace(
111                                            "rename column @table@.@old-column@ TO @new-column@;",
112                                            REWORD_TEMPLATE, template);
113                            }
114                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
115                                    String[] template = buildColumnTypeTokens(line);
116    
117                                    line = StringUtil.replace(
118                                            "alter table @table@ modify (@old-column@ @type@);",
119                                            REWORD_TEMPLATE, template);
120                            }
121                            else if (line.indexOf(DROP_INDEX) != -1) {
122                                    String[] tokens = StringUtil.split(line, " ");
123    
124                                    line = StringUtil.replace(
125                                            "drop index @index@;", "@index@", tokens[2]);
126                            }
127                            else if (line.indexOf("typeSettings text") > 0) {
128                                    line = StringUtil.replace(
129                                            line, "typeSettings text", "typeSettings lvarchar(4096)");
130                            }
131                            else if (line.indexOf("varchar(300)") > 0) {
132                                    line = StringUtil.replace(
133                                            line, "varchar(300)", "lvarchar(300)");
134                            }
135                            else if (line.indexOf("varchar(500)") > 0) {
136                                    line = StringUtil.replace(
137                                            line, "varchar(500)", "lvarchar(500)");
138                            }
139                            else if (line.indexOf("varchar(1000)") > 0) {
140                                    line = StringUtil.replace(
141                                            line, "varchar(1000)", "lvarchar(1000)");
142                            }
143                            else if (line.indexOf("varchar(1024)") > 0) {
144                                    line = StringUtil.replace(
145                                            line, "varchar(1024)", "lvarchar(1024)");
146                            }
147                            else if (line.indexOf("1970-01-01") > 0) {
148                                    line = StringUtil.replace(
149                                            line, "1970-01-01", "1970-01-01 00:00:00.0");
150                            }
151                            else if (line.indexOf("create table") >= 0) {
152                                    createTable = true;
153                            }
154                            else if ((line.indexOf(");") >= 0) && createTable) {
155                                    line = StringUtil.replace(
156                                            line, ");",
157                                            ")\nextent size 16 next size 16\nlock mode row;");
158    
159                                    createTable = false;
160                            }
161                            else if (line.indexOf("commit;") >= 0) {
162                                    line = StringPool.BLANK;
163                            }
164    
165                            sb.append(line);
166                            sb.append("\n");
167                    }
168    
169                    unsyncBufferedReader.close();
170    
171                    return sb.toString();
172            }
173    
174            private static String[] _INFORMIX_TEMPLATE = {
175                    "--", "'T'", "'F'",
176                    "'1970-01-01'", "CURRENT YEAR TO FRACTION",
177                    " byte in table", " boolean", " datetime YEAR TO FRACTION",
178                    " float", " int", " int8",
179                    " lvarchar", " text", " varchar",
180                    "", "commit"
181            };
182    
183            private static InformixDB _instance = new InformixDB();
184    
185    }