1
22
23 package com.liferay.portal.dao.db;
24
25 import com.liferay.portal.kernel.dao.db.DB;
26 import com.liferay.portal.kernel.util.FileUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.StringReader;
33
34
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 BufferedReader br = new BufferedReader(new StringReader(data));
103
104 StringBuilder sb = new StringBuilder();
105
106 String line = null;
107
108 boolean createTable = false;
109
110 while ((line = br.readLine()) != null) {
111 if (line.startsWith(ALTER_COLUMN_NAME)) {
112 String[] template = buildColumnNameTokens(line);
113
114 line = StringUtil.replace(
115 "rename column @table@.@old-column@ TO @new-column@;",
116 REWORD_TEMPLATE, template);
117 }
118 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
119 String[] template = buildColumnTypeTokens(line);
120
121 line = StringUtil.replace(
122 "alter table @table@ modify (@old-column@ @type@);",
123 REWORD_TEMPLATE, template);
124 }
125 else if (line.indexOf("typeSettings text") > 0) {
126 line = StringUtil.replace(
127 line, "typeSettings text", "typeSettings lvarchar(4096)");
128 }
129 else if (line.indexOf("varchar(300)") > 0) {
130 line = StringUtil.replace(
131 line, "varchar(300)", "lvarchar(300)");
132 }
133 else if (line.indexOf("varchar(500)") > 0) {
134 line = StringUtil.replace(
135 line, "varchar(500)", "lvarchar(500)");
136 }
137 else if (line.indexOf("varchar(1000)") > 0) {
138 line = StringUtil.replace(
139 line, "varchar(1000)", "lvarchar(1000)");
140 }
141 else if (line.indexOf("varchar(1024)") > 0) {
142 line = StringUtil.replace(
143 line, "varchar(1024)", "lvarchar(1024)");
144 }
145 else if (line.indexOf("1970-01-01") > 0) {
146 line = StringUtil.replace(
147 line, "1970-01-01", "1970-01-01 00:00:00.0");
148 }
149 else if (line.indexOf("create table") >= 0) {
150 createTable = true;
151 }
152 else if ((line.indexOf(");") >= 0) && createTable) {
153 line = StringUtil.replace(
154 line, ");",
155 ")\nextent size 16 next size 16\nlock mode row;");
156
157 createTable = false;
158 }
159 else if (line.indexOf("commit;") >= 0) {
160 line = StringPool.BLANK;
161 }
162
163 sb.append(line);
164 sb.append("\n");
165 }
166
167 br.close();
168
169 return sb.toString();
170 }
171
172 private static String[] _INFORMIX_TEMPLATE = {
173 "--", "'T'", "'F'",
174 "'1970-01-01'", "CURRENT YEAR TO FRACTION",
175 " byte in table", " boolean", " datetime YEAR TO FRACTION",
176 " float", " int", " int8",
177 " lvarchar", " text", " varchar",
178 "", "commit"
179 };
180
181 private static InformixDB _instance = new InformixDB();
182
183 }