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="PostgreSQLUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Alexander Chow
036: *
037: */
038: public class PostgreSQLUtil 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:
051: return template;
052: }
053:
054: protected PostgreSQLUtil() {
055: }
056:
057: protected void buildCreateFile(String databaseName, boolean minimal)
058: throws IOException {
059:
060: String minimalSuffix = getMinimalSuffix(minimal);
061:
062: File file = new File("../sql/create" + minimalSuffix
063: + "/create" + minimalSuffix + "-postgresql.sql");
064:
065: StringMaker sm = new StringMaker();
066:
067: sm.append("drop database " + databaseName + ";\n");
068: sm.append("create database " + databaseName
069: + " encoding = 'UNICODE';\n");
070: sm.append("\\c " + databaseName + ";\n\n");
071: sm.append(FileUtil.read("../sql/portal" + minimalSuffix
072: + "/portal" + minimalSuffix + "-postgresql.sql"));
073: sm.append("\n\n");
074: sm.append(FileUtil
075: .read("../sql/indexes/indexes-postgresql.sql"));
076: sm.append("\n\n");
077: sm.append(FileUtil
078: .read("../sql/sequences/sequences-postgresql.sql"));
079:
080: FileUtil.write(file, sm.toString());
081: }
082:
083: protected String getServerName() {
084: return "postgresql";
085: }
086:
087: protected String[] getTemplate() {
088: return _POSTGRESQL;
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
103: .replace(
104: "alter table @table@ alter @old-column@ type @type@;",
105: REWORD_TEMPLATE, template);
106: } else if (line.startsWith(ALTER_COLUMN_NAME)) {
107: String[] template = buildColumnNameTokens(line);
108:
109: line = StringUtil
110: .replace(
111: "alter table @table@ rename @old-column@ to @new-column@;",
112: REWORD_TEMPLATE, template);
113: } else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
114: String[] tokens = StringUtil.split(line, " ");
115:
116: line = StringUtil
117: .replace(
118: "alter table @table@ drop constraint @table@_pkey;",
119: "@table@", tokens[2]);
120: }
121:
122: sm.append(line);
123: sm.append("\n");
124: }
125:
126: br.close();
127:
128: return sm.toString();
129: }
130:
131: private static String[] _POSTGRESQL = { "--", "true", "false",
132: "'01/01/1970'", "current_timestamp", " bool", " timestamp",
133: " double precision", " integer", " bigint", " text",
134: " text", " varchar", "", "commit" };
135:
136: private static PostgreSQLUtil _instance = new PostgreSQLUtil();
137:
138: }
|