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    
081                    if (population != BARE) {
082                            sb.append("connect to ");
083                            sb.append(databaseName);
084                            sb.append(";\n");
085                            sb.append(getCreateTablesContent(sqlDir, suffix));
086                            sb.append("\n\n");
087                            sb.append(readFile(sqlDir + "/indexes/indexes-derby.sql"));
088                            sb.append("\n\n");
089                            sb.append(readFile(sqlDir + "/sequences/sequences-derby.sql"));
090                    }
091    
092                    return sb.toString();
093            }
094    
095            @Override
096            protected String getServerName() {
097                    return "derby";
098            }
099    
100            @Override
101            protected String[] getTemplate() {
102                    return _DERBY;
103            }
104    
105            @Override
106            protected String reword(String data) throws IOException {
107                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
108                            new UnsyncStringReader(data));
109    
110                    StringBundler sb = new StringBundler();
111    
112                    String line = null;
113    
114                    while ((line = unsyncBufferedReader.readLine()) != null) {
115                            if (line.startsWith(ALTER_COLUMN_NAME) ||
116                                    line.startsWith(ALTER_COLUMN_TYPE)) {
117    
118                                    line = "-- " + line;
119    
120                                    if (_log.isWarnEnabled()) {
121                                            _log.warn(
122                                                    "This statement is not supported by Derby: " + line);
123                                    }
124                            }
125                            else if (line.startsWith(ALTER_TABLE_NAME)) {
126                                    String[] template = buildTableNameTokens(line);
127    
128                                    line = StringUtil.replace(
129                                            "alter table @old-table@ to @new-table@;",
130                                            RENAME_TABLE_TEMPLATE, template);
131                            }
132                            else if (line.contains(DROP_INDEX)) {
133                                    String[] tokens = StringUtil.split(line, ' ');
134    
135                                    line = StringUtil.replace(
136                                            "drop index @index@;", "@index@", tokens[2]);
137                            }
138    
139                            sb.append(line);
140                            sb.append("\n");
141                    }
142    
143                    unsyncBufferedReader.close();
144    
145                    return sb.toString();
146            }
147    
148            private static final String[] _DERBY = {
149                    "--", "1", "0", "'1970-01-01-00.00.00.000000'", "current timestamp",
150                    " blob", " blob", " smallint", " timestamp", " double", " integer",
151                    " bigint", " varchar(4000)", " clob", " varchar",
152                    " generated always as identity", "commit"
153            };
154    
155            private static final boolean _SUPPORTS_ALTER_COLUMN_NAME = false;
156    
157            private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;
158    
159            private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
160    
161            private static DerbyDB _instance = new DerbyDB();
162    
163    }