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.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                    StringBundler sb = new StringBundler(14);
114    
115                    sb.append("drop database if exists ");
116                    sb.append(databaseName);
117                    sb.append(";\n");
118                    sb.append("create database ");
119                    sb.append(databaseName);
120                    sb.append(" character set utf8;\n");
121    
122                    if (population != BARE) {
123                            sb.append("use ");
124                            sb.append(databaseName);
125                            sb.append(";\n\n");
126    
127                            String suffix = getSuffix(population);
128    
129                            sb.append(getCreateTablesContent(sqlDir, suffix));
130                            sb.append("\n\n");
131                            sb.append(readFile(sqlDir + "/indexes/indexes-mysql.sql"));
132                            sb.append("\n\n");
133                            sb.append(readFile(sqlDir + "/sequences/sequences-mysql.sql"));
134                    }
135    
136                    return sb.toString();
137            }
138    
139            @Override
140            protected String getServerName() {
141                    return "mysql";
142            }
143    
144            @Override
145            protected String[] getTemplate() {
146                    return _MYSQL;
147            }
148    
149            @Override
150            protected String reword(String data) throws IOException {
151                    try (UnsyncBufferedReader unsyncBufferedReader =
152                                    new UnsyncBufferedReader(new UnsyncStringReader(data))) {
153    
154                            StringBundler sb = new StringBundler();
155    
156                            boolean createTable = false;
157    
158                            String line = null;
159    
160                            while ((line = unsyncBufferedReader.readLine()) != null) {
161                                    if (StringUtil.startsWith(line, "create table")) {
162                                            createTable = true;
163                                    }
164                                    else if (line.startsWith(ALTER_COLUMN_NAME)) {
165                                            String[] template = buildColumnNameTokens(line);
166    
167                                            line = StringUtil.replace(
168                                                    "alter table @table@ change column @old-column@ " +
169                                                            "@new-column@ @type@;",
170                                                    REWORD_TEMPLATE, template);
171                                    }
172                                    else if (line.startsWith(ALTER_COLUMN_TYPE)) {
173                                            String[] template = buildColumnTypeTokens(line);
174    
175                                            line = StringUtil.replace(
176                                                    "alter table @table@ modify @old-column@ @type@;",
177                                                    REWORD_TEMPLATE, template);
178                                    }
179                                    else if (line.startsWith(ALTER_TABLE_NAME)) {
180                                            String[] template = buildTableNameTokens(line);
181    
182                                            line = StringUtil.replace(
183                                                    "rename table @old-table@ to @new-table@;",
184                                                    RENAME_TABLE_TEMPLATE, template);
185                                    }
186    
187                                    int pos = line.indexOf(";");
188    
189                                    if (createTable && (pos != -1)) {
190                                            createTable = false;
191    
192                                            line =
193                                                    line.substring(0, pos) + " engine " +
194                                                    PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
195                                    }
196    
197                                    sb.append(line);
198                                    sb.append("\n");
199                            }
200    
201                            return sb.toString();
202                    }
203            }
204    
205            private static final String[] _MYSQL = {
206                    "##", "1", "0", "'1970-01-01'", "now()", " longblob", " longblob",
207                    " tinyint", " datetime", " double", " integer", " bigint", " longtext",
208                    " longtext", " varchar", "  auto_increment", "commit"
209            };
210    
211            private static final boolean _SUPPORTS_DATE_MILLISECONDS = false;
212    
213            private static final boolean _SUPPORTS_UPDATE_WITH_INNER_JOIN = true;
214    
215            private static final MySQLDB _instance = new MySQLDB();
216    
217    }