001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools.sql;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.util.FileUtil;
027:
028: import java.io.BufferedReader;
029: import java.io.File;
030: import java.io.IOException;
031: import java.io.StringReader;
032:
033: /**
034: * <a href="InformixUtil.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Neil Griffin
037: *
038: */
039: public class InformixUtil extends DBUtil {
040:
041: public static DBUtil getInstance() {
042: return _instance;
043: }
044:
045: public String buildSQL(String template) throws IOException {
046: template = convertTimestamp(template);
047: template = StringUtil
048: .replace(template, TEMPLATE, getTemplate());
049:
050: template = reword(template);
051: template = removeNull(template);
052:
053: return template;
054: }
055:
056: protected InformixUtil() {
057: }
058:
059: protected void buildCreateFile(String databaseName, boolean minimal)
060: throws IOException {
061:
062: String minimalSuffix = getMinimalSuffix(minimal);
063:
064: File file = new File("../sql/create" + minimalSuffix
065: + "/create" + minimalSuffix + "-informix.sql");
066:
067: StringMaker sm = new StringMaker();
068:
069: sm.append("database sysmaster;\n");
070: sm.append("drop database " + databaseName + ";\n");
071: sm.append("create database " + databaseName + " WITH LOG;\n");
072: sm.append("\n");
073: sm
074: .append("create procedure 'lportal'.isnull(test_string varchar)\n");
075: sm.append("returning boolean;\n");
076: sm.append("IF test_string IS NULL THEN\n");
077: sm.append(" RETURN 't';\n");
078: sm.append("ELSE\n");
079: sm.append(" RETURN 'f';\n");
080: sm.append("END IF\n");
081: sm.append("end procedure;\n");
082: sm.append("\n\n");
083: sm.append(FileUtil.read("../sql/portal" + minimalSuffix
084: + "/portal" + minimalSuffix + "-informix.sql"));
085: sm.append("\n\n");
086: sm.append(FileUtil.read("../sql/indexes/indexes-informix.sql"));
087: sm.append("\n\n");
088: sm.append(FileUtil
089: .read("../sql/sequences/sequences-informix.sql"));
090:
091: FileUtil.write(file, sm.toString());
092: }
093:
094: protected String getServerName() {
095: return "informix";
096: }
097:
098: protected String[] getTemplate() {
099: return _INFORMIX_TEMPLATE;
100: }
101:
102: protected String reword(String data) throws IOException {
103: BufferedReader br = new BufferedReader(new StringReader(data));
104:
105: StringMaker sm = new StringMaker();
106:
107: String line = null;
108:
109: boolean createTable = false;
110:
111: while ((line = br.readLine()) != null) {
112: if (line.startsWith(ALTER_COLUMN_TYPE)) {
113: String[] template = buildColumnTypeTokens(line);
114:
115: line = StringUtil
116: .replace(
117: "alter table @table@ modify (@old-column@ @type@);",
118: REWORD_TEMPLATE, template);
119: } else if (line.startsWith(ALTER_COLUMN_NAME)) {
120: String[] template = buildColumnNameTokens(line);
121:
122: line = StringUtil
123: .replace(
124: "rename column @table@.@old-column@ TO @new-column@;",
125: REWORD_TEMPLATE, template);
126: } else if (line.indexOf("typeSettings text") > 0) {
127: line = StringUtil.replace(line, "typeSettings text",
128: "typeSettings lvarchar(4096)");
129: } else if (line.indexOf("varchar(300)") > 0) {
130: line = StringUtil.replace(line, "varchar(300)",
131: "lvarchar(300)");
132: } else if (line.indexOf("varchar(500)") > 0) {
133: line = StringUtil.replace(line, "varchar(500)",
134: "lvarchar(500)");
135: } else if (line.indexOf("varchar(1000)") > 0) {
136: line = StringUtil.replace(line, "varchar(1000)",
137: "lvarchar(1000)");
138: } else if (line.indexOf("varchar(1024)") > 0) {
139: line = StringUtil.replace(line, "varchar(1024)",
140: "lvarchar(1024)");
141: } else if (line.indexOf("1970-01-01") > 0) {
142: line = StringUtil.replace(line, "1970-01-01",
143: "1970-01-01 00:00:00.0");
144: } else if (line.indexOf("create table") >= 0) {
145: createTable = true;
146: } else if ((line.indexOf(");") >= 0) && createTable) {
147: line = StringUtil
148: .replace(line, ");",
149: ")\nextent size 16 next size 16\nlock mode row;");
150:
151: createTable = false;
152: } else if (line.indexOf("commit;") >= 0) {
153: line = StringPool.BLANK;
154: }
155:
156: sm.append(line);
157: sm.append("\n");
158: }
159:
160: br.close();
161:
162: return sm.toString();
163: }
164:
165: private static String[] _INFORMIX_TEMPLATE = { "--", "'T'", "'F'",
166: "'1970-01-01'", "CURRENT YEAR TO FRACTION", " boolean",
167: " datetime YEAR TO FRACTION", " float", " int", " int8",
168: " lvarchar", " text", " varchar", "", "commit" };
169:
170: private static InformixUtil _instance = new InformixUtil();
171:
172: }
|