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.IOException;
31 import java.io.StringReader;
32
33
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 }