Skip to content

Commit a48ee9d

Browse files
arjopoldervaartjfcabral
authored andcommitted
db2 changes
1 parent 4a052cc commit a48ee9d

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Configuration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ public Dialect getDialect(String dbType){
274274
if(dbType.equalsIgnoreCase("Oracle")){
275275
dialect = new OracleDialect();
276276
}
277+
else if(dbType.startsWith("DB2")) {
278+
dialect = new DB2Dialect();
279+
}
277280
else if(dbType.equalsIgnoreCase("MySQL")){
278281
dialect = new MySQLDialect();
279282
}

activejdbc/src/main/java/org/javalite/activejdbc/cache/QueryCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void purgeTableCache(MetaModel metaModel) {
132132
*/
133133
public void purgeTableCache(String tableName) {
134134
MetaModel mm = metaModelFor(tableName);
135-
if(mm != null && enabled && mm.cached()){
135+
if(mm != null && enabled && mm.cached()){
136136
cacheManager.flush(new CacheEvent(mm.getTableName(), getClass().getName()));
137137
}
138138
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.javalite.activejdbc.dialects;
2+
3+
import org.javalite.activejdbc.MetaModel;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* @author William Janssen, Arjo Poldervaart
10+
*/
11+
12+
public class DB2Dialect extends DefaultDialect {
13+
private final List<String> EMPTY_LIST = new ArrayList<String>();
14+
15+
@Override
16+
public String formSelect(String tableName, String[] columns, String subQuery, List<String> orderBys, long limit, long offset) {
17+
// if table name is null, sub query contains a full SQL statement
18+
// we will need a more complex query if offset != -1
19+
// example:
20+
// SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY item_number) as ROWNUMBER, items.* from items WHERE item_description like ?) as TEMP WHERE TEMP.ROWNUMBER BETWEEN 271 AND 280
21+
// select * from (select row_number() over(order by item_number) as rownumber, ORIG.* from (select * from items where item_description like '%2%') as ORIG) as TEMP WHERE TEMP.ROWNUMBER BETWEEN 271 AND 280
22+
boolean needOffset = offset != -1;
23+
boolean needLimit = limit != -1;
24+
StringBuilder fullQuery = new StringBuilder(subQuery == null? 0: subQuery.length());
25+
26+
if (needOffset) {
27+
// first create inner query without order by
28+
StringBuilder innerQuery = new StringBuilder(subQuery == null? 0: subQuery.length());
29+
appendSelect(innerQuery, tableName, columns, null, subQuery, EMPTY_LIST);
30+
31+
fullQuery.append("SELECT * FROM (SELECT ROW_NUMBER() OVER(");
32+
appendOrderBy(fullQuery, orderBys);
33+
fullQuery.append(") AS ROWNUMBER, ORIG.* from (");
34+
fullQuery.append(innerQuery.toString());
35+
fullQuery.append(") as ORIG) as TEMP WHERE TEMP.ROWNUMBER ");
36+
if (needLimit) {
37+
fullQuery.append(" BETWEEN ");
38+
fullQuery.append(offset+1);
39+
fullQuery.append(" AND ");
40+
fullQuery.append(offset+limit);
41+
} else {
42+
fullQuery.append(" > ");
43+
fullQuery.append(offset);
44+
}
45+
46+
} else {
47+
appendSelect(fullQuery, tableName, columns, null, subQuery, orderBys);
48+
if (needLimit) {
49+
fullQuery.append(" FETCH FIRST ").append(limit).append(" ROWS ONLY");
50+
}
51+
}
52+
53+
return fullQuery.toString();
54+
}
55+
56+
@Override
57+
protected void appendEmptyRow(MetaModel metaModel, StringBuilder query) {
58+
query.append('(').append(metaModel.getIdName()).append(") VALUES (DEFAULT)");
59+
}
60+
}

0 commit comments

Comments
 (0)