001    /**
002     * Copyright (c) 2000-2010 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            public String buildSQL(String template) throws IOException {
048                    template = convertTimestamp(template);
049                    template = replaceTemplate(template, getTemplate());
050    
051                    template = reword(template);
052                    template = StringUtil.replace(template, "\\'", "''");
053    
054                    return template;
055            }
056    
057            public List<Index> getIndexes() throws SQLException {
058                    List<Index> indexes = new ArrayList<Index>();
059    
060                    Connection con = null;
061                    PreparedStatement ps = null;
062                    ResultSet rs = null;
063    
064                    try {
065                            con = DataAccess.getConnection();
066    
067                            StringBundler sb = new StringBundler(4);
068    
069                            sb.append("select distinct(index_name), table_name, non_unique ");
070                            sb.append("from information_schema.statistics where ");
071                            sb.append("index_schema = database() and (index_name like ");
072                            sb.append("'LIFERAY_%' or index_name like 'IX_%')");
073    
074                            String sql = sb.toString();
075    
076                            ps = con.prepareStatement(sql);
077    
078                            rs = ps.executeQuery();
079    
080                            while (rs.next()) {
081                                    String indexName = rs.getString("index_name");
082                                    String tableName = rs.getString("table_name");
083                                    boolean unique = !rs.getBoolean("non_unique");
084    
085                                    indexes.add(new Index(indexName, tableName, unique));
086                            }
087                    }
088                    finally {
089                            DataAccess.cleanUp(con, ps, rs);
090                    }
091    
092                    return indexes;
093            }
094    
095            public boolean isSupportsDateMilliseconds() {
096                    return _SUPPORTS_DATE_MILLISECONDS;
097            }
098    
099            public boolean isSupportsUpdateWithInnerJoin() {
100                    return _SUPPORTS_UPDATE_WITH_INNER_JOIN;
101            }
102    
103            protected MySQLDB() {
104                    super(TYPE_MYSQL);
105            }
106    
107            protected String buildCreateFileContent(
108                            String sqlDir, String databaseName, int population)
109                    throws IOException {
110    
111                    String suffix = getSuffix(population);
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                    sb.append("use ");
122                    sb.append(databaseName);
123                    sb.append(";\n\n");
124                    sb.append(
125                            readFile(
126                                    sqlDir + "/portal" + suffix + "/portal" + suffix +
127                                            "-mysql.sql"));
128                    sb.append("\n\n");
129                    sb.append(readFile(sqlDir + "/indexes/indexes-mysql.sql"));
130                    sb.append("\n\n");
131                    sb.append(readFile(sqlDir + "/sequences/sequences-mysql.sql"));
132    
133                    return sb.toString();
134            }
135    
136            protected String getServerName() {
137                    return "mysql";
138            }
139    
140            protected String[] getTemplate() {
141                    return _MYSQL;
142            }
143    
144            protected String reword(String data) throws IOException {
145                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
146                            new UnsyncStringReader(data));
147    
148                    boolean createTable = false;
149    
150                    StringBundler sb = new StringBundler();
151    
152                    String line = null;
153    
154                    while ((line = unsyncBufferedReader.readLine()) != null) {
155                            if (StringUtil.startsWith(line, "create table")) {
156                                    createTable = true;
157                            }
158                            else if (line.startsWith(ALTER_COLUMN_NAME)) {
159                                    String[] template = buildColumnNameTokens(line);
160    
161                                    line = StringUtil.replace(
162                                            "alter table @table@ change column @old-column@ " +
163                                                    "@new-column@ @type@;",
164                                            REWORD_TEMPLATE, template);
165                            }
166                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
167                                    String[] template = buildColumnTypeTokens(line);
168    
169                                    line = StringUtil.replace(
170                                            "alter table @table@ modify @old-column@ @type@;",
171                                            REWORD_TEMPLATE, template);
172                            }
173    
174                            int pos = line.indexOf(";");
175    
176                            if (createTable && (pos != -1)) {
177                                    createTable = false;
178    
179                                    line =
180                                            line.substring(0, pos) + " engine " +
181                                                    PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
182                            }
183    
184                            sb.append(line);
185                            sb.append("\n");
186                    }
187    
188                    unsyncBufferedReader.close();
189    
190                    return sb.toString();
191            }
192    
193            private static String[] _MYSQL = {
194                    "##", "1", "0",
195                    "'1970-01-01'", "now()",
196                    " blob", " tinyint", " datetime",
197                    " double", " integer", " bigint",
198                    " longtext", " longtext", " varchar",
199                    "  auto_increment", "commit"
200            };
201    
202            private static boolean _SUPPORTS_DATE_MILLISECONDS = false;
203    
204            private static boolean _SUPPORTS_UPDATE_WITH_INNER_JOIN = true;
205    
206            private static MySQLDB _instance = new MySQLDB();
207    
208    }