1 package org.apache.torque.adapter; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.sql.Connection; 23 import java.sql.SQLException; 24 import java.sql.Statement; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.torque.sql.Query; 29 30 /** 31 * This is used in order to connect to a MySQL database using the MM 32 * drivers. Simply comment the above and uncomment this code below and 33 * fill in the appropriate values for DB_NAME, DB_HOST, DB_USER, 34 * DB_PASS. 35 * 36 * <P><a href="http://www.mysql.com/">http://www.mysql.com/</a> 37 * <p>"jdbc:mysql://" + DB_HOST + "/" + DB_NAME + "?user=" + 38 * DB_USER + "&password=" + DB_PASS; 39 * 40 * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a> 41 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> 42 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> 43 * @version $Id: MysqlAdapter.java 1355228 2012-06-29 03:38:08Z tfischer $ 44 */ 45 public class MysqlAdapter extends AbstractAdapter 46 { 47 /** 48 * Serial version 49 */ 50 private static final long serialVersionUID = 7547291410802807010L; 51 52 /** The class log. */ 53 private static Log log = LogFactory.getLog(DerbyAdapter.class); 54 55 /** 56 * Empty protected constructor. 57 */ 58 protected MysqlAdapter() 59 { 60 // empty 61 } 62 63 /** 64 * This method is used to ignore case. 65 * 66 * @param in The string to transform to upper case. 67 * @return The upper case string. 68 */ 69 @Override 70 public String toUpperCase(String in) 71 { 72 return in; 73 } 74 75 /** 76 * This method is used to ignore case. 77 * 78 * @param in The string whose case to ignore. 79 * @return The string in a case that can be ignored. 80 */ 81 @Override 82 public String ignoreCase(String in) 83 { 84 return in; 85 } 86 87 /** 88 * @see org.apache.torque.adapter.Adapter#getIDMethodType() 89 */ 90 @Override 91 public IDMethod getIDMethodType() 92 { 93 return IDMethod.AUTO_INCREMENT; 94 } 95 96 /** 97 * Returns the SQL to get the database key of the last row 98 * inserted, which in this case is <code>SELECT 99 * LAST_INSERT_ID()</code>. 100 * 101 * @see org.apache.torque.adapter.Adapter#getIDMethodSQL(Object obj) 102 */ 103 @Override 104 public String getIDMethodSQL(Object obj) 105 { 106 return "SELECT LAST_INSERT_ID()"; 107 } 108 109 /** 110 * Locks the specified table. 111 * 112 * @param con The JDBC connection to use. 113 * @param table The name of the table to lock. 114 * @exception SQLException No Statement could be created or 115 * executed. 116 */ 117 @Override 118 public void lockTable(Connection con, String table) throws SQLException 119 { 120 Statement statement = null; 121 try 122 { 123 statement = con.createStatement(); 124 StringBuffer stmt = new StringBuffer(); 125 stmt.append("LOCK TABLE ").append(table).append(" WRITE"); 126 statement.executeUpdate(stmt.toString()); 127 statement.close(); 128 statement = null; 129 } 130 finally 131 { 132 if (statement != null) 133 { 134 try 135 { 136 statement.close(); 137 } 138 catch (SQLException e) 139 { 140 log.warn("Could not close statement", e); 141 } 142 } 143 } 144 } 145 146 /** 147 * Unlocks the specified table. 148 * 149 * @param con The JDBC connection to use. 150 * @param table The name of the table to unlock. 151 * @exception SQLException No Statement could be created or 152 * executed. 153 */ 154 @Override 155 public void unlockTable(Connection con, String table) throws SQLException 156 { 157 Statement statement = null; 158 try 159 { 160 statement = con.createStatement(); 161 statement.executeUpdate("UNLOCK TABLES"); 162 statement.close(); 163 statement = null; 164 } 165 finally 166 { 167 if (statement != null) 168 { 169 try 170 { 171 statement.close(); 172 } 173 catch (SQLException e) 174 { 175 log.warn("Could not close statement", e); 176 } 177 } 178 } 179 } 180 181 /** 182 * Generate a LIMIT offset, limit clause if offset > 0 183 * or an LIMIT limit clause if limit is > 0 and offset 184 * is 0. 185 * 186 * @param query The query to modify 187 * @param offset the offset Value 188 * @param limit the limit Value 189 */ 190 @Override 191 public void generateLimits(Query query, long offset, int limit) 192 { 193 if (offset > 0) 194 { 195 if (limit >= 0) 196 { 197 query.setLimit(Integer.toString(limit)); 198 } 199 else 200 { 201 // Limit must always be set in mysql if offset is set 202 query.setLimit("18446744073709551615"); 203 } 204 query.setOffset(Long.toString(offset)); 205 } 206 else 207 { 208 if (limit >= 0) 209 { 210 query.setLimit(Integer.toString(limit)); 211 } 212 } 213 214 query.setPreLimit(null); 215 query.setPostLimit(null); 216 } 217 218 /** 219 * Return true for MySQL 220 * @see org.apache.torque.adapter.AbstractAdapter#supportsNativeLimit() 221 */ 222 @Override 223 public boolean supportsNativeLimit() 224 { 225 return true; 226 } 227 228 /** 229 * Return true for MySQL 230 * @see org.apache.torque.adapter.AbstractAdapter#supportsNativeOffset() 231 */ 232 @Override 233 public boolean supportsNativeOffset() 234 { 235 return true; 236 } 237 }