001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools.sql;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.util.FileUtil;
026:
027: import java.io.BufferedReader;
028: import java.io.File;
029: import java.io.IOException;
030: import java.io.StringReader;
031:
032: /**
033: * <a href="OracleUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Alexander Chow
036: *
037: */
038: public class OracleUtil extends DBUtil {
039:
040: public static DBUtil getInstance() {
041: return _instance;
042: }
043:
044: public String buildSQL(String template) throws IOException {
045: template = _preBuildSQL(template);
046: template = _postBuildSQL(template);
047:
048: return template;
049: }
050:
051: public void buildSQLFile(String fileName) throws IOException {
052: String oracle = buildTemplate(fileName);
053:
054: oracle = _preBuildSQL(oracle);
055:
056: BufferedReader br = new BufferedReader(new StringReader(oracle));
057:
058: StringMaker imageSM = new StringMaker();
059: StringMaker journalArticleSM = new StringMaker();
060: StringMaker journalStructureSM = new StringMaker();
061: StringMaker journalTemplateSM = new StringMaker();
062:
063: String line = null;
064:
065: while ((line = br.readLine()) != null) {
066: if (line.startsWith("insert into Image")) {
067: _convertToOracleCSV(line, imageSM);
068: } else if (line.startsWith("insert into JournalArticle (")) {
069: _convertToOracleCSV(line, journalArticleSM);
070: } else if (line
071: .startsWith("insert into JournalStructure (")) {
072: _convertToOracleCSV(line, journalStructureSM);
073: } else if (line.startsWith("insert into JournalTemplate (")) {
074: _convertToOracleCSV(line, journalTemplateSM);
075: }
076: }
077:
078: br.close();
079:
080: if (imageSM.length() > 0) {
081: FileUtil.write("../sql/" + fileName + "/" + fileName
082: + "-oracle-image.csv", imageSM.toString());
083: }
084:
085: if (journalArticleSM.length() > 0) {
086: FileUtil.write("../sql/" + fileName + "/" + fileName
087: + "-oracle-journalarticle.csv", journalArticleSM
088: .toString());
089: }
090:
091: if (journalStructureSM.length() > 0) {
092: FileUtil.write("../sql/" + fileName + "/" + fileName
093: + "-oracle-journalstructure.csv",
094: journalStructureSM.toString());
095: }
096:
097: if (journalTemplateSM.length() > 0) {
098: FileUtil.write("../sql/" + fileName + "/" + fileName
099: + "-oracle-journaltemplate.csv", journalTemplateSM
100: .toString());
101: }
102:
103: oracle = _postBuildSQL(oracle);
104:
105: FileUtil.write("../sql/" + fileName + "/" + fileName
106: + "-oracle.sql", oracle);
107: }
108:
109: protected OracleUtil() {
110: }
111:
112: protected void buildCreateFile(String databaseName, boolean minimal)
113: throws IOException {
114:
115: String minimalSuffix = getMinimalSuffix(minimal);
116:
117: File file = new File("../sql/create" + minimalSuffix
118: + "/create" + minimalSuffix + "-oracle.sql");
119:
120: StringMaker sm = new StringMaker();
121:
122: sm.append("drop user &1 cascade;\n");
123: sm.append("create user &1 identified by &2;\n");
124: sm.append("grant connect,resource to &1;\n");
125: sm.append("connect &1/&2;\n");
126: sm.append("set define off;\n");
127: sm.append("\n");
128: sm.append(FileUtil.read("../sql/portal" + minimalSuffix
129: + "/portal" + minimalSuffix + "-oracle.sql"));
130: sm.append("\n\n");
131: sm.append(FileUtil.read("../sql/indexes/indexes-oracle.sql"));
132: sm.append("\n\n");
133: sm.append(FileUtil
134: .read("../sql/sequences/sequences-oracle.sql"));
135: sm.append("\n");
136: sm.append("quit");
137:
138: FileUtil.write(file, sm.toString());
139: }
140:
141: protected String getServerName() {
142: return "oracle";
143: }
144:
145: protected String[] getTemplate() {
146: return _ORACLE;
147: }
148:
149: protected String reword(String data) throws IOException {
150: BufferedReader br = new BufferedReader(new StringReader(data));
151:
152: StringMaker sm = new StringMaker();
153:
154: String line = null;
155:
156: while ((line = br.readLine()) != null) {
157: if (line.startsWith(ALTER_COLUMN_TYPE)) {
158: String[] template = buildColumnTypeTokens(line);
159:
160: line = StringUtil
161: .replace(
162: "alter table @table@ modify @old-column@ @type@;",
163: REWORD_TEMPLATE, template);
164: } else if (line.startsWith(ALTER_COLUMN_NAME)) {
165: String[] template = buildColumnNameTokens(line);
166:
167: line = StringUtil.replace(
168: "alter table @table@ rename column @old-column@ to "
169: + "@new-column@;", REWORD_TEMPLATE,
170: template);
171: }
172:
173: sm.append(line);
174: sm.append("\n");
175: }
176:
177: br.close();
178:
179: return sm.toString();
180: }
181:
182: private void _convertToOracleCSV(String line, StringMaker sm) {
183: int x = line.indexOf("values (");
184: int y = line.lastIndexOf(");");
185:
186: line = line.substring(x + 8, y);
187:
188: line = StringUtil.replace(line, "sysdate, ", "20050101, ");
189:
190: sm.append(line);
191: sm.append("\n");
192: }
193:
194: private String _preBuildSQL(String template) throws IOException {
195: template = convertTimestamp(template);
196: template = StringUtil
197: .replace(template, TEMPLATE, getTemplate());
198:
199: template = reword(template);
200: template = StringUtil.replace(template, new String[] { "\\\\",
201: "\\'", "\\\"" }, new String[] { "\\", "''", "\"" });
202:
203: return template;
204: }
205:
206: private String _postBuildSQL(String template) throws IOException {
207: template = removeLongInserts(template);
208: template = StringUtil.replace(template, "\\n", "'||CHR(10)||'");
209:
210: return template;
211: }
212:
213: private static String[] _ORACLE = { "--", "1", "0",
214: "to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')",
215: "sysdate", " number(1, 0)", " timestamp", " number(30,20)",
216: " number(30,0)", " number(30,0)", " varchar2(4000)",
217: " clob", " varchar2", "", "commit" };
218:
219: private static OracleUtil _instance = new OracleUtil();
220:
221: }
|