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