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.IOException;
32 import java.io.StringReader;
33
34
41 public class DerbyUtil 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 = removeNull(template);
54 template = StringUtil.replace(template , "\\'", "''");
55
56 return template;
57 }
58
59 public boolean isSupportsAlterColumnName() {
60 return _SUPPORTS_ALTER_COLUMN_NAME;
61 }
62
63 public boolean isSupportsAlterColumnType() {
64 return _SUPPORTS_ALTER_COLUMN_TYPE;
65 }
66
67 protected DerbyUtil() {
68 super(TYPE_DERBY);
69 }
70
71 protected String buildCreateFileContent(String databaseName, int population)
72 throws IOException {
73
74 String suffix = getSuffix(population);
75
76 StringBuilder sb = new StringBuilder();
77
78 sb.append("drop database " + databaseName + ";\n");
79 sb.append("create database " + databaseName + ";\n");
80 sb.append("connect to " + databaseName + ";\n");
81 sb.append(
82 FileUtil.read(
83 "../sql/portal" + suffix + "/portal" + suffix + "-derby.sql"));
84 sb.append("\n\n");
85 sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
86 sb.append("\n\n");
87 sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
88
89 return sb.toString();
90 }
91
92 protected String getServerName() {
93 return "derby";
94 }
95
96 protected String[] getTemplate() {
97 return _DERBY;
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_NAME) ||
109 line.startsWith(ALTER_COLUMN_TYPE)) {
110
111 line = "-- " + line;
112
113 if (_log.isWarnEnabled()) {
114 _log.warn(
115 "This statement is not supported by Derby: " + line);
116 }
117 }
118
119 sb.append(line);
120 sb.append("\n");
121 }
122
123 br.close();
124
125 return sb.toString();
126 }
127
128 private static String[] _DERBY = {
129 "--", "1", "0",
130 "'1970-01-01-00.00.00.000000'", "current timestamp",
131 " blob", " smallint", " timestamp",
132 " double", " integer", " bigint",
133 " long varchar", " clob", " varchar",
134 " generated always as identity", "commit"
135 };
136
137 private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
138
139 private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
140
141 private static Log _log = LogFactoryUtil.getLog(DerbyUtil.class);
142
143 private static DerbyUtil _instance = new DerbyUtil();
144
145 }