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="SQLServerUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Alexander Chow
036: *
037: */
038: public class SQLServerUtil 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 = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
051: template = StringUtil.replace(template, new String[] { "\\\\",
052: "\\'", "\\\"", "\\n", "\\r" }, new String[] { "\\",
053: "''", "\"", "\n", "\r" });
054:
055: return template;
056: }
057:
058: protected SQLServerUtil() {
059: }
060:
061: protected void buildCreateFile(String databaseName, boolean minimal)
062: throws IOException {
063:
064: String minimalSuffix = getMinimalSuffix(minimal);
065:
066: File file = new File("../sql/create" + minimalSuffix
067: + "/create" + minimalSuffix + "-sql-server.sql");
068:
069: StringMaker sm = new StringMaker();
070:
071: sm.append("drop database " + databaseName + ";\n");
072: sm.append("create database " + databaseName + ";\n");
073: sm.append("\n");
074: sm.append("go\n");
075: sm.append("\n");
076: sm.append("use " + databaseName + ";\n\n");
077: sm.append(FileUtil.read("../sql/portal" + minimalSuffix
078: + "/portal" + minimalSuffix + "-sql-server.sql"));
079: sm.append("\n\n");
080: sm.append(FileUtil
081: .read("../sql/indexes/indexes-sql-server.sql"));
082: sm.append("\n\n");
083: sm.append(FileUtil
084: .read("../sql/sequences/sequences-sql-server.sql"));
085:
086: FileUtil.write(file, sm.toString());
087: }
088:
089: protected String getServerName() {
090: return "sql-server";
091: }
092:
093: protected String[] getTemplate() {
094: return _SQL_SERVER;
095: }
096:
097: protected String reword(String data) throws IOException {
098: BufferedReader br = new BufferedReader(new StringReader(data));
099:
100: StringMaker sm = new StringMaker();
101:
102: String line = null;
103:
104: while ((line = br.readLine()) != null) {
105: if (line.startsWith(ALTER_COLUMN_TYPE)) {
106: String[] template = buildColumnTypeTokens(line);
107:
108: line = StringUtil
109: .replace(
110: "alter table @table@ alter column @old-column@ @type@;",
111: REWORD_TEMPLATE, template);
112: } else if (line.startsWith(ALTER_COLUMN_NAME)) {
113: String[] template = buildColumnNameTokens(line);
114:
115: line = StringUtil.replace(
116: "exec sp_rename '@table@.@old-column@', '@new-column@', "
117: + "'column';", REWORD_TEMPLATE,
118: template);
119: }
120:
121: sm.append(line);
122: sm.append("\n");
123: }
124:
125: br.close();
126:
127: return sm.toString();
128: }
129:
130: private static String[] _SQL_SERVER = { "--", "1", "0",
131: "'19700101'", "GetDate()", " bit", " datetime", " float",
132: " int", " bigint", " varchar(2000)", " text", " varchar",
133: " identity(1,1)", "go" };
134:
135: private static SQLServerUtil _instance = new SQLServerUtil();
136:
137: }
|