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.dao.db.Index;
019 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
021 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringUtil;
026
027 import java.io.IOException;
028
029 import java.sql.Connection;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032 import java.sql.SQLException;
033
034 import java.util.ArrayList;
035 import java.util.List;
036 import java.util.regex.Matcher;
037 import java.util.regex.Pattern;
038
039
044 public class OracleDB extends BaseDB {
045
046 public static DB getInstance() {
047 return _instance;
048 }
049
050 @Override
051 public String buildSQL(String template) throws IOException {
052 template = _preBuildSQL(template);
053 template = _postBuildSQL(template);
054
055 return template;
056 }
057
058 @Override
059 public void buildSQLFile(String sqlDir, String fileName)
060 throws IOException {
061
062 String oracle = buildTemplate(sqlDir, fileName);
063
064 oracle = _preBuildSQL(oracle);
065
066 StringBundler imageSB = new StringBundler();
067 StringBundler journalArticleSB = new StringBundler();
068
069 try (UnsyncBufferedReader unsyncBufferedReader =
070 new UnsyncBufferedReader(new UnsyncStringReader(oracle))) {
071
072 String line = null;
073
074 while ((line = unsyncBufferedReader.readLine()) != null) {
075 if (line.startsWith("insert into Image")) {
076 _convertToOracleCSV(line, imageSB);
077 }
078 else if (line.startsWith("insert into JournalArticle (")) {
079 _convertToOracleCSV(line, journalArticleSB);
080 }
081 }
082 }
083
084 if (imageSB.length() > 0) {
085 FileUtil.write(
086 sqlDir + "/" + fileName + "/" + fileName + "-oracle-image.csv",
087 imageSB.toString());
088 }
089
090 if (journalArticleSB.length() > 0) {
091 FileUtil.write(
092 sqlDir + "/" + fileName + "/" + fileName +
093 "-oracle-journalarticle.csv",
094 journalArticleSB.toString());
095 }
096
097 oracle = _postBuildSQL(oracle);
098
099 FileUtil.write(
100 sqlDir + "/" + fileName + "/" + fileName + "-oracle.sql", oracle);
101 }
102
103 @Override
104 public List<Index> getIndexes(Connection con) throws SQLException {
105 List<Index> indexes = new ArrayList<Index>();
106
107 PreparedStatement ps = null;
108 ResultSet rs = null;
109
110 try {
111 StringBundler sb = new StringBundler(3);
112
113 sb.append("select index_name, table_name, uniqueness from ");
114 sb.append("user_indexes where index_name like 'LIFERAY_%' or ");
115 sb.append("index_name like 'IX_%'");
116
117 String sql = sb.toString();
118
119 ps = con.prepareStatement(sql);
120
121 rs = ps.executeQuery();
122
123 while (rs.next()) {
124 String indexName = rs.getString("index_name");
125 String tableName = rs.getString("table_name");
126 String uniqueness = rs.getString("uniqueness");
127
128 boolean unique = true;
129
130 if (StringUtil.equalsIgnoreCase(uniqueness, "NONUNIQUE")) {
131 unique = false;
132 }
133
134 indexes.add(new Index(indexName, tableName, unique));
135 }
136 }
137 finally {
138 DataAccess.cleanUp(null, ps, rs);
139 }
140
141 return indexes;
142 }
143
144 @Override
145 public boolean isSupportsInlineDistinct() {
146 return _SUPPORTS_INLINE_DISTINCT;
147 }
148
149 protected OracleDB() {
150 super(TYPE_ORACLE);
151 }
152
153 @Override
154 protected String buildCreateFileContent(
155 String sqlDir, String databaseName, int population)
156 throws IOException {
157
158 String suffix = getSuffix(population);
159
160 StringBundler sb = new StringBundler(13);
161
162 sb.append("drop user &1 cascade;\n");
163 sb.append("create user &1 identified by &2;\n");
164 sb.append("grant connect,resource to &1;\n");
165
166 if (population != BARE) {
167 sb.append("connect &1/&2;\n");
168 sb.append("set define off;\n");
169 sb.append("\n");
170 sb.append(getCreateTablesContent(sqlDir, suffix));
171 sb.append("\n\n");
172 sb.append(readFile(sqlDir + "/indexes/indexes-oracle.sql"));
173 sb.append("\n\n");
174 sb.append(readFile(sqlDir + "/sequences/sequences-oracle.sql"));
175 sb.append("\n");
176 }
177
178 sb.append("quit");
179
180 return sb.toString();
181 }
182
183 @Override
184 protected String getServerName() {
185 return "oracle";
186 }
187
188 @Override
189 protected String[] getTemplate() {
190 return _ORACLE;
191 }
192
193 @Override
194 protected String replaceTemplate(String template, String[] actual) {
195
196
197
198 Matcher matcher = _varcharPattern.matcher(template);
199
200 StringBuffer sb = new StringBuffer();
201
202 while (matcher.find()) {
203 int size = GetterUtil.getInteger(matcher.group(1));
204
205 if (size > 4000) {
206 size = 4000;
207 }
208
209 matcher.appendReplacement(sb, "VARCHAR2(" + size + " CHAR)");
210 }
211
212 matcher.appendTail(sb);
213
214 template = sb.toString();
215
216 return super.replaceTemplate(template, actual);
217 }
218
219 @Override
220 protected String reword(String data) throws IOException {
221 try (UnsyncBufferedReader unsyncBufferedReader =
222 new UnsyncBufferedReader(new UnsyncStringReader(data))) {
223
224 StringBundler sb = new StringBundler();
225
226 String line = null;
227
228 while ((line = unsyncBufferedReader.readLine()) != null) {
229 if (line.startsWith(ALTER_COLUMN_NAME)) {
230 String[] template = buildColumnNameTokens(line);
231
232 line = StringUtil.replace(
233 "alter table @table@ rename column @old-column@ to " +
234 "@new-column@;",
235 REWORD_TEMPLATE, template);
236 }
237 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
238 String[] template = buildColumnTypeTokens(line);
239
240 line = StringUtil.replace(
241 "alter table @table@ modify @old-column@ @type@;",
242 REWORD_TEMPLATE, template);
243 }
244 else if (line.startsWith(ALTER_TABLE_NAME)) {
245 String[] template = buildTableNameTokens(line);
246
247 line = StringUtil.replace(
248 "alter table @old-table@ rename to @new-table@;",
249 RENAME_TABLE_TEMPLATE, template);
250 }
251 else if (line.contains(DROP_INDEX)) {
252 String[] tokens = StringUtil.split(line, ' ');
253
254 line = StringUtil.replace(
255 "drop index @index@;", "@index@", tokens[2]);
256 }
257
258 sb.append(line);
259 sb.append("\n");
260 }
261
262 return sb.toString();
263 }
264 }
265
266 private void _convertToOracleCSV(String line, StringBundler sb) {
267 int x = line.indexOf("values (");
268 int y = line.lastIndexOf(");");
269
270 line = line.substring(x + 8, y);
271
272 line = StringUtil.replace(line, "sysdate, ", "20050101, ");
273
274 sb.append(line);
275 sb.append("\n");
276 }
277
278 private String _postBuildSQL(String template) throws IOException {
279 template = removeLongInserts(template);
280 template = StringUtil.replace(template, "\\n", "'||CHR(10)||'");
281
282 return template;
283 }
284
285 private String _preBuildSQL(String template) throws IOException {
286 template = convertTimestamp(template);
287 template = replaceTemplate(template, getTemplate());
288
289 template = reword(template);
290 template = StringUtil.replace(
291 template,
292 new String[] {"\\\\", "\\'", "\\\""},
293 new String[] {"\\", "''", "\""});
294
295 return template;
296 }
297
298 private static final String[] _ORACLE = {
299 "--", "1", "0",
300 "to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')", "sysdate",
301 " blob", " blob", " number(1, 0)", " timestamp", " number(30,20)",
302 " number(30,0)", " number(30,0)", " varchar2(4000)", " clob",
303 " varchar2", "", "commit"
304 };
305
306 private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
307
308 private static final OracleDB _instance = new OracleDB();
309
310 private static final Pattern _varcharPattern = Pattern.compile(
311 "VARCHAR\\((\\d+)\\)");
312
313 }