1   /**
2    * Copyright (c) 2000-2008 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      }
60  
61      protected void buildCreateFile(String databaseName, boolean minimal)
62          throws IOException {
63  
64          String minimalSuffix = getMinimalSuffix(minimal);
65  
66          File file = new File(
67              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
68                  "-informix.sql");
69  
70          StringBuilder sb = new StringBuilder();
71  
72          sb.append("database sysmaster;\n");
73          sb.append("drop database " + databaseName + ";\n");
74          sb.append("create database " + databaseName + " WITH LOG;\n");
75          sb.append("\n");
76          sb.append("create procedure 'lportal'.isnull(test_string varchar)\n");
77          sb.append("returning boolean;\n");
78          sb.append("IF test_string IS NULL THEN\n");
79          sb.append("\tRETURN 't';\n");
80          sb.append("ELSE\n");
81          sb.append("\tRETURN 'f';\n");
82          sb.append("END IF\n");
83          sb.append("end procedure;\n");
84          sb.append("\n\n");
85          sb.append(
86              FileUtil.read(
87                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
88                      "-informix.sql"));
89          sb.append("\n\n");
90          sb.append(FileUtil.read("../sql/indexes/indexes-informix.sql"));
91          sb.append("\n\n");
92          sb.append(FileUtil.read("../sql/sequences/sequences-informix.sql"));
93  
94          FileUtil.write(file, sb.toString());
95      }
96  
97      protected String getServerName() {
98          return "informix";
99      }
100 
101     protected String[] getTemplate() {
102         return _INFORMIX_TEMPLATE;
103     }
104 
105     protected String reword(String data) throws IOException {
106         BufferedReader br = new BufferedReader(new StringReader(data));
107 
108         StringBuilder sb = new StringBuilder();
109 
110         String line = null;
111 
112         boolean createTable = false;
113 
114         while ((line = br.readLine()) != null) {
115             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.startsWith(ALTER_COLUMN_NAME)) {
123                 String[] template = buildColumnNameTokens(line);
124 
125                 line = StringUtil.replace(
126                     "rename column @table@.@old-column@ TO @new-column@;",
127                     REWORD_TEMPLATE, template);
128             }
129             else if (line.indexOf("typeSettings text") > 0) {
130                 line = StringUtil.replace(
131                     line, "typeSettings text", "typeSettings lvarchar(4096)");
132             }
133             else if (line.indexOf("varchar(300)") > 0) {
134                 line = StringUtil.replace(
135                     line, "varchar(300)", "lvarchar(300)");
136             }
137             else if (line.indexOf("varchar(500)") > 0) {
138                 line = StringUtil.replace(
139                     line, "varchar(500)", "lvarchar(500)");
140             }
141             else if (line.indexOf("varchar(1000)") > 0) {
142                 line = StringUtil.replace(
143                     line, "varchar(1000)", "lvarchar(1000)");
144             }
145             else if (line.indexOf("varchar(1024)") > 0) {
146                 line = StringUtil.replace(
147                     line, "varchar(1024)", "lvarchar(1024)");
148             }
149             else if (line.indexOf("1970-01-01") > 0) {
150                 line = StringUtil.replace(
151                     line, "1970-01-01", "1970-01-01 00:00:00.0");
152             }
153             else if (line.indexOf("create table") >= 0) {
154                 createTable = true;
155             }
156             else if ((line.indexOf(");") >= 0) && createTable) {
157                 line = StringUtil.replace(
158                     line, ");",
159                     ")\nextent size 16 next size 16\nlock mode row;");
160 
161                 createTable = false;
162             }
163             else if (line.indexOf("commit;") >= 0) {
164                 line = StringPool.BLANK;
165             }
166 
167             sb.append(line);
168             sb.append("\n");
169         }
170 
171         br.close();
172 
173         return sb.toString();
174     }
175 
176     private static String[] _INFORMIX_TEMPLATE = {
177         "--", "'T'", "'F'",
178         "'1970-01-01'", "CURRENT YEAR TO FRACTION",
179         " byte in table", " boolean", " datetime YEAR TO FRACTION",
180         " float", " int", " int8",
181         " lvarchar", " text", " varchar",
182         "", "commit"
183     };
184 
185     private static InformixUtil _instance = new InformixUtil();
186 
187 }