1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  
24  import java.io.IOException;
25  
26  /**
27   * <a href="InformixDB.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Neil Griffin
30   * @author Sandeep Soni
31   * @author Ganesh Ram
32   */
33  public class InformixDB extends BaseDB {
34  
35      public static DB getInstance() {
36          return _instance;
37      }
38  
39      public String buildSQL(String template) throws IOException {
40          template = convertTimestamp(template);
41          template = replaceTemplate(template, getTemplate());
42  
43          template = reword(template);
44          template = removeNull(template);
45  
46          return template;
47      }
48  
49      protected InformixDB() {
50          super(TYPE_INFORMIX);
51      }
52  
53      protected String buildCreateFileContent(
54              String sqlDir, String databaseName, int population)
55          throws IOException {
56  
57          String suffix = getSuffix(population);
58  
59          StringBundler sb = new StringBundler(22);
60  
61          sb.append("database sysmaster;\n");
62          sb.append("drop database ");
63          sb.append(databaseName);
64          sb.append(";\n");
65          sb.append("create database ");
66          sb.append(databaseName);
67          sb.append(" WITH LOG;\n");
68          sb.append("\n");
69          sb.append("create procedure 'lportal'.isnull(test_string varchar)\n");
70          sb.append("returning boolean;\n");
71          sb.append("IF test_string IS NULL THEN\n");
72          sb.append("\tRETURN 't';\n");
73          sb.append("ELSE\n");
74          sb.append("\tRETURN 'f';\n");
75          sb.append("END IF\n");
76          sb.append("end procedure;\n");
77          sb.append("\n\n");
78          sb.append(
79              readFile(
80                  sqlDir + "/portal" + suffix + "/portal" + suffix +
81                      "-informix.sql"));
82          sb.append("\n\n");
83          sb.append(readFile(sqlDir + "/indexes/indexes-informix.sql"));
84          sb.append("\n\n");
85          sb.append(readFile(sqlDir + "/sequences/sequences-informix.sql"));
86  
87          return sb.toString();
88      }
89  
90      protected String getServerName() {
91          return "informix";
92      }
93  
94      protected String[] getTemplate() {
95          return _INFORMIX_TEMPLATE;
96      }
97  
98      protected String reword(String data) throws IOException {
99          UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
100             new UnsyncStringReader(data));
101 
102         StringBundler sb = new StringBundler();
103 
104         String line = null;
105 
106         boolean createTable = false;
107 
108         while ((line = unsyncBufferedReader.readLine()) != null) {
109             if (line.startsWith(ALTER_COLUMN_NAME)) {
110                 String[] template = buildColumnNameTokens(line);
111 
112                 line = StringUtil.replace(
113                     "rename column @table@.@old-column@ TO @new-column@;",
114                     REWORD_TEMPLATE, template);
115             }
116             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
117                 String[] template = buildColumnTypeTokens(line);
118 
119                 line = StringUtil.replace(
120                     "alter table @table@ modify (@old-column@ @type@);",
121                     REWORD_TEMPLATE, template);
122             }
123             else if (line.indexOf(DROP_INDEX) != -1) {
124                 String[] tokens = StringUtil.split(line, " ");
125 
126                 line = StringUtil.replace(
127                     "drop index @index@;", "@index@", tokens[2]);
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         unsyncBufferedReader.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 InformixDB _instance = new InformixDB();
186 
187 }