KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > adapter > DBMM


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 JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
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 473821 2006-11-11 22:37:25Z tv $
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 JavaDoc 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 JavaDoc toUpperCase(String JavaDoc 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 JavaDoc ignoreCase(String JavaDoc in)
80     {
81         return in;
82     }
83
84     /**
85      * @see org.apache.torque.adapter.DB#getIDMethodType()
86      */

87     public String JavaDoc 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 JavaDoc getIDMethodSQL(Object JavaDoc 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 JavaDoc con, String JavaDoc table) throws SQLException JavaDoc
113     {
114         Statement JavaDoc statement = con.createStatement();
115         StringBuffer JavaDoc stmt = new StringBuffer JavaDoc();
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 JavaDoc con, String JavaDoc table) throws SQLException JavaDoc
129     {
130         Statement JavaDoc statement = con.createStatement();
131         statement.executeUpdate("UNLOCK TABLES");
132     }
133
134     /**
135      * Generate a LIMIT offset, limit clause if offset &gt; 0
136      * or an LIMIT limit clause if limit is &gt; 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         StringBuffer JavaDoc limitStringBuffer = new StringBuffer JavaDoc();
146
147         if (offset > 0)
148         {
149             limitStringBuffer.append(offset)
150                     .append(", ")
151                     .append(limit);
152         }
153         else
154         {
155             if (limit >= 0)
156             {
157                 limitStringBuffer.append(limit);
158             }
159         }
160
161         query.setLimit(limitStringBuffer.toString());
162         query.setPreLimit(null);
163         query.setPostLimit(null);
164     }
165
166     /**
167      * This method is used to chek whether the database supports
168      * limiting the size of the resultset.
169      *
170      * @return LIMIT_STYLE_MYSQL.
171      * @deprecated This should not be exposed to the outside
172      */

173     public int getLimitStyle()
174     {
175         return DB.LIMIT_STYLE_MYSQL;
176     }
177
178     /**
179      * Return true for MySQL
180      * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
181      */

182     public boolean supportsNativeLimit()
183     {
184         return true;
185     }
186
187     /**
188      * Return true for MySQL
189      * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeOffset()
190      */

191     public boolean supportsNativeOffset()
192     {
193         return true;
194     }
195
196     /**
197      * This method overrides the JDBC escapes used to format dates
198      * using a <code>DateFormat</code>. As of version 2.0.11, the MM
199      * JDBC driver does not implement JDBC 3.0 escapes.
200      *
201      * @param date the date to format
202      * @return The properly formatted date String.
203      */

204     public String JavaDoc getDateString(Date JavaDoc date)
205     {
206         char delim = getStringDelimiter();
207         return (delim + new SimpleDateFormat JavaDoc(DATE_FORMAT).format(date) + delim);
208     }
209 }
210
Popular Tags