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.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 = template.replaceAll(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                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
105                            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: " + line);
118                                    }
119                            }
120                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
121                                    String[] template = buildColumnTypeTokens(line);
122    
123                                    line = StringUtil.replace(
124                                            "alter table @table@ alter @old-column@ @type@;",
125                                            REWORD_TEMPLATE, template);
126                            }
127                            else if (line.startsWith(ALTER_TABLE_NAME)) {
128                                    String[] template = buildTableNameTokens(line);
129    
130                                    line = StringUtil.replace(
131                                            "alter table @old-table@ rename to @new-table@;",
132                                            RENAME_TABLE_TEMPLATE, template);
133                            }
134                            else if (line.contains(DROP_INDEX)) {
135                                    String[] tokens = StringUtil.split(line, ' ');
136    
137                                    line = StringUtil.replace(
138                                            "drop index @index@;", "@index@", tokens[2]);
139                            }
140                            else if (line.contains(DROP_PRIMARY_KEY)) {
141                                    String[] tokens = StringUtil.split(line, ' ');
142    
143                                    line = StringUtil.replace(
144                                            "alter table @table@ drop constraint @table@_pkey;",
145                                            "@table@", tokens[2]);
146                            }
147    
148                            sb.append(line);
149                            sb.append("\n");
150                    }
151    
152                    unsyncBufferedReader.close();
153    
154                    return sb.toString();
155            }
156    
157            private static final String[] _INGRES = {
158                    "--", "1", "0", "'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    }