package org.hibernate.dialect;
import java.lang.reflect.Method;
import java.sql.Types;
import java.util.Locale;
import org.hibernate.MappingException;
import org.hibernate.dialect.function.AnsiTrimFunction;
import org.hibernate.dialect.function.DerbyConcatFunction;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.sql.CaseFragment;
import org.hibernate.sql.DerbyCaseFragment;
import org.jboss.logging.Logger;
@Deprecated
public class DerbyDialect extends DB2Dialect {
@SuppressWarnings("deprecation")
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
DerbyDialect.class.getName()
);
private int driverVersionMajor;
private int driverVersionMinor;
@SuppressWarnings("deprecation")
public DerbyDialect() {
super();
if ( this.getClass() == DerbyDialect.class ) {
LOG.deprecatedDerbyDialect();
}
registerFunction( "concat", new DerbyConcatFunction() );
registerFunction( "trim", new AnsiTrimFunction() );
registerColumnType( Types.BLOB, "blob" );
determineDriverVersion();
if ( driverVersionMajor > 10 || ( driverVersionMajor == 10 && driverVersionMinor >= 7 ) ) {
registerColumnType( Types.BOOLEAN, "boolean" );
}
}
private void determineDriverVersion() {
try {
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 );
driverVersionMinor = (Integer) minorVersionGetter.invoke( null, ReflectHelper.NO_PARAMS );
}
catch ( Exception e ) {
LOG.unableToLoadDerbyDriver( e.getMessage() );
driverVersionMajor = -1;
driverVersionMinor = -1;
}
}
private boolean isTenPointFiveReleaseOrNewer() {
return driverVersionMajor > 10 || ( driverVersionMajor == 10 && driverVersionMinor >= 5 );
}
@Override
public String getCrossJoinSeparator() {
return ", ";
}
@Override
public CaseFragment createCaseFragment() {
return new DerbyCaseFragment();
}
@Override
public boolean dropConstraints() {
return true;
}
@Override
public boolean supportsSequences() {
return driverVersionMajor > 10 || ( driverVersionMajor == 10 && driverVersionMinor >= 6 );
}
@Override
public String getSequenceNextValString(String sequenceName) {
if ( supportsSequences() ) {
return "values next value for " + sequenceName;
}
else {
throw new MappingException( "Derby does not support sequence prior to release 10.6.1.0" );
}
}
@Override
public boolean supportsLimit() {
return isTenPointFiveReleaseOrNewer();
}
@Override
public boolean () {
return false;
}
@Override
@SuppressWarnings("deprecation")
public boolean supportsLimitOffset() {
return isTenPointFiveReleaseOrNewer();
}
@Override
public String getForUpdateString() {
return " for update with rs";
}
@Override
public String getWriteLockString(int timeout) {
return " for update with rs";
}
@Override
public String getReadLockString(int timeout) {
return " for read only with rs";
}
@Override
public String getLimitString(String query, final int offset, final int limit) {
final StringBuilder sb = new StringBuilder(query.length() + 50);
final String normalizedSelect = query.toLowerCase(Locale.ROOT).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();
}
@Override
public boolean supportsVariableLimit() {
return false;
}
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;
}
@Override
public String getQuerySequencesString() {
return null ;
}
@Override
public boolean supportsLobValueChangePropogation() {
return false;
}
@Override
public boolean supportsUnboundedLobLocatorMaterialization() {
return false;
}
}