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.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            public String buildSQL(String template) throws IOException {
047                    template = convertTimestamp(template);
048                    template = replaceTemplate(template, getTemplate());
049    
050                    template = reword(template);
051    
052                    return template;
053            }
054    
055            public List<Index> getIndexes() throws SQLException {
056                    List<Index> indexes = new ArrayList<Index>();
057    
058                    Connection con = null;
059                    PreparedStatement ps = null;
060                    ResultSet rs = null;
061    
062                    try {
063                            con = DataAccess.getConnection();
064    
065                            StringBundler sb = new StringBundler(3);
066    
067                            sb.append("select indexname, tablename, indexdef from pg_indexes ");
068                            sb.append("where indexname like 'liferay_%' or indexname like ");
069                            sb.append("'ix_%'");
070    
071                            String sql = sb.toString();
072    
073                            ps = con.prepareStatement(sql);
074    
075                            rs = ps.executeQuery();
076    
077                            while (rs.next()) {
078                                    String indexName = rs.getString("indexname");
079                                    String tableName = rs.getString("tablename");
080                                    String indexSQL = rs.getString("indexdef").toLowerCase().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(con, ps, rs);
093                    }
094    
095                    return indexes;
096            }
097    
098            protected PostgreSQLDB() {
099                    super(TYPE_POSTGRESQL);
100            }
101    
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(
120                            readFile(
121                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
122                                            "-postgresql.sql"));
123                    sb.append("\n\n");
124                    sb.append(readFile(sqlDir + "/indexes/indexes-postgresql.sql"));
125                    sb.append("\n\n");
126                    sb.append(readFile(sqlDir + "/sequences/sequences-postgresql.sql"));
127    
128                    return sb.toString();
129            }
130    
131            protected String getServerName() {
132                    return "postgresql";
133            }
134    
135            protected String[] getTemplate() {
136                    return _POSTGRESQL;
137            }
138    
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.indexOf(DROP_INDEX) != -1) {
164                                    String[] tokens = StringUtil.split(line, " ");
165    
166                                    line = StringUtil.replace(
167                                            "drop index @index@;", "@index@", tokens[2]);
168                            }
169                            else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
170                                    String[] tokens = StringUtil.split(line, " ");
171    
172                                    line = StringUtil.replace(
173                                            "alter table @table@ drop constraint @table@_pkey;",
174                                            "@table@", tokens[2]);
175                            }
176    
177                            sb.append(line);
178                            sb.append("\n");
179                    }
180    
181                    unsyncBufferedReader.close();
182    
183                    return sb.toString();
184            }
185    
186            private static String[] _POSTGRESQL = {
187                    "--", "true", "false",
188                    "'01/01/1970'", "current_timestamp",
189                    " bytea", " bool", " timestamp",
190                    " double precision", " integer", " bigint",
191                    " text", " text", " varchar",
192                    "", "commit"
193            };
194    
195            private static PostgreSQLDB _instance = new PostgreSQLDB();
196    
197    }