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="DerbyUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Alexander Chow
036: *
037: */
038: public class DerbyUtil 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 = _removeLongInserts(derby);
051: template = removeNull(template);
052: template = StringUtil.replace(template, "\\'", "''");
053:
054: return template;
055: }
056:
057: protected DerbyUtil() {
058: }
059:
060: protected void buildCreateFile(String databaseName, boolean minimal)
061: throws IOException {
062:
063: String minimalSuffix = getMinimalSuffix(minimal);
064:
065: File file = new File("../sql/create" + minimalSuffix
066: + "/create" + minimalSuffix + "-derby.sql");
067:
068: StringMaker sm = new StringMaker();
069:
070: sm.append("drop database " + databaseName + ";\n");
071: sm.append("create database " + databaseName + ";\n");
072: sm.append("connect to " + databaseName + ";\n");
073: sm.append(FileUtil.read("../sql/portal" + minimalSuffix
074: + "/portal" + minimalSuffix + "-derby.sql"));
075: sm.append("\n\n");
076: sm.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
077: sm.append("\n\n");
078: sm
079: .append(FileUtil
080: .read("../sql/sequences/sequences-derby.sql"));
081:
082: FileUtil.write(file, sm.toString());
083: }
084:
085: protected String getServerName() {
086: return "derby";
087: }
088:
089: protected String[] getTemplate() {
090: return _DERBY;
091: }
092:
093: protected String reword(String data) throws IOException {
094: BufferedReader br = new BufferedReader(new StringReader(data));
095:
096: StringMaker sm = new StringMaker();
097:
098: String line = null;
099:
100: while ((line = br.readLine()) != null) {
101: if (line.startsWith(ALTER_COLUMN_TYPE)
102: || line.startsWith(ALTER_COLUMN_NAME)) {
103:
104: line = "-- " + line;
105: }
106:
107: sm.append(line);
108: sm.append("\n");
109: }
110:
111: br.close();
112:
113: return sm.toString();
114: }
115:
116: private static String[] _DERBY = { "--", "1", "0",
117: "'1970-01-01-00.00.00.000000'", "current timestamp",
118: " smallint", " timestamp", " double", " integer",
119: " bigint", " long varchar", " clob", " varchar",
120: " generated always as identity", "commit" };
121:
122: private static DerbyUtil _instance = new DerbyUtil();
123:
124: }
|