1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.verify;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.tools.sql.DBUtil;
29  import com.liferay.portal.util.PropsValues;
30  
31  import java.sql.Connection;
32  import java.sql.PreparedStatement;
33  import java.sql.ResultSet;
34  
35  /**
36   * <a href="VerifyMySQL.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class VerifyMySQL extends VerifyProcess {
41  
42      protected void alterTableEngine(String tableName) throws Exception {
43          if (_log.isInfoEnabled()) {
44              _log.info(
45                  "Updating table " + tableName + " to use engine " +
46                      PropsValues.DATABASE_MYSQL_ENGINE);
47          }
48  
49          Connection con = null;
50          PreparedStatement ps = null;
51  
52          try {
53              con = DataAccess.getConnection();
54  
55              ps = con.prepareStatement(
56                  "alter table " + tableName + " engine " +
57                      PropsValues.DATABASE_MYSQL_ENGINE);
58  
59              ps.executeUpdate();
60          }
61          finally {
62              DataAccess.cleanUp(con, ps);
63          }
64      }
65  
66      protected void doVerify() throws Exception {
67          DBUtil dbUtil = DBUtil.getInstance();
68  
69          if (!dbUtil.getType().equals(DBUtil.TYPE_MYSQL)) {
70              return;
71          }
72  
73          Connection con = null;
74          PreparedStatement ps = null;
75          ResultSet rs = null;
76  
77          try {
78              con = DataAccess.getConnection();
79  
80              ps = con.prepareStatement("show table status");
81  
82              rs = ps.executeQuery();
83  
84              while (rs.next()) {
85                  String tableName = rs.getString("Name");
86                  String engine = rs.getString("Engine");
87  
88                  if (!engine.equalsIgnoreCase(
89                          PropsValues.DATABASE_MYSQL_ENGINE)) {
90  
91                      alterTableEngine(tableName);
92                  }
93              }
94          }
95          finally {
96              DataAccess.cleanUp(con, ps, rs);
97          }
98      }
99  
100     private static Log _log = LogFactoryUtil.getLog(VerifyMySQL.class);
101 
102 }