001    /**
002     * Copyright (c) 2000-present 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.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.io.IOException;
024    
025    /**
026     * @author Alexander Chow
027     * @author Bruno Farache
028     * @author Sandeep Soni
029     * @author Ganesh Ram
030     */
031    public class SybaseDB extends BaseDB {
032    
033            public SybaseDB(int majorVersion, int minorVersion) {
034                    super(TYPE_SYBASE, majorVersion, minorVersion);
035            }
036    
037            @Override
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            @Override
054            public boolean isSupportsInlineDistinct() {
055                    return _SUPPORTS_INLINE_DISTINCT;
056            }
057    
058            @Override
059            protected String buildCreateFileContent(
060                            String sqlDir, String databaseName, int population)
061                    throws IOException {
062    
063                    String suffix = getSuffix(population);
064    
065                    StringBundler sb = new StringBundler(19);
066    
067                    sb.append("use master\n");
068                    sb.append("exec sp_dboption '");
069                    sb.append(databaseName);
070                    sb.append("', ");
071                    sb.append("'allow nulls by default' , true\n");
072                    sb.append("go\n\n");
073                    sb.append("exec sp_dboption '");
074                    sb.append(databaseName);
075                    sb.append("', ");
076                    sb.append("'select into/bulkcopy/pllsort' , true\n");
077                    sb.append("go\n\n");
078    
079                    if (population != BARE) {
080                            sb.append("use ");
081                            sb.append(databaseName);
082                            sb.append("\n\n");
083                            sb.append(getCreateTablesContent(sqlDir, suffix));
084                            sb.append("\n\n");
085                            sb.append(readFile(sqlDir + "/indexes/indexes-sybase.sql"));
086                            sb.append("\n\n");
087                            sb.append(readFile(sqlDir + "/sequences/sequences-sybase.sql"));
088                    }
089    
090                    return sb.toString();
091            }
092    
093            @Override
094            protected String getServerName() {
095                    return "sybase";
096            }
097    
098            @Override
099            protected String[] getTemplate() {
100                    return _SYBASE;
101            }
102    
103            @Override
104            protected String reword(String data) throws IOException {
105                    try (UnsyncBufferedReader unsyncBufferedReader =
106                                    new UnsyncBufferedReader(new UnsyncStringReader(data))) {
107    
108                            StringBundler sb = new StringBundler();
109    
110                            String line = null;
111    
112                            while ((line = unsyncBufferedReader.readLine()) != null) {
113                                    if (line.contains(DROP_COLUMN)) {
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@', " +
122                                                            "'@new-column@', '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@ modify @old-column@ @type@;",
130                                                    REWORD_TEMPLATE, template);
131                                    }
132    
133                                    else if (line.startsWith(ALTER_TABLE_NAME)) {
134                                            String[] template = buildTableNameTokens(line);
135    
136                                            line = StringUtil.replace(
137                                                    "exec sp_rename @old-table@, @new-table@;",
138                                                    RENAME_TABLE_TEMPLATE, template);
139                                    }
140                                    else if (line.contains(DROP_INDEX)) {
141                                            String[] tokens = StringUtil.split(line, ' ');
142    
143                                            String tableName = tokens[4];
144    
145                                            if (tableName.endsWith(StringPool.SEMICOLON)) {
146                                                    tableName = tableName.substring(
147                                                            0, tableName.length() - 1);
148                                            }
149    
150                                            line = StringUtil.replace(
151                                                    "drop index @table@.@index@;", "@table@", tableName);
152                                            line = StringUtil.replace(line, "@index@", tokens[2]);
153                                    }
154    
155                                    sb.append(line);
156                                    sb.append("\n");
157                            }
158    
159                            return sb.toString();
160                    }
161            }
162    
163            protected static final String DROP_COLUMN = "drop column";
164    
165            private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
166    
167            private static final String[] _SYBASE = {
168                    "--", "1", "0", "'19700101'", "getdate()", " image", " image", " int",
169                    " bigdatetime", " float", " int", " decimal(20,0)", " varchar(1000)",
170                    " text", " varchar", "  identity(1,1)", "go"
171            };
172    
173    }