FileDocCategorySizeDatePackage
TableStructure.javaAPI DocHibernate 3.2.54372Thu Mar 15 04:33:16 GMT 2007org.hibernate.id.enhanced

TableStructure

public class TableStructure extends org.hibernate.engine.TransactionHelper implements DatabaseStructure
Describes a table used to mimic sequence behavior
author
Steve Ebersole

Fields Summary
private static final Log
log
private static final Log
SQL_LOG
private final String
tableName
private final String
valueColumnName
private final int
initialValue
private final int
incrementSize
private final String
select
private final String
update
private boolean
applyIncrementSizeToSourceValues
private int
accessCounter
Constructors Summary
public TableStructure(org.hibernate.dialect.Dialect dialect, String tableName, String valueColumnName, int initialValue, int incrementSize)


	           
		this.tableName = tableName;
		this.initialValue = initialValue;
		this.incrementSize = incrementSize;
		this.valueColumnName = valueColumnName;

		select = "select " + valueColumnName + " id_val" +
				" from " + dialect.appendLockHint( LockMode.UPGRADE, tableName ) +
				dialect.getForUpdateString();

		update = "update " + tableName +
				" set " + valueColumnName + "= ?" +
				" where " + valueColumnName + "=?";
	
Methods Summary
public AccessCallbackbuildCallback(org.hibernate.engine.SessionImplementor session)

		return new AccessCallback() {
			public long getNextValue() {
				return ( ( Number ) doWorkInNewTransaction( session ) ).longValue();
			}
		};
	
protected java.io.SerializabledoWorkInCurrentTransaction(java.sql.Connection conn, java.lang.String sql)

		long result;
		int rows;
		do {
			sql = select;
			SQL_LOG.debug( sql );
			PreparedStatement qps = conn.prepareStatement( select );
			try {
				ResultSet rs = qps.executeQuery();
				if ( !rs.next() ) {
					String err = "could not read a hi value - you need to populate the table: " + tableName;
					log.error( err );
					throw new IdentifierGenerationException( err );
				}
				result = rs.getLong( 1 );
				rs.close();
			}
			catch ( SQLException sqle ) {
				log.error( "could not read a hi value", sqle );
				throw sqle;
			}
			finally {
				qps.close();
			}

			sql = update;
			SQL_LOG.debug( sql );
			PreparedStatement ups = conn.prepareStatement( update );
			try {
				int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
				ups.setLong( 1, result + increment );
				ups.setLong( 2, result );
				rows = ups.executeUpdate();
			}
			catch ( SQLException sqle ) {
				log.error( "could not update hi value in: " + tableName, sqle );
				throw sqle;
			}
			finally {
				ups.close();
			}
		} while ( rows == 0 );

		accessCounter++;

		return new Long( result );
	
public intgetIncrementSize()

		return incrementSize;
	
public java.lang.StringgetName()

		return tableName;
	
public intgetTimesAccessed()

		return accessCounter;
	
public voidprepare(Optimizer optimizer)

		applyIncrementSizeToSourceValues = optimizer.applyIncrementSizeToSourceValues();
	
public java.lang.String[]sqlCreateStrings(org.hibernate.dialect.Dialect dialect)

		return new String[] {
				"create table " + tableName + " ( " + valueColumnName + " " + dialect.getTypeName( Types.BIGINT ) + " )",
				"insert into " + tableName + " values ( " + initialValue + " )"
		};
	
public java.lang.String[]sqlDropStrings(org.hibernate.dialect.Dialect dialect)

		StringBuffer sqlDropString = new StringBuffer().append( "drop table " );
		if ( dialect.supportsIfExistsBeforeTableName() ) {
			sqlDropString.append( "if exists " );
		}
		sqlDropString.append( tableName ).append( dialect.getCascadeConstraintsString() );
		if ( dialect.supportsIfExistsAfterTableName() ) {
			sqlDropString.append( " if exists" );
		}
		return new String[] { sqlDropString.toString() };