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