1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }