001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
059            protected void doVerify() throws Exception {
060                    DB db = DBFactoryUtil.getDB();
061    
062                    String dbType = db.getType();
063    
064                    if (!dbType.equals(DB.TYPE_MYSQL)) {
065                            return;
066                    }
067    
068                    Connection con = null;
069                    PreparedStatement ps = null;
070                    ResultSet rs = null;
071    
072                    try {
073                            con = DataAccess.getConnection();
074    
075                            ps = con.prepareStatement("show table status");
076    
077                            rs = ps.executeQuery();
078    
079                            while (rs.next()) {
080                                    String tableName = rs.getString("Name");
081                                    String engine = GetterUtil.getString(rs.getString("Engine"));
082                                    String comment = GetterUtil.getString(rs.getString("Comment"));
083    
084                                    if (comment.equalsIgnoreCase("VIEW")) {
085                                            continue;
086                                    }
087    
088                                    if (engine.equalsIgnoreCase(
089                                                    PropsValues.DATABASE_MYSQL_ENGINE)) {
090    
091                                            continue;
092                                    }
093    
094                                    alterTableEngine(tableName);
095                            }
096                    }
097                    finally {
098                            DataAccess.cleanUp(con, ps, rs);
099                    }
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(VerifyMySQL.class);
103    
104    }