1 package org.hibernate.sql; 3 4 import java.util.Iterator ; 5 import java.util.Map ; 6 7 import org.hibernate.LockMode; 8 import org.hibernate.QueryException; 9 import org.hibernate.dialect.Dialect; 10 import org.hibernate.util.StringHelper; 11 12 15 public class ForUpdateFragment { 16 private final StringBuffer aliases = new StringBuffer (); 17 private boolean isNowaitEnabled; 18 private final Dialect dialect; 19 20 public ForUpdateFragment(Dialect dialect) { 21 this.dialect = dialect; 22 } 23 24 public ForUpdateFragment addTableAlias(String alias) { 25 if ( aliases.length()>0 ) aliases.append(", "); 26 aliases.append(alias); 27 return this; 28 } 29 30 public String toFragmentString() { 31 if ( aliases.length()==0 ) return ""; 32 return isNowaitEnabled ? 33 dialect.getForUpdateNowaitString( aliases.toString() ) : 34 dialect.getForUpdateString( aliases.toString() ); 35 } 36 37 public ForUpdateFragment setNowaitEnabled(boolean nowait) { 38 isNowaitEnabled = nowait; 39 return this; 40 } 41 42 public ForUpdateFragment(Dialect dialect, Map lockModes, Map keyColumnNames) 43 throws QueryException { 44 this(dialect); 45 LockMode upgradeType = null; 46 Iterator iter = lockModes.entrySet().iterator(); 47 while ( iter.hasNext() ) { 48 final Map.Entry me = (Map.Entry ) iter.next(); 49 final LockMode lockMode = (LockMode) me.getValue(); 50 if ( LockMode.READ.lessThan(lockMode) ) { 51 final String tableAlias = (String ) me.getKey(); 52 if ( dialect.forUpdateOfColumns() ) { 53 String [] keyColumns = (String []) keyColumnNames.get(tableAlias); if (keyColumns==null) { 55 throw new IllegalArgumentException ("alias not found: " + tableAlias); 56 } 57 keyColumns = StringHelper.qualify(tableAlias, keyColumns); 58 for (int i=0; i<keyColumns.length; i++) addTableAlias( keyColumns[i] ); 59 } 60 else { 61 addTableAlias(tableAlias); 62 } 63 if ( upgradeType!=null && lockMode!=upgradeType ) { 64 throw new QueryException("mixed LockModes"); 65 } 66 upgradeType = lockMode; 67 } 68 } 69 70 if ( upgradeType==LockMode.UPGRADE_NOWAIT ) setNowaitEnabled(true); 71 } 72 } 73 | Popular Tags |