|  | 
|  | 1 | +/* Hibernate, Relational Persistence for Idiomatic Java | 
|  | 2 | + * | 
|  | 3 | + * SPDX-License-Identifier: Apache-2.0 | 
|  | 4 | + * Copyright: Red Hat Inc. and Hibernate Authors | 
|  | 5 | + */ | 
|  | 6 | +package org.hibernate.reactive.persister.entity.impl; | 
|  | 7 | + | 
|  | 8 | +import java.util.Iterator; | 
|  | 9 | + | 
|  | 10 | +import org.hibernate.HibernateException; | 
|  | 11 | +import org.hibernate.MappingException; | 
|  | 12 | +import org.hibernate.dialect.CockroachDB192Dialect; | 
|  | 13 | +import org.hibernate.dialect.DB2Dialect; | 
|  | 14 | +import org.hibernate.dialect.Dialect; | 
|  | 15 | +import org.hibernate.dialect.PostgreSQL81Dialect; | 
|  | 16 | +import org.hibernate.dialect.SQLServerDialect; | 
|  | 17 | +import org.hibernate.id.IdentityGenerator; | 
|  | 18 | +import org.hibernate.id.PostInsertIdentityPersister; | 
|  | 19 | +import org.hibernate.id.insert.IdentifierGeneratingInsert; | 
|  | 20 | +import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate; | 
|  | 21 | +import org.hibernate.sql.Insert; | 
|  | 22 | + | 
|  | 23 | +/** | 
|  | 24 | + * Fix the insert and select id queries generated by Hibernate ORM | 
|  | 25 | + */ | 
|  | 26 | +public class ReactiveIdentityGenerator extends IdentityGenerator { | 
|  | 27 | + | 
|  | 28 | +	@Override | 
|  | 29 | +	public InsertGeneratedIdentifierDelegate getInsertGeneratedIdentifierDelegate( | 
|  | 30 | +			PostInsertIdentityPersister persister, Dialect dialect, boolean isGetGeneratedKeysEnabled) | 
|  | 31 | +			throws HibernateException { | 
|  | 32 | +		return new ReactiveInsertAndSelectDelegate( persister, dialect ); | 
|  | 33 | +	} | 
|  | 34 | + | 
|  | 35 | +	public static class ReactiveInsertAndSelectDelegate extends InsertSelectDelegate { | 
|  | 36 | + | 
|  | 37 | +		private final PostInsertIdentityPersister persister; | 
|  | 38 | +		private final Dialect dialect; | 
|  | 39 | + | 
|  | 40 | +		public ReactiveInsertAndSelectDelegate(PostInsertIdentityPersister persister, Dialect dialect) { | 
|  | 41 | +			super( persister, dialect ); | 
|  | 42 | +			this.persister = persister; | 
|  | 43 | +			this.dialect = dialect; | 
|  | 44 | +		} | 
|  | 45 | + | 
|  | 46 | +		@Override | 
|  | 47 | +		public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() { | 
|  | 48 | +			IdentifierGeneratingInsert insert = createInsert(); | 
|  | 49 | +			insert.addIdentityColumn( persister.getRootTableKeyColumnNames()[0] ); | 
|  | 50 | +			return insert; | 
|  | 51 | +		} | 
|  | 52 | + | 
|  | 53 | +		private IdentifierGeneratingInsert createInsert() { | 
|  | 54 | +			if ( dialect instanceof PostgreSQL81Dialect || dialect instanceof CockroachDB192Dialect ) { | 
|  | 55 | +				return new PostgresIdentifierGeneratingInsert( dialect ); | 
|  | 56 | +			} | 
|  | 57 | +			if ( dialect instanceof SQLServerDialect ) { | 
|  | 58 | +				return new SqlServerIdentifierGeneratingInsert( dialect ); | 
|  | 59 | +			} | 
|  | 60 | +			if ( dialect instanceof DB2Dialect ) { | 
|  | 61 | +				return new Db2IdentifierGeneratingInsert( dialect ); | 
|  | 62 | +			} | 
|  | 63 | +			return super.prepareIdentifierGeneratingInsert(); | 
|  | 64 | +		} | 
|  | 65 | +	} | 
|  | 66 | + | 
|  | 67 | +	public static class Db2IdentifierGeneratingInsert extends IdentifierGeneratingInsert { | 
|  | 68 | + | 
|  | 69 | +		private String identityColumnName; | 
|  | 70 | + | 
|  | 71 | +		public Db2IdentifierGeneratingInsert(Dialect dialect) { | 
|  | 72 | +			super( dialect ); | 
|  | 73 | +		} | 
|  | 74 | + | 
|  | 75 | +		@Override | 
|  | 76 | +		public Insert addIdentityColumn(String columnName) { | 
|  | 77 | +			this.identityColumnName = columnName; | 
|  | 78 | +			return super.addIdentityColumn( columnName ); | 
|  | 79 | +		} | 
|  | 80 | + | 
|  | 81 | +		/** | 
|  | 82 | +		 * @see Insert#toStatementString() | 
|  | 83 | +		 */ | 
|  | 84 | +		@Override | 
|  | 85 | +		public String toStatementString() { | 
|  | 86 | +			return "select " + identityColumnName + " from NEW TABLE (" + super.toStatementString() + ")"; | 
|  | 87 | +		} | 
|  | 88 | +	} | 
|  | 89 | + | 
|  | 90 | +	public static class PostgresIdentifierGeneratingInsert extends IdentifierGeneratingInsert { | 
|  | 91 | + | 
|  | 92 | +		private String identityColumnName; | 
|  | 93 | + | 
|  | 94 | +		public PostgresIdentifierGeneratingInsert(Dialect dialect) { | 
|  | 95 | +			super( dialect ); | 
|  | 96 | +		} | 
|  | 97 | + | 
|  | 98 | +		@Override | 
|  | 99 | +		public Insert addIdentityColumn(String columnName) { | 
|  | 100 | +			this.identityColumnName = columnName; | 
|  | 101 | +			return super.addIdentityColumn( columnName ); | 
|  | 102 | +		} | 
|  | 103 | + | 
|  | 104 | +		@Override | 
|  | 105 | +		public String toStatementString() { | 
|  | 106 | +			return super.toStatementString() + " returning " + identityColumnName; | 
|  | 107 | +		} | 
|  | 108 | +	} | 
|  | 109 | + | 
|  | 110 | +	public static class SqlServerIdentifierGeneratingInsert extends IdentifierGeneratingInsert { | 
|  | 111 | +		private String identityColumnName; | 
|  | 112 | + | 
|  | 113 | +		public SqlServerIdentifierGeneratingInsert(Dialect dialect) { | 
|  | 114 | +			super( dialect ); | 
|  | 115 | +		} | 
|  | 116 | + | 
|  | 117 | +		@Override | 
|  | 118 | +		public Insert addIdentityColumn(String columnName) { | 
|  | 119 | +			this.identityColumnName = columnName; | 
|  | 120 | +			return super.addIdentityColumn( columnName ); | 
|  | 121 | +		} | 
|  | 122 | + | 
|  | 123 | +		/** | 
|  | 124 | +		 * @see Insert#toStatementString() | 
|  | 125 | +		 */ | 
|  | 126 | +		public String toStatementString() { | 
|  | 127 | +			StringBuilder buf = new StringBuilder( columns.size() * 15 + tableName.length() + 10 ); | 
|  | 128 | +			if ( comment != null ) { | 
|  | 129 | +				buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " ); | 
|  | 130 | +			} | 
|  | 131 | +			buf.append( "insert into " ).append( tableName ); | 
|  | 132 | +			if ( columns.size() == 0 ) { | 
|  | 133 | +				if ( getDialect().supportsNoColumnsInsert() ) { | 
|  | 134 | +					// This line is missing in ORM | 
|  | 135 | +					buf.append( " output inserted." ).append( identityColumnName ); | 
|  | 136 | +					buf.append( ' ' ).append( getDialect().getNoColumnsInsertString() ); | 
|  | 137 | +				} | 
|  | 138 | +				else { | 
|  | 139 | +					throw new MappingException( String.format( | 
|  | 140 | +							"The INSERT statement for table [%s] contains no column, and this is not supported by [%s]", | 
|  | 141 | +							tableName, | 
|  | 142 | +							getDialect() | 
|  | 143 | +					) | 
|  | 144 | +					); | 
|  | 145 | +				} | 
|  | 146 | +			} | 
|  | 147 | +			else { | 
|  | 148 | +				buf.append( " (" ); | 
|  | 149 | +				Iterator<String> iter = columns.keySet().iterator(); | 
|  | 150 | +				while ( iter.hasNext() ) { | 
|  | 151 | +					buf.append( iter.next() ); | 
|  | 152 | +					if ( iter.hasNext() ) { | 
|  | 153 | +						buf.append( ", " ); | 
|  | 154 | +					} | 
|  | 155 | +				} | 
|  | 156 | +				buf.append( ")"); | 
|  | 157 | +				// This line is missing in ORM | 
|  | 158 | +				buf.append( " output inserted." ).append( identityColumnName ); | 
|  | 159 | +				buf.append( " values (" ); | 
|  | 160 | +				iter = columns.values().iterator(); | 
|  | 161 | +				while ( iter.hasNext() ) { | 
|  | 162 | +					buf.append( iter.next() ); | 
|  | 163 | +					if ( iter.hasNext() ) { | 
|  | 164 | +						buf.append( ", " ); | 
|  | 165 | +					} | 
|  | 166 | +				} | 
|  | 167 | +				buf.append( ')' ); | 
|  | 168 | +			} | 
|  | 169 | +			return buf.toString(); | 
|  | 170 | +		} | 
|  | 171 | +	} | 
|  | 172 | + | 
|  | 173 | +} | 
0 commit comments