1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools.sql;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.util.FileUtil;
29  
30  import java.io.BufferedReader;
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.StringReader;
34  
35  /**
36   * <a href="InformixUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Neil Griffin
39   *
40   */
41  public class InformixUtil extends DBUtil {
42  
43      public static DBUtil getInstance() {
44          return _instance;
45      }
46  
47      public String buildSQL(String template) throws IOException {
48          template = convertTimestamp(template);
49          template = StringUtil.replace(template, TEMPLATE, getTemplate());
50  
51          template = reword(template);
52          template = removeNull(template);
53  
54          return template;
55      }
56  
57      protected InformixUtil() {
58      }
59  
60      protected void buildCreateFile(String databaseName, boolean minimal)
61          throws IOException {
62  
63          String minimalSuffix = getMinimalSuffix(minimal);
64  
65          File file = new File(
66              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
67                  "-informix.sql");
68  
69          StringMaker sm = new StringMaker();
70  
71          sm.append("database sysmaster;\n");
72          sm.append("drop database " + databaseName + ";\n");
73          sm.append("create database " + databaseName + " WITH LOG;\n");
74          sm.append("\n");
75          sm.append("create procedure 'lportal'.isnull(test_string varchar)\n");
76          sm.append("returning boolean;\n");
77          sm.append("IF test_string IS NULL THEN\n");
78          sm.append("    RETURN 't';\n");
79          sm.append("ELSE\n");
80          sm.append("    RETURN 'f';\n");
81          sm.append("END IF\n");
82          sm.append("end procedure;\n");
83          sm.append("\n\n");
84          sm.append(
85              FileUtil.read(
86                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
87                      "-informix.sql"));
88          sm.append("\n\n");
89          sm.append(FileUtil.read("../sql/indexes/indexes-informix.sql"));
90          sm.append("\n\n");
91          sm.append(FileUtil.read("../sql/sequences/sequences-informix.sql"));
92  
93          FileUtil.write(file, sm.toString());
94      }
95  
96      protected String getServerName() {
97          return "informix";
98      }
99  
100     protected String[] getTemplate() {
101         return _INFORMIX_TEMPLATE;
102     }
103 
104     protected String reword(String data) throws IOException {
105         BufferedReader br = new BufferedReader(new StringReader(data));
106 
107         StringMaker sm = new StringMaker();
108 
109         String line = null;
110 
111         boolean createTable = false;
112 
113         while ((line = br.readLine()) != null) {
114             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.startsWith(ALTER_COLUMN_NAME)) {
122                 String[] template = buildColumnNameTokens(line);
123 
124                 line = StringUtil.replace(
125                     "rename column @table@.@old-column@ TO @new-column@;",
126                     REWORD_TEMPLATE, template);
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             sm.append(line);
167             sm.append("\n");
168         }
169 
170         br.close();
171 
172         return sm.toString();
173     }
174 
175     private static String[] _INFORMIX_TEMPLATE = {
176         "--", "'T'", "'F'",
177         "'1970-01-01'", "CURRENT YEAR TO FRACTION",
178         " boolean", " datetime YEAR TO FRACTION", " float",
179         " int", " int8",
180         " lvarchar", " text", " varchar",
181         "", "commit"
182     };
183 
184     private static InformixUtil _instance = new InformixUtil();
185 
186 }