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.IOException;
30 import java.io.StringReader;
31
32
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 }