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.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 David Maier
029     */
030    public class IngresDB 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, "\\n", "'+x'0a'+'");
043    
044                    return template;
045            }
046    
047            @Override
048            public boolean isSupportsAlterColumnName() {
049                    return _SUPPORTS_ALTER_COLUMN_NAME;
050            }
051    
052            protected IngresDB() {
053                    super(TYPE_INGRES);
054            }
055    
056            @Override
057            protected String buildCreateFileContent(
058                    String sqlDir, String databaseName, int population) {
059    
060                    return null;
061            }
062    
063            @Override
064            protected String getServerName() {
065                    return "ingres";
066            }
067    
068            @Override
069            protected String[] getTemplate() {
070                    return _INGRES;
071            }
072    
073            @Override
074            protected String replaceTemplate(String template, String[] actual) {
075                    if ((template == null) || (TEMPLATE == null) || (actual == null)) {
076                            return null;
077                    }
078    
079                    if (TEMPLATE.length != actual.length) {
080                            return template;
081                    }
082    
083                    for (int i = 0; i < TEMPLATE.length; i++) {
084                            if (TEMPLATE[i].equals("##") ||
085                                    TEMPLATE[i].equals("'01/01/1970'")) {
086    
087                                    template = StringUtil.replace(template, TEMPLATE[i], actual[i]);
088                            }
089                            else if (TEMPLATE[i].equals("COMMIT_TRANSACTION")) {
090                                    template = StringUtil.replace(
091                                            template, TEMPLATE[i] + ";", actual[i]);
092                            }
093                            else {
094                                    template = template.replaceAll(
095                                            "\\b" + TEMPLATE[i] + "\\b", actual[i]);
096                            }
097                    }
098    
099                    return template;
100            }
101    
102            @Override
103            protected String reword(String data) throws IOException {
104                    try (UnsyncBufferedReader unsyncBufferedReader =
105                                    new UnsyncBufferedReader(new UnsyncStringReader(data))) {
106    
107                            StringBundler sb = new StringBundler();
108    
109                            String line = null;
110    
111                            while ((line = unsyncBufferedReader.readLine()) != null) {
112                                    if (line.startsWith(ALTER_COLUMN_NAME)) {
113                                            line = "-- " + line;
114    
115                                            if (_log.isWarnEnabled()) {
116                                                    _log.warn(
117                                                            "This statement is not supported by Ingres: " +
118                                                                    line);
119                                            }
120                                    }
121                                    else if (line.startsWith(ALTER_COLUMN_TYPE)) {
122                                            String[] template = buildColumnTypeTokens(line);
123    
124                                            line = StringUtil.replace(
125                                                    "alter table @table@ alter @old-column@ @type@;",
126                                                    REWORD_TEMPLATE, template);
127                                    }
128                                    else if (line.startsWith(ALTER_TABLE_NAME)) {
129                                            String[] template = buildTableNameTokens(line);
130    
131                                            line = StringUtil.replace(
132                                                    "alter table @old-table@ rename to @new-table@;",
133                                                    RENAME_TABLE_TEMPLATE, template);
134                                    }
135                                    else if (line.contains(DROP_INDEX)) {
136                                            String[] tokens = StringUtil.split(line, ' ');
137    
138                                            line = StringUtil.replace(
139                                                    "drop index @index@;", "@index@", tokens[2]);
140                                    }
141                                    else if (line.contains(DROP_PRIMARY_KEY)) {
142                                            String[] tokens = StringUtil.split(line, ' ');
143    
144                                            line = StringUtil.replace(
145                                                    "alter table @table@ drop constraint @table@_pkey;",
146                                                    "@table@", tokens[2]);
147                                    }
148    
149                                    sb.append(line);
150                                    sb.append("\n");
151                            }
152    
153                            return sb.toString();
154                    }
155            }
156    
157            private static final String[] _INGRES = {
158                    "--", "TRUE", "FALSE", "'1970-01-01'", "date('now')", " blob", " blob",
159                    " tinyint", " timestamp", " float", " integer", " bigint",
160                    " varchar(1000)", " long varchar", " varchar", "", "commit;\\g"
161            };
162    
163            private static final boolean _SUPPORTS_ALTER_COLUMN_NAME = false;
164    
165            private static Log _log = LogFactoryUtil.getLog(IngresDB.class);
166    
167            private static IngresDB _instance = new IngresDB();
168    
169    }