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