1
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.File;
30 import java.io.IOException;
31 import java.io.StringReader;
32
33
41 public class PostgreSQLUtil extends DBUtil {
42
43 public static DBUtil getInstance() {
44 return _instance;
45 }
46
47 public String buildSQL(String template) throws IOException {
48 template = convertTimestamp(template);
49 template = replaceTemplate(template, getTemplate());
50
51 template = reword(template);
52
53 return template;
54 }
55
56 protected PostgreSQLUtil() {
57 super(TYPE_POSTGRESQL);
58 }
59
60 protected void buildCreateFile(String databaseName, boolean minimal)
61 throws IOException {
62
63 String minimalSuffix = getMinimalSuffix(minimal);
64
65 File file = new File(
66 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
67 "-postgresql.sql");
68
69 StringBuilder sb = new StringBuilder();
70
71 sb.append("drop database " + databaseName + ";\n");
72 sb.append(
73 "create database " + databaseName + " encoding = 'UNICODE';\n");
74 sb.append("\\c " + databaseName + ";\n\n");
75 sb.append(
76 FileUtil.read(
77 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
78 "-postgresql.sql"));
79 sb.append("\n\n");
80 sb.append(FileUtil.read("../sql/indexes/indexes-postgresql.sql"));
81 sb.append("\n\n");
82 sb.append(FileUtil.read("../sql/sequences/sequences-postgresql.sql"));
83
84 FileUtil.write(file, sb.toString());
85 }
86
87 protected String getServerName() {
88 return "postgresql";
89 }
90
91 protected String[] getTemplate() {
92 return _POSTGRESQL;
93 }
94
95 protected String reword(String data) throws IOException {
96 BufferedReader br = new BufferedReader(new StringReader(data));
97
98 StringBuilder sb = new StringBuilder();
99
100 String line = null;
101
102 while ((line = br.readLine()) != null) {
103 if (line.startsWith(ALTER_COLUMN_NAME)) {
104 String[] template = buildColumnNameTokens(line);
105
106 line = StringUtil.replace(
107 "alter table @table@ rename @old-column@ to @new-column@;",
108 REWORD_TEMPLATE, template);
109 }
110 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
111 String[] template = buildColumnTypeTokens(line);
112
113 line = StringUtil.replace(
114 "alter table @table@ alter @old-column@ type @type@ " +
115 "using @old-column@::@type@;",
116 REWORD_TEMPLATE, template);
117 }
118 else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
119 String[] tokens = StringUtil.split(line, " ");
120
121 line = StringUtil.replace(
122 "alter table @table@ drop constraint @table@_pkey;",
123 "@table@", tokens[2]);
124 }
125
126 sb.append(line);
127 sb.append("\n");
128 }
129
130 br.close();
131
132 return sb.toString();
133 }
134
135 private static String[] _POSTGRESQL = {
136 "--", "true", "false",
137 "'01/01/1970'", "current_timestamp",
138 " bytea", " bool", " timestamp",
139 " double precision", " integer", " bigint",
140 " text", " text", " varchar",
141 "", "commit"
142 };
143
144 private static PostgreSQLUtil _instance = new PostgreSQLUtil();
145
146 }