001    /**
002     * Copyright (c) 2000-present 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.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.io.IOException;
023    
024    /**
025     * @author Alexander Chow
026     * @author Sandeep Soni
027     * @author Ganesh Ram
028     */
029    public class HypersonicDB extends BaseDB {
030    
031            public HypersonicDB(int majorVersion, int minorVersion) {
032                    super(TYPE_HYPERSONIC, majorVersion, minorVersion);
033            }
034    
035            @Override
036            public String buildSQL(String template) throws IOException {
037                    template = convertTimestamp(template);
038                    template = replaceTemplate(template, getTemplate());
039    
040                    template = reword(template);
041                    template = StringUtil.replace(template, "\\'", "''");
042    
043                    return template;
044            }
045    
046            @Override
047            protected String buildCreateFileContent(
048                    String sqlDir, String databaseName, int population) {
049    
050                    return null;
051            }
052    
053            @Override
054            protected String getServerName() {
055                    return "hypersonic";
056            }
057    
058            @Override
059            protected String[] getTemplate() {
060                    return _HYPERSONIC;
061            }
062    
063            @Override
064            protected String reword(String data) throws IOException {
065                    try (UnsyncBufferedReader unsyncBufferedReader =
066                                    new UnsyncBufferedReader(new UnsyncStringReader(data))) {
067    
068                            StringBundler sb = new StringBundler();
069    
070                            String line = null;
071    
072                            while ((line = unsyncBufferedReader.readLine()) != null) {
073                                    if (line.startsWith(ALTER_COLUMN_NAME)) {
074                                            String[] template = buildColumnNameTokens(line);
075    
076                                            line = StringUtil.replace(
077                                                    "alter table @table@ alter column @old-column@ rename" +
078                                                            " to @new-column@;",
079                                                    REWORD_TEMPLATE, template);
080                                    }
081                                    else if (line.startsWith(ALTER_COLUMN_TYPE)) {
082                                            String[] template = buildColumnTypeTokens(line);
083    
084                                            line = StringUtil.replace(
085                                                    "alter table @table@ alter column @old-column@ " +
086                                                            "@type@ @nullable@;",
087                                                    REWORD_TEMPLATE, template);
088                                    }
089                                    else if (line.startsWith(ALTER_TABLE_NAME)) {
090                                            String[] template = buildTableNameTokens(line);
091    
092                                            line = StringUtil.replace(
093                                                    "alter table @old-table@ rename to @new-table@;",
094                                                    RENAME_TABLE_TEMPLATE, template);
095                                    }
096                                    else if (line.contains(DROP_INDEX)) {
097                                            String[] tokens = StringUtil.split(line, ' ');
098    
099                                            line = StringUtil.replace(
100                                                    "drop index @index@;", "@index@", tokens[2]);
101                                    }
102    
103                                    sb.append(line);
104                                    sb.append("\n");
105                            }
106    
107                            return sb.toString();
108                    }
109            }
110    
111            private static final String[] _HYPERSONIC = {
112                    "//", "true", "false", "'1970-01-01 00:00:00'", "now()", " blob",
113                    " blob", " bit", " timestamp", " double", " int", " bigint",
114                    " longvarchar", " longvarchar", " varchar", "", "commit"
115            };
116    
117    }