001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.db;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
019    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.IOException;
025    
026    /**
027     * @author Alexander Chow
028     * @author Bruno Farache
029     * @author Sandeep Soni
030     * @author Ganesh Ram
031     */
032    public class SybaseDB extends BaseDB {
033    
034            public static DB getInstance() {
035                    return _instance;
036            }
037    
038            public String buildSQL(String template) throws IOException {
039                    template = convertTimestamp(template);
040                    template = replaceTemplate(template, getTemplate());
041    
042                    template = reword(template);
043                    template = StringUtil.replace(template, ");\n", ")\ngo\n");
044                    template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
045                    template = StringUtil.replace(
046                            template,
047                            new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
048                            new String[] {"\\", "''", "\"", "\n", "\r"});
049    
050                    return template;
051            }
052    
053            public boolean isSupportsInlineDistinct() {
054                    return _SUPPORTS_INLINE_DISTINCT;
055            }
056    
057            protected SybaseDB() {
058                    super(TYPE_SYBASE);
059            }
060    
061            protected String buildCreateFileContent(
062                            String sqlDir, String databaseName, int population)
063                    throws IOException {
064    
065                    String suffix = getSuffix(population);
066    
067                    StringBundler sb = new StringBundler(19);
068    
069                    sb.append("use master\n");
070                    sb.append("exec sp_dboption '");
071                    sb.append(databaseName);
072                    sb.append("', ");
073                    sb.append("'allow nulls by default' , true\n");
074                    sb.append("go\n\n");
075                    sb.append("exec sp_dboption '");
076                    sb.append(databaseName);
077                    sb.append("', ");
078                    sb.append("'select into/bulkcopy/pllsort' , true\n");
079                    sb.append("go\n\n");
080    
081                    sb.append("use ");
082                    sb.append(databaseName);
083                    sb.append("\n\n");
084                    sb.append(
085                            readFile(
086                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
087                                            "-sybase.sql"));
088                    sb.append("\n\n");
089                    sb.append(readFile(sqlDir + "/indexes/indexes-sybase.sql"));
090                    sb.append("\n\n");
091                    sb.append(readFile(sqlDir + "/sequences/sequences-sybase.sql"));
092    
093                    return sb.toString();
094            }
095    
096            protected String getServerName() {
097                    return "sybase";
098            }
099    
100            protected String[] getTemplate() {
101                    return _SYBASE;
102            }
103    
104            protected String reword(String data) throws IOException {
105                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
106                            new UnsyncStringReader(data));
107    
108                    StringBundler sb = new StringBundler();
109    
110                    String line = null;
111    
112                    while ((line = unsyncBufferedReader.readLine()) != null) {
113                            if (line.indexOf(DROP_COLUMN) != -1) {
114                                    line = StringUtil.replace(line, " drop column ", " drop ");
115                            }
116    
117                            if (line.startsWith(ALTER_COLUMN_NAME)) {
118                                    String[] template = buildColumnNameTokens(line);
119    
120                                    line = StringUtil.replace(
121                                            "exec sp_rename '@table@.@old-column@', '@new-column@', " +
122                                                    "'column';",
123                                            REWORD_TEMPLATE, template);
124                            }
125                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
126                                    String[] template = buildColumnTypeTokens(line);
127    
128                                    line = StringUtil.replace(
129                                            "alter table @table@ alter column @old-column@ @type@;",
130                                            REWORD_TEMPLATE, template);
131                            }
132                            else if (line.indexOf(DROP_INDEX) != -1) {
133                                    String[] tokens = StringUtil.split(line, " ");
134    
135                                    String tableName = tokens[4];
136    
137                                    if (tableName.endsWith(StringPool.SEMICOLON)) {
138                                            tableName = tableName.substring(0, tableName.length() - 1);
139                                    }
140    
141                                    line = StringUtil.replace(
142                                            "drop index @table@.@index@;", "@table@", tableName);
143                                    line = StringUtil.replace(line, "@index@", tokens[2]);
144                            }
145    
146                            sb.append(line);
147                            sb.append("\n");
148                    }
149    
150                    unsyncBufferedReader.close();
151    
152                    return sb.toString();
153            }
154    
155            protected static String DROP_COLUMN = "drop column";
156    
157            private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
158    
159            private static String[] _SYBASE = {
160                    "--", "1", "0",
161                    "'19700101'", "getdate()",
162                    " image", " int", " datetime",
163                    " float", " int", " decimal(20,0)",
164                    " varchar(1000)", " text", " varchar",
165                    "  identity(1,1)", "go"
166            };
167    
168            private static SybaseDB _instance = new SybaseDB();
169    
170    }