KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: InFragment.java,v 1.6 2005/06/05 06:36:26 oneovthafew Exp $
2
package org.hibernate.sql;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.hibernate.util.StringHelper;
9
10 /**
11  * An SQL IN expression.
12  * <br>
13  * <code>... in(...)</code>
14  * <br>
15  * @author Gavin King
16  */

17 public class InFragment {
18
19     public static final String JavaDoc NULL = "null";
20     public static final String JavaDoc NOT_NULL = "not null";
21
22     private String JavaDoc columnName;
23     private List JavaDoc values = new ArrayList JavaDoc();
24
25     /**
26      * @param value, an SQL literal, NULL, or NOT_NULL
27      */

28     public InFragment addValue(Object JavaDoc value) {
29         values.add(value);
30         return this;
31     }
32
33     public InFragment setColumn(String JavaDoc columnName) {
34         this.columnName = columnName;
35         return this;
36     }
37
38     public InFragment setColumn(String JavaDoc alias, String JavaDoc columnName) {
39         this.columnName = StringHelper.qualify(alias, columnName);
40         return setColumn(this.columnName);
41     }
42
43     public InFragment setFormula(String JavaDoc alias, String JavaDoc formulaTemplate) {
44         this.columnName = StringHelper.replace(formulaTemplate, Template.TEMPLATE, alias);
45         return setColumn(this.columnName);
46     }
47
48     public String JavaDoc toFragmentString() {
49         if ( values.size()==0 ) return "1=2";
50         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( values.size() * 5 );
51         buf.append(columnName);
52         //following doesn't handle (null, not null) but unnecessary
53
//since this would mean all rows
54
if ( values.size()>1 ) {
55             boolean allowNull = false;
56             buf.append(" in (");
57             Iterator JavaDoc iter = values.iterator();
58             while ( iter.hasNext() ) {
59                 Object JavaDoc value = iter.next();
60                 if ( NULL.equals(value) ) {
61                     allowNull = true;
62                 }
63                 else if ( NOT_NULL.equals(value) ) {
64                     throw new IllegalArgumentException JavaDoc("not null makes no sense for in expression");
65                 }
66                 else {
67                     buf.append(value);
68                     buf.append(", ");
69                 }
70             }
71             buf.setLength( buf.length()-2 );
72             buf.append(')');
73             if (allowNull) {
74                 buf.insert(0, " is null or ")
75                     .insert(0, columnName)
76                     .insert(0, '(')
77                     .append(')');
78             }
79         }
80         else {
81             Object JavaDoc value = values.iterator().next();
82             if ( NULL.equals(value) ) {
83                 buf.append(" is null");
84             }
85             else if ( NOT_NULL.equals(value) ) {
86                 buf.append(" is not null");
87             }
88             else {
89                 buf.append("=").append(value);
90             }
91         }
92         return buf.toString();
93     }
94 }
95
Popular Tags