KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > sql > ForUpdateFragment


1 //$Id: ForUpdateFragment.java,v 1.5 2005/05/17 10:36:35 oneovthafew Exp $
2
package org.hibernate.sql;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
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 /**
13  * @author Gavin King
14  */

15 public class ForUpdateFragment {
16     private final StringBuffer JavaDoc aliases = new StringBuffer JavaDoc();
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 JavaDoc alias) {
25         if ( aliases.length()>0 ) aliases.append(", ");
26         aliases.append(alias);
27         return this;
28     }
29     
30     public String JavaDoc 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 JavaDoc lockModes, Map JavaDoc keyColumnNames)
43     throws QueryException {
44         this(dialect);
45         LockMode upgradeType = null;
46         Iterator JavaDoc iter = lockModes.entrySet().iterator();
47         while ( iter.hasNext() ) {
48             final Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
49             final LockMode lockMode = (LockMode) me.getValue();
50             if ( LockMode.READ.lessThan(lockMode) ) {
51                 final String JavaDoc tableAlias = (String JavaDoc) me.getKey();
52                 if ( dialect.forUpdateOfColumns() ) {
53                     String JavaDoc[] keyColumns = (String JavaDoc[]) keyColumnNames.get(tableAlias); //use the id column alias
54
if (keyColumns==null) {
55                         throw new IllegalArgumentException JavaDoc("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