1 package org.apache.torque.adapter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import java.util.Date;
26 import java.text.SimpleDateFormat;
27
28 import org.apache.torque.util.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: DBMM.java 522044 2007-03-24 16:00:57Z tfischer $
44 */
45 public class DBMM extends AbstractDBAdapter
46 {
47 /***
48 * Serial version
49 */
50 private static final long serialVersionUID = 7547291410802807010L;
51
52 /*** A specialized date format for MySQL. */
53 private static final String DATE_FORMAT = "yyyyMMddHHmmss";
54
55 /***
56 * Empty protected constructor.
57 */
58 protected DBMM()
59 {
60 }
61
62 /***
63 * This method is used to ignore case.
64 *
65 * @param in The string to transform to upper case.
66 * @return The upper case string.
67 */
68 public String toUpperCase(String in)
69 {
70 return in;
71 }
72
73 /***
74 * This method is used to ignore case.
75 *
76 * @param in The string whose case to ignore.
77 * @return The string in a case that can be ignored.
78 */
79 public String ignoreCase(String in)
80 {
81 return in;
82 }
83
84 /***
85 * @see org.apache.torque.adapter.DB#getIDMethodType()
86 */
87 public String getIDMethodType()
88 {
89 return AUTO_INCREMENT;
90 }
91
92 /***
93 * Returns the SQL to get the database key of the last row
94 * inserted, which in this case is <code>SELECT
95 * LAST_INSERT_ID()</code>.
96 *
97 * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object obj)
98 */
99 public String getIDMethodSQL(Object obj)
100 {
101 return "SELECT LAST_INSERT_ID()";
102 }
103
104 /***
105 * Locks the specified table.
106 *
107 * @param con The JDBC connection to use.
108 * @param table The name of the table to lock.
109 * @exception SQLException No Statement could be created or
110 * executed.
111 */
112 public void lockTable(Connection con, String table) throws SQLException
113 {
114 Statement statement = con.createStatement();
115 StringBuffer stmt = new StringBuffer();
116 stmt.append("LOCK TABLE ").append(table).append(" WRITE");
117 statement.executeUpdate(stmt.toString());
118 }
119
120 /***
121 * Unlocks the specified table.
122 *
123 * @param con The JDBC connection to use.
124 * @param table The name of the table to unlock.
125 * @exception SQLException No Statement could be created or
126 * executed.
127 */
128 public void unlockTable(Connection con, String table) throws SQLException
129 {
130 Statement statement = con.createStatement();
131 statement.executeUpdate("UNLOCK TABLES");
132 }
133
134 /***
135 * Generate a LIMIT offset, limit clause if offset > 0
136 * or an LIMIT limit clause if limit is > 0 and offset
137 * is 0.
138 *
139 * @param query The query to modify
140 * @param offset the offset Value
141 * @param limit the limit Value
142 */
143 public void generateLimits(Query query, int offset, int limit)
144 {
145 if (offset > 0)
146 {
147 if (limit >=0 )
148 {
149 query.setLimit(Integer.toString(limit));
150 }
151 else
152 {
153
154 query.setLimit("18446744073709551615");
155 }
156 query.setOffset(Integer.toString(offset));
157 }
158 else
159 {
160 if (limit >= 0)
161 {
162 query.setLimit(Integer.toString(limit));
163 }
164 }
165
166 query.setPreLimit(null);
167 query.setPostLimit(null);
168 }
169
170 /***
171 * This method is used to chek whether the database supports
172 * limiting the size of the resultset.
173 *
174 * @return LIMIT_STYLE_MYSQL.
175 * @deprecated This should not be exposed to the outside
176 */
177 public int getLimitStyle()
178 {
179 return DB.LIMIT_STYLE_MYSQL;
180 }
181
182 /***
183 * Return true for MySQL
184 * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
185 */
186 public boolean supportsNativeLimit()
187 {
188 return true;
189 }
190
191 /***
192 * Return true for MySQL
193 * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeOffset()
194 */
195 public boolean supportsNativeOffset()
196 {
197 return true;
198 }
199
200 /***
201 * This method overrides the JDBC escapes used to format dates
202 * using a <code>DateFormat</code>. As of version 2.0.11, the MM
203 * JDBC driver does not implement JDBC 3.0 escapes.
204 *
205 * @param date the date to format
206 * @return The properly formatted date String.
207 */
208 public String getDateString(Date date)
209 {
210 char delim = getStringDelimiter();
211 return (delim + new SimpleDateFormat(DATE_FORMAT).format(date) + delim);
212 }
213 }