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.io.unsync.UnsyncBufferedReader;
27 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.FileUtil;
31 import com.liferay.portal.kernel.util.StringUtil;
32
33 import java.io.IOException;
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 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
103 new UnsyncStringReader(data));
104
105 StringBuilder sb = new StringBuilder();
106
107 String line = null;
108
109 while ((line = unsyncBufferedReader.readLine()) != null) {
110 if (line.startsWith(ALTER_COLUMN_NAME) ||
111 line.startsWith(ALTER_COLUMN_TYPE)) {
112
113 line = "-- " + line;
114
115 if (_log.isWarnEnabled()) {
116 _log.warn(
117 "This statement is not supported by Derby: " + line);
118 }
119 }
120
121 sb.append(line);
122 sb.append("\n");
123 }
124
125 unsyncBufferedReader.close();
126
127 return sb.toString();
128 }
129
130 private static String[] _DERBY = {
131 "--", "1", "0",
132 "'1970-01-01-00.00.00.000000'", "current timestamp",
133 " blob", " smallint", " timestamp",
134 " double", " integer", " bigint",
135 " varchar(4000)", " clob", " varchar",
136 " generated always as identity", "commit"
137 };
138
139 private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
140
141 private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
142
143 private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
144
145 private static DerbyDB _instance = new DerbyDB();
146
147 }