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.Statement;
025: import java.text.SimpleDateFormat;
026: import java.util.Date;
027:
028: import org.apache.torque.TorqueException;
029: import org.apache.torque.util.Criteria;
030: import org.apache.torque.util.Query;
031: import org.apache.torque.util.SqlExpression;
032:
033: /**
034: * This is used to connect to a Sybase database using Sybase's
035: * JConnect JDBC driver.
036: *
037: * <B>NOTE:</B><I>Currently JConnect does not implement the required
038: * methods for ResultSetMetaData, and therefore the village API's may
039: * not function. For connection pooling, everything works.</I>
040: *
041: * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a>
042: * @version $Id: DBSybase.java 586553 2007-10-19 17:33:53Z gmonroe $
043: */
044: public class DBSybase extends AbstractDBAdapter {
045: /**
046: * Serial version
047: */
048: private static final long serialVersionUID = 4782996646843056810L;
049:
050: /** date format */
051: private static final String DATE_FORMAT = "yyyyMMdd HH:mm:ss";
052:
053: /**
054: * Empty constructor.
055: */
056: protected DBSybase() {
057: }
058:
059: /**
060: * This method is used to ignore case.
061: *
062: * @param in The string to transform to upper case.
063: * @return The upper case string.
064: */
065: public String toUpperCase(String in) {
066: return new StringBuffer("UPPER(").append(in).append(")")
067: .toString();
068: }
069:
070: /**
071: * This method is used to ignore case.
072: *
073: * @param in The string whose case to ignore.
074: * @return The string in a case that can be ignored.
075: */
076: public String ignoreCase(String in) {
077: return new StringBuffer("UPPER(").append(in).append(")")
078: .toString();
079: }
080:
081: /**
082: * @see org.apache.torque.adapter.DB#getIDMethodType()
083: */
084: public String getIDMethodType() {
085: return AUTO_INCREMENT;
086: }
087:
088: /**
089: * Returns the last value from an identity column (available on a
090: * per-session basis from the global variable
091: * <code>@@identity</code>).
092: *
093: * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object obj)
094: */
095: public String getIDMethodSQL(Object unused) {
096: return "select @@identity";
097: }
098:
099: /**
100: * Locks the specified table.
101: *
102: * @param con The JDBC connection to use.
103: * @param table The name of the table to lock.
104: * @throws SQLException No Statement could be created or executed.
105: */
106: public void lockTable(Connection con, String table)
107: throws SQLException {
108: Statement statement = con.createStatement();
109:
110: StringBuffer stmt = new StringBuffer();
111: stmt.append("SELECT next_id FROM ").append(table).append(
112: " FOR UPDATE");
113:
114: statement.executeQuery(stmt.toString());
115: }
116:
117: /**
118: * Unlocks the specified table.
119: *
120: * @param con The JDBC connection to use.
121: * @param table The name of the table to unlock.
122: * @throws SQLException No Statement could be created or executed.
123: */
124: public void unlockTable(Connection con, String table)
125: throws SQLException {
126: // Tables in Sybase are unlocked when a commit is issued. The
127: // user may have issued a commit but do it here to be sure.
128: con.commit();
129: }
130:
131: /**
132: * This method is used to chek whether the database supports
133: * limiting the size of the resultset.
134: *
135: * @return LIMIT_STYLE_SYBASE.
136: * @deprecated This should not be exposed to the outside
137: */
138: public int getLimitStyle() {
139: return DB.LIMIT_STYLE_SYBASE;
140: }
141:
142: /**
143: * Return true for Sybase
144: * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
145: */
146: public boolean supportsNativeLimit() {
147: return true;
148: }
149:
150: /**
151: * Modify a query to add limit and offset values for Sybase.
152: *
153: * @param query The query to modify
154: * @param offset the offset Value
155: * @param limit the limit Value
156: *
157: * @throws TorqueException if any error occurs when building the query
158: */
159: public void generateLimits(Query query, int offset, int limit)
160: throws TorqueException {
161: if (limit < 0 && offset >= 0) // Offset only test
162: {
163: return;
164: }
165: if (limit + offset > 0) {
166: query.setRowcount(String.valueOf(limit + offset));
167: } else if (limit + offset == 0) {
168: // This is necessary to create the empty result set that Torque expects
169: query.getWhereClause().add(
170: SqlExpression.build("1", new Integer(0),
171: Criteria.EQUAL));
172: }
173: }
174:
175: /**
176: * This method overrides the JDBC escapes used to format dates
177: * using a <code>DateFormat</code>. As of version 11, the Sybase
178: * JDBC driver does not implement JDBC 3.0 escapes.
179: *
180: * @param date the date to format
181: * @return The properly formatted date String.
182: */
183: public String getDateString(Date date) {
184: char delim = getStringDelimiter();
185: return (delim + new SimpleDateFormat(DATE_FORMAT).format(date) + delim);
186: }
187:
188: /**
189: * Determines whether backslashes (\) should be escaped in explicit SQL
190: * strings. If true is returned, a BACKSLASH will be changed to "\\".
191: * If false is returned, a BACKSLASH will be left as "\".
192: *
193: * Sybase (and MSSQL) doesn't define a default escape character,
194: * so false is returned.
195: *
196: * @return false
197: * @see org.apache.torque.adapter.DB#escapeText()
198: */
199: public boolean escapeText() {
200: return false;
201: }
202:
203: /**
204: * Whether an escape clause in like should be used.
205: * Example : select * from AUTHOR where AUTHOR.NAME like '\_%' ESCAPE '\';
206: *
207: * Sybase needs this, so this implementation always returns
208: * <code>true</code>.
209: *
210: * @return whether the escape clause should be appended or not.
211: */
212: public boolean useEscapeClauseForLike() {
213: return true;
214: }
215: }
|