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