HHH-3972 - Adding FETCH FIRST and OFFSET support to DerbyDialect
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17867 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
a1e8d7cb0d
commit
9909f9ce72
|
@ -27,6 +27,7 @@ package org.hibernate.dialect;
|
||||||
import org.hibernate.Hibernate;
|
import org.hibernate.Hibernate;
|
||||||
import org.hibernate.QueryException;
|
import org.hibernate.QueryException;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.util.ReflectHelper;
|
||||||
import org.hibernate.engine.Mapping;
|
import org.hibernate.engine.Mapping;
|
||||||
import org.hibernate.engine.SessionFactoryImplementor;
|
import org.hibernate.engine.SessionFactoryImplementor;
|
||||||
import org.hibernate.type.Type;
|
import org.hibernate.type.Type;
|
||||||
|
@ -39,6 +40,10 @@ import org.hibernate.sql.DerbyCaseFragment;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hibernate Dialect for Cloudscape 10 - aka Derby. This implements both an
|
* Hibernate Dialect for Cloudscape 10 - aka Derby. This implements both an
|
||||||
|
@ -49,13 +54,39 @@ import java.util.ArrayList;
|
||||||
* @author Simon Johnston
|
* @author Simon Johnston
|
||||||
*/
|
*/
|
||||||
public class DerbyDialect extends DB2Dialect {
|
public class DerbyDialect extends DB2Dialect {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger( DerbyDialect.class );
|
||||||
|
|
||||||
|
private int driverVersionMajor;
|
||||||
|
private int driverVersionMinor;
|
||||||
|
|
||||||
public DerbyDialect() {
|
public DerbyDialect() {
|
||||||
super();
|
super();
|
||||||
registerFunction( "concat", new DerbyConcatFunction() );
|
registerFunction( "concat", new DerbyConcatFunction() );
|
||||||
registerFunction( "trim", new DerbyTrimFunctionEmulation() );
|
registerFunction( "trim", new DerbyTrimFunctionEmulation() );
|
||||||
|
determineDriverVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*package*/ void determineDriverVersion() {
|
||||||
|
try {
|
||||||
|
// locate the derby sysinfo class and query its version info
|
||||||
|
final Class sysinfoClass = ReflectHelper.classForName( "org.apache.derby.tools.sysinfo", this.getClass() );
|
||||||
|
final Method majorVersionGetter = sysinfoClass.getMethod( "getMajorVersion", ReflectHelper.NO_PARAM_SIGNATURE );
|
||||||
|
final Method minorVersionGetter = sysinfoClass.getMethod( "getMinorVersion", ReflectHelper.NO_PARAM_SIGNATURE );
|
||||||
|
driverVersionMajor = ( (Integer) majorVersionGetter.invoke( null, ReflectHelper.NO_PARAMS ) ).intValue();
|
||||||
|
driverVersionMinor = ( (Integer) minorVersionGetter.invoke( null, ReflectHelper.NO_PARAMS ) ).intValue();
|
||||||
|
}
|
||||||
|
catch ( Exception e ) {
|
||||||
|
log.warn( "Unable to load/access derby driver class sysinfo to check versions : " + e );
|
||||||
|
driverVersionMajor = -1;
|
||||||
|
driverVersionMinor = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ boolean isTenPointFiveReleaseOrNewer() {
|
||||||
|
return driverVersionMajor > 10 || ( driverVersionMajor == 10 && driverVersionMinor >= 5 );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is different in Cloudscape to DB2.
|
* This is different in Cloudscape to DB2.
|
||||||
*/
|
*/
|
||||||
|
@ -83,11 +114,75 @@ public class DerbyDialect extends DB2Dialect {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsLimit() {
|
public boolean supportsLimit() {
|
||||||
return false;
|
return isTenPointFiveReleaseOrNewer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supportsLimitOffset() {
|
public boolean supportsLimitOffset() {
|
||||||
return false;
|
return isTenPointFiveReleaseOrNewer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
* <p/>
|
||||||
|
* From Derby 10.5 Docs:
|
||||||
|
* <pre>
|
||||||
|
* Query
|
||||||
|
* [ORDER BY clause]
|
||||||
|
* [result offset clause]
|
||||||
|
* [fetch first clause]
|
||||||
|
* [FOR UPDATE clause]
|
||||||
|
* [WITH {RR|RS|CS|UR}]
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
public String getLimitString(String query, final int offset, final int limit) {
|
||||||
|
StringBuffer sb = new StringBuffer(query.length() + 50);
|
||||||
|
|
||||||
|
final String normalizedSelect = query.toLowerCase().trim();
|
||||||
|
final int forUpdateIndex = normalizedSelect.lastIndexOf( "for update") ;
|
||||||
|
|
||||||
|
if ( hasForUpdateClause( forUpdateIndex ) ) {
|
||||||
|
sb.append( query.substring( 0, forUpdateIndex-1 ) );
|
||||||
|
}
|
||||||
|
else if ( hasWithClause( normalizedSelect ) ) {
|
||||||
|
sb.append( query.substring( 0, getWithIndex( query ) - 1 ) );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sb.append( query );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( offset == 0 ) {
|
||||||
|
sb.append( " fetch first " );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sb.append( " offset " ).append( offset ).append( " rows fetch next " );
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append( limit ).append( " rows only" );
|
||||||
|
|
||||||
|
if ( hasForUpdateClause( forUpdateIndex ) ) {
|
||||||
|
sb.append(' ');
|
||||||
|
sb.append( query.substring( forUpdateIndex ) );
|
||||||
|
}
|
||||||
|
else if ( hasWithClause( normalizedSelect ) ) {
|
||||||
|
sb.append( ' ' ).append( query.substring( getWithIndex( query ) ) );
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasForUpdateClause(int forUpdateIndex) {
|
||||||
|
return forUpdateIndex >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasWithClause(String normalizedSelect){
|
||||||
|
return normalizedSelect.startsWith( "with ", normalizedSelect.length()-7 );
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getWithIndex(String querySelect) {
|
||||||
|
int i = querySelect.lastIndexOf( "with " );
|
||||||
|
if ( i < 0 ) {
|
||||||
|
i = querySelect.lastIndexOf( "WITH " );
|
||||||
|
}
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getQuerySequencesString() {
|
public String getQuerySequencesString() {
|
||||||
|
|
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
|
||||||
|
* third-party contributors as indicated by either @author tags or express
|
||||||
|
* copyright attribution statements applied by the authors. All
|
||||||
|
* third-party contributions are distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.dialect;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testing of patched support for Derby limit and ofset queries; see HHH-3972
|
||||||
|
*
|
||||||
|
* @author Evan Leonard
|
||||||
|
*/
|
||||||
|
public class DerbyDialectTestCase extends TestCase {
|
||||||
|
|
||||||
|
private static class LocalDerbyDialect extends DerbyDialect {
|
||||||
|
protected boolean isTenPointFiveReleaseOrNewer() {
|
||||||
|
return true; // for test sake :)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInsertLimitClause() {
|
||||||
|
final int limit = 50;
|
||||||
|
final String input = "select * from tablename t where t.cat = 5";
|
||||||
|
final String expected = "select * from tablename t where t.cat = 5 fetch first " + limit + " rows only";
|
||||||
|
|
||||||
|
final String actual = new LocalDerbyDialect().getLimitString( input, 0, limit );
|
||||||
|
assertEquals( expected, actual );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInsertLimitWithOffsetClause() {
|
||||||
|
final int limit = 50;
|
||||||
|
final int offset = 200;
|
||||||
|
final String input = "select * from tablename t where t.cat = 5";
|
||||||
|
final String expected = "select * from tablename t where t.cat = 5 offset " + offset + " rows fetch next " + limit + " rows only";
|
||||||
|
|
||||||
|
final String actual = new LocalDerbyDialect().getLimitString( input, offset, limit );
|
||||||
|
assertEquals( expected, actual );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testInsertLimitWithForUpdateClause() {
|
||||||
|
final int limit = 50;
|
||||||
|
final int offset = 200;
|
||||||
|
final String input = "select c11 as col1, c12 as col2, c13 as col13 from t1 for update of c11, c13";
|
||||||
|
final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 offset " + offset
|
||||||
|
+ " rows fetch next " + limit + " rows only for update of c11, c13";
|
||||||
|
|
||||||
|
final String actual = new LocalDerbyDialect().getLimitString( input, offset, limit );
|
||||||
|
assertEquals( expected, actual );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInsertLimitWithWithClause() {
|
||||||
|
final int limit = 50;
|
||||||
|
final int offset = 200;
|
||||||
|
final String input = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' with rr";
|
||||||
|
final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' offset " + offset
|
||||||
|
+ " rows fetch next " + limit + " rows only with rr";
|
||||||
|
|
||||||
|
final String actual = new LocalDerbyDialect().getLimitString( input, offset, limit );
|
||||||
|
assertEquals( expected, actual );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInsertLimitWithForUpdateAndWithClauses() {
|
||||||
|
final int limit = 50;
|
||||||
|
final int offset = 200;
|
||||||
|
final String input = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' for update of c11,c13 with rr";
|
||||||
|
final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' offset " + offset
|
||||||
|
+ " rows fetch next " + limit + " rows only for update of c11,c13 with rr";
|
||||||
|
|
||||||
|
final String actual = new LocalDerbyDialect().getLimitString( input, offset, limit );
|
||||||
|
assertEquals( expected, actual );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue