1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.db;
24  
25  import com.liferay.portal.kernel.dao.db.DB;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.StringReader;
32  
33  /**
34   * <a href="PostgreSQLDB.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alexander Chow
37   * @author Sandeep Soni
38   * @author Ganesh Ram
39   */
40  public class PostgreSQLDB extends BaseDB {
41  
42      public static DB getInstance() {
43          return _instance;
44      }
45  
46      public String buildSQL(String template) throws IOException {
47          template = convertTimestamp(template);
48          template = replaceTemplate(template, getTemplate());
49  
50          template = reword(template);
51  
52          return template;
53      }
54  
55      protected PostgreSQLDB() {
56          super(TYPE_POSTGRESQL);
57      }
58  
59      protected String buildCreateFileContent(String databaseName, int population)
60          throws IOException {
61  
62          String suffix = getSuffix(population);
63  
64          StringBuilder sb = new StringBuilder();
65  
66          sb.append("drop database " + databaseName + ";\n");
67          sb.append(
68              "create database " + databaseName + " encoding = 'UNICODE';\n");
69          sb.append("\\c " + databaseName + ";\n\n");
70          sb.append(
71              FileUtil.read(
72                  "../sql/portal" + suffix + "/portal" + suffix +
73                      "-postgresql.sql"));
74          sb.append("\n\n");
75          sb.append(FileUtil.read("../sql/indexes/indexes-postgresql.sql"));
76          sb.append("\n\n");
77          sb.append(FileUtil.read("../sql/sequences/sequences-postgresql.sql"));
78  
79          return sb.toString();
80      }
81  
82      protected String getServerName() {
83          return "postgresql";
84      }
85  
86      protected String[] getTemplate() {
87          return _POSTGRESQL;
88      }
89  
90      protected String reword(String data) throws IOException {
91          BufferedReader br = new BufferedReader(new StringReader(data));
92  
93          StringBuilder sb = new StringBuilder();
94  
95          String line = null;
96  
97          while ((line = br.readLine()) != null) {
98              if (line.startsWith(ALTER_COLUMN_NAME)) {
99                  String[] template = buildColumnNameTokens(line);
100 
101                 line = StringUtil.replace(
102                     "alter table @table@ rename @old-column@ to @new-column@;",
103                     REWORD_TEMPLATE, template);
104             }
105             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
106                 String[] template = buildColumnTypeTokens(line);
107 
108                 line = StringUtil.replace(
109                     "alter table @table@ alter @old-column@ type @type@ " +
110                         "using @old-column@::@type@;",
111                     REWORD_TEMPLATE, template);
112             }
113             else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
114                 String[] tokens = StringUtil.split(line, " ");
115 
116                 line = StringUtil.replace(
117                     "alter table @table@ drop constraint @table@_pkey;",
118                     "@table@", tokens[2]);
119             }
120 
121             sb.append(line);
122             sb.append("\n");
123         }
124 
125         br.close();
126 
127         return sb.toString();
128     }
129 
130     private static String[] _POSTGRESQL = {
131         "--", "true", "false",
132         "'01/01/1970'", "current_timestamp",
133         " bytea", " bool", " timestamp",
134         " double precision", " integer", " bigint",
135         " text", " text", " varchar",
136         "", "commit"
137     };
138 
139     private static PostgreSQLDB _instance = new PostgreSQLDB();
140 
141 }