001    /**
002     * Copyright (c) 2000-2012 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            @Override
039            public String buildSQL(String template) throws IOException {
040                    template = convertTimestamp(template);
041                    template = replaceTemplate(template, getTemplate());
042    
043                    template = reword(template);
044                    template = StringUtil.replace(template, ");\n", ")\ngo\n");
045                    template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
046                    template = StringUtil.replace(
047                            template,
048                            new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
049                            new String[] {"\\", "''", "\"", "\n", "\r"});
050    
051                    return template;
052            }
053    
054            @Override
055            public boolean isSupportsInlineDistinct() {
056                    return _SUPPORTS_INLINE_DISTINCT;
057            }
058    
059            protected SybaseDB() {
060                    super(TYPE_SYBASE);
061            }
062    
063            @Override
064            protected String buildCreateFileContent(
065                            String sqlDir, String databaseName, int population)
066                    throws IOException {
067    
068                    String suffix = getSuffix(population);
069    
070                    StringBundler sb = new StringBundler(19);
071    
072                    sb.append("use master\n");
073                    sb.append("exec sp_dboption '");
074                    sb.append(databaseName);
075                    sb.append("', ");
076                    sb.append("'allow nulls by default' , true\n");
077                    sb.append("go\n\n");
078                    sb.append("exec sp_dboption '");
079                    sb.append(databaseName);
080                    sb.append("', ");
081                    sb.append("'select into/bulkcopy/pllsort' , true\n");
082                    sb.append("go\n\n");
083    
084                    sb.append("use ");
085                    sb.append(databaseName);
086                    sb.append("\n\n");
087                    sb.append(
088                            readFile(
089                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
090                                            "-sybase.sql"));
091                    sb.append("\n\n");
092                    sb.append(readFile(sqlDir + "/indexes/indexes-sybase.sql"));
093                    sb.append("\n\n");
094                    sb.append(readFile(sqlDir + "/sequences/sequences-sybase.sql"));
095    
096                    return sb.toString();
097            }
098    
099            @Override
100            protected String getServerName() {
101                    return "sybase";
102            }
103    
104            @Override
105            protected String[] getTemplate() {
106                    return _SYBASE;
107            }
108    
109            @Override
110            protected String reword(String data) throws IOException {
111                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
112                            new UnsyncStringReader(data));
113    
114                    StringBundler sb = new StringBundler();
115    
116                    String line = null;
117    
118                    while ((line = unsyncBufferedReader.readLine()) != null) {
119                            if (line.indexOf(DROP_COLUMN) != -1) {
120                                    line = StringUtil.replace(line, " drop column ", " drop ");
121                            }
122    
123                            if (line.startsWith(ALTER_COLUMN_NAME)) {
124                                    String[] template = buildColumnNameTokens(line);
125    
126                                    line = StringUtil.replace(
127                                            "exec sp_rename '@table@.@old-column@', '@new-column@', " +
128                                                    "'column';",
129                                            REWORD_TEMPLATE, template);
130                            }
131                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
132                                    String[] template = buildColumnTypeTokens(line);
133    
134                                    line = StringUtil.replace(
135                                            "alter table @table@ modify @old-column@ @type@;",
136                                            REWORD_TEMPLATE, template);
137                            }
138                            else if (line.indexOf(DROP_INDEX) != -1) {
139                                    String[] tokens = StringUtil.split(line, ' ');
140    
141                                    String tableName = tokens[4];
142    
143                                    if (tableName.endsWith(StringPool.SEMICOLON)) {
144                                            tableName = tableName.substring(0, tableName.length() - 1);
145                                    }
146    
147                                    line = StringUtil.replace(
148                                            "drop index @table@.@index@;", "@table@", tableName);
149                                    line = StringUtil.replace(line, "@index@", tokens[2]);
150                            }
151    
152                            sb.append(line);
153                            sb.append("\n");
154                    }
155    
156                    unsyncBufferedReader.close();
157    
158                    return sb.toString();
159            }
160    
161            protected static final String DROP_COLUMN = "drop column";
162    
163            private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
164    
165            private static final String[] _SYBASE = {
166                    "--", "1", "0", "'19700101'", "getdate()", " image", " image", " int",
167                    " datetime", " float", " int", " decimal(20,0)", " varchar(1000)",
168                    " text", " varchar", "  identity(1,1)", "go"
169            };
170    
171            private static SybaseDB _instance = new SybaseDB();
172    
173    }