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.StringUtil;
025: import com.liferay.util.FileUtil;
026:
027: import java.io.BufferedReader;
028: import java.io.File;
029: import java.io.IOException;
030: import java.io.StringReader;
031:
032: /**
033: * <a href="FirebirdUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Alexander Chow
036: *
037: */
038: public class FirebirdUtil extends DBUtil {
039:
040: public static DBUtil getInstance() {
041: return _instance;
042: }
043:
044: public String buildSQL(String template) throws IOException {
045: template = convertTimestamp(template);
046: template = StringUtil
047: .replace(template, TEMPLATE, getTemplate());
048:
049: template = reword(template);
050: template = removeInserts(template);
051: template = removeNull(template);
052:
053: return template;
054: }
055:
056: protected FirebirdUtil() {
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 + "-firebird.sql");
066:
067: StringMaker sm = new StringMaker();
068:
069: sm
070: .append("create database '"
071: + databaseName
072: + ".gdb' page_size 8192 user 'sysdba' password 'masterkey';\n");
073: sm.append("connect '" + databaseName
074: + ".gdb' user 'sysdba' password 'masterkey';\n");
075: sm
076: .append(readSQL("../sql/portal" + minimalSuffix
077: + "/portal" + minimalSuffix + "-firebird.sql",
078: _FIREBIRD[0], ";\n"));
079:
080: FileUtil.write(file, sm.toString());
081: }
082:
083: protected String getServerName() {
084: return "firebird";
085: }
086:
087: protected String[] getTemplate() {
088: return _FIREBIRD;
089: }
090:
091: protected String reword(String data) throws IOException {
092: BufferedReader br = new BufferedReader(new StringReader(data));
093:
094: StringMaker sm = new StringMaker();
095:
096: String line = null;
097:
098: while ((line = br.readLine()) != null) {
099: if (line.startsWith(ALTER_COLUMN_TYPE)) {
100: String[] template = buildColumnTypeTokens(line);
101:
102: line = StringUtil.replace(
103: "alter table @table@ alter column \"@old-column@\" "
104: + "type @type@;", REWORD_TEMPLATE,
105: template);
106: } else if (line.startsWith(ALTER_COLUMN_NAME)) {
107: String[] template = buildColumnNameTokens(line);
108:
109: line = StringUtil.replace(
110: "alter table @table@ alter column \"@old-column@\" to "
111: + "\"@new-column@\";", REWORD_TEMPLATE,
112: template);
113: }
114:
115: sm.append(line);
116: sm.append("\n");
117: }
118:
119: br.close();
120:
121: return sm.toString();
122: }
123:
124: private static String[] _FIREBIRD = { "--", "1", "0",
125: "'01/01/1970'", "current_timestamp", " smallint",
126: " timestamp", " double precision", " integer", " int64",
127: " varchar(4000)", " blob", " varchar", "", "commit" };
128:
129: private static FirebirdUtil _instance = new FirebirdUtil();
130:
131: }
|