|
4 | 4 | */ |
5 | 5 | package org.hibernate.id.enhanced; |
6 | 6 |
|
7 | | -import java.sql.Connection; |
8 | 7 | import java.sql.SQLException; |
9 | 8 |
|
10 | 9 | import org.hibernate.AssertionFailure; |
11 | | -import org.hibernate.HibernateException; |
12 | 10 | import org.hibernate.boot.model.relational.Database; |
13 | 11 | import org.hibernate.boot.model.relational.InitCommand; |
14 | 12 | import org.hibernate.boot.model.relational.QualifiedName; |
15 | 13 | import org.hibernate.boot.model.relational.Sequence; |
16 | 14 | import org.hibernate.boot.model.relational.SqlStringGenerationContext; |
17 | | -import org.hibernate.dialect.Dialect; |
18 | 15 | import org.hibernate.engine.spi.SharedSessionContractImplementor; |
19 | 16 | import org.hibernate.id.IntegralDataTypeHolder; |
20 | 17 | import org.hibernate.mapping.Table; |
21 | 18 |
|
22 | 19 | import static org.hibernate.engine.jdbc.JdbcLogging.JDBC_LOGGER; |
23 | 20 | import static org.hibernate.id.IdentifierGeneratorHelper.getIntegralDataTypeHolder; |
| 21 | +import static org.hibernate.id.enhanced.ResyncHelper.getNextSequenceValue; |
| 22 | +import static org.hibernate.id.enhanced.ResyncHelper.getMaxPrimaryKey; |
24 | 23 |
|
25 | 24 | /** |
26 | 25 | * Describes a sequence. |
@@ -176,44 +175,14 @@ public void registerExtraExportables(Table table, Optimizer optimizer) { |
176 | 175 | final String tableName = context.format( table.getQualifiedTableName() ); |
177 | 176 | final String primaryKeyColumnName = table.getPrimaryKey().getColumn( 0 ).getName(); |
178 | 177 | final int adjustment = optimizer.getAdjustment(); |
179 | | - final long max = getMax( connection, primaryKeyColumnName, tableName ); |
180 | | - final long current = getCurrent( connection, sequenceName, context.getDialect() ); |
| 178 | + final long max = getMaxPrimaryKey( connection, primaryKeyColumnName, tableName ); |
| 179 | + final long current = getNextSequenceValue( connection, sequenceName, context.getDialect() ); |
181 | 180 | final long newValue = Math.max( max + adjustment, current ); |
182 | 181 | final String restart = "alter sequence " + sequenceName + " restart with " + newValue; |
183 | 182 | return new InitCommand( restart ); |
184 | 183 | } ); |
185 | 184 | } |
186 | 185 |
|
187 | | - |
188 | | - private long getCurrent(Connection connection, String sequenceName, Dialect dialect) { |
189 | | - final String sequenceCurrentValue = |
190 | | - dialect.getSequenceSupport() |
191 | | - .getSequenceNextValString( sequenceName ); |
192 | | - try ( var select = connection.prepareStatement( sequenceCurrentValue ) ) { |
193 | | - try ( var resultSet = select.executeQuery() ) { |
194 | | - resultSet.next(); |
195 | | - return resultSet.getLong(1); |
196 | | - } |
197 | | - } |
198 | | - catch (SQLException e) { |
199 | | - throw new HibernateException( "Could not fetch the current sequence value from the database", e ); |
200 | | - } |
201 | | - } |
202 | | - |
203 | | - private static long getMax(Connection connection, String primaryKeyColumnName, String tableName) { |
204 | | - final String selectMax = |
205 | | - "select max(" + primaryKeyColumnName + ") from " + tableName; |
206 | | - try ( var select = connection.prepareStatement( selectMax ) ) { |
207 | | - try ( var resultSet = select.executeQuery() ) { |
208 | | - resultSet.next(); |
209 | | - return resultSet.getLong(1); |
210 | | - } |
211 | | - } |
212 | | - catch (SQLException e) { |
213 | | - throw new HibernateException( "Could not fetch the max primary key from the database", e ); |
214 | | - } |
215 | | - } |
216 | | - |
217 | 186 | @Override |
218 | 187 | public boolean isPhysicalSequence() { |
219 | 188 | return true; |
|
0 commit comments