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