View Javadoc

1   package org.apache.torque.adapter;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Turbine" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Turbine", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import java.util.Date;
58  import java.io.Serializable;
59  import java.sql.Connection;
60  import java.sql.SQLException;
61  import java.sql.Timestamp;
62  
63  /***
64   * <code>DB</code> defines the interface for a Torque database
65   * adapter.  Support for new databases is added by subclassing
66   * <code>DB</code> and implementing its abstract interface, and by
67   * registering the new database adapter and its corresponding
68   * JDBC driver in the service configuration file.
69   *
70   * <p>The Torque database adapters exist to present a uniform
71   * interface to database access across all available databases.  Once
72   * the necessary adapters have been written and configured,
73   * transparent swapping of databases is theoretically supported with
74   * <i>zero code changes</i> and minimal configuration file
75   * modifications.
76   *
77   * <p>Torque uses the driver class name to find the right adapter.
78   * A JDBC driver corresponding to your adapter must be added to the properties
79   * file, using the fully-qualified class name of the driver. If no driver is
80   * specified for your database, <code>driver.default</code> is used.
81   *
82   * <pre>
83   * #### MySQL MM Driver
84   * database.default.driver=org.gjt.mm.mysql.Driver
85   * database.default.url=jdbc:mysql://localhost/DATABASENAME
86   * </pre>
87   *
88   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
89   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
90   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
91   * @version $Id: DB.java,v 1.31 2003/01/08 16:43:57 henning Exp $
92   */
93  public abstract class DB implements Serializable, IDMethod
94  {
95      /*** Database does not support limiting result sets. */
96      public static final int LIMIT_STYLE_NONE = 0;
97  
98      /*** <code>SELECT ... LIMIT <limit>, [&lt;offset&gt;]</code> */
99      public static final int LIMIT_STYLE_POSTGRES = 1;
100 
101     /*** <code>SELECT ... LIMIT [<offset>, ] &lt;offset&gt;</code> */
102     public static final int LIMIT_STYLE_MYSQL = 2;
103 
104     /*** <code>SET ROWCOUNT &lt;offset&gt; SELECT ... SET ROWCOUNT 0</code> */
105     public static final int LIMIT_STYLE_SYBASE = 3;
106 
107     /*** <code><pre>SELECT ... WHERE ... AND ROWNUM < <limit></pre></code> */
108     public static final int LIMIT_STYLE_ORACLE = 4;
109 
110     /***
111      * Empty constructor.
112      */
113     protected DB()
114     {
115     }
116 
117     /***
118      * This method is used to ignore case.
119      *
120      * @param in The string to transform to upper case.
121      * @return The upper case string.
122      */
123     public abstract String toUpperCase(String in);
124 
125     /***
126      * Returns the character used to indicate the beginning and end of
127      * a piece of text used in a SQL statement (generally a single
128      * quote).
129      *
130      * @return The text delimeter.
131      */
132     public char getStringDelimiter()
133     {
134         return '\'';
135     }
136 
137     /***
138      * Returns the constant from the {@link
139      * org.apache.torque.adapter.IDMethod} interface denoting which
140      * type of primary key generation method this type of RDBMS uses.
141      *
142      * @return IDMethod constant
143      */
144     public abstract String getIDMethodType();
145 
146     /***
147      * Returns SQL used to get the most recently inserted primary key.
148      * Databases which have no support for this return
149      * <code>null</code>.
150      *
151      * @param obj Information used for key generation.
152      * @return The most recently inserted database key.
153      */
154     public abstract String getIDMethodSQL(Object obj);
155 
156     /***
157      * Locks the specified table.
158      *
159      * @param con The JDBC connection to use.
160      * @param table The name of the table to lock.
161      * @throws SQLException No Statement could be created or executed.
162      */
163     public abstract void lockTable(Connection con, String table)
164             throws SQLException;
165 
166     /***
167      * Unlocks the specified table.
168      *
169      * @param con The JDBC connection to use.
170      * @param table The name of the table to unlock.
171      * @throws SQLException No Statement could be created or executed.
172      */
173     public abstract void unlockTable(Connection con, String table)
174             throws SQLException;
175 
176     /***
177      * This method is used to ignore case.
178      *
179      * @param in The string whose case to ignore.
180      * @return The string in a case that can be ignored.
181      */
182     public abstract String ignoreCase(String in);
183 
184     /***
185      * This method is used to ignore case in an ORDER BY clause.
186      * Usually it is the same as ignoreCase, but some databases
187      * (Interbase for example) does not use the same SQL in ORDER BY
188      * and other clauses.
189      *
190      * @param in The string whose case to ignore.
191      * @return The string in a case that can be ignored.
192      */
193     public String ignoreCaseInOrderBy(String in)
194     {
195         return ignoreCase(in);
196     }
197 
198     /***
199      * This method is used to check whether the database natively
200      * supports limiting the size of the resultset.
201      *
202      * @return True if the database natively supports limiting the
203      * size of the resultset.
204      */
205     public boolean supportsNativeLimit()
206     {
207         return false;
208     }
209 
210     /***
211      * This method is used to check whether the database natively
212      * supports returning results starting at an offset position other
213      * than 0.
214      *
215      * @return True if the database natively supports returning
216      * results starting at an offset position other than 0.
217      */
218     public boolean supportsNativeOffset()
219     {
220         return false;
221     }
222 
223    /***
224     * This method is for the SqlExpression.quoteAndEscape rules.  The rule is,
225     * any string in a SqlExpression with a BACKSLASH will either be changed to
226     * "//" or left as "\".  SapDB does not need the escape character.
227     *
228     * @return true if the database needs to escape text in SqlExpressions.
229     */
230 
231     public boolean escapeText()
232     {
233         return true;
234     }
235 
236     /***
237      * This method is used to check whether the database supports
238      * limiting the size of the resultset.
239      *
240      * @return The limit style for the database.
241      */
242     public int getLimitStyle()
243     {
244         return LIMIT_STYLE_NONE;
245     }
246 
247     /***
248      * This method is used to format any date string.
249      * Database can use different default date formats.
250      *
251      * @param date the Date to format
252      * @return The proper date formatted String.
253      */
254     public String getDateString(Date date)
255     {
256         Timestamp ts = null;
257         if (date instanceof Timestamp)
258         {
259             ts = (Timestamp) date;
260         }
261         else
262         {
263             ts = new Timestamp(date.getTime());
264         }
265 
266         return ("{ts '" + ts + "'}");
267     }
268 
269     /***
270      * This method is used to format a boolean string.
271      *
272      * @param b the Boolean to format
273      * @return The proper date formatted String.
274      */
275     public String getBooleanString(Boolean b)
276     {
277         return (Boolean.TRUE.equals(b) ? "1" : "0");
278     }
279 }