001    /**
002     * Copyright (c) 2000-2012 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 HypersonicDB 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 = StringUtil.replace(template, "\\'", "''");
043    
044                    return template;
045            }
046    
047            protected HypersonicDB() {
048                    super(TYPE_HYPERSONIC);
049            }
050    
051            @Override
052            protected String buildCreateFileContent(
053                    String sqlDir, String databaseName, int population) {
054    
055                    return null;
056            }
057    
058            @Override
059            protected String getServerName() {
060                    return "hypersonic";
061            }
062    
063            @Override
064            protected String[] getTemplate() {
065                    return _HYPERSONIC;
066            }
067    
068            @Override
069            protected String reword(String data) throws IOException {
070                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
071                            new UnsyncStringReader(data));
072    
073                    StringBundler sb = new StringBundler();
074    
075                    String line = null;
076    
077                    while ((line = unsyncBufferedReader.readLine()) != null) {
078                            if (line.startsWith(ALTER_COLUMN_NAME)) {
079                                    String[] template = buildColumnNameTokens(line);
080    
081                                    line = StringUtil.replace(
082                                            "alter table @table@ alter column @old-column@ rename to " +
083                                                    "@new-column@;",
084                                            REWORD_TEMPLATE, template);
085                            }
086                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
087                                    String[] template = buildColumnTypeTokens(line);
088    
089                                    line = StringUtil.replace(
090                                            "alter table @table@ alter column @old-column@ @type@ " +
091                                                    "@nullable@;",
092                                            REWORD_TEMPLATE, template);
093                            }
094                            else if (line.indexOf(DROP_INDEX) != -1) {
095                                    String[] tokens = StringUtil.split(line, ' ');
096    
097                                    line = StringUtil.replace(
098                                            "drop index @index@;", "@index@", tokens[2]);
099                            }
100    
101                            sb.append(line);
102                            sb.append("\n");
103                    }
104    
105                    unsyncBufferedReader.close();
106    
107                    return sb.toString();
108            }
109    
110            private static final String[] _HYPERSONIC = {
111                    "//", "true", "false", "'1970-01-01 00:00:00'", "now()", " blob",
112                    " blob", " bit", " timestamp", " double", " int", " bigint",
113                    " longvarchar", " longvarchar", " varchar", "", "commit"
114            };
115    
116            private static HypersonicDB _instance = new HypersonicDB();
117    
118    }