1
22
23 package com.liferay.portal.tools.sql;
24
25 import com.liferay.portal.kernel.util.StringUtil;
26
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.StringReader;
30
31
38 public class FirebirdUtil extends DBUtil {
39
40 public static DBUtil getInstance() {
41 return _instance;
42 }
43
44 public String buildSQL(String template) throws IOException {
45 template = convertTimestamp(template);
46 template = replaceTemplate(template, getTemplate());
47
48 template = reword(template);
49 template = removeInserts(template);
50 template = removeNull(template);
51
52 return template;
53 }
54
55 protected FirebirdUtil() {
56 super(TYPE_FIREBIRD);
57 }
58
59 protected FirebirdUtil(String type) {
60 super(type);
61 }
62
63 protected String buildCreateFileContent(String databaseName, int population)
64 throws IOException {
65
66 String suffix = getSuffix(population);
67
68 StringBuilder sb = new StringBuilder();
69
70 sb.append(
71 "create database '" + databaseName +
72 ".gdb' page_size 8192 user 'sysdba' password 'masterkey';\n");
73 sb.append(
74 "connect '" + databaseName +
75 ".gdb' user 'sysdba' password 'masterkey';\n");
76 sb.append(
77 readSQL(
78 "../sql/portal" + suffix + "/portal" + suffix + "-firebird.sql",
79 _FIREBIRD[0], ";\n"));
80
81 return sb.toString();
82 }
83
84 protected String getServerName() {
85 return "firebird";
86 }
87
88 protected String[] getTemplate() {
89 return _FIREBIRD;
90 }
91
92 protected String reword(String data) throws IOException {
93 BufferedReader br = new BufferedReader(new StringReader(data));
94
95 StringBuilder sb = new StringBuilder();
96
97 String line = null;
98
99 while ((line = br.readLine()) != null) {
100 if (line.startsWith(ALTER_COLUMN_NAME)) {
101 String[] template = buildColumnNameTokens(line);
102
103 line = StringUtil.replace(
104 "alter table @table@ alter column \"@old-column@\" to " +
105 "\"@new-column@\";",
106 REWORD_TEMPLATE, template);
107 }
108 else 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
117 sb.append(line);
118 sb.append("\n");
119 }
120
121 br.close();
122
123 return sb.toString();
124 }
125
126 private static String[] _FIREBIRD = {
127 "--", "1", "0",
128 "'01/01/1970'", "current_timestamp",
129 " blob", " smallint", " timestamp",
130 " double precision", " integer", " int64",
131 " varchar(4000)", " blob", " varchar",
132 "", "commit"
133 };
134
135 private static FirebirdUtil _instance = new FirebirdUtil();
136
137 }