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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.io.IOException;
026    
027    /**
028     * @author Alexander Chow
029     * @author Sandeep Soni
030     * @author Ganesh Ram
031     */
032    public class DerbyDB extends BaseDB {
033    
034            public static DB getInstance() {
035                    return _instance;
036            }
037    
038            public String buildSQL(String template) throws IOException {
039                    template = convertTimestamp(template);
040                    template = replaceTemplate(template, getTemplate());
041    
042                    template = reword(template );
043                    //template = _removeLongInserts(derby);
044                    template = removeNull(template);
045                    template = StringUtil.replace(template , "\\'", "''");
046    
047                    return template;
048            }
049    
050            public boolean isSupportsAlterColumnName() {
051                    return _SUPPORTS_ALTER_COLUMN_NAME;
052            }
053    
054            public boolean isSupportsAlterColumnType() {
055                    return _SUPPORTS_ALTER_COLUMN_TYPE;
056            }
057    
058            protected DerbyDB() {
059                    super(TYPE_DERBY);
060            }
061    
062            protected String buildCreateFileContent(
063                            String sqlDir, String databaseName, int population)
064                    throws IOException {
065    
066                    String suffix = getSuffix(population);
067    
068                    StringBundler sb = new StringBundler(14);
069    
070                    sb.append("drop database ");
071                    sb.append(databaseName);
072                    sb.append(";\n");
073                    sb.append("create database ");
074                    sb.append(databaseName);
075                    sb.append(";\n");
076                    sb.append("connect to ");
077                    sb.append(databaseName);
078                    sb.append(";\n");
079                    sb.append(
080                            readFile(
081                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
082                                            "-derby.sql"));
083                    sb.append("\n\n");
084                    sb.append(readFile(sqlDir + "/indexes/indexes-derby.sql"));
085                    sb.append("\n\n");
086                    sb.append(readFile(sqlDir + "/sequences/sequences-derby.sql"));
087    
088                    return sb.toString();
089            }
090    
091            protected String getServerName() {
092                    return "derby";
093            }
094    
095            protected String[] getTemplate() {
096                    return _DERBY;
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.startsWith(ALTER_COLUMN_NAME) ||
109                                    line.startsWith(ALTER_COLUMN_TYPE)) {
110    
111                                    line = "-- " + line;
112    
113                                    if (_log.isWarnEnabled()) {
114                                            _log.warn(
115                                                    "This statement is not supported by Derby: " + line);
116                                    }
117                            }
118                            else if (line.indexOf(DROP_INDEX) != -1) {
119                                    String[] tokens = StringUtil.split(line, " ");
120    
121                                    line = StringUtil.replace(
122                                            "drop index @index@;", "@index@", tokens[2]);
123                            }
124    
125                            sb.append(line);
126                            sb.append("\n");
127                    }
128    
129                    unsyncBufferedReader.close();
130    
131                    return sb.toString();
132            }
133    
134            private static String[] _DERBY = {
135                    "--", "1", "0",
136                    "'1970-01-01-00.00.00.000000'", "current timestamp",
137                    " blob", " smallint", " timestamp",
138                    " double", " integer", " bigint",
139                    " varchar(4000)", " clob", " varchar",
140                    " generated always as identity", "commit"
141            };
142    
143            private static final boolean _SUPPORTS_ALTER_COLUMN_NAME = false;
144    
145            private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;
146    
147            private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
148    
149            private static DerbyDB _instance = new DerbyDB();
150    
151    }