1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.db;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  
25  import java.io.IOException;
26  
27  /**
28   * <a href="InformixDB.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Neil Griffin
31   * @author Sandeep Soni
32   * @author Ganesh Ram
33   */
34  public class InformixDB extends BaseDB {
35  
36      public static DB getInstance() {
37          return _instance;
38      }
39  
40      public String buildSQL(String template) throws IOException {
41          template = convertTimestamp(template);
42          template = replaceTemplate(template, getTemplate());
43  
44          template = reword(template);
45          template = removeNull(template);
46  
47          return template;
48      }
49  
50      protected InformixDB() {
51          super(TYPE_INFORMIX);
52      }
53  
54      protected String buildCreateFileContent(
55              String sqlDir, String databaseName, int population)
56          throws IOException {
57  
58          String suffix = getSuffix(population);
59  
60          StringBundler sb = new StringBundler(22);
61  
62          sb.append("database sysmaster;\n");
63          sb.append("drop database ");
64          sb.append(databaseName);
65          sb.append(";\n");
66          sb.append("create database ");
67          sb.append(databaseName);
68          sb.append(" WITH LOG;\n");
69          sb.append("\n");
70          sb.append("create procedure 'lportal'.isnull(test_string varchar)\n");
71          sb.append("returning boolean;\n");
72          sb.append("IF test_string IS NULL THEN\n");
73          sb.append("\tRETURN 't';\n");
74          sb.append("ELSE\n");
75          sb.append("\tRETURN 'f';\n");
76          sb.append("END IF\n");
77          sb.append("end procedure;\n");
78          sb.append("\n\n");
79          sb.append(
80              FileUtil.read(
81                  sqlDir + "/portal" + suffix + "/portal" + suffix +
82                      "-informix.sql"));
83          sb.append("\n\n");
84          sb.append(FileUtil.read(sqlDir + "/indexes/indexes-informix.sql"));
85          sb.append("\n\n");
86          sb.append(FileUtil.read(sqlDir + "/sequences/sequences-informix.sql"));
87  
88          return sb.toString();
89      }
90  
91      protected String getServerName() {
92          return "informix";
93      }
94  
95      protected String[] getTemplate() {
96          return _INFORMIX_TEMPLATE;
97      }
98  
99      protected String reword(String data) throws IOException {
100         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
101             new UnsyncStringReader(data));
102 
103         StringBundler sb = new StringBundler();
104 
105         String line = null;
106 
107         boolean createTable = false;
108 
109         while ((line = unsyncBufferedReader.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(DROP_INDEX) != -1) {
125                 String[] tokens = StringUtil.split(line, " ");
126 
127                 line = StringUtil.replace(
128                     "drop index @index@;", "@index@", tokens[2]);
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         unsyncBufferedReader.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 InformixDB _instance = new InformixDB();
187 
188 }