001: package org.apache.torque.adapter;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.sql.Connection;
023: import java.sql.SQLException;
024: import java.sql.Timestamp;
025: import java.util.Date;
026:
027: import org.apache.torque.TorqueException;
028: import org.apache.torque.util.Query;
029:
030: /**
031: * This class is the abstract base for any database adapter
032: * Support for new databases is added by subclassing this
033: * class and implementing its abstract methods, and by
034: * registering the new database adapter and its corresponding
035: * JDBC driver in the service configuration file.
036: *
037: * <p>The Torque database adapters exist to present a uniform
038: * interface to database access across all available databases. Once
039: * the necessary adapters have been written and configured,
040: * transparent swapping of databases is theoretically supported with
041: * <i>zero code changes</i> and minimal configuration file
042: * modifications.
043: *
044: * <p>Torque uses the driver class name to find the right adapter.
045: * A JDBC driver corresponding to your adapter must be added to the properties
046: * file, using the fully-qualified class name of the driver. If no driver is
047: * specified for your database, <code>driver.default</code> is used.
048: *
049: * <pre>
050: * #### MySQL MM Driver
051: * database.default.driver=org.gjt.mm.mysql.Driver
052: * database.default.url=jdbc:mysql://localhost/DATABASENAME
053: * </pre>
054: *
055: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
056: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
057: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
058: * @author <a href="mailto:vido@ldh.org">Augustin Vidovic</a>
059: * @version $Id: DB.java 393063 2006-04-10 20:59:16Z tfischer $
060: */
061: public abstract class AbstractDBAdapter implements DB {
062: /**
063: * Empty constructor.
064: */
065: protected AbstractDBAdapter() {
066: }
067:
068: /**
069: * This method is used to ignore case.
070: *
071: * @param in The string to transform to upper case.
072: * @return The upper case string.
073: */
074: public abstract String toUpperCase(String in);
075:
076: /**
077: * Returns the character used to indicate the beginning and end of
078: * a piece of text used in a SQL statement (generally a single
079: * quote).
080: *
081: * @return The text delimeter.
082: */
083: public char getStringDelimiter() {
084: return '\'';
085: }
086:
087: /**
088: * Returns the constant from the {@link
089: * org.apache.torque.adapter.IDMethod} interface denoting which
090: * type of primary key generation method this type of RDBMS uses.
091: *
092: * @return IDMethod constant
093: */
094: public abstract String getIDMethodType();
095:
096: /**
097: * Returns SQL used to get the most recently inserted primary key.
098: * Databases which have no support for this return
099: * <code>null</code>.
100: *
101: * @param obj Information used for key generation.
102: * @return The most recently inserted database key.
103: */
104: public abstract String getIDMethodSQL(Object obj);
105:
106: /**
107: * Locks the specified table.
108: *
109: * @param con The JDBC connection to use.
110: * @param table The name of the table to lock.
111: * @throws SQLException No Statement could be created or executed.
112: */
113: public abstract void lockTable(Connection con, String table)
114: throws SQLException;
115:
116: /**
117: * Unlocks the specified table.
118: *
119: * @param con The JDBC connection to use.
120: * @param table The name of the table to unlock.
121: * @throws SQLException No Statement could be created or executed.
122: */
123: public abstract void unlockTable(Connection con, String table)
124: throws SQLException;
125:
126: /**
127: * This method is used to ignore case.
128: *
129: * @param in The string whose case to ignore.
130: * @return The string in a case that can be ignored.
131: */
132: public abstract String ignoreCase(String in);
133:
134: /**
135: * This method is used to ignore case in an ORDER BY clause.
136: * Usually it is the same as ignoreCase, but some databases
137: * (Interbase for example) does not use the same SQL in ORDER BY
138: * and other clauses.
139: *
140: * @param in The string whose case to ignore.
141: * @return The string in a case that can be ignored.
142: */
143: public String ignoreCaseInOrderBy(String in) {
144: return ignoreCase(in);
145: }
146:
147: /**
148: * This method is used to check whether the database natively
149: * supports limiting the size of the resultset.
150: *
151: * @return True if the database natively supports limiting the
152: * size of the resultset.
153: */
154: public boolean supportsNativeLimit() {
155: return false;
156: }
157:
158: /**
159: * This method is used to check whether the database natively
160: * supports returning results starting at an offset position other
161: * than 0.
162: *
163: * @return True if the database natively supports returning
164: * results starting at an offset position other than 0.
165: */
166: public boolean supportsNativeOffset() {
167: return false;
168: }
169:
170: /**
171: * This method is used to generate the database specific query
172: * extension to limit the number of record returned.
173: *
174: * @param query The query to modify
175: * @param offset the offset Value
176: * @param limit the limit Value
177: *
178: * @throws TorqueException if any error occurs when building the query
179: */
180: public void generateLimits(Query query, int offset, int limit)
181: throws TorqueException {
182: if (supportsNativeLimit()) {
183: query.setLimit(String.valueOf(limit));
184: }
185: }
186:
187: /**
188: * This method is for the SqlExpression.quoteAndEscape rules. The rule is,
189: * any string in a SqlExpression with a BACKSLASH will either be changed to
190: * "\\" or left as "\". SapDB does not need the escape character.
191: *
192: * @return true if the database needs to escape text in SqlExpressions.
193: */
194:
195: public boolean escapeText() {
196: return true;
197: }
198:
199: /**
200: * This method is used to check whether the database supports
201: * limiting the size of the resultset.
202: *
203: * @return The limit style for the database.
204: * @deprecated This should not be exposed to the outside
205: */
206: public int getLimitStyle() {
207: return LIMIT_STYLE_NONE;
208: }
209:
210: /**
211: * This method is used to format any date string.
212: * Database can use different default date formats.
213: *
214: * @param date the Date to format
215: * @return The proper date formatted String.
216: */
217: public String getDateString(Date date) {
218: Timestamp ts = null;
219: if (date instanceof Timestamp) {
220: ts = (Timestamp) date;
221: } else {
222: ts = new Timestamp(date.getTime());
223: }
224:
225: return ("{ts '" + ts + "'}");
226: }
227:
228: /**
229: * This method is used to format a boolean string.
230: *
231: * @param b the Boolean to format
232: * @return The proper date formatted String.
233: */
234: public String getBooleanString(Boolean b) {
235: return (Boolean.TRUE.equals(b) ? "1" : "0");
236: }
237:
238: /**
239: * Whether ILIKE should be used for case insensitive like clauses.
240: *
241: * As most databases do not use ILIKE, this implementation returns false.
242: * This behaviour may be overwritten in subclasses.
243: *
244: * @return true if ilike should be used for case insensitive likes,
245: * false if ignoreCase should be applied to the compared strings.
246: */
247: public boolean useIlike() {
248: return false;
249: }
250:
251: /**
252: * Whether an escape clause in like should be used.
253: * Example : select * from AUTHOR where AUTHOR.NAME like '\_%' ESCAPE '\';
254: *
255: * As most databases do not need the escape clause, this implementation
256: * always returns <code>false</code>. This behaviour can be overwritten
257: * in subclasses.
258: *
259: * @return whether the escape clause should be appended or not.
260: */
261: public boolean useEscapeClauseForLike() {
262: return false;
263: }
264: }
|