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