1
19
20 package com.liferay.portal.tools.sql;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.FileUtil;
25 import com.liferay.portal.kernel.util.StringUtil;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.StringReader;
31
32
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 = replaceTemplate(template, getTemplate());
49
50 template = reword(template );
51 template = removeNull(template);
53 template = StringUtil.replace(template , "\\'", "''");
54
55 return template;
56 }
57
58 public boolean isSupportsAlterColumnName() {
59 return _SUPPORTS_ALTER_COLUMN_NAME;
60 }
61
62 public boolean isSupportsAlterColumnType() {
63 return _SUPPORTS_ALTER_COLUMN_TYPE;
64 }
65
66 protected DerbyUtil() {
67 super(TYPE_DERBY);
68 }
69
70 protected void buildCreateFile(String databaseName, boolean minimal)
71 throws IOException {
72
73 String minimalSuffix = getMinimalSuffix(minimal);
74
75 File file = new File(
76 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
77 "-derby.sql");
78
79 StringBuilder sb = new StringBuilder();
80
81 sb.append("drop database " + databaseName + ";\n");
82 sb.append("create database " + databaseName + ";\n");
83 sb.append("connect to " + databaseName + ";\n");
84 sb.append(
85 FileUtil.read(
86 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
87 "-derby.sql"));
88 sb.append("\n\n");
89 sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
90 sb.append("\n\n");
91 sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
92
93 FileUtil.write(file, sb.toString());
94 }
95
96 protected String getServerName() {
97 return "derby";
98 }
99
100 protected String[] getTemplate() {
101 return _DERBY;
102 }
103
104 protected String reword(String data) throws IOException {
105 BufferedReader br = new BufferedReader(new StringReader(data));
106
107 StringBuilder sb = new StringBuilder();
108
109 String line = null;
110
111 while ((line = br.readLine()) != null) {
112 if (line.startsWith(ALTER_COLUMN_NAME) ||
113 line.startsWith(ALTER_COLUMN_TYPE)) {
114
115 line = "-- " + line;
116
117 if (_log.isWarnEnabled()) {
118 _log.warn(
119 "This statement is not supported by Derby: " + line);
120 }
121 }
122
123 sb.append(line);
124 sb.append("\n");
125 }
126
127 br.close();
128
129 return sb.toString();
130 }
131
132 private static String[] _DERBY = {
133 "--", "1", "0",
134 "'1970-01-01-00.00.00.000000'", "current timestamp",
135 " blob", " smallint", " timestamp",
136 " double", " integer", " bigint",
137 " varchar(4000)", " clob", " varchar",
138 " generated always as identity", "commit"
139 };
140
141 private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
142
143 private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
144
145 private static Log _log = LogFactoryUtil.getLog(DerbyUtil.class);
146
147 private static DerbyUtil _instance = new DerbyUtil();
148
149 }