001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public String buildSQL(String template) throws IOException {
037                    template = convertTimestamp(template);
038                    template = replaceTemplate(template, getTemplate());
039    
040                    template = reword(template);
041                    template = removeInserts(template);
042                    template = removeNull(template);
043    
044                    return template;
045            }
046    
047            protected FirebirdDB() {
048                    super(TYPE_FIREBIRD);
049            }
050    
051            protected FirebirdDB(String type) {
052                    super(type);
053            }
054    
055            protected String buildCreateFileContent(
056                            String sqlDir, String databaseName, int population)
057                    throws IOException {
058    
059                    String suffix = getSuffix(population);
060    
061                    StringBundler sb = new StringBundler(7);
062    
063                    sb.append("create database '");
064                    sb.append(databaseName);
065                    sb.append(".gdb' page_size 8192 user 'sysdba' password 'masterkey';\n");
066                    sb.append("connect '");
067                    sb.append(databaseName);
068                    sb.append(".gdb' user 'sysdba' password 'masterkey';\n");
069                    sb.append(
070                            readSQL(
071                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
072                                            "-firebird.sql",
073                                    _FIREBIRD[0], ";\n"));
074    
075                    return sb.toString();
076            }
077    
078            protected String getServerName() {
079                    return "firebird";
080            }
081    
082            protected String[] getTemplate() {
083                    return _FIREBIRD;
084            }
085    
086            protected String reword(String data) throws IOException {
087                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
088                            new UnsyncStringReader(data));
089    
090                    StringBundler sb = new StringBundler();
091    
092                    String line = null;
093    
094                    while ((line = unsyncBufferedReader.readLine()) != null) {
095                            if (line.startsWith(ALTER_COLUMN_NAME)) {
096                                    String[] template = buildColumnNameTokens(line);
097    
098                                    line = StringUtil.replace(
099                                            "alter table @table@ alter column \"@old-column@\" to " +
100                                                    "\"@new-column@\";",
101                                            REWORD_TEMPLATE, template);
102                            }
103                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
104                                    String[] template = buildColumnTypeTokens(line);
105    
106                                    line = StringUtil.replace(
107                                            "alter table @table@ alter column \"@old-column@\" " +
108                                                    "type @type@;",
109                                            REWORD_TEMPLATE, template);
110                            }
111                            else if (line.indexOf(DROP_INDEX) != -1) {
112                                    String[] tokens = StringUtil.split(line, " ");
113    
114                                    line = StringUtil.replace(
115                                            "drop index @index@;", "@index@", tokens[2]);
116                            }
117    
118                            sb.append(line);
119                            sb.append("\n");
120                    }
121    
122                    unsyncBufferedReader.close();
123    
124                    return sb.toString();
125            }
126    
127            private static String[] _FIREBIRD = {
128                    "--", "1", "0",
129                    "'01/01/1970'", "current_timestamp",
130                    " blob", " smallint", " timestamp",
131                    " double precision", " integer", " int64",
132                    " varchar(4000)", " blob", " varchar",
133                    "", "commit"
134            };
135    
136            private static FirebirdDB _instance = new FirebirdDB();
137    
138    }