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