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="MySQLUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Alexander Chow
036: *
037: */
038: public class MySQLUtil extends DBUtil {
039:
040: public static DBUtil getInstance() {
041: return _instance;
042: }
043:
044: public String buildSQL(String template) throws IOException {
045: template = convertTimestamp(template);
046: template = StringUtil
047: .replace(template, TEMPLATE, getTemplate());
048:
049: template = reword(template);
050: template = StringUtil.replace(template, "\\'", "''");
051:
052: return template;
053: }
054:
055: protected MySQLUtil() {
056: }
057:
058: protected void buildCreateFile(String databaseName, boolean minimal)
059: throws IOException {
060:
061: String minimalSuffix = getMinimalSuffix(minimal);
062:
063: File file = new File("../sql/create" + minimalSuffix
064: + "/create" + minimalSuffix + "-mysql.sql");
065:
066: StringMaker sm = new StringMaker();
067:
068: sm.append("drop database if exists " + databaseName + ";\n");
069: sm.append("create database " + databaseName
070: + " character set utf8;\n");
071: sm.append("use ");
072: sm.append(databaseName);
073: sm.append(";\n\n");
074: sm.append(FileUtil.read("../sql/portal" + minimalSuffix
075: + "/portal" + minimalSuffix + "-mysql.sql"));
076: sm.append("\n\n");
077: sm.append(FileUtil.read("../sql/indexes/indexes-mysql.sql"));
078: sm.append("\n\n");
079: sm
080: .append(FileUtil
081: .read("../sql/sequences/sequences-mysql.sql"));
082:
083: FileUtil.write(file, sm.toString());
084: }
085:
086: protected String getServerName() {
087: return "mysql";
088: }
089:
090: protected String[] getTemplate() {
091: return _MYSQL;
092: }
093:
094: protected String reword(String data) throws IOException {
095: BufferedReader br = new BufferedReader(new StringReader(data));
096:
097: StringMaker sm = new StringMaker();
098:
099: String line = null;
100:
101: while ((line = br.readLine()) != null) {
102: if (line.startsWith(ALTER_COLUMN_TYPE)) {
103: String[] template = buildColumnTypeTokens(line);
104:
105: line = StringUtil
106: .replace(
107: "alter table @table@ modify @old-column@ @type@;",
108: REWORD_TEMPLATE, template);
109: } else if (line.startsWith(ALTER_COLUMN_NAME)) {
110: String[] template = buildColumnNameTokens(line);
111:
112: line = StringUtil.replace(
113: "alter table @table@ change column @old-column@ "
114: + "@new-column@ @type@;",
115: REWORD_TEMPLATE, template);
116: }
117:
118: sm.append(line);
119: sm.append("\n");
120: }
121:
122: br.close();
123:
124: return sm.toString();
125: }
126:
127: private static String[] _MYSQL = { "##", "1", "0", "'1970-01-01'",
128: "now()", " tinyint", " datetime", " double", " integer",
129: " bigint", " longtext", " longtext", " varchar",
130: " auto_increment", "commit" };
131:
132: private static MySQLUtil _instance = new MySQLUtil();
133:
134: }
|