HHH-18892 add sha() and md5() functions to HQL

This commit is contained in:
Gavin King 2024-12-03 10:58:24 +01:00
parent 5b3770e289
commit b36ee674ca
10 changed files with 71 additions and 4 deletions

View File

@ -413,7 +413,6 @@ public class CockroachDialect extends Dialect {
functionFactory.substr(); functionFactory.substr();
functionFactory.reverse(); functionFactory.reverse();
functionFactory.repeat(); functionFactory.repeat();
functionFactory.md5();
functionFactory.sha1(); functionFactory.sha1();
functionFactory.octetLength(); functionFactory.octetLength();
functionFactory.bitLength(); functionFactory.bitLength();
@ -503,6 +502,9 @@ public class CockroachDialect extends Dialect {
new PostgreSQLTruncFunction( true, functionContributions.getTypeConfiguration() ) new PostgreSQLTruncFunction( true, functionContributions.getTypeConfiguration() )
); );
functionContributions.getFunctionRegistry().registerAlternateKey( "truncate", "trunc" ); functionContributions.getFunctionRegistry().registerAlternateKey( "truncate", "trunc" );
functionFactory.sha( "digest(?1, 'sha256')" );
functionFactory.md5( "digest(?1, 'md5')" );
} }
@Override @Override

View File

@ -452,6 +452,9 @@ public class DB2Dialect extends Dialect {
functionFactory.unnest_db2( getMaximumSeriesSize() ); functionFactory.unnest_db2( getMaximumSeriesSize() );
functionFactory.generateSeries_recursive( getMaximumSeriesSize(), false, true ); functionFactory.generateSeries_recursive( getMaximumSeriesSize(), false, true );
functionFactory.sha( "hash(?1, 2)" );
functionFactory.md5( "hash(?1, 0)" );
} }
/** /**

View File

@ -368,6 +368,9 @@ public class H2Dialect extends Dialect {
functionFactory.unnest_h2( getMaximumArraySize() ); functionFactory.unnest_h2( getMaximumArraySize() );
functionFactory.generateSeries_h2( getMaximumSeriesSize() ); functionFactory.generateSeries_h2( getMaximumSeriesSize() );
functionFactory.jsonTable_h2( getMaximumArraySize() ); functionFactory.jsonTable_h2( getMaximumArraySize() );
functionFactory.sha( "hash('SHA-256', ?1)" );
functionFactory.md5( "hash('MD5', ?1)" );
} }
/** /**

View File

@ -517,6 +517,9 @@ public class HANADialect extends Dialect {
// functionFactory.xmlextract(); // functionFactory.xmlextract();
functionFactory.generateSeries_hana( getMaximumSeriesSize() ); functionFactory.generateSeries_hana( getMaximumSeriesSize() );
functionFactory.sha( "hash_sha256(?1)" );
functionFactory.md5( "hash_md5(?1)" );
} }
/** /**

View File

@ -590,7 +590,6 @@ public class MySQLDialect extends Dialect {
functionFactory.space(); functionFactory.space();
functionFactory.repeat(); functionFactory.repeat();
functionFactory.pad_space(); functionFactory.pad_space();
functionFactory.md5();
functionFactory.yearMonthDay(); functionFactory.yearMonthDay();
functionFactory.hourMinuteSecond(); functionFactory.hourMinuteSecond();
functionFactory.dayofweekmonthyear(); functionFactory.dayofweekmonthyear();
@ -606,7 +605,6 @@ public class MySQLDialect extends Dialect {
functionFactory.crc32(); functionFactory.crc32();
functionFactory.sha1(); functionFactory.sha1();
functionFactory.sha2(); functionFactory.sha2();
functionFactory.sha();
functionFactory.bitLength(); functionFactory.bitLength();
functionFactory.octetLength(); functionFactory.octetLength();
functionFactory.ascii(); functionFactory.ascii();
@ -688,6 +686,9 @@ public class MySQLDialect extends Dialect {
if ( supportsRecursiveCTE() ) { if ( supportsRecursiveCTE() ) {
functionFactory.generateSeries_recursive( getMaximumSeriesSize(), false, false ); functionFactory.generateSeries_recursive( getMaximumSeriesSize(), false, false );
} }
functionFactory.sha( "unhex(sha2(?1, 256))" );
functionFactory.md5( "unhex(md5(?1))" );
} }
/** /**

View File

@ -440,6 +440,9 @@ public class OracleDialect extends Dialect {
functionFactory.unnest_oracle(); functionFactory.unnest_oracle();
functionFactory.generateSeries_recursive( getMaximumSeriesSize(), true, false ); functionFactory.generateSeries_recursive( getMaximumSeriesSize(), true, false );
functionFactory.jsonTable_oracle(); functionFactory.jsonTable_oracle();
functionFactory.sha( "standard_hash(?1, 'SHA256')" );
functionFactory.md5( "standard_hash(?1, 'MD5')" );
} }
/** /**

View File

@ -550,7 +550,6 @@ public class PostgreSQLDialect extends Dialect {
functionFactory.pi(); functionFactory.pi();
functionFactory.trim2(); functionFactory.trim2();
functionFactory.repeat(); functionFactory.repeat();
functionFactory.md5();
functionFactory.initcap(); functionFactory.initcap();
functionFactory.substr(); functionFactory.substr();
functionFactory.substring_substr(); functionFactory.substring_substr();
@ -690,6 +689,9 @@ public class PostgreSQLDialect extends Dialect {
functionFactory.unnest_postgresql(); functionFactory.unnest_postgresql();
} }
functionFactory.generateSeries( null, "ordinality", false ); functionFactory.generateSeries( null, "ordinality", false );
functionFactory.sha( "sha256(?1)" );
functionFactory.md5( "decode(md5(?1), 'hex')" );
} }
@Override @Override

View File

@ -487,6 +487,9 @@ public class SQLServerDialect extends AbstractTransactSQLDialect {
functionFactory.generateSeries_recursive( getMaximumSeriesSize(), false, false ); functionFactory.generateSeries_recursive( getMaximumSeriesSize(), false, false );
} }
} }
functionFactory.sha( "hashbytes('SHA2_256', ?1)" );
functionFactory.md5( "hashbytes('MD5', ?1)" );
} }
/** /**

View File

@ -68,6 +68,7 @@ public class CommonFunctionFactory {
private final BasicType<Boolean> booleanType; private final BasicType<Boolean> booleanType;
private final BasicType<Character> characterType; private final BasicType<Character> characterType;
private final BasicType<String> stringType; private final BasicType<String> stringType;
private final BasicType<byte[]> binaryType;
private final BasicType<Integer> integerType; private final BasicType<Integer> integerType;
private final BasicType<Long> longType; private final BasicType<Long> longType;
private final BasicType<Double> doubleType; private final BasicType<Double> doubleType;
@ -90,6 +91,7 @@ public class CommonFunctionFactory {
characterType = basicTypeRegistry.resolve(StandardBasicTypes.CHARACTER); characterType = basicTypeRegistry.resolve(StandardBasicTypes.CHARACTER);
booleanType = basicTypeRegistry.resolve(StandardBasicTypes.BOOLEAN); booleanType = basicTypeRegistry.resolve(StandardBasicTypes.BOOLEAN);
stringType = basicTypeRegistry.resolve(StandardBasicTypes.STRING); stringType = basicTypeRegistry.resolve(StandardBasicTypes.STRING);
binaryType = basicTypeRegistry.resolve(StandardBasicTypes.BINARY);
integerType = basicTypeRegistry.resolve(StandardBasicTypes.INTEGER); integerType = basicTypeRegistry.resolve(StandardBasicTypes.INTEGER);
doubleType = basicTypeRegistry.resolve(StandardBasicTypes.DOUBLE); doubleType = basicTypeRegistry.resolve(StandardBasicTypes.DOUBLE);
} }
@ -813,6 +815,7 @@ public class CommonFunctionFactory {
functionRegistry.registerAlternateKey( "repeat", "replicate" ); functionRegistry.registerAlternateKey( "repeat", "replicate" );
} }
@Deprecated(since = "7")
public void md5() { public void md5() {
functionRegistry.namedDescriptorBuilder( "md5" ) functionRegistry.namedDescriptorBuilder( "md5" )
.setInvariantType(stringType) .setInvariantType(stringType)
@ -2313,6 +2316,7 @@ public class CommonFunctionFactory {
.register(); .register();
} }
@Deprecated(since = "7")
public void crc32() { public void crc32() {
functionRegistry.namedDescriptorBuilder( "crc32" ) functionRegistry.namedDescriptorBuilder( "crc32" )
.setInvariantType(integerType) .setInvariantType(integerType)
@ -2321,6 +2325,23 @@ public class CommonFunctionFactory {
.register(); .register();
} }
public void md5(String pattern) {
functionRegistry.patternDescriptorBuilder( "md5", pattern )
.setInvariantType(binaryType)
.setParameterTypes( STRING )
.setExactArgumentCount( 1 )
.register();
}
public void sha(String pattern) {
functionRegistry.patternDescriptorBuilder( "sha", pattern )
.setInvariantType(binaryType)
.setParameterTypes( STRING )
.setExactArgumentCount( 1 )
.register();
}
@Deprecated(since = "7")
public void sha1() { public void sha1() {
functionRegistry.namedDescriptorBuilder( "sha1" ) functionRegistry.namedDescriptorBuilder( "sha1" )
.setInvariantType(stringType) .setInvariantType(stringType)
@ -2329,6 +2350,7 @@ public class CommonFunctionFactory {
.register(); .register();
} }
@Deprecated(since = "7")
public void sha2() { public void sha2() {
functionRegistry.namedDescriptorBuilder( "sha2" ) functionRegistry.namedDescriptorBuilder( "sha2" )
.setInvariantType(stringType) .setInvariantType(stringType)
@ -2337,6 +2359,7 @@ public class CommonFunctionFactory {
.register(); .register();
} }
@Deprecated(since = "7")
public void sha() { public void sha() {
functionRegistry.namedDescriptorBuilder( "sha" ) functionRegistry.namedDescriptorBuilder( "sha" )
.setInvariantType(stringType) .setInvariantType(stringType)

View File

@ -15,6 +15,7 @@ import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.DB2Dialect; import org.hibernate.dialect.DB2Dialect;
import org.hibernate.community.dialect.DerbyDialect; import org.hibernate.community.dialect.DerbyDialect;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.HANADialect;
import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MariaDBDialect; import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MySQLDialect; import org.hibernate.dialect.MySQLDialect;
@ -47,6 +48,8 @@ import org.junit.jupiter.api.Test;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Duration; import java.time.Duration;
@ -2596,4 +2599,25 @@ public class FunctionTests {
.getSingleResultOrNull(); .getSingleResultOrNull();
}); });
} }
@Test
@RequiresDialect(PostgreSQLDialect.class)
@RequiresDialect(MySQLDialect.class)
@RequiresDialect(OracleDialect.class)
@RequiresDialect(DB2Dialect.class)
@RequiresDialect(SQLServerDialect.class)
@RequiresDialect(H2Dialect.class)
@RequiresDialect(HANADialect.class)
@RequiresDialect(CockroachDialect.class)
public void testSha256Function(SessionFactoryScope scope) {
scope.inTransaction(s -> {
byte[] bytes = s.createSelectionQuery("select sha('hello')", byte[].class).getSingleResult();
try {
assertArrayEquals( MessageDigest.getInstance( "SHA-256" ).digest("hello".getBytes()), bytes );
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException( e );
}
});
}
} }