|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.dialect.sql.ast; |
| 6 | + |
| 7 | + |
| 8 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 9 | +import org.hibernate.sql.ast.spi.SqlAstTranslatorWithUpsert; |
| 10 | +import org.hibernate.sql.ast.tree.Statement; |
| 11 | +import org.hibernate.sql.exec.spi.JdbcOperation; |
| 12 | +import org.hibernate.sql.model.ast.ColumnValueBinding; |
| 13 | +import org.hibernate.sql.model.internal.OptionalTableUpdate; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Jan Schatteman |
| 19 | + */ |
| 20 | +public class SqlAstTranslatorWithOnDuplicateKeyUpdate<T extends JdbcOperation> extends SqlAstTranslatorWithUpsert<T> { |
| 21 | + |
| 22 | + public SqlAstTranslatorWithOnDuplicateKeyUpdate(SessionFactoryImplementor sessionFactory, Statement statement) { |
| 23 | + super( sessionFactory, statement ); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected void renderUpsertStatement(OptionalTableUpdate optionalTableUpdate) { |
| 28 | +/* |
| 29 | + Template: (for an entity with @Version, and without using values() - but this might require changes in parameter binding) |
| 30 | + INSERT INTO employees (id, name, salary, version) |
| 31 | + VALUES (?, ?, ?, ?) AS t |
| 32 | + ON DUPLICATE KEY UPDATE |
| 33 | + name = IF(employees.version=?,t.name,employees.name), |
| 34 | + salary = IF(employees.version=?,t.salary,employees.salary), |
| 35 | + version = IF(employees.version=?,t.version,employees.version), |
| 36 | +
|
| 37 | + So, initially we'll have: |
| 38 | + INSERT INTO employees (id, name, salary, version) |
| 39 | + VALUES (?, ?, ?, ?) |
| 40 | + ON DUPLICATE KEY UPDATE |
| 41 | + name = IF(version=@oldversion:=?,VALUES(name), employees.name), |
| 42 | + salary = IF(version=@oldversion?,VALUES(salary),employees.salary), |
| 43 | + version = IF(version=@oldversion?,VALUES(version),employees.version), |
| 44 | +*/ |
| 45 | + renderInsertInto( optionalTableUpdate ); |
| 46 | + appendSql( " " ); |
| 47 | + renderOnDuplicateKeyUpdate( optionalTableUpdate ); |
| 48 | + } |
| 49 | + |
| 50 | + protected void renderInsertInto(OptionalTableUpdate optionalTableUpdate) { |
| 51 | + appendSql( "insert into " ); |
| 52 | + appendSql( optionalTableUpdate.getMutatingTable().getTableName() ); |
| 53 | + appendSql( " (" ); |
| 54 | + |
| 55 | + final List<ColumnValueBinding> keyBindings = optionalTableUpdate.getKeyBindings(); |
| 56 | + for ( ColumnValueBinding keyBinding : keyBindings ) { |
| 57 | + appendSql( keyBinding.getColumnReference().getColumnExpression() ); |
| 58 | + appendSql( ',' ); |
| 59 | + } |
| 60 | + |
| 61 | + optionalTableUpdate.forEachValueBinding( (columnPosition, columnValueBinding) -> { |
| 62 | + appendSql( columnValueBinding.getColumnReference().getColumnExpression() ); |
| 63 | + if ( columnPosition != optionalTableUpdate.getValueBindings().size() - 1 ) { |
| 64 | + appendSql( ',' ); |
| 65 | + } |
| 66 | + } ); |
| 67 | + |
| 68 | + appendSql( ") values (" ); |
| 69 | + |
| 70 | + for ( ColumnValueBinding keyBinding : keyBindings ) { |
| 71 | + keyBinding.getValueExpression().accept( this ); |
| 72 | + appendSql( ',' ); |
| 73 | + } |
| 74 | + |
| 75 | + optionalTableUpdate.forEachValueBinding( (columnPosition, columnValueBinding) -> { |
| 76 | + if ( columnPosition > 0 ) { |
| 77 | + appendSql( ',' ); |
| 78 | + } |
| 79 | + columnValueBinding.getValueExpression().accept( this ); |
| 80 | + } ); |
| 81 | + appendSql( ")" ); |
| 82 | + } |
| 83 | + |
| 84 | + protected void renderOnDuplicateKeyUpdate(OptionalTableUpdate optionalTableUpdate) { |
| 85 | + |
| 86 | + |
| 87 | + appendSql( "on duplicate key update " ); |
| 88 | + optionalTableUpdate.forEachValueBinding( (columnPosition, columnValueBinding) -> { |
| 89 | + final String columnName = columnValueBinding.getColumnReference().getColumnExpression(); |
| 90 | + if ( columnPosition > 0 ) { |
| 91 | + appendSql( ',' ); |
| 92 | + } |
| 93 | + appendSql( columnName ); |
| 94 | + append( " = " ); |
| 95 | + |
| 96 | + if ( optionalTableUpdate.getNumberOfOptimisticLockBindings() > 0 ) { |
| 97 | + renderVersionedUpdate( optionalTableUpdate, columnPosition, columnValueBinding ); |
| 98 | + } |
| 99 | + else { |
| 100 | + renderNonVersionedUpdate( optionalTableUpdate, columnValueBinding ); |
| 101 | + } |
| 102 | + } ); |
| 103 | + } |
| 104 | + |
| 105 | + private void renderVersionedUpdate(OptionalTableUpdate optionalTableUpdate, Integer columnPosition, ColumnValueBinding columnValueBinding) { |
| 106 | + final String tableName = optionalTableUpdate.getMutatingTable().getTableName(); |
| 107 | + appendSql( "if(" ); |
| 108 | + renderVersionRestriction( tableName, optionalTableUpdate.getOptimisticLockBindings(), columnPosition ); |
| 109 | + appendSql( "," ); |
| 110 | + appendSql( "values(" ); |
| 111 | + appendSql( columnValueBinding.getColumnReference().getColumnExpression() ); |
| 112 | + appendSql( ")" ); |
| 113 | + appendSql( "," ); |
| 114 | + columnValueBinding.getColumnReference().appendColumnForWrite( this, tableName ); |
| 115 | + appendSql( ")" ); |
| 116 | + } |
| 117 | + |
| 118 | + private void renderNonVersionedUpdate(OptionalTableUpdate optionalTableUpdate, ColumnValueBinding columnValueBinding) { |
| 119 | + appendSql( "values(" ); |
| 120 | + appendSql( columnValueBinding.getColumnReference().getColumnExpression() ); |
| 121 | + appendSql( ")" ); |
| 122 | + } |
| 123 | + |
| 124 | + private void renderVersionRestriction(String tableName, List<ColumnValueBinding> optimisticLockBindings, int index) { |
| 125 | + final String operator = index == 0 ? ":=" : ""; |
| 126 | + final String versionVariable = "@oldversion" + operator; |
| 127 | + for (int i = 0; i < optimisticLockBindings.size(); i++) { |
| 128 | +// if ( i>0 ) { |
| 129 | +// appendSql(" and "); |
| 130 | +// } |
| 131 | + final ColumnValueBinding binding = optimisticLockBindings.get( i ); |
| 132 | + binding.getColumnReference().appendColumnForWrite( this, tableName ); |
| 133 | + appendSql( "=" ); |
| 134 | + appendSql( versionVariable ); |
| 135 | +// if ( i == 0 ) { |
| 136 | + if ( index == 0) { |
| 137 | + binding.getValueExpression().accept( this ); |
| 138 | + } |
| 139 | +// } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments