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