1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.util.PropsValues;
24  
25  import java.sql.Connection;
26  import java.sql.PreparedStatement;
27  import java.sql.ResultSet;
28  
29  /**
30   * <a href="VerifyMySQL.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class VerifyMySQL extends VerifyProcess {
35  
36      protected void alterTableEngine(String tableName) throws Exception {
37          if (_log.isInfoEnabled()) {
38              _log.info(
39                  "Updating table " + tableName + " to use engine " +
40                      PropsValues.DATABASE_MYSQL_ENGINE);
41          }
42  
43          Connection con = null;
44          PreparedStatement ps = null;
45  
46          try {
47              con = DataAccess.getConnection();
48  
49              ps = con.prepareStatement(
50                  "alter table " + tableName + " engine " +
51                      PropsValues.DATABASE_MYSQL_ENGINE);
52  
53              ps.executeUpdate();
54          }
55          finally {
56              DataAccess.cleanUp(con, ps);
57          }
58      }
59  
60      protected void doVerify() throws Exception {
61          DB db = DBFactoryUtil.getDB();
62  
63          if (!db.getType().equals(DB.TYPE_MYSQL)) {
64              return;
65          }
66  
67          Connection con = null;
68          PreparedStatement ps = null;
69          ResultSet rs = null;
70  
71          try {
72              con = DataAccess.getConnection();
73  
74              ps = con.prepareStatement("show table status");
75  
76              rs = ps.executeQuery();
77  
78              while (rs.next()) {
79                  String tableName = rs.getString("Name");
80                  String engine = GetterUtil.getString(rs.getString("Engine"));
81  
82                  if (!engine.equalsIgnoreCase(
83                          PropsValues.DATABASE_MYSQL_ENGINE)) {
84  
85                      alterTableEngine(tableName);
86                  }
87              }
88          }
89          finally {
90              DataAccess.cleanUp(con, ps, rs);
91          }
92      }
93  
94      private static Log _log = LogFactoryUtil.getLog(VerifyMySQL.class);
95  
96  }