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