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.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.startsWith(ALTER_TABLE_NAME)) {
095                                    String[] template = buildTableNameTokens(line);
096    
097                                    line = StringUtil.replace(
098                                            "alter table @old-table@ rename to @new-table@;",
099                                            RENAME_TABLE_TEMPLATE, template);
100                            }
101                            else if (line.contains(DROP_INDEX)) {
102                                    String[] tokens = StringUtil.split(line, ' ');
103    
104                                    line = StringUtil.replace(
105                                            "drop index @index@;", "@index@", tokens[2]);
106                            }
107    
108                            sb.append(line);
109                            sb.append("\n");
110                    }
111    
112                    unsyncBufferedReader.close();
113    
114                    return sb.toString();
115            }
116    
117            private static final String[] _HYPERSONIC = {
118                    "//", "true", "false", "'1970-01-01 00:00:00'", "now()", " blob",
119                    " blob", " bit", " timestamp", " double", " int", " bigint",
120                    " longvarchar", " longvarchar", " varchar", "", "commit"
121            };
122    
123            private static HypersonicDB _instance = new HypersonicDB();
124    
125    }