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