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