make SelectGenerator capable of using 'insert ... returning ...'

This commit is contained in:
Gavin 2022-12-18 03:34:05 +01:00 committed by Gavin King
parent 679ed3bbee
commit d49b568d7b
4 changed files with 37 additions and 7 deletions

View File

@ -3394,10 +3394,10 @@ public abstract class Dialect implements ConversionContext {
}
/**
* Certain dialects support a subset of {@code ScrollModes}.
* Certain dialects support a subset of {@link ScrollMode}s.
* Provide a default to be used by Criteria and Query.
*
* @return ScrollMode
* @return the default {@link ScrollMode} to use.
*/
public ScrollMode defaultScrollMode() {
return ScrollMode.SCROLL_INSENSITIVE;
@ -3408,7 +3408,7 @@ public abstract class Dialect implements ConversionContext {
* For example:
* {@code select * from Table1 where col1 in (select col1 from Table2 order by col2 limit 1 offset 1)}
*
* @return boolean
* @return {@code true} if it does
*/
public boolean supportsOffsetInSubquery() {
return false;
@ -3419,7 +3419,7 @@ public abstract class Dialect implements ConversionContext {
* For example:
* {@code select * from Table1 where col1 in (select col1 from Table2 order by col2 limit 1)}
*
* @return boolean
* @return {@code true} if it does
*/
public boolean supportsOrderByInSubquery() {
return true;
@ -3430,12 +3430,24 @@ public abstract class Dialect implements ConversionContext {
* For example:
* {@code select col1, (select col2 from Table2 where ...) from Table1}
*
* @return boolean
* @return {@code true} if it does
*/
public boolean supportsSubqueryInSelect() {
return true;
}
/**
* Does this dialect fully support {@code insert ... returning ...} in some form?
*
* @return {@code true} if {@link org.hibernate.id.insert.InsertReturningDelegate}
* works for any sort of primary key column (not just identity columns), or
* {@code false} if {@code InsertReturningDelegate} does not work, or only
* works for specialized identity/"autoincrement" columns
*/
public boolean supportsInsertReturning() {
return false;
}
/**
* Does this dialect support the given fetch clause type.
*

View File

@ -22,7 +22,6 @@ import org.hibernate.query.spi.QueryEngine;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.spi.StandardSqlAstTranslatorFactory;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.exec.spi.JdbcOperation;
@ -236,6 +235,11 @@ public class MariaDBDialect extends MySQLDialect {
return false;
}
@Override
public boolean supportsInsertReturning() {
return true;
}
@Override
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
throws SQLException {

View File

@ -1218,6 +1218,11 @@ public class PostgreSQLDialect extends Dialect {
return true;
}
@Override
public boolean supportsInsertReturning() {
return true;
}
@Override
public boolean supportsOffsetInSubquery() {
return true;

View File

@ -12,6 +12,7 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.generator.InDatabaseGenerator;
import org.hibernate.id.factory.spi.StandardGenerator;
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;
import org.hibernate.id.insert.InsertReturningDelegate;
import org.hibernate.id.insert.UniqueKeySelectingDelegate;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.ServiceRegistry;
@ -100,7 +101,15 @@ public class SelectGenerator
@Override
public InsertGeneratedIdentifierDelegate getGeneratedIdentifierDelegate(PostInsertIdentityPersister persister) {
Dialect dialect = persister.getFactory().getJdbcServices().getDialect();
return new UniqueKeySelectingDelegate( persister, dialect, getUniqueKeyPropertyName( persister ) );
if ( dialect.supportsInsertReturning() ) {
//TODO: this is not quite right, since TableInsertReturningBuilder and then TableInsertStandard
// ultimately end up calling the SqlAstTranslator to generate the SQL which on H2 delegates
// back to IdentityColumnSupport, and this just might not be an identity column
return new InsertReturningDelegate( persister, dialect );
}
else {
return new UniqueKeySelectingDelegate( persister, dialect, getUniqueKeyPropertyName( persister ) );
}
}
@Override