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