001
014
015 package com.liferay.portal.dao.db;
016
017 import com.liferay.portal.kernel.dao.db.DB;
018 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
019 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringUtil;
025
026 import java.io.IOException;
027
028
033 public class DerbyDB extends BaseDB {
034
035 public static DB getInstance() {
036 return _instance;
037 }
038
039 public String buildSQL(String template) throws IOException {
040 template = convertTimestamp(template);
041 template = replaceTemplate(template, getTemplate());
042
043 template = reword(template );
044
045 template = removeNull(template);
046 template = StringUtil.replace(template , "\\'", "''");
047
048 return template;
049 }
050
051 public boolean isSupportsAlterColumnName() {
052 return _SUPPORTS_ALTER_COLUMN_NAME;
053 }
054
055 public boolean isSupportsAlterColumnType() {
056 return _SUPPORTS_ALTER_COLUMN_TYPE;
057 }
058
059 protected DerbyDB() {
060 super(TYPE_DERBY);
061 }
062
063 protected String buildCreateFileContent(
064 String sqlDir, String databaseName, int population)
065 throws IOException {
066
067 String suffix = getSuffix(population);
068
069 StringBundler sb = new StringBundler(14);
070
071 sb.append("drop database ");
072 sb.append(databaseName);
073 sb.append(";\n");
074 sb.append("create database ");
075 sb.append(databaseName);
076 sb.append(";\n");
077 sb.append("connect to ");
078 sb.append(databaseName);
079 sb.append(";\n");
080 sb.append(
081 FileUtil.read(
082 sqlDir + "/portal" + suffix + "/portal" + suffix +
083 "-derby.sql"));
084 sb.append("\n\n");
085 sb.append(FileUtil.read(sqlDir + "/indexes/indexes-derby.sql"));
086 sb.append("\n\n");
087 sb.append(FileUtil.read(sqlDir + "/sequences/sequences-derby.sql"));
088
089 return sb.toString();
090 }
091
092 protected String getServerName() {
093 return "derby";
094 }
095
096 protected String[] getTemplate() {
097 return _DERBY;
098 }
099
100 protected String reword(String data) throws IOException {
101 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
102 new UnsyncStringReader(data));
103
104 StringBundler sb = new StringBundler();
105
106 String line = null;
107
108 while ((line = unsyncBufferedReader.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 else if (line.indexOf(DROP_INDEX) != -1) {
120 String[] tokens = StringUtil.split(line, " ");
121
122 line = StringUtil.replace(
123 "drop index @index@;", "@index@", tokens[2]);
124 }
125
126 sb.append(line);
127 sb.append("\n");
128 }
129
130 unsyncBufferedReader.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 " varchar(4000)", " 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(DerbyDB.class);
149
150 private static DerbyDB _instance = new DerbyDB();
151
152 }