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