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:
025: import org.apache.torque.util.Query;
026:
027: /**
028: * This is used to connect via the Application-Driver to DB2
029: * databases.
030: *
031: * <a href="http://www-306.ibm.com/software/data/db2/">http://www-306.ibm.com/software/data/db2/</a>
032: *
033: * @author <a href="mailto:hakan42@gmx.de">Hakan Tandogan</a>
034: * @author <a href="mailto:vido@ldh.org">Augustin Vidovic</a>
035: * @version $Id: DBDB2App.java 473821 2006-11-11 22:37:25Z tv $
036: */
037: public class DBDB2App extends AbstractDBAdapter {
038: /**
039: * Serial version
040: */
041: private static final long serialVersionUID = -3097347241360840675L;
042:
043: /**
044: * Empty constructor.
045: */
046: protected DBDB2App() {
047: }
048:
049: /**
050: * This method is used to ignore case.
051: *
052: * @param in The string to transform to upper case.
053: * @return The upper case string.
054: */
055: public String toUpperCase(String in) {
056: String s = new StringBuffer("UPPER(").append(in).append(")")
057: .toString();
058: return s;
059: }
060:
061: /**
062: * This method is used to ignore case.
063: *
064: * @param in The string whose case to ignore.
065: * @return The string in a case that can be ignored.
066: */
067: public String ignoreCase(String in) {
068: String s = new StringBuffer("UPPER(").append(in).append(")")
069: .toString();
070: return s;
071: }
072:
073: /**
074: * @see org.apache.torque.adapter.DB#getIDMethodType()
075: */
076: public String getIDMethodType() {
077: return NO_ID_METHOD;
078: }
079:
080: /**
081: * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object obj)
082: */
083: public String getIDMethodSQL(Object obj) {
084: return null;
085: }
086:
087: /**
088: * Locks the specified table.
089: *
090: * @param con The JDBC connection to use.
091: * @param table The name of the table to lock.
092: * @exception SQLException No Statement could be created or executed.
093: */
094: public void lockTable(Connection con, String table)
095: throws SQLException {
096: }
097:
098: /**
099: * Unlocks the specified table.
100: *
101: * @param con The JDBC connection to use.
102: * @param table The name of the table to unlock.
103: * @exception SQLException No Statement could be created or executed.
104: */
105: public void unlockTable(Connection con, String table)
106: throws SQLException {
107: }
108:
109: /**
110: * This method is used to check whether the database supports
111: * limiting the size of the resultset.
112: *
113: * @return LIMIT_STYLE_DB2.
114: * @deprecated This should not be exposed to the outside
115: */
116: public int getLimitStyle() {
117: return DB.LIMIT_STYLE_DB2;
118: }
119:
120: /**
121: * Return true for DB2
122: * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
123: */
124: public boolean supportsNativeLimit() {
125: return true;
126: }
127:
128: /**
129: * Return true for DB2
130: * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeOffset()
131: */
132: public boolean supportsNativeOffset() {
133: return true;
134: }
135:
136: /**
137: * Build DB2 (OLAP) -style query with limit or offset.
138: * If the original SQL is in variable: query then the requlting
139: * SQL looks like this:
140: * <pre>
141: * SELECT B.* FROM (
142: * SELECT A.*, row_number() over() as TORQUE$ROWNUM FROM (
143: * query
144: * ) A
145: * ) B WHERE B.TORQUE$ROWNUM > offset AND B.TORQUE$ROWNUM
146: * <= offset + limit
147: * </pre>
148: *
149: * @param query The query to modify
150: * @param offset the offset Value
151: * @param limit the limit Value
152: */
153: public void generateLimits(Query query, int offset, int limit) {
154: StringBuffer preLimit = new StringBuffer()
155: .append("SELECT B.* FROM ( ")
156: .append(
157: "SELECT A.*, row_number() over() AS TORQUE$ROWNUM FROM ( ");
158:
159: StringBuffer postLimit = new StringBuffer().append(" ) A ")
160: .append(" ) B WHERE ");
161:
162: if (offset > 0) {
163: postLimit.append(" B.TORQUE$ROWNUM > ").append(offset);
164:
165: if (limit >= 0) {
166: postLimit.append(" AND B.TORQUE$ROWNUM <= ").append(
167: offset + limit);
168: }
169: } else {
170: postLimit.append(" B.TORQUE$ROWNUM <= ").append(limit);
171: }
172:
173: query.setPreLimit(preLimit.toString());
174: query.setPostLimit(postLimit.toString());
175: query.setLimit(null);
176: }
177: }
|