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 protected DerbyUtil() {
62 super(TYPE_DERBY);
63 }
64
65 protected void buildCreateFile(String databaseName, boolean minimal)
66 throws IOException {
67
68 String minimalSuffix = getMinimalSuffix(minimal);
69
70 File file = new File(
71 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
72 "-derby.sql");
73
74 StringBuilder sb = new StringBuilder();
75
76 sb.append("drop database " + databaseName + ";\n");
77 sb.append("create database " + databaseName + ";\n");
78 sb.append("connect to " + databaseName + ";\n");
79 sb.append(
80 FileUtil.read(
81 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
82 "-derby.sql"));
83 sb.append("\n\n");
84 sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
85 sb.append("\n\n");
86 sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
87
88 FileUtil.write(file, sb.toString());
89 }
90
91 protected String getServerName() {
92 return "derby";
93 }
94
95 protected String[] getTemplate() {
96 return _DERBY;
97 }
98
99 protected String reword(String data) throws IOException {
100 BufferedReader br = new BufferedReader(new StringReader(data));
101
102 StringBuilder sb = new StringBuilder();
103
104 String line = null;
105
106 while ((line = br.readLine()) != null) {
107 if (line.startsWith(ALTER_COLUMN_TYPE) ||
108 line.startsWith(ALTER_COLUMN_NAME)) {
109
110 line = "-- " + line;
111
112 if (_log.isWarnEnabled()) {
113 _log.warn(
114 "This statement is not supported by Derby: " + line);
115 }
116 }
117
118 sb.append(line);
119 sb.append("\n");
120 }
121
122 br.close();
123
124 return sb.toString();
125 }
126
127 private static String[] _DERBY = {
128 "--", "1", "0",
129 "'1970-01-01-00.00.00.000000'", "current timestamp",
130 " blob", " smallint", " timestamp",
131 " double", " integer", " bigint",
132 " varchar(4000)", " clob", " varchar",
133 " generated always as identity", "commit"
134 };
135
136 private static Log _log = LogFactoryUtil.getLog(DBUtil.class);
137
138 private static DerbyUtil _instance = new DerbyUtil();
139
140 }