001: package org.apache.torque.adapter;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.sql.Connection;
023: import java.sql.SQLException;
024: import java.sql.Statement;
025: import java.util.Date;
026: import java.text.SimpleDateFormat;
027:
028: import org.apache.torque.util.Query;
029:
030: /**
031: * This is used in order to connect to a MySQL database using the MM
032: * drivers. Simply comment the above and uncomment this code below and
033: * fill in the appropriate values for DB_NAME, DB_HOST, DB_USER,
034: * DB_PASS.
035: *
036: * <P><a href="http://www.mysql.com/">http://www.mysql.com/</a>
037: * <p>"jdbc:mysql://" + DB_HOST + "/" + DB_NAME + "?user=" +
038: * DB_USER + "&password=" + DB_PASS;
039: *
040: * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
041: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
042: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
043: * @version $Id: DBMM.java 522044 2007-03-24 16:00:57Z tfischer $
044: */
045: public class DBMM extends AbstractDBAdapter {
046: /**
047: * Serial version
048: */
049: private static final long serialVersionUID = 7547291410802807010L;
050:
051: /** A specialized date format for MySQL. */
052: private static final String DATE_FORMAT = "yyyyMMddHHmmss";
053:
054: /**
055: * Empty protected constructor.
056: */
057: protected DBMM() {
058: }
059:
060: /**
061: * This method is used to ignore case.
062: *
063: * @param in The string to transform to upper case.
064: * @return The upper case string.
065: */
066: public String toUpperCase(String in) {
067: return in;
068: }
069:
070: /**
071: * This method is used to ignore case.
072: *
073: * @param in The string whose case to ignore.
074: * @return The string in a case that can be ignored.
075: */
076: public String ignoreCase(String in) {
077: return in;
078: }
079:
080: /**
081: * @see org.apache.torque.adapter.DB#getIDMethodType()
082: */
083: public String getIDMethodType() {
084: return AUTO_INCREMENT;
085: }
086:
087: /**
088: * Returns the SQL to get the database key of the last row
089: * inserted, which in this case is <code>SELECT
090: * LAST_INSERT_ID()</code>.
091: *
092: * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object obj)
093: */
094: public String getIDMethodSQL(Object obj) {
095: return "SELECT LAST_INSERT_ID()";
096: }
097:
098: /**
099: * Locks the specified table.
100: *
101: * @param con The JDBC connection to use.
102: * @param table The name of the table to lock.
103: * @exception SQLException No Statement could be created or
104: * executed.
105: */
106: public void lockTable(Connection con, String table)
107: throws SQLException {
108: Statement statement = con.createStatement();
109: StringBuffer stmt = new StringBuffer();
110: stmt.append("LOCK TABLE ").append(table).append(" WRITE");
111: statement.executeUpdate(stmt.toString());
112: }
113:
114: /**
115: * Unlocks the specified table.
116: *
117: * @param con The JDBC connection to use.
118: * @param table The name of the table to unlock.
119: * @exception SQLException No Statement could be created or
120: * executed.
121: */
122: public void unlockTable(Connection con, String table)
123: throws SQLException {
124: Statement statement = con.createStatement();
125: statement.executeUpdate("UNLOCK TABLES");
126: }
127:
128: /**
129: * Generate a LIMIT offset, limit clause if offset > 0
130: * or an LIMIT limit clause if limit is > 0 and offset
131: * is 0.
132: *
133: * @param query The query to modify
134: * @param offset the offset Value
135: * @param limit the limit Value
136: */
137: public void generateLimits(Query query, int offset, int limit) {
138: if (offset > 0) {
139: if (limit >= 0) {
140: query.setLimit(Integer.toString(limit));
141: } else {
142: // Limit must always be set in mysql if offset is set
143: query.setLimit("18446744073709551615");
144: }
145: query.setOffset(Integer.toString(offset));
146: } else {
147: if (limit >= 0) {
148: query.setLimit(Integer.toString(limit));
149: }
150: }
151:
152: query.setPreLimit(null);
153: query.setPostLimit(null);
154: }
155:
156: /**
157: * This method is used to chek whether the database supports
158: * limiting the size of the resultset.
159: *
160: * @return LIMIT_STYLE_MYSQL.
161: * @deprecated This should not be exposed to the outside
162: */
163: public int getLimitStyle() {
164: return DB.LIMIT_STYLE_MYSQL;
165: }
166:
167: /**
168: * Return true for MySQL
169: * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
170: */
171: public boolean supportsNativeLimit() {
172: return true;
173: }
174:
175: /**
176: * Return true for MySQL
177: * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeOffset()
178: */
179: public boolean supportsNativeOffset() {
180: return true;
181: }
182:
183: /**
184: * This method overrides the JDBC escapes used to format dates
185: * using a <code>DateFormat</code>. As of version 2.0.11, the MM
186: * JDBC driver does not implement JDBC 3.0 escapes.
187: *
188: * @param date the date to format
189: * @return The properly formatted date String.
190: */
191: public String getDateString(Date date) {
192: char delim = getStringDelimiter();
193: return (delim + new SimpleDateFormat(DATE_FORMAT).format(date) + delim);
194: }
195: }
|