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 FirebirdUtil 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 template = removeInserts(template);
53 template = removeNull(template);
54
55 return template;
56 }
57
58 protected FirebirdUtil() {
59 }
60
61 protected void buildCreateFile(String databaseName, boolean minimal)
62 throws IOException {
63
64 String minimalSuffix = getMinimalSuffix(minimal);
65
66 File file = new File(
67 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
68 "-firebird.sql");
69
70 StringBuilder sb = new StringBuilder();
71
72 sb.append(
73 "create database '" + databaseName +
74 ".gdb' page_size 8192 user 'sysdba' password 'masterkey';\n");
75 sb.append(
76 "connect '" + databaseName +
77 ".gdb' user 'sysdba' password 'masterkey';\n");
78 sb.append(
79 readSQL(
80 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
81 "-firebird.sql",
82 _FIREBIRD[0], ";\n"));
83
84 FileUtil.write(file, sb.toString());
85 }
86
87 protected String getServerName() {
88 return "firebird";
89 }
90
91 protected String[] getTemplate() {
92 return _FIREBIRD;
93 }
94
95 protected String reword(String data) throws IOException {
96 BufferedReader br = new BufferedReader(new StringReader(data));
97
98 StringBuilder sb = new StringBuilder();
99
100 String line = null;
101
102 while ((line = br.readLine()) != null) {
103 if (line.startsWith(ALTER_COLUMN_TYPE)) {
104 String[] template = buildColumnTypeTokens(line);
105
106 line = StringUtil.replace(
107 "alter table @table@ alter column \"@old-column@\" " +
108 "type @type@;",
109 REWORD_TEMPLATE, template);
110 }
111 else if (line.startsWith(ALTER_COLUMN_NAME)) {
112 String[] template = buildColumnNameTokens(line);
113
114 line = StringUtil.replace(
115 "alter table @table@ alter column \"@old-column@\" to " +
116 "\"@new-column@\";",
117 REWORD_TEMPLATE, template);
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[] _FIREBIRD = {
130 "--", "1", "0",
131 "'01/01/1970'", "current_timestamp",
132 " blob", " smallint", " timestamp",
133 " double precision", " integer", " int64",
134 " varchar(4000)", " blob", " varchar",
135 "", "commit"
136 };
137
138 private static FirebirdUtil _instance = new FirebirdUtil();
139
140 }