1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.tools.sql;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  
28  import java.io.BufferedReader;
29  import java.io.IOException;
30  import java.io.StringReader;
31  
32  /**
33   * <a href="PostgreSQLUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alexander Chow
36   * @author Sandeep Soni
37   * @author Ganesh Ram
38   */
39  public class PostgreSQLUtil extends DBUtil {
40  
41      public static DBUtil getInstance() {
42          return _instance;
43      }
44  
45      public String buildSQL(String template) throws IOException {
46          template = convertTimestamp(template);
47          template = replaceTemplate(template, getTemplate());
48  
49          template = reword(template);
50  
51          return template;
52      }
53  
54      protected PostgreSQLUtil() {
55          super(TYPE_POSTGRESQL);
56      }
57  
58      protected String buildCreateFileContent(String databaseName, int population)
59          throws IOException {
60  
61          String suffix = getSuffix(population);
62  
63          StringBuilder sb = new StringBuilder();
64  
65          sb.append("drop database " + databaseName + ";\n");
66          sb.append(
67              "create database " + databaseName + " encoding = 'UNICODE';\n");
68          sb.append("\\c " + databaseName + ";\n\n");
69          sb.append(
70              FileUtil.read(
71                  "../sql/portal" + suffix + "/portal" + suffix +
72                      "-postgresql.sql"));
73          sb.append("\n\n");
74          sb.append(FileUtil.read("../sql/indexes/indexes-postgresql.sql"));
75          sb.append("\n\n");
76          sb.append(FileUtil.read("../sql/sequences/sequences-postgresql.sql"));
77  
78          return sb.toString();
79      }
80  
81      protected String getServerName() {
82          return "postgresql";
83      }
84  
85      protected String[] getTemplate() {
86          return _POSTGRESQL;
87      }
88  
89      protected String reword(String data) throws IOException {
90          BufferedReader br = new BufferedReader(new StringReader(data));
91  
92          StringBuilder sb = new StringBuilder();
93  
94          String line = null;
95  
96          while ((line = br.readLine()) != null) {
97              if (line.startsWith(ALTER_COLUMN_NAME)) {
98                  String[] template = buildColumnNameTokens(line);
99  
100                 line = StringUtil.replace(
101                     "alter table @table@ rename @old-column@ to @new-column@;",
102                     REWORD_TEMPLATE, template);
103             }
104             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
105                 String[] template = buildColumnTypeTokens(line);
106 
107                 line = StringUtil.replace(
108                     "alter table @table@ alter @old-column@ type @type@ " +
109                         "using @old-column@::@type@;",
110                     REWORD_TEMPLATE, template);
111             }
112             else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
113                 String[] tokens = StringUtil.split(line, " ");
114 
115                 line = StringUtil.replace(
116                     "alter table @table@ drop constraint @table@_pkey;",
117                     "@table@", tokens[2]);
118             }
119 
120             sb.append(line);
121             sb.append("\n");
122         }
123 
124         br.close();
125 
126         return sb.toString();
127     }
128 
129     private static String[] _POSTGRESQL = {
130         "--", "true", "false",
131         "'01/01/1970'", "current_timestamp",
132         " bytea", " bool", " timestamp",
133         " double precision", " integer", " bigint",
134         " text", " text", " varchar",
135         "", "commit"
136     };
137 
138     private static PostgreSQLUtil _instance = new PostgreSQLUtil();
139 
140 }