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 = StringUtil.toLowerCase(
080                                            rs.getString("indexdef").trim());
081    
082                                    boolean unique = true;
083    
084                                    if (indexSQL.startsWith("create index ")) {
085                                            unique = false;
086                                    }
087    
088                                    indexes.add(new Index(indexName, tableName, unique));
089                            }
090                    }
091                    finally {
092                            DataAccess.cleanUp(null, ps, rs);
093                    }
094    
095                    return indexes;
096            }
097    
098            @Override
099            public boolean isSupportsQueryingAfterException() {
100                    return _SUPPORTS_QUERYING_AFTER_EXCEPTION;
101            }
102    
103            protected PostgreSQLDB() {
104                    super(TYPE_POSTGRESQL);
105            }
106    
107            @Override
108            protected String buildCreateFileContent(
109                            String sqlDir, String databaseName, int population)
110                    throws IOException {
111    
112                    String suffix = getSuffix(population);
113    
114                    StringBundler sb = new StringBundler(14);
115    
116                    sb.append("drop database ");
117                    sb.append(databaseName);
118                    sb.append(";\n");
119                    sb.append("create database ");
120                    sb.append(databaseName);
121                    sb.append(" encoding = 'UNICODE';\n");
122    
123                    if (population != BARE) {
124                            sb.append("\\c ");
125                            sb.append(databaseName);
126                            sb.append(";\n\n");
127                            sb.append(getCreateTablesContent(sqlDir, suffix));
128                            sb.append("\n\n");
129                            sb.append(readFile(sqlDir + "/indexes/indexes-postgresql.sql"));
130                            sb.append("\n\n");
131                            sb.append(readFile(sqlDir + "/sequences/sequences-postgresql.sql"));
132                    }
133    
134                    return sb.toString();
135            }
136    
137            @Override
138            protected String getServerName() {
139                    return "postgresql";
140            }
141    
142            @Override
143            protected String[] getTemplate() {
144                    return _POSTGRESQL;
145            }
146    
147            @Override
148            protected String reword(String data) throws IOException {
149                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
150                            new UnsyncStringReader(data));
151    
152                    StringBundler sb = new StringBundler();
153    
154                    String line = null;
155    
156                    while ((line = unsyncBufferedReader.readLine()) != null) {
157                            if (line.startsWith(ALTER_COLUMN_NAME)) {
158                                    String[] template = buildColumnNameTokens(line);
159    
160                                    line = StringUtil.replace(
161                                            "alter table @table@ rename @old-column@ to @new-column@;",
162                                            REWORD_TEMPLATE, template);
163                            }
164                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
165                                    String[] template = buildColumnTypeTokens(line);
166    
167                                    line = StringUtil.replace(
168                                            "alter table @table@ alter @old-column@ type @type@ " +
169                                                    "using @old-column@::@type@;",
170                                            REWORD_TEMPLATE, template);
171                            }
172                            else if (line.startsWith(ALTER_TABLE_NAME)) {
173                                    String[] template = buildTableNameTokens(line);
174    
175                                    line = StringUtil.replace(
176                                            "alter table @old-table@ rename to @new-table@;",
177                                            RENAME_TABLE_TEMPLATE, template);
178                            }
179                            else if (line.contains(DROP_INDEX)) {
180                                    String[] tokens = StringUtil.split(line, ' ');
181    
182                                    line = StringUtil.replace(
183                                            "drop index @index@;", "@index@", tokens[2]);
184                            }
185                            else if (line.contains(DROP_PRIMARY_KEY)) {
186                                    String[] tokens = StringUtil.split(line, ' ');
187    
188                                    line = StringUtil.replace(
189                                            "alter table @table@ drop constraint @table@_pkey;",
190                                            "@table@", tokens[2]);
191                            }
192                            else if (line.contains("\\\'")) {
193                                    line = StringUtil.replace(line, "\\\'", "\'\'");
194                            }
195    
196                            sb.append(line);
197                            sb.append("\n");
198                    }
199    
200                    unsyncBufferedReader.close();
201    
202                    return sb.toString();
203            }
204    
205            private static final String[] _POSTGRESQL = {
206                    "--", "true", "false", "'01/01/1970'", "current_timestamp", " oid",
207                    " bytea", " bool", " timestamp", " double precision", " integer",
208                    " bigint", " text", " text", " varchar", "", "commit"
209            };
210    
211            private static final boolean _SUPPORTS_QUERYING_AFTER_EXCEPTION = false;
212    
213            private static PostgreSQLDB _instance = new PostgreSQLDB();
214    
215    }