001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.db;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.Index;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
021    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.io.IOException;
027    
028    import java.sql.Connection;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    import java.sql.SQLException;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    /**
037     * @author Alexander Chow
038     * @author Sandeep Soni
039     * @author Ganesh Ram
040     */
041    public class MySQLDB extends BaseDB {
042    
043            public static DB getInstance() {
044                    return _instance;
045            }
046    
047            @Override
048            public String buildSQL(String template) throws IOException {
049                    template = convertTimestamp(template);
050                    template = replaceTemplate(template, getTemplate());
051    
052                    template = reword(template);
053                    template = StringUtil.replace(template, "\\'", "''");
054    
055                    return template;
056            }
057    
058            @Override
059            public List<Index> getIndexes(Connection con) throws SQLException {
060                    List<Index> indexes = new ArrayList<Index>();
061    
062                    PreparedStatement ps = null;
063                    ResultSet rs = null;
064    
065                    try {
066                            StringBundler sb = new StringBundler(4);
067    
068                            sb.append("select distinct(index_name), table_name, non_unique ");
069                            sb.append("from information_schema.statistics where ");
070                            sb.append("index_schema = database() and (index_name like ");
071                            sb.append("'LIFERAY_%' or index_name like 'IX_%')");
072    
073                            String sql = sb.toString();
074    
075                            ps = con.prepareStatement(sql);
076    
077                            rs = ps.executeQuery();
078    
079                            while (rs.next()) {
080                                    String indexName = rs.getString("index_name");
081                                    String tableName = rs.getString("table_name");
082                                    boolean unique = !rs.getBoolean("non_unique");
083    
084                                    indexes.add(new Index(indexName, tableName, unique));
085                            }
086                    }
087                    finally {
088                            DataAccess.cleanUp(null, ps, rs);
089                    }
090    
091                    return indexes;
092            }
093    
094            @Override
095            public boolean isSupportsDateMilliseconds() {
096                    return _SUPPORTS_DATE_MILLISECONDS;
097            }
098    
099            @Override
100            public boolean isSupportsUpdateWithInnerJoin() {
101                    return _SUPPORTS_UPDATE_WITH_INNER_JOIN;
102            }
103    
104            protected MySQLDB() {
105                    super(TYPE_MYSQL);
106            }
107    
108            @Override
109            protected String buildCreateFileContent(
110                            String sqlDir, String databaseName, int population)
111                    throws IOException {
112    
113                    String suffix = getSuffix(population);
114    
115                    StringBundler sb = new StringBundler(14);
116    
117                    sb.append("drop database if exists ");
118                    sb.append(databaseName);
119                    sb.append(";\n");
120                    sb.append("create database ");
121                    sb.append(databaseName);
122                    sb.append(" character set utf8;\n");
123                    sb.append("use ");
124                    sb.append(databaseName);
125                    sb.append(";\n\n");
126                    sb.append(getCreateTablesContent(sqlDir, suffix));
127                    sb.append("\n\n");
128                    sb.append(readFile(sqlDir + "/indexes/indexes-mysql.sql"));
129                    sb.append("\n\n");
130                    sb.append(readFile(sqlDir + "/sequences/sequences-mysql.sql"));
131    
132                    return sb.toString();
133            }
134    
135            @Override
136            protected String getServerName() {
137                    return "mysql";
138            }
139    
140            @Override
141            protected String[] getTemplate() {
142                    return _MYSQL;
143            }
144    
145            @Override
146            protected String reword(String data) throws IOException {
147                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
148                            new UnsyncStringReader(data));
149    
150                    boolean createTable = false;
151    
152                    StringBundler sb = new StringBundler();
153    
154                    String line = null;
155    
156                    while ((line = unsyncBufferedReader.readLine()) != null) {
157                            if (StringUtil.startsWith(line, "create table")) {
158                                    createTable = true;
159                            }
160                            else if (line.startsWith(ALTER_COLUMN_NAME)) {
161                                    String[] template = buildColumnNameTokens(line);
162    
163                                    line = StringUtil.replace(
164                                            "alter table @table@ change column @old-column@ " +
165                                                    "@new-column@ @type@;",
166                                            REWORD_TEMPLATE, template);
167                            }
168                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
169                                    String[] template = buildColumnTypeTokens(line);
170    
171                                    line = StringUtil.replace(
172                                            "alter table @table@ modify @old-column@ @type@;",
173                                            REWORD_TEMPLATE, template);
174                            }
175                            else if (line.startsWith(ALTER_TABLE_NAME)) {
176                                    String[] template = buildTableNameTokens(line);
177    
178                                    line = StringUtil.replace(
179                                            "rename table @old-table@ to @new-table@;",
180                                            RENAME_TABLE_TEMPLATE, template);
181                            }
182    
183                            int pos = line.indexOf(";");
184    
185                            if (createTable && (pos != -1)) {
186                                    createTable = false;
187    
188                                    line =
189                                            line.substring(0, pos) + " engine " +
190                                                    PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
191                            }
192    
193                            sb.append(line);
194                            sb.append("\n");
195                    }
196    
197                    unsyncBufferedReader.close();
198    
199                    return sb.toString();
200            }
201    
202            private static final String[] _MYSQL = {
203                    "##", "1", "0", "'1970-01-01'", "now()", " longblob", " longblob",
204                    " tinyint", " datetime", " double", " integer", " bigint", " longtext",
205                    " longtext", " varchar", "  auto_increment", "commit"
206            };
207    
208            private static final boolean _SUPPORTS_DATE_MILLISECONDS = false;
209    
210            private static final boolean _SUPPORTS_UPDATE_WITH_INNER_JOIN = true;
211    
212            private static MySQLDB _instance = new MySQLDB();
213    
214    }