View Javadoc

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  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 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 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 &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 limitStringBuffer = new StringBuffer();
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 getDateString(Date date)
205     {
206         char delim = getStringDelimiter();
207         return (delim + new SimpleDateFormat(DATE_FORMAT).format(date) + delim);
208     }
209 }