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