001    /**
002     * Copyright (c) 2000-2013 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.verify;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.sql.Connection;
027    import java.sql.PreparedStatement;
028    import java.sql.ResultSet;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class VerifyMySQL extends VerifyProcess {
034    
035            protected void alterTableEngine(String tableName) throws Exception {
036                    if (_log.isInfoEnabled()) {
037                            _log.info(
038                                    "Updating table " + tableName + " to use engine " +
039                                            PropsValues.DATABASE_MYSQL_ENGINE);
040                    }
041    
042                    Connection con = null;
043                    PreparedStatement ps = null;
044    
045                    try {
046                            con = DataAccess.getUpgradeOptimizedConnection();
047    
048                            ps = con.prepareStatement(
049                                    "alter table " + tableName + " engine " +
050                                            PropsValues.DATABASE_MYSQL_ENGINE);
051    
052                            ps.executeUpdate();
053                    }
054                    finally {
055                            DataAccess.cleanUp(con, ps);
056                    }
057            }
058    
059            @Override
060            protected void doVerify() throws Exception {
061                    DB db = DBFactoryUtil.getDB();
062    
063                    String dbType = db.getType();
064    
065                    if (!dbType.equals(DB.TYPE_MYSQL)) {
066                            return;
067                    }
068    
069                    Connection con = null;
070                    PreparedStatement ps = null;
071                    ResultSet rs = null;
072    
073                    try {
074                            con = DataAccess.getUpgradeOptimizedConnection();
075    
076                            ps = con.prepareStatement("show table status");
077    
078                            rs = ps.executeQuery();
079    
080                            while (rs.next()) {
081                                    String tableName = rs.getString("Name");
082    
083                                    if (!isPortalTableName(tableName)) {
084                                            continue;
085                                    }
086    
087                                    String engine = GetterUtil.getString(rs.getString("Engine"));
088                                    String comment = GetterUtil.getString(rs.getString("Comment"));
089    
090                                    if (StringUtil.equalsIgnoreCase(comment, "VIEW")) {
091                                            continue;
092                                    }
093    
094                                    if (StringUtil.equalsIgnoreCase(
095                                                    engine, PropsValues.DATABASE_MYSQL_ENGINE)) {
096    
097                                            continue;
098                                    }
099    
100                                    alterTableEngine(tableName);
101                            }
102                    }
103                    finally {
104                            DataAccess.cleanUp(con, ps, rs);
105                    }
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(VerifyMySQL.class);
109    
110    }