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