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