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.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    
072                    if (population != BARE) {
073                            if (!sqlDir.endsWith("/WEB-INF/sql")) {
074                                    sb.append(
075                                            readSQL(
076                                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
077                                                            "-firebird.sql",
078                                                    _FIREBIRD[0], ";\n"));
079                            }
080                            else {
081                                    sb.append(
082                                            readSQL(
083                                                    sqlDir + "/tables" + suffix + "/tables" + suffix +
084                                                            "-firebird.sql",
085                                                    _FIREBIRD[0], ";\n"));
086                            }
087                    }
088    
089                    return sb.toString();
090            }
091    
092            @Override
093            protected String getServerName() {
094                    return "firebird";
095            }
096    
097            @Override
098            protected String[] getTemplate() {
099                    return _FIREBIRD;
100            }
101    
102            @Override
103            protected String reword(String data) throws IOException {
104                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
105                            new UnsyncStringReader(data));
106    
107                    StringBundler sb = new StringBundler();
108    
109                    String line = null;
110    
111                    while ((line = unsyncBufferedReader.readLine()) != null) {
112                            if (line.startsWith(ALTER_COLUMN_NAME)) {
113                                    String[] template = buildColumnNameTokens(line);
114    
115                                    line = StringUtil.replace(
116                                            "alter table @table@ alter column \"@old-column@\" to " +
117                                                    "\"@new-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@\" " +
125                                                    "type @type@;",
126                                            REWORD_TEMPLATE, template);
127                            }
128                            else if (line.contains(DROP_INDEX)) {
129                                    String[] tokens = StringUtil.split(line, ' ');
130    
131                                    line = StringUtil.replace(
132                                            "drop index @index@;", "@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            private static final String[] _FIREBIRD = {
145                    "--", "1", "0", "'01/01/1970'", "current_timestamp", " blob", " blob",
146                    " smallint", " timestamp", " double precision", " integer", " int64",
147                    " varchar(4000)", " blob", " varchar", "", "commit"
148            };
149    
150            private static FirebirdDB _instance = new FirebirdDB();
151    
152    }