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