1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.db;
24  
25  import com.liferay.portal.kernel.dao.db.DB;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.StringReader;
32  
33  /**
34   * <a href="OracleDB.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alexander Chow
37   * @author Sandeep Soni
38   * @author Ganesh Ram
39   */
40  public class OracleDB extends BaseDB {
41  
42      public static DB getInstance() {
43          return _instance;
44      }
45  
46      public String buildSQL(String template) throws IOException {
47          template = _preBuildSQL(template);
48          template = _postBuildSQL(template);
49  
50          return template;
51      }
52  
53      public void buildSQLFile(String fileName) throws IOException {
54          String oracle = buildTemplate(fileName);
55  
56          oracle = _preBuildSQL(oracle);
57  
58          BufferedReader br = new BufferedReader(new StringReader(oracle));
59  
60          StringBuilder imageSB = new StringBuilder();
61          StringBuilder journalArticleSB = new StringBuilder();
62          StringBuilder journalStructureSB = new StringBuilder();
63          StringBuilder journalTemplateSB = new StringBuilder();
64  
65          String line = null;
66  
67          while ((line = br.readLine()) != null) {
68              if (line.startsWith("insert into Image")) {
69                  _convertToOracleCSV(line, imageSB);
70              }
71              else if (line.startsWith("insert into JournalArticle (")) {
72                  _convertToOracleCSV(line, journalArticleSB);
73              }
74              else if (line.startsWith("insert into JournalStructure (")) {
75                  _convertToOracleCSV(line, journalStructureSB);
76              }
77              else if (line.startsWith("insert into JournalTemplate (")) {
78                  _convertToOracleCSV(line, journalTemplateSB);
79              }
80          }
81  
82          br.close();
83  
84          if (imageSB.length() > 0) {
85              FileUtil.write(
86                  "../sql/" + fileName + "/" + fileName + "-oracle-image.csv",
87                  imageSB.toString());
88          }
89  
90          if (journalArticleSB.length() > 0) {
91              FileUtil.write(
92                  "../sql/" + fileName + "/" + fileName +
93                      "-oracle-journalarticle.csv",
94                  journalArticleSB.toString());
95          }
96  
97          if (journalStructureSB.length() > 0) {
98              FileUtil.write(
99                  "../sql/" + fileName + "/" + fileName +
100                     "-oracle-journalstructure.csv",
101                 journalStructureSB.toString());
102         }
103 
104         if (journalTemplateSB.length() > 0) {
105             FileUtil.write(
106                 "../sql/" + fileName + "/" + fileName +
107                     "-oracle-journaltemplate.csv",
108                 journalTemplateSB.toString());
109         }
110 
111         oracle = _postBuildSQL(oracle);
112 
113         FileUtil.write(
114             "../sql/" + fileName + "/" + fileName + "-oracle.sql", oracle);
115     }
116 
117     protected OracleDB() {
118         super(TYPE_ORACLE);
119     }
120 
121     protected String buildCreateFileContent(String databaseName, int population)
122         throws IOException {
123 
124         String suffix = getSuffix(population);
125 
126         StringBuilder sb = new StringBuilder();
127 
128         sb.append("drop user &1 cascade;\n");
129         sb.append("create user &1 identified by &2;\n");
130         sb.append("grant connect,resource to &1;\n");
131         sb.append("connect &1/&2;\n");
132         sb.append("set define off;\n");
133         sb.append("\n");
134         sb.append(
135             FileUtil.read(
136                 "../sql/portal" + suffix + "/portal" + suffix + "-oracle.sql"));
137         sb.append("\n\n");
138         sb.append(FileUtil.read("../sql/indexes/indexes-oracle.sql"));
139         sb.append("\n\n");
140         sb.append(FileUtil.read("../sql/sequences/sequences-oracle.sql"));
141         sb.append("\n");
142         sb.append("quit");
143 
144         return sb.toString();
145     }
146 
147     protected String getServerName() {
148         return "oracle";
149     }
150 
151     protected String[] getTemplate() {
152         return _ORACLE;
153     }
154 
155     protected String reword(String data) throws IOException {
156         BufferedReader br = new BufferedReader(new StringReader(data));
157 
158         StringBuilder sb = new StringBuilder();
159 
160         String line = null;
161 
162         while ((line = br.readLine()) != null) {
163             if (line.startsWith(ALTER_COLUMN_NAME)) {
164                 String[] template = buildColumnNameTokens(line);
165 
166                 line = StringUtil.replace(
167                     "alter table @table@ rename column @old-column@ to " +
168                         "@new-column@;",
169                     REWORD_TEMPLATE, template);
170             }
171             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
172                 String[] template = buildColumnTypeTokens(line);
173 
174                 line = StringUtil.replace(
175                     "alter table @table@ modify @old-column@ @type@;",
176                     REWORD_TEMPLATE, template);
177             }
178 
179             sb.append(line);
180             sb.append("\n");
181         }
182 
183         br.close();
184 
185         return sb.toString();
186     }
187 
188     private void _convertToOracleCSV(String line, StringBuilder sb) {
189         int x = line.indexOf("values (");
190         int y = line.lastIndexOf(");");
191 
192         line = line.substring(x + 8, y);
193 
194         line = StringUtil.replace(line, "sysdate, ", "20050101, ");
195 
196         sb.append(line);
197         sb.append("\n");
198     }
199 
200     private String _preBuildSQL(String template) throws IOException {
201         template = convertTimestamp(template);
202         template = replaceTemplate(template, getTemplate());
203 
204         template = reword(template);
205         template = StringUtil.replace(
206             template,
207             new String[] {"\\\\", "\\'", "\\\""},
208             new String[] {"\\", "''", "\""});
209 
210         return template;
211     }
212 
213     private String _postBuildSQL(String template) throws IOException {
214         template = removeLongInserts(template);
215         template = StringUtil.replace(template, "\\n", "'||CHR(10)||'");
216 
217         return template;
218     }
219 
220     private static String[] _ORACLE = {
221         "--", "1", "0",
222         "to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')", "sysdate",
223         " blob", " number(1, 0)", " timestamp",
224         " number(30,20)", " number(30,0)", " number(30,0)",
225         " varchar2(4000)", " clob", " varchar2",
226         "", "commit"
227     };
228 
229     private static OracleDB _instance = new OracleDB();
230 
231 }