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.StringUtil;
022    
023    import java.io.IOException;
024    
025    /**
026     * @author Alexander Chow
027     * @author Sandeep Soni
028     * @author Ganesh Ram
029     */
030    public class FirebirdDB extends BaseDB {
031    
032            public static DB getInstance() {
033                    return _instance;
034            }
035    
036            @Override
037            public String buildSQL(String template) throws IOException {
038                    template = convertTimestamp(template);
039                    template = replaceTemplate(template, getTemplate());
040    
041                    template = reword(template);
042                    template = removeInserts(template);
043                    template = removeNull(template);
044    
045                    return template;
046            }
047    
048            protected FirebirdDB() {
049                    super(TYPE_FIREBIRD);
050            }
051    
052            protected FirebirdDB(String type) {
053                    super(type);
054            }
055    
056            @Override
057            protected String buildCreateFileContent(
058                            String sqlDir, String databaseName, int population)
059                    throws IOException {
060    
061                    String suffix = getSuffix(population);
062    
063                    StringBundler sb = new StringBundler(7);
064    
065                    sb.append("create database '");
066                    sb.append(databaseName);
067                    sb.append(".gdb' page_size 8192 user 'sysdba' password 'masterkey';\n");
068                    sb.append("connect '");
069                    sb.append(databaseName);
070                    sb.append(".gdb' user 'sysdba' password 'masterkey';\n");
071                    sb.append(
072                            readSQL(
073                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
074                                            "-firebird.sql",
075                                    _FIREBIRD[0], ";\n"));
076    
077                    return sb.toString();
078            }
079    
080            @Override
081            protected String getServerName() {
082                    return "firebird";
083            }
084    
085            @Override
086            protected String[] getTemplate() {
087                    return _FIREBIRD;
088            }
089    
090            @Override
091            protected String reword(String data) throws IOException {
092                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
093                            new UnsyncStringReader(data));
094    
095                    StringBundler sb = new StringBundler();
096    
097                    String line = null;
098    
099                    while ((line = unsyncBufferedReader.readLine()) != null) {
100                            if (line.startsWith(ALTER_COLUMN_NAME)) {
101                                    String[] template = buildColumnNameTokens(line);
102    
103                                    line = StringUtil.replace(
104                                            "alter table @table@ alter column \"@old-column@\" to " +
105                                                    "\"@new-column@\";",
106                                            REWORD_TEMPLATE, template);
107                            }
108                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
109                                    String[] template = buildColumnTypeTokens(line);
110    
111                                    line = StringUtil.replace(
112                                            "alter table @table@ alter column \"@old-column@\" " +
113                                                    "type @type@;",
114                                            REWORD_TEMPLATE, template);
115                            }
116                            else if (line.indexOf(DROP_INDEX) != -1) {
117                                    String[] tokens = StringUtil.split(line, ' ');
118    
119                                    line = StringUtil.replace(
120                                            "drop index @index@;", "@index@", tokens[2]);
121                            }
122    
123                            sb.append(line);
124                            sb.append("\n");
125                    }
126    
127                    unsyncBufferedReader.close();
128    
129                    return sb.toString();
130            }
131    
132            private static final String[] _FIREBIRD = {
133                    "--", "1", "0", "'01/01/1970'", "current_timestamp", " blob", " blob",
134                    " smallint", " timestamp", " double precision", " integer", " int64",
135                    " varchar(4000)", " blob", " varchar", "", "commit"
136            };
137    
138            private static FirebirdDB _instance = new FirebirdDB();
139    
140    }