1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools.sql;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.util.PropsValues;
25  
26  import java.io.BufferedReader;
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.StringReader;
30  
31  /**
32   * <a href="MySQLUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Alexander Chow
35   * @author Sandeep Soni
36   * @author Ganesh Ram
37   *
38   */
39  public class MySQLUtil extends DBUtil {
40  
41      public static DBUtil getInstance() {
42          return _instance;
43      }
44  
45      public String buildSQL(String template) throws IOException {
46          template = convertTimestamp(template);
47          template = replaceTemplate(template, getTemplate());
48  
49          template = reword(template);
50          template = StringUtil.replace(template, "\\'", "''");
51  
52          return template;
53      }
54  
55      public boolean isSupportsUpdateWithInnerJoin() {
56          return _SUPPORTS_UPDATE_WITH_INNER_JOIN;
57      }
58  
59      protected MySQLUtil() {
60          super(TYPE_MYSQL);
61      }
62  
63      protected void buildCreateFile(String databaseName, boolean minimal)
64          throws IOException {
65  
66          String minimalSuffix = getMinimalSuffix(minimal);
67  
68          File file = new File(
69              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
70                  "-mysql.sql");
71  
72          StringBuilder sb = new StringBuilder();
73  
74          sb.append("drop database if exists " + databaseName + ";\n");
75          sb.append("create database " + databaseName + " character set utf8;\n");
76          sb.append("use ");
77          sb.append(databaseName);
78          sb.append(";\n\n");
79          sb.append(
80              FileUtil.read(
81                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
82                      "-mysql.sql"));
83          sb.append("\n\n");
84          sb.append(FileUtil.read("../sql/indexes/indexes-mysql.sql"));
85          sb.append("\n\n");
86          sb.append(FileUtil.read("../sql/sequences/sequences-mysql.sql"));
87  
88          FileUtil.write(file, sb.toString());
89      }
90  
91      protected String getServerName() {
92          return "mysql";
93      }
94  
95      protected String[] getTemplate() {
96          return _MYSQL;
97      }
98  
99      protected String reword(String data) throws IOException {
100         BufferedReader br = new BufferedReader(new StringReader(data));
101 
102         boolean createTable = false;
103 
104         StringBuilder sb = new StringBuilder();
105 
106         String line = null;
107 
108         while ((line = br.readLine()) != null) {
109             if (StringUtil.startsWith(line, "create table")) {
110                 createTable = true;
111             }
112             else if (line.startsWith(ALTER_COLUMN_NAME)) {
113                 String[] template = buildColumnNameTokens(line);
114 
115                 line = StringUtil.replace(
116                     "alter table @table@ change column @old-column@ " +
117                         "@new-column@ @type@;",
118                     REWORD_TEMPLATE, template);
119             }
120             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
121                 String[] template = buildColumnTypeTokens(line);
122 
123                 line = StringUtil.replace(
124                     "alter table @table@ modify @old-column@ @type@;",
125                     REWORD_TEMPLATE, template);
126             }
127 
128             int pos = line.indexOf(";");
129 
130             if (createTable && (pos != -1)) {
131                 createTable = false;
132 
133                 line =
134                     line.substring(0, pos) + " engine " +
135                         PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
136             }
137 
138             sb.append(line);
139             sb.append("\n");
140         }
141 
142         br.close();
143 
144         return sb.toString();
145     }
146 
147     private static String[] _MYSQL = {
148         "##", "1", "0",
149         "'1970-01-01'", "now()",
150         " blob", " tinyint", " datetime",
151         " double", " integer", " bigint",
152         " longtext", " longtext", " varchar",
153         "  auto_increment", "commit"
154     };
155 
156     private static boolean _SUPPORTS_UPDATE_WITH_INNER_JOIN = true;
157 
158     private static MySQLUtil _instance = new MySQLUtil();
159 
160 }