mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-27 14:30:16 +00:00
suppport 'from new table' on DB2 for retrieving generated primary keys
This commit is contained in:
parent
c4e27c0a2e
commit
01fa0c0fe9
@ -45,8 +45,6 @@
|
||||
import org.hibernate.procedure.internal.DB2CallableStatementSupport;
|
||||
import org.hibernate.procedure.spi.CallableStatementSupport;
|
||||
import org.hibernate.query.spi.QueryEngine;
|
||||
import org.hibernate.procedure.internal.DB2CallableStatementSupport;
|
||||
import org.hibernate.procedure.spi.CallableStatementSupport;
|
||||
import org.hibernate.query.sqm.IntervalType;
|
||||
import org.hibernate.query.sqm.TemporalUnit;
|
||||
import org.hibernate.query.sqm.mutation.internal.cte.CteInsertStrategy;
|
||||
@ -842,6 +840,11 @@ public IdentityColumnSupport getIdentityColumnSupport() {
|
||||
return new DB2IdentityColumnSupport();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsInsertReturning() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsValuesList() {
|
||||
return true;
|
||||
|
@ -39,6 +39,10 @@
|
||||
import org.hibernate.sql.ast.tree.select.SelectStatement;
|
||||
import org.hibernate.sql.ast.tree.update.UpdateStatement;
|
||||
import org.hibernate.sql.exec.spi.JdbcOperation;
|
||||
import org.hibernate.sql.model.internal.TableInsertStandard;
|
||||
|
||||
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
|
||||
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
|
||||
|
||||
/**
|
||||
* A SQL AST translator for DB2.
|
||||
@ -349,16 +353,15 @@ protected void visitInsertStatementOnly(InsertSelectStatement statement) {
|
||||
|
||||
protected boolean renderReturningClause(MutationStatement statement) {
|
||||
final List<ColumnReference> returningColumns = statement.getReturningColumns();
|
||||
final int size = returningColumns.size();
|
||||
if ( size == 0 ) {
|
||||
if ( isEmpty( returningColumns ) ) {
|
||||
return false;
|
||||
}
|
||||
appendSql( "select " );
|
||||
String separator = "";
|
||||
for ( int i = 0; i < size; i++ ) {
|
||||
appendSql( separator );
|
||||
for ( int i = 0; i < returningColumns.size(); i++ ) {
|
||||
if ( i > 0 ) {
|
||||
appendSql( ", " );
|
||||
}
|
||||
appendSql( returningColumns.get( i ).getColumnExpression() );
|
||||
separator = ",";
|
||||
}
|
||||
if ( statement instanceof DeleteStatement ) {
|
||||
appendSql( " from old table (" );
|
||||
@ -369,6 +372,28 @@ protected boolean renderReturningClause(MutationStatement statement) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitStandardTableInsert(TableInsertStandard tableInsert) {
|
||||
final List<ColumnReference> returningColumns = tableInsert.getReturningColumns();
|
||||
if ( isNotEmpty( returningColumns ) ) {
|
||||
appendSql( "select " );
|
||||
|
||||
for ( int i = 0; i < returningColumns.size(); i++ ) {
|
||||
if ( i > 0 ) {
|
||||
appendSql( ", " );
|
||||
}
|
||||
appendSql( returningColumns.get( i ).getColumnExpression() );
|
||||
}
|
||||
|
||||
appendSql( " from new table ( " ); // 'from final table' does not seem to play well with triggers
|
||||
super.visitStandardTableInsert( tableInsert );
|
||||
appendSql( ")" );
|
||||
}
|
||||
else {
|
||||
super.visitStandardTableInsert( tableInsert );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderComparison(Expression lhs, ComparisonOperator operator, Expression rhs) {
|
||||
if ( getDB2Version().isSameOrAfter( 11, 1 ) ) {
|
||||
|
@ -11,7 +11,6 @@
|
||||
import org.hibernate.LockMode;
|
||||
import org.hibernate.dialect.identity.H2IdentityColumnSupport;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.query.sqm.ComparisonOperator;
|
||||
import org.hibernate.sql.ast.Clause;
|
||||
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
|
||||
@ -38,6 +37,8 @@
|
||||
import org.hibernate.sql.exec.spi.JdbcOperation;
|
||||
import org.hibernate.sql.model.internal.TableInsertStandard;
|
||||
|
||||
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
|
||||
|
||||
/**
|
||||
* A SQL AST translator for H2.
|
||||
*
|
||||
@ -53,7 +54,7 @@ public H2SqlAstTranslator(SessionFactoryImplementor sessionFactory, Statement st
|
||||
|
||||
@Override
|
||||
public void visitStandardTableInsert(TableInsertStandard tableInsert) {
|
||||
if ( CollectionHelper.isNotEmpty( tableInsert.getReturningColumns() ) ) {
|
||||
if ( isNotEmpty( tableInsert.getReturningColumns() ) ) {
|
||||
visitReturningInsertStatement( tableInsert );
|
||||
}
|
||||
else {
|
||||
@ -238,7 +239,7 @@ protected boolean renderPrimaryTableReference(TableGroup tableGroup, LockMode lo
|
||||
// i.e. `join ( (select ...) alias join ... )`, so we have to introduce a dummy table reference
|
||||
if ( tableRef instanceof QueryPartTableReference || tableRef.getTableId().startsWith( "(select" ) ) {
|
||||
final boolean realTableGroup = tableGroup.isRealTableGroup()
|
||||
&& ( CollectionHelper.isNotEmpty( tableGroup.getTableReferenceJoins() )
|
||||
&& ( isNotEmpty( tableGroup.getTableReferenceJoins() )
|
||||
|| hasNestedTableGroupsToRender( tableGroup.getNestedTableGroupJoins() ) );
|
||||
if ( realTableGroup ) {
|
||||
appendSql( "dual cross join " );
|
||||
|
@ -15,6 +15,11 @@ public boolean supportsIdentityColumns() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsInsertSelectIdentity() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentitySelectString(String table, String column, int type) {
|
||||
return "values identity_val_local()";
|
||||
@ -29,4 +34,9 @@ public String getIdentityColumnString(int type) {
|
||||
public String getIdentityInsertString() {
|
||||
return "default";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String appendIdentitySelectToInsert(String identityColumnName, String insertString) {
|
||||
return "select " + identityColumnName + " from final table ( " + insertString + " )"; //TODO: should it be 'from new table'?
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user