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