1 package org.apache.torque.adapter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.SQLException;
24
25 import org.apache.torque.util.Query;
26
27 /***
28 * This is used to connect via the Application-Driver to DB2
29 * databases.
30 *
31 * <a href="http://www-306.ibm.com/software/data/db2/">http://www-306.ibm.com/software/data/db2/</a>
32 *
33 * @author <a href="mailto:hakan42@gmx.de">Hakan Tandogan</a>
34 * @author <a href="mailto:vido@ldh.org">Augustin Vidovic</a>
35 * @version $Id: DBDB2App.java 473821 2006-11-11 22:37:25Z tv $
36 */
37 public class DBDB2App extends AbstractDBAdapter
38 {
39 /***
40 * Serial version
41 */
42 private static final long serialVersionUID = -3097347241360840675L;
43
44 /***
45 * Empty constructor.
46 */
47 protected DBDB2App()
48 {
49 }
50
51 /***
52 * This method is used to ignore case.
53 *
54 * @param in The string to transform to upper case.
55 * @return The upper case string.
56 */
57 public String toUpperCase(String in)
58 {
59 String s = new StringBuffer("UPPER(").append(in).append(")").toString();
60 return s;
61 }
62
63 /***
64 * This method is used to ignore case.
65 *
66 * @param in The string whose case to ignore.
67 * @return The string in a case that can be ignored.
68 */
69 public String ignoreCase(String in)
70 {
71 String s = new StringBuffer("UPPER(").append(in).append(")").toString();
72 return s;
73 }
74
75 /***
76 * @see org.apache.torque.adapter.DB#getIDMethodType()
77 */
78 public String getIDMethodType()
79 {
80 return NO_ID_METHOD;
81 }
82
83 /***
84 * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object obj)
85 */
86 public String getIDMethodSQL(Object obj)
87 {
88 return null;
89 }
90
91 /***
92 * Locks the specified table.
93 *
94 * @param con The JDBC connection to use.
95 * @param table The name of the table to lock.
96 * @exception SQLException No Statement could be created or executed.
97 */
98 public void lockTable(Connection con, String table) throws SQLException
99 {
100 }
101
102 /***
103 * Unlocks the specified table.
104 *
105 * @param con The JDBC connection to use.
106 * @param table The name of the table to unlock.
107 * @exception SQLException No Statement could be created or executed.
108 */
109 public void unlockTable(Connection con, String table) throws SQLException
110 {
111 }
112
113 /***
114 * This method is used to check whether the database supports
115 * limiting the size of the resultset.
116 *
117 * @return LIMIT_STYLE_DB2.
118 * @deprecated This should not be exposed to the outside
119 */
120 public int getLimitStyle()
121 {
122 return DB.LIMIT_STYLE_DB2;
123 }
124
125 /***
126 * Return true for DB2
127 * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
128 */
129 public boolean supportsNativeLimit()
130 {
131 return true;
132 }
133
134 /***
135 * Return true for DB2
136 * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeOffset()
137 */
138 public boolean supportsNativeOffset()
139 {
140 return true;
141 }
142
143 /***
144 * Build DB2 (OLAP) -style query with limit or offset.
145 * If the original SQL is in variable: query then the requlting
146 * SQL looks like this:
147 * <pre>
148 * SELECT B.* FROM (
149 * SELECT A.*, row_number() over() as TORQUE$ROWNUM FROM (
150 * query
151 * ) A
152 * ) B WHERE B.TORQUE$ROWNUM > offset AND B.TORQUE$ROWNUM
153 * <= offset + limit
154 * </pre>
155 *
156 * @param query The query to modify
157 * @param offset the offset Value
158 * @param limit the limit Value
159 */
160 public void generateLimits(Query query, int offset, int limit)
161 {
162 StringBuffer preLimit = new StringBuffer()
163 .append("SELECT B.* FROM ( ")
164 .append("SELECT A.*, row_number() over() AS TORQUE$ROWNUM FROM ( ");
165
166 StringBuffer postLimit = new StringBuffer()
167 .append(" ) A ")
168 .append(" ) B WHERE ");
169
170 if (offset > 0)
171 {
172 postLimit.append(" B.TORQUE$ROWNUM > ")
173 .append(offset);
174
175 if (limit >= 0)
176 {
177 postLimit.append(" AND B.TORQUE$ROWNUM <= ")
178 .append(offset + limit);
179 }
180 }
181 else
182 {
183 postLimit.append(" B.TORQUE$ROWNUM <= ")
184 .append(limit);
185 }
186
187 query.setPreLimit(preLimit.toString());
188 query.setPostLimit(postLimit.toString());
189 query.setLimit(null);
190 }
191 }