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.tools.sql;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.util.PropsValues;
28  
29  import java.io.BufferedReader;
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.StringReader;
33  
34  /**
35   * <a href="MySQLUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Alexander Chow
38   * @author Sandeep Soni
39   * @author Ganesh Ram
40   *
41   */
42  public class MySQLUtil extends DBUtil {
43  
44      public static DBUtil getInstance() {
45          return _instance;
46      }
47  
48      public String buildSQL(String template) throws IOException {
49          template = convertTimestamp(template);
50          template = replaceTemplate(template, getTemplate());
51  
52          template = reword(template);
53          template = StringUtil.replace(template, "\\'", "''");
54  
55          return template;
56      }
57  
58      protected MySQLUtil() {
59          super(TYPE_MYSQL);
60      }
61  
62      protected void buildCreateFile(String databaseName, boolean minimal)
63          throws IOException {
64  
65          String minimalSuffix = getMinimalSuffix(minimal);
66  
67          File file = new File(
68              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
69                  "-mysql.sql");
70  
71          StringBuilder sb = new StringBuilder();
72  
73          sb.append("drop database if exists " + databaseName + ";\n");
74          sb.append("create database " + databaseName + " character set utf8;\n");
75          sb.append("use ");
76          sb.append(databaseName);
77          sb.append(";\n\n");
78          sb.append(
79              FileUtil.read(
80                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
81                      "-mysql.sql"));
82          sb.append("\n\n");
83          sb.append(FileUtil.read("../sql/indexes/indexes-mysql.sql"));
84          sb.append("\n\n");
85          sb.append(FileUtil.read("../sql/sequences/sequences-mysql.sql"));
86  
87          FileUtil.write(file, sb.toString());
88      }
89  
90      protected String getServerName() {
91          return "mysql";
92      }
93  
94      protected String[] getTemplate() {
95          return _MYSQL;
96      }
97  
98      protected String reword(String data) throws IOException {
99          BufferedReader br = new BufferedReader(new StringReader(data));
100 
101         boolean createTable = false;
102 
103         StringBuilder sb = new StringBuilder();
104 
105         String line = null;
106 
107         while ((line = br.readLine()) != null) {
108             if (StringUtil.startsWith(line, "create table")) {
109                 createTable = true;
110             }
111             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
112                 String[] template = buildColumnTypeTokens(line);
113 
114                 line = StringUtil.replace(
115                     "alter table @table@ modify @old-column@ @type@;",
116                     REWORD_TEMPLATE, template);
117             }
118             else if (line.startsWith(ALTER_COLUMN_NAME)) {
119                 String[] template = buildColumnNameTokens(line);
120 
121                 line = StringUtil.replace(
122                     "alter table @table@ change column @old-column@ " +
123                         "@new-column@ @type@;",
124                     REWORD_TEMPLATE, template);
125             }
126 
127             int pos = line.indexOf(";");
128 
129             if (createTable && (pos != -1)) {
130                 createTable = false;
131 
132                 line =
133                     line.substring(0, pos) + " engine " +
134                         PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
135             }
136 
137             sb.append(line);
138             sb.append("\n");
139         }
140 
141         br.close();
142 
143         return sb.toString();
144     }
145 
146     private static String[] _MYSQL = {
147         "##", "1", "0",
148         "'1970-01-01'", "now()",
149         " blob", " tinyint", " datetime",
150         " double", " integer", " bigint",
151         " longtext", " longtext", " varchar",
152         "  auto_increment", "commit"
153     };
154 
155     private static MySQLUtil _instance = new MySQLUtil();
156 
157 }