KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > SetLiteral


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SetLiteral.java,v 1.2 2004/01/25 22:31:27 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import javax.jdo.JDOUserException;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17
18 /**
19  * A SetLiteral is a SQL expression that will test if a column of table
20  * falls within the given Set of values. This is used for <code>Query</code>s
21  * where a transient Set is passed in as a parameter.
22  *
23  * @author <a HREF="mailto:pierreg0@users.sourceforge.net">Kelly Grizzle</a>
24  * @version $Revision: 1.2 $
25  */

26
27 class SetLiteral extends SetExpression
28 {
29     private final Set JavaDoc value;
30     private final boolean isEmpty;
31     private final boolean containsNull;
32     private final DatabaseAdapter dba;
33
34
35     /**
36      * Construct a SetLiteral.
37      *
38      * @param qs The QueryStatement the SetLiteral will be used in.
39      * @param value The transient Set that is the value of the SetLiteral.
40      */

41
42     public SetLiteral(QueryStatement qs, Set JavaDoc value)
43     {
44         super(qs);
45
46         this.value = value;
47
48         containsNull = (null != value) && value.contains(null);
49         dba = qs.getStoreManager().getDatabaseAdapter();
50
51         /*
52          * We'll consider the Set to be empty if it is null, is truly empty,
53          * or only contains null. If it contains null we need a special case
54          * when creating the SQL.
55          */

56         isEmpty =
57             (null == value) ||
58             (value.isEmpty()) ||
59             (1 == value.size() && containsNull);
60
61         /*
62          * If the Set is empty, don't build the list of SQLExpressions.
63          */

64         if (!isEmpty)
65         {
66             st.append("(");
67
68             boolean hadPrev = false;
69
70             for (Iterator JavaDoc it = value.iterator(); it.hasNext(); )
71             {
72                 Object JavaDoc current = it.next();
73
74                 if (null != current)
75                 {
76                     Mapping m = dba.getMapping(current.getClass());
77                     SQLExpression expr = m.newSQLLiteral(qs, current);
78
79                     /*
80                      * Append the SQLExpression (should be a literal) for the current element.
81                      */

82                     st.append(hadPrev ? "," : "");
83                     st.append(expr);
84
85                     hadPrev = true;
86                 }
87             }
88
89             st.append(")");
90         }
91     }
92     
93
94     /**
95      * Return the BooleanExpression that results from SetLiteral.contains(SQLExpression).
96      *
97      * @param expr The SQLExpression that is checked for membership in the Set.
98      *
99      * @return The BooleanExpression that results from SetLiteral.contains(SQLExpression).
100      */

101     public BooleanExpression containsMethod(SQLExpression expr)
102     {
103         BooleanExpression returnVal = isEmpty ? new BooleanLiteral(qs, false) : expr.in(this);
104
105         /*
106          * The expression FIELD IN ('Foo','Bar',NULL) does not work as we need it to.
107          * To get around this, if there is a null in the Set parameter, we will explicitly
108          * check if expr is NULL.
109          */

110         if (containsNull)
111             returnVal = returnVal.ior(expr.eq(new NullLiteral(qs)));
112
113         return returnVal;
114     }
115
116
117     public BooleanExpression isEmptyMethod()
118     {
119         return new BooleanLiteral(qs, isEmpty);
120     }
121 }
122
Popular Tags