View Javadoc

1   package org.apache.torque.adapter;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.sql.Connection;
20  import java.sql.SQLException;
21  import java.sql.Statement;
22  import java.util.Date;
23  import java.text.SimpleDateFormat;
24  
25  /***
26   * This is used in order to connect to a MySQL database using the MM
27   * drivers.  Simply comment the above and uncomment this code below and
28   * fill in the appropriate values for DB_NAME, DB_HOST, DB_USER,
29   * DB_PASS.
30   *
31   * <P><A HREF="http://www.worldserver.com/mm.mysql/">
32   * http://www.worldserver.com/mm.mysql/</A>
33   * <p>"jdbc:mysql://" + DB_HOST + "/" + DB_NAME + "?user=" +
34   * DB_USER + "&password=" + DB_PASS;
35   *
36   * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
37   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
38   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
39   * @version $Id: DBMM.java 239636 2005-08-24 12:38:09Z henning $
40   */
41  public class DBMM extends DB
42  {
43  
44      /*** A specialized date format for MySQL. */
45      private static final String DATE_FORMAT = "yyyyMMddHHmmss";
46  
47      /***
48       * Empty protected constructor.
49       */
50      protected DBMM()
51      {
52      }
53  
54      /***
55       * This method is used to ignore case.
56       *
57       * @param in The string to transform to upper case.
58       * @return The upper case string.
59       */
60      public String toUpperCase(String in)
61      {
62          return in;
63      }
64  
65      /***
66       * This method is used to ignore case.
67       *
68       * @param in The string whose case to ignore.
69       * @return The string in a case that can be ignored.
70       */
71      public String ignoreCase(String in)
72      {
73          return in;
74      }
75  
76      /***
77       * @see org.apache.torque.adapter.DB#getIDMethodType()
78       */
79      public String getIDMethodType()
80      {
81          return AUTO_INCREMENT;
82      }
83  
84      /***
85       * Returns the SQL to get the database key of the last row
86       * inserted, which in this case is <code>SELECT
87       * LAST_INSERT_ID()</code>.
88       *
89       * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object obj)
90       */
91      public String getIDMethodSQL(Object obj)
92      {
93          return "SELECT LAST_INSERT_ID()";
94      }
95  
96      /***
97       * Locks the specified table.
98       *
99       * @param con The JDBC connection to use.
100      * @param table The name of the table to lock.
101      * @exception SQLException No Statement could be created or
102      * executed.
103      */
104     public void lockTable(Connection con, String table) throws SQLException
105     {
106         Statement statement = con.createStatement();
107         StringBuffer stmt = new StringBuffer();
108         stmt.append("LOCK TABLE ").append(table).append(" WRITE");
109         statement.executeUpdate(stmt.toString());
110     }
111 
112     /***
113      * Unlocks the specified table.
114      *
115      * @param con The JDBC connection to use.
116      * @param table The name of the table to unlock.
117      * @exception SQLException No Statement could be created or
118      * executed.
119      */
120     public void unlockTable(Connection con, String table) throws SQLException
121     {
122         Statement statement = con.createStatement();
123         statement.executeUpdate("UNLOCK TABLES");
124     }
125 
126     /***
127      * This method is used to chek whether the database natively
128      * supports limiting the size of the resultset.
129      *
130      * @return True.
131      */
132     public boolean supportsNativeLimit()
133     {
134         return true;
135     }
136 
137     /***
138      * This method is used to chek whether the database natively
139      * supports returning results starting at an offset position other
140      * than 0.
141      *
142      * @return True.
143      */
144     public boolean supportsNativeOffset()
145     {
146         return true;
147     }
148 
149     /***
150      * This method is used to chek whether the database supports
151      * limiting the size of the resultset.
152      *
153      * @return LIMIT_STYLE_MYSQL.
154      */
155     public int getLimitStyle()
156     {
157         return DB.LIMIT_STYLE_MYSQL;
158     }
159 
160     /***
161      * This method overrides the JDBC escapes used to format dates
162      * using a <code>DateFormat</code>.  As of version 2.0.11, the MM
163      * JDBC driver does not implement JDBC 3.0 escapes.
164      *
165      * @param date the date to format
166      * @return The properly formatted date String.
167      */
168     public String getDateString(Date date)
169     {
170         char delim = getStringDelimiter();
171         return (delim + new SimpleDateFormat(DATE_FORMAT).format(date) + delim);
172     }
173 }