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 DerbyUtil 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 = removeNull(template);
53 template = StringUtil.replace(template , "\\'", "''");
54
55 return template;
56 }
57
58 protected DerbyUtil() {
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 "-derby.sql");
69
70 StringMaker sm = new StringMaker();
71
72 sm.append("drop database " + databaseName + ";\n");
73 sm.append("create database " + databaseName + ";\n");
74 sm.append("connect to " + databaseName + ";\n");
75 sm.append(
76 FileUtil.read(
77 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
78 "-derby.sql"));
79 sm.append("\n\n");
80 sm.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
81 sm.append("\n\n");
82 sm.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
83
84 FileUtil.write(file, sm.toString());
85 }
86
87 protected String getServerName() {
88 return "derby";
89 }
90
91 protected String[] getTemplate() {
92 return _DERBY;
93 }
94
95 protected String reword(String data) throws IOException {
96 BufferedReader br = new BufferedReader(new StringReader(data));
97
98 StringMaker sm = new StringMaker();
99
100 String line = null;
101
102 while ((line = br.readLine()) != null) {
103 if (line.startsWith(ALTER_COLUMN_TYPE) ||
104 line.startsWith(ALTER_COLUMN_NAME)) {
105
106 line = "-- " + line;
107 }
108
109 sm.append(line);
110 sm.append("\n");
111 }
112
113 br.close();
114
115 return sm.toString();
116 }
117
118 private static String[] _DERBY = {
119 "--", "1", "0",
120 "'1970-01-01-00.00.00.000000'", "current timestamp",
121 " smallint", " timestamp", " double",
122 " integer", " bigint",
123 " long varchar", " clob", " varchar",
124 " generated always as identity", "commit"
125 };
126
127 private static DerbyUtil _instance = new DerbyUtil();
128
129 }