1
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
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 }