001    /**
002     * Copyright (c) 2000-2013 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                    sb.append("use ");
084                    sb.append(databaseName);
085                    sb.append("\n\n");
086                    sb.append(getCreateTablesContent(sqlDir, suffix));
087                    sb.append("\n\n");
088                    sb.append(readFile(sqlDir + "/indexes/indexes-sybase.sql"));
089                    sb.append("\n\n");
090                    sb.append(readFile(sqlDir + "/sequences/sequences-sybase.sql"));
091    
092                    return sb.toString();
093            }
094    
095            @Override
096            protected String getServerName() {
097                    return "sybase";
098            }
099    
100            @Override
101            protected String[] getTemplate() {
102                    return _SYBASE;
103            }
104    
105            @Override
106            protected String reword(String data) throws IOException {
107                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
108                            new UnsyncStringReader(data));
109    
110                    StringBundler sb = new StringBundler();
111    
112                    String line = null;
113    
114                    while ((line = unsyncBufferedReader.readLine()) != null) {
115                            if (line.contains(DROP_COLUMN)) {
116                                    line = StringUtil.replace(line, " drop column ", " drop ");
117                            }
118    
119                            if (line.startsWith(ALTER_COLUMN_NAME)) {
120                                    String[] template = buildColumnNameTokens(line);
121    
122                                    line = StringUtil.replace(
123                                            "exec sp_rename '@table@.@old-column@', '@new-column@', " +
124                                                    "'column';",
125                                            REWORD_TEMPLATE, template);
126                            }
127                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
128                                    String[] template = buildColumnTypeTokens(line);
129    
130                                    line = StringUtil.replace(
131                                            "alter table @table@ modify @old-column@ @type@;",
132                                            REWORD_TEMPLATE, template);
133                            }
134    
135                            else if (line.startsWith(ALTER_TABLE_NAME)) {
136                                    String[] template = buildTableNameTokens(line);
137    
138                                    line = StringUtil.replace(
139                                            "exec sp_rename @old-table@, @new-table@;",
140                                            RENAME_TABLE_TEMPLATE, template);
141                            }
142                            else if (line.contains(DROP_INDEX)) {
143                                    String[] tokens = StringUtil.split(line, ' ');
144    
145                                    String tableName = tokens[4];
146    
147                                    if (tableName.endsWith(StringPool.SEMICOLON)) {
148                                            tableName = tableName.substring(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                    unsyncBufferedReader.close();
161    
162                    return sb.toString();
163            }
164    
165            protected static final String DROP_COLUMN = "drop column";
166    
167            private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
168    
169            private static final String[] _SYBASE = {
170                    "--", "1", "0", "'19700101'", "getdate()", " image", " image", " int",
171                    " datetime", " float", " int", " decimal(20,0)", " varchar(1000)",
172                    " text", " varchar", "  identity(1,1)", "go"
173            };
174    
175            private static SybaseDB _instance = new SybaseDB();
176    
177    }