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