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