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.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            @Override
039            public String buildSQL(String template) throws IOException {
040                    template = convertTimestamp(template);
041                    template = replaceTemplate(template, getTemplate());
042    
043                    template = reword(template );
044                    //template = _removeLongInserts(derby);
045                    template = removeNull(template);
046                    template = StringUtil.replace(template , "\\'", "''");
047    
048                    return template;
049            }
050    
051            @Override
052            public boolean isSupportsAlterColumnName() {
053                    return _SUPPORTS_ALTER_COLUMN_NAME;
054            }
055    
056            @Override
057            public boolean isSupportsAlterColumnType() {
058                    return _SUPPORTS_ALTER_COLUMN_TYPE;
059            }
060    
061            protected DerbyDB() {
062                    super(TYPE_DERBY);
063            }
064    
065            @Override
066            protected String buildCreateFileContent(
067                            String sqlDir, String databaseName, int population)
068                    throws IOException {
069    
070                    String suffix = getSuffix(population);
071    
072                    StringBundler sb = new StringBundler(14);
073    
074                    sb.append("drop database ");
075                    sb.append(databaseName);
076                    sb.append(";\n");
077                    sb.append("create database ");
078                    sb.append(databaseName);
079                    sb.append(";\n");
080                    sb.append("connect to ");
081                    sb.append(databaseName);
082                    sb.append(";\n");
083                    sb.append(getCreateTablesContent(sqlDir, suffix));
084                    sb.append("\n\n");
085                    sb.append(readFile(sqlDir + "/indexes/indexes-derby.sql"));
086                    sb.append("\n\n");
087                    sb.append(readFile(sqlDir + "/sequences/sequences-derby.sql"));
088    
089                    return sb.toString();
090            }
091    
092            @Override
093            protected String getServerName() {
094                    return "derby";
095            }
096    
097            @Override
098            protected String[] getTemplate() {
099                    return _DERBY;
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                                    line.startsWith(ALTER_COLUMN_TYPE)) {
114    
115                                    line = "-- " + line;
116    
117                                    if (_log.isWarnEnabled()) {
118                                            _log.warn(
119                                                    "This statement is not supported by Derby: " + line);
120                                    }
121                            }
122                            else if (line.startsWith(ALTER_TABLE_NAME)) {
123                                    String[] template = buildTableNameTokens(line);
124    
125                                    line = StringUtil.replace(
126                                            "alter table @old-table@ to @new-table@;",
127                                            RENAME_TABLE_TEMPLATE, template);
128                            }
129                            else if (line.contains(DROP_INDEX)) {
130                                    String[] tokens = StringUtil.split(line, ' ');
131    
132                                    line = StringUtil.replace(
133                                            "drop index @index@;", "@index@", tokens[2]);
134                            }
135    
136                            sb.append(line);
137                            sb.append("\n");
138                    }
139    
140                    unsyncBufferedReader.close();
141    
142                    return sb.toString();
143            }
144    
145            private static final String[] _DERBY = {
146                    "--", "1", "0", "'1970-01-01-00.00.00.000000'", "current timestamp",
147                    " blob", " blob", " smallint", " timestamp", " double", " integer",
148                    " bigint", " varchar(4000)", " clob", " varchar",
149                    " generated always as identity", "commit"
150            };
151    
152            private static final boolean _SUPPORTS_ALTER_COLUMN_NAME = false;
153    
154            private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;
155    
156            private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
157    
158            private static DerbyDB _instance = new DerbyDB();
159    
160    }