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.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.util.PropsValues;
024    
025    import java.sql.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class VerifyMySQL extends VerifyProcess {
033    
034            protected void alterTableEngine(String tableName) throws Exception {
035                    if (_log.isInfoEnabled()) {
036                            _log.info(
037                                    "Updating table " + tableName + " to use engine " +
038                                            PropsValues.DATABASE_MYSQL_ENGINE);
039                    }
040    
041                    Connection con = null;
042                    PreparedStatement ps = null;
043    
044                    try {
045                            con = DataAccess.getConnection();
046    
047                            ps = con.prepareStatement(
048                                    "alter table " + tableName + " engine " +
049                                            PropsValues.DATABASE_MYSQL_ENGINE);
050    
051                            ps.executeUpdate();
052                    }
053                    finally {
054                            DataAccess.cleanUp(con, ps);
055                    }
056            }
057    
058            protected void doVerify() throws Exception {
059                    DB db = DBFactoryUtil.getDB();
060    
061                    if (!db.getType().equals(DB.TYPE_MYSQL)) {
062                            return;
063                    }
064    
065                    Connection con = null;
066                    PreparedStatement ps = null;
067                    ResultSet rs = null;
068    
069                    try {
070                            con = DataAccess.getConnection();
071    
072                            ps = con.prepareStatement("show table status");
073    
074                            rs = ps.executeQuery();
075    
076                            while (rs.next()) {
077                                    String tableName = rs.getString("Name");
078                                    String engine = GetterUtil.getString(rs.getString("Engine"));
079    
080                                    if (!engine.equalsIgnoreCase(
081                                                    PropsValues.DATABASE_MYSQL_ENGINE)) {
082    
083                                            alterTableEngine(tableName);
084                                    }
085                            }
086                    }
087                    finally {
088                            DataAccess.cleanUp(con, ps, rs);
089                    }
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(VerifyMySQL.class);
093    
094    }