1   /**
2    * Copyright (c) 2000-2009 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.FileUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.BufferedReader;
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.StringReader;
33  
34  /**
35   * <a href="InformixUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Neil Griffin
38   * @author Sandeep Soni
39   * @author Ganesh Ram
40   *
41   */
42  public class InformixUtil extends DBUtil {
43  
44      public static DBUtil getInstance() {
45          return _instance;
46      }
47  
48      public String buildSQL(String template) throws IOException {
49          template = convertTimestamp(template);
50          template = replaceTemplate(template, getTemplate());
51  
52          template = reword(template);
53          template = removeNull(template);
54  
55          return template;
56      }
57  
58      protected InformixUtil() {
59          super(TYPE_INFORMIX);
60      }
61  
62      protected void buildCreateFile(String databaseName, boolean minimal)
63          throws IOException {
64  
65          String minimalSuffix = getMinimalSuffix(minimal);
66  
67          File file = new File(
68              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
69                  "-informix.sql");
70  
71          StringBuilder sb = new StringBuilder();
72  
73          sb.append("database sysmaster;\n");
74          sb.append("drop database " + databaseName + ";\n");
75          sb.append("create database " + databaseName + " WITH LOG;\n");
76          sb.append("\n");
77          sb.append("create procedure 'lportal'.isnull(test_string varchar)\n");
78          sb.append("returning boolean;\n");
79          sb.append("IF test_string IS NULL THEN\n");
80          sb.append("\tRETURN 't';\n");
81          sb.append("ELSE\n");
82          sb.append("\tRETURN 'f';\n");
83          sb.append("END IF\n");
84          sb.append("end procedure;\n");
85          sb.append("\n\n");
86          sb.append(
87              FileUtil.read(
88                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
89                      "-informix.sql"));
90          sb.append("\n\n");
91          sb.append(FileUtil.read("../sql/indexes/indexes-informix.sql"));
92          sb.append("\n\n");
93          sb.append(FileUtil.read("../sql/sequences/sequences-informix.sql"));
94  
95          FileUtil.write(file, sb.toString());
96      }
97  
98      protected String getServerName() {
99          return "informix";
100     }
101 
102     protected String[] getTemplate() {
103         return _INFORMIX_TEMPLATE;
104     }
105 
106     protected String reword(String data) throws IOException {
107         BufferedReader br = new BufferedReader(new StringReader(data));
108 
109         StringBuilder sb = new StringBuilder();
110 
111         String line = null;
112 
113         boolean createTable = false;
114 
115         while ((line = br.readLine()) != null) {
116             if (line.startsWith(ALTER_COLUMN_NAME)) {
117                 String[] template = buildColumnNameTokens(line);
118 
119                 line = StringUtil.replace(
120                     "rename column @table@.@old-column@ TO @new-column@;",
121                     REWORD_TEMPLATE, template);
122             }
123             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
124                 String[] template = buildColumnTypeTokens(line);
125 
126                 line = StringUtil.replace(
127                     "alter table @table@ modify (@old-column@ @type@);",
128                     REWORD_TEMPLATE, template);
129             }
130             else if (line.indexOf("typeSettings text") > 0) {
131                 line = StringUtil.replace(
132                     line, "typeSettings text", "typeSettings lvarchar(4096)");
133             }
134             else if (line.indexOf("varchar(300)") > 0) {
135                 line = StringUtil.replace(
136                     line, "varchar(300)", "lvarchar(300)");
137             }
138             else if (line.indexOf("varchar(500)") > 0) {
139                 line = StringUtil.replace(
140                     line, "varchar(500)", "lvarchar(500)");
141             }
142             else if (line.indexOf("varchar(1000)") > 0) {
143                 line = StringUtil.replace(
144                     line, "varchar(1000)", "lvarchar(1000)");
145             }
146             else if (line.indexOf("varchar(1024)") > 0) {
147                 line = StringUtil.replace(
148                     line, "varchar(1024)", "lvarchar(1024)");
149             }
150             else if (line.indexOf("1970-01-01") > 0) {
151                 line = StringUtil.replace(
152                     line, "1970-01-01", "1970-01-01 00:00:00.0");
153             }
154             else if (line.indexOf("create table") >= 0) {
155                 createTable = true;
156             }
157             else if ((line.indexOf(");") >= 0) && createTable) {
158                 line = StringUtil.replace(
159                     line, ");",
160                     ")\nextent size 16 next size 16\nlock mode row;");
161 
162                 createTable = false;
163             }
164             else if (line.indexOf("commit;") >= 0) {
165                 line = StringPool.BLANK;
166             }
167 
168             sb.append(line);
169             sb.append("\n");
170         }
171 
172         br.close();
173 
174         return sb.toString();
175     }
176 
177     private static String[] _INFORMIX_TEMPLATE = {
178         "--", "'T'", "'F'",
179         "'1970-01-01'", "CURRENT YEAR TO FRACTION",
180         " byte in table", " boolean", " datetime YEAR TO FRACTION",
181         " float", " int", " int8",
182         " lvarchar", " text", " varchar",
183         "", "commit"
184     };
185 
186     private static InformixUtil _instance = new InformixUtil();
187 
188 }