|
5 | 5 | */ |
6 | 6 | package org.hibernate.reactive.persister.entity.impl; |
7 | 7 |
|
8 | | -import java.util.Iterator; |
9 | 8 |
|
10 | | -import org.hibernate.MappingException; |
11 | | -import org.hibernate.boot.model.relational.SqlStringGenerationContext; |
12 | | -import org.hibernate.dialect.CockroachDialect; |
13 | | -import org.hibernate.dialect.DB2Dialect; |
| 9 | +import org.hibernate.cfg.AvailableSettings; |
14 | 10 | import org.hibernate.dialect.Dialect; |
15 | | -import org.hibernate.dialect.PostgreSQLDialect; |
16 | | -import org.hibernate.dialect.SQLServerDialect; |
17 | 11 | import org.hibernate.dialect.identity.CockroachDBIdentityColumnSupport; |
18 | | -import org.hibernate.engine.spi.SessionFactoryImplementor; |
19 | 12 | import org.hibernate.id.IdentityGenerator; |
20 | 13 | import org.hibernate.id.PostInsertIdentityPersister; |
21 | | -import org.hibernate.id.insert.IdentifierGeneratingInsert; |
22 | 14 | import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate; |
23 | | -import org.hibernate.id.insert.InsertReturningDelegate; |
24 | | -import org.hibernate.reactive.id.insert.ReactiveBasicSelectingDelegate; |
25 | | -import org.hibernate.reactive.id.insert.ReactiveInsertReturningDelegate; |
26 | | -import org.hibernate.sql.Insert; |
| 15 | +import org.hibernate.reactive.logging.impl.Log; |
27 | 16 |
|
28 | | -import static org.hibernate.reactive.dialect.ReactiveDialectWrapper.instanceOf; |
| 17 | +import static java.lang.invoke.MethodHandles.lookup; |
| 18 | +import static org.hibernate.reactive.logging.impl.LoggerFactory.make; |
29 | 19 |
|
30 | 20 | /** |
31 | 21 | * Fix the insert and select id queries generated by Hibernate ORM |
32 | 22 | */ |
33 | 23 | public class ReactiveIdentityGenerator extends IdentityGenerator { |
34 | 24 |
|
| 25 | + private static final Log LOG = make( Log.class, lookup() ); |
| 26 | + |
35 | 27 | /** |
36 | 28 | * @see CockroachDBIdentityColumnSupport#supportsIdentityColumns() for some limitations related to CockraochDB |
37 | 29 | */ |
38 | 30 | @Override |
39 | 31 | public InsertGeneratedIdentifierDelegate getGeneratedIdentifierDelegate(PostInsertIdentityPersister persister) { |
40 | 32 | Dialect dialect = persister.getFactory().getJdbcServices().getDialect(); |
41 | | - if ( persister.getFactory().getSessionFactoryOptions().isGetGeneratedKeysEnabled() ) { |
42 | | - return dialect.getIdentityColumnSupport().buildGetGeneratedKeysDelegate( persister, dialect ); |
43 | | - } |
44 | | - if ( dialect.getIdentityColumnSupport().supportsInsertSelectIdentity() |
45 | | - // Basically, this is the only approach that works for Cockroach at the moment. |
46 | | - // That said, there are some limitations about identity generation with Cockroach. |
47 | | - // Check CockroachDBIdentityColumnSupport#supportsIdentityColumns for more details |
48 | | - || instanceOf( dialect, CockroachDialect.class ) ) { |
49 | | - return new ReactiveInsertReturningDelegate( persister, dialect ); |
50 | | - } |
51 | | - return new ReactiveBasicSelectingDelegate( persister, dialect ); |
52 | | - } |
53 | | - |
54 | | - public static class ReactiveInsertAndSelectDelegate extends InsertReturningDelegate { |
55 | | - |
56 | | - private final PostInsertIdentityPersister persister; |
57 | | - private final Dialect dialect; |
58 | | - private final SessionFactoryImplementor factory; |
59 | | - |
60 | | - public ReactiveInsertAndSelectDelegate(PostInsertIdentityPersister persister, Dialect dialect) { |
61 | | - super( persister, dialect ); |
62 | | - this.persister = persister; |
63 | | - this.dialect = dialect; |
64 | | - this.factory = persister.getFactory(); |
65 | | - } |
66 | | - |
67 | | - @Override |
68 | | - public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert(SqlStringGenerationContext context) { |
69 | | - IdentifierGeneratingInsert insert = createInsert( context ); |
70 | | - insert.addIdentityColumn( persister.getRootTableKeyColumnNames()[0] ); |
71 | | - return insert; |
72 | | - } |
73 | | - |
74 | | - private IdentifierGeneratingInsert createInsert(SqlStringGenerationContext context) { |
75 | | - if ( dialect instanceof PostgreSQLDialect || dialect instanceof CockroachDialect ) { |
76 | | - return new PostgresIdentifierGeneratingInsert( factory ); |
77 | | - } |
78 | | - if ( dialect instanceof SQLServerDialect ) { |
79 | | - return new SqlServerIdentifierGeneratingInsert( factory ); |
80 | | - } |
81 | | - if ( dialect instanceof DB2Dialect ) { |
82 | | - return new Db2IdentifierGeneratingInsert( factory ); |
83 | | - } |
84 | | - return super.prepareIdentifierGeneratingInsert( context ); |
85 | | - } |
| 33 | + boolean generatedKeysEnabled = persister.getFactory().getSessionFactoryOptions().isGetGeneratedKeysEnabled(); |
| 34 | + if ( !generatedKeysEnabled ) { |
| 35 | + LOG.debugf( "Ignoring property `%s`", AvailableSettings.USE_GET_GENERATED_KEYS ); |
| 36 | + } |
| 37 | + // With JDBC, it's possible to select different type of queries for the retrieval of the id after |
| 38 | + // an insert. But, we don't need this in Hibernate Reactive, and it's easier to just run the most efficient query |
| 39 | + // for the selected database. |
| 40 | + return dialect.getIdentityColumnSupport().buildGetGeneratedKeysDelegate( persister, dialect ); |
86 | 41 | } |
87 | | - |
88 | | - public static class Db2IdentifierGeneratingInsert extends IdentifierGeneratingInsert { |
89 | | - |
90 | | - private String identityColumnName; |
91 | | - |
92 | | - public Db2IdentifierGeneratingInsert(SessionFactoryImplementor sessionFactory) { |
93 | | - super( sessionFactory ); |
94 | | - } |
95 | | - |
96 | | - @Override |
97 | | - public Insert addIdentityColumn(String columnName) { |
98 | | - this.identityColumnName = columnName; |
99 | | - return super.addIdentityColumn( columnName ); |
100 | | - } |
101 | | - |
102 | | - /** |
103 | | - * @see Insert#toStatementString() |
104 | | - */ |
105 | | - @Override |
106 | | - public String toStatementString() { |
107 | | - return "select " + identityColumnName + " from new table (" + super.toStatementString() + ")"; |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - public static class PostgresIdentifierGeneratingInsert extends IdentifierGeneratingInsert { |
112 | | - |
113 | | - private String identityColumnName; |
114 | | - |
115 | | - public PostgresIdentifierGeneratingInsert(SessionFactoryImplementor sessionFactory) { |
116 | | - super( sessionFactory ); |
117 | | - } |
118 | | - |
119 | | - @Override |
120 | | - public Insert addIdentityColumn(String columnName) { |
121 | | - this.identityColumnName = columnName; |
122 | | - return super.addIdentityColumn( columnName ); |
123 | | - } |
124 | | - |
125 | | - @Override |
126 | | - public String toStatementString() { |
127 | | - return super.toStatementString() + " returning " + identityColumnName; |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - public static class SqlServerIdentifierGeneratingInsert extends IdentifierGeneratingInsert { |
132 | | - private String identityColumnName; |
133 | | - |
134 | | - public SqlServerIdentifierGeneratingInsert(SessionFactoryImplementor sessionFactory) { |
135 | | - super( sessionFactory ); |
136 | | - } |
137 | | - |
138 | | - @Override |
139 | | - public Insert addIdentityColumn(String columnName) { |
140 | | - this.identityColumnName = columnName; |
141 | | - return super.addIdentityColumn( columnName ); |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * @see Insert#toStatementString() |
146 | | - */ |
147 | | - public String toStatementString() { |
148 | | - StringBuilder buf = new StringBuilder( columns.size() * 15 + tableName.length() + 10 ); |
149 | | - if ( comment != null ) { |
150 | | - buf.append( "/* " ).append( Dialect.escapeComment( comment ) ).append( " */ " ); |
151 | | - } |
152 | | - buf.append( "insert into " ).append( tableName ); |
153 | | - if ( columns.size() == 0 ) { |
154 | | - if ( getDialect().supportsNoColumnsInsert() ) { |
155 | | - // This line is missing in ORM |
156 | | - buf.append( " output inserted." ).append( identityColumnName ); |
157 | | - buf.append( ' ' ).append( getDialect().getNoColumnsInsertString() ); |
158 | | - } |
159 | | - else { |
160 | | - throw new MappingException( String.format( |
161 | | - "The INSERT statement for table [%s] contains no column, and this is not supported by [%s]", |
162 | | - tableName, |
163 | | - getDialect() ) ); |
164 | | - } |
165 | | - } |
166 | | - else { |
167 | | - buf.append( " (" ); |
168 | | - Iterator<String> iter = columns.keySet().iterator(); |
169 | | - while ( iter.hasNext() ) { |
170 | | - buf.append( iter.next() ); |
171 | | - if ( iter.hasNext() ) { |
172 | | - buf.append( ", " ); |
173 | | - } |
174 | | - } |
175 | | - buf.append( ')'); |
176 | | - // This line is missing in ORM |
177 | | - buf.append( " output inserted." ).append( identityColumnName ); |
178 | | - buf.append( " values (" ); |
179 | | - iter = columns.values().iterator(); |
180 | | - while ( iter.hasNext() ) { |
181 | | - buf.append( iter.next() ); |
182 | | - if ( iter.hasNext() ) { |
183 | | - buf.append( ", " ); |
184 | | - } |
185 | | - } |
186 | | - buf.append( ')' ); |
187 | | - } |
188 | | - return buf.toString(); |
189 | | - } |
190 | | - } |
191 | | - |
192 | 42 | } |
0 commit comments