KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: DecodeCaseFragment.java,v 1.2 2004/12/02 05:09:49 oneovthafew Exp $
2
package org.hibernate.sql;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 /**
8  An Oracle-style DECODE function.
9  <br>
10  <code>decode(pkvalue, key1, 1, key2, 2, ..., 0)</code>
11  <br>
12
13  @author Simon Harris
14  */

15 public class DecodeCaseFragment extends CaseFragment {
16
17     public String JavaDoc toFragmentString() {
18         
19         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( cases.size() * 15 + 10 )
20             .append("decode(");
21
22         Iterator JavaDoc iter = cases.entrySet().iterator();
23         while ( iter.hasNext() ) {
24             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
25
26             if ( iter.hasNext() ) {
27                 buf.append(", ")
28                     .append( me.getKey() )
29                     .append(", ")
30                     .append( me.getValue() );
31             }
32             else {
33                 buf.insert( 7, me.getKey() )
34                     .append(", ")
35                     .append( me.getValue() );
36             }
37         }
38
39         buf.append(')');
40         
41         if (returnColumnName!=null) {
42             buf.append(" as ")
43                 .append(returnColumnName);
44         }
45         
46         return buf.toString();
47     }
48 }
Popular Tags