001    /**
002     * Copyright (c) 2000-present 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.Index;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
020    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
021    import com.liferay.portal.kernel.util.GetterUtil;
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 MySQLDB(int majorVersion, int minorVersion) {
044                    super(TYPE_MYSQL, majorVersion, minorVersion);
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<>();
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 isSupportsUpdateWithInnerJoin() {
096                    return _SUPPORTS_UPDATE_WITH_INNER_JOIN;
097            }
098    
099            @Override
100            protected String buildCreateFileContent(
101                            String sqlDir, String databaseName, int population)
102                    throws IOException {
103    
104                    StringBundler sb = new StringBundler(14);
105    
106                    sb.append("drop database if exists ");
107                    sb.append(databaseName);
108                    sb.append(";\n");
109                    sb.append("create database ");
110                    sb.append(databaseName);
111                    sb.append(" character set utf8;\n");
112    
113                    if (population != BARE) {
114                            sb.append("use ");
115                            sb.append(databaseName);
116                            sb.append(";\n\n");
117    
118                            String suffix = getSuffix(population);
119    
120                            sb.append(getCreateTablesContent(sqlDir, suffix));
121                            sb.append("\n\n");
122                            sb.append(readFile(sqlDir + "/indexes/indexes-mysql.sql"));
123                            sb.append("\n\n");
124                            sb.append(readFile(sqlDir + "/sequences/sequences-mysql.sql"));
125                    }
126    
127                    return sb.toString();
128            }
129    
130            @Override
131            protected String getServerName() {
132                    return "mysql";
133            }
134    
135            @Override
136            protected String[] getTemplate() {
137                    if (GetterUtil.getFloat(getVersionString()) >= 5.6F) {
138                            _MYSQL[8] = " datetime(6)";
139                    }
140    
141                    return _MYSQL;
142            }
143    
144            @Override
145            protected String reword(String data) throws IOException {
146                    try (UnsyncBufferedReader unsyncBufferedReader =
147                                    new UnsyncBufferedReader(new UnsyncStringReader(data))) {
148    
149                            StringBundler sb = new StringBundler();
150    
151                            boolean createTable = false;
152    
153                            String line = null;
154    
155                            while ((line = unsyncBufferedReader.readLine()) != null) {
156                                    if (StringUtil.startsWith(line, "create table")) {
157                                            createTable = true;
158                                    }
159                                    else if (line.startsWith(ALTER_COLUMN_NAME)) {
160                                            String[] template = buildColumnNameTokens(line);
161    
162                                            line = StringUtil.replace(
163                                                    "alter table @table@ change column @old-column@ " +
164                                                            "@new-column@ @type@;",
165                                                    REWORD_TEMPLATE, template);
166                                    }
167                                    else if (line.startsWith(ALTER_COLUMN_TYPE)) {
168                                            String[] template = buildColumnTypeTokens(line);
169    
170                                            line = StringUtil.replace(
171                                                    "alter table @table@ modify @old-column@ @type@;",
172                                                    REWORD_TEMPLATE, template);
173                                    }
174                                    else if (line.startsWith(ALTER_TABLE_NAME)) {
175                                            String[] template = buildTableNameTokens(line);
176    
177                                            line = StringUtil.replace(
178                                                    "rename table @old-table@ to @new-table@;",
179                                                    RENAME_TABLE_TEMPLATE, template);
180                                    }
181    
182                                    int pos = line.indexOf(";");
183    
184                                    if (createTable && (pos != -1)) {
185                                            createTable = false;
186    
187                                            line =
188                                                    line.substring(0, pos) + " engine " +
189                                                    PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
190                                    }
191    
192                                    sb.append(line);
193                                    sb.append("\n");
194                            }
195    
196                            return sb.toString();
197                    }
198            }
199    
200            private static final String[] _MYSQL = {
201                    "##", "1", "0", "'1970-01-01'", "now()", " longblob", " longblob",
202                    " tinyint", " datetime", " double", " integer", " bigint", " longtext",
203                    " longtext", " varchar", "  auto_increment", "commit"
204            };
205    
206            private static final boolean _SUPPORTS_UPDATE_WITH_INNER_JOIN = true;
207    
208    }