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.dao.db.Index;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
021    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.io.IOException;
026    
027    import java.sql.Connection;
028    import java.sql.PreparedStatement;
029    import java.sql.ResultSet;
030    import java.sql.SQLException;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    
035    /**
036     * @author Alexander Chow
037     * @author Sandeep Soni
038     * @author Ganesh Ram
039     */
040    public class PostgreSQLDB extends BaseDB {
041    
042            public static DB getInstance() {
043                    return _instance;
044            }
045    
046            @Override
047            public String buildSQL(String template) throws IOException {
048                    template = convertTimestamp(template);
049                    template = replaceTemplate(template, getTemplate());
050    
051                    template = reword(template);
052    
053                    return template;
054            }
055    
056            @Override
057            public List<Index> getIndexes(Connection con) throws SQLException {
058                    List<Index> indexes = new ArrayList<Index>();
059    
060                    PreparedStatement ps = null;
061                    ResultSet rs = null;
062    
063                    try {
064                            StringBundler sb = new StringBundler(3);
065    
066                            sb.append("select indexname, tablename, indexdef from pg_indexes ");
067                            sb.append("where indexname like 'liferay_%' or indexname like ");
068                            sb.append("'ix_%'");
069    
070                            String sql = sb.toString();
071    
072                            ps = con.prepareStatement(sql);
073    
074                            rs = ps.executeQuery();
075    
076                            while (rs.next()) {
077                                    String indexName = rs.getString("indexname");
078                                    String tableName = rs.getString("tablename");
079                                    String indexSQL = rs.getString("indexdef").toLowerCase().trim();
080    
081                                    boolean unique = true;
082    
083                                    if (indexSQL.startsWith("create index ")) {
084                                            unique = false;
085                                    }
086    
087                                    indexes.add(new Index(indexName, tableName, unique));
088                            }
089                    }
090                    finally {
091                            DataAccess.cleanUp(null, ps, rs);
092                    }
093    
094                    return indexes;
095            }
096    
097            @Override
098            public boolean isSupportsQueryingAfterException() {
099                    return _SUPPORTS_QUERYING_AFTER_EXCEPTION;
100            }
101    
102            protected PostgreSQLDB() {
103                    super(TYPE_POSTGRESQL);
104            }
105    
106            @Override
107            protected String buildCreateFileContent(
108                            String sqlDir, String databaseName, int population)
109                    throws IOException {
110    
111                    String suffix = getSuffix(population);
112    
113                    StringBundler sb = new StringBundler(14);
114    
115                    sb.append("drop database ");
116                    sb.append(databaseName);
117                    sb.append(";\n");
118                    sb.append("create database ");
119                    sb.append(databaseName);
120                    sb.append(" encoding = 'UNICODE';\n");
121                    sb.append("\\c ");
122                    sb.append(databaseName);
123                    sb.append(";\n\n");
124                    sb.append(getCreateTablesContent(sqlDir, suffix));
125                    sb.append("\n\n");
126                    sb.append(readFile(sqlDir + "/indexes/indexes-postgresql.sql"));
127                    sb.append("\n\n");
128                    sb.append(readFile(sqlDir + "/sequences/sequences-postgresql.sql"));
129    
130                    return sb.toString();
131            }
132    
133            @Override
134            protected String getServerName() {
135                    return "postgresql";
136            }
137    
138            @Override
139            protected String[] getTemplate() {
140                    return _POSTGRESQL;
141            }
142    
143            @Override
144            protected String reword(String data) throws IOException {
145                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
146                            new UnsyncStringReader(data));
147    
148                    StringBundler sb = new StringBundler();
149    
150                    String line = null;
151    
152                    while ((line = unsyncBufferedReader.readLine()) != null) {
153                            if (line.startsWith(ALTER_COLUMN_NAME)) {
154                                    String[] template = buildColumnNameTokens(line);
155    
156                                    line = StringUtil.replace(
157                                            "alter table @table@ rename @old-column@ to @new-column@;",
158                                            REWORD_TEMPLATE, template);
159                            }
160                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
161                                    String[] template = buildColumnTypeTokens(line);
162    
163                                    line = StringUtil.replace(
164                                            "alter table @table@ alter @old-column@ type @type@ " +
165                                                    "using @old-column@::@type@;",
166                                            REWORD_TEMPLATE, template);
167                            }
168                            else if (line.startsWith(ALTER_TABLE_NAME)) {
169                                    String[] template = buildTableNameTokens(line);
170    
171                                    line = StringUtil.replace(
172                                            "alter table @old-table@ rename to @new-table@;",
173                                            RENAME_TABLE_TEMPLATE, template);
174                            }
175                            else if (line.contains(DROP_INDEX)) {
176                                    String[] tokens = StringUtil.split(line, ' ');
177    
178                                    line = StringUtil.replace(
179                                            "drop index @index@;", "@index@", tokens[2]);
180                            }
181                            else if (line.contains(DROP_PRIMARY_KEY)) {
182                                    String[] tokens = StringUtil.split(line, ' ');
183    
184                                    line = StringUtil.replace(
185                                            "alter table @table@ drop constraint @table@_pkey;",
186                                            "@table@", tokens[2]);
187                            }
188                            else if (line.contains("\\\'")) {
189                                    line = StringUtil.replace(line, "\\\'", "\'\'");
190                            }
191    
192                            sb.append(line);
193                            sb.append("\n");
194                    }
195    
196                    unsyncBufferedReader.close();
197    
198                    return sb.toString();
199            }
200    
201            private static final String[] _POSTGRESQL = {
202                    "--", "true", "false", "'01/01/1970'", "current_timestamp", " oid",
203                    " bytea", " bool", " timestamp", " double precision", " integer",
204                    " bigint", " text", " text", " varchar", "", "commit"
205            };
206    
207            private static final boolean _SUPPORTS_QUERYING_AFTER_EXCEPTION = false;
208    
209            private static PostgreSQLDB _instance = new PostgreSQLDB();
210    
211    }