KickJava   Java API By Example, From Geeks To Geeks.

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


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: CandidateSetExpression.java,v 1.2 2003/08/12 18:15:34 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import javax.jdo.JDOUserException;
14
15
16 /**
17  * A set expression that represents some set field in a query candidate class,
18  * or a set field in an object linked from the candidate class by navigation.
19  * <p>
20  * When navigated through using contains(expr), the elements of the set are
21  * relationally joined onto the query statement.
22  *
23  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
24  * @version $Revision: 1.2 $
25  */

26
27 class CandidateSetExpression extends SetExpression
28 {
29     private final QueryStatement.QueryColumn ownerQsc;
30     private final SetStore setStore;
31     private final String JavaDoc fieldName;
32     private final DatabaseAdapter dba;
33
34
35     public CandidateSetExpression(QueryStatement qs, QueryStatement.QueryColumn ownerQsc, SetStore setStore, String JavaDoc fieldName)
36     {
37         super(qs);
38
39         this.ownerQsc = ownerQsc;
40         this.setStore = setStore;
41         this.fieldName = fieldName;
42
43         dba = qs.getStoreManager().getDatabaseAdapter();
44     }
45     
46
47     /**
48      * Executed when the contains() method is found in a query filter.
49      *
50      * @param expr The SQLExpression passed as a parameter to contains().
51      *
52      * @return The BooleanExpression resulting from the contains() method.
53      */

54
55     public BooleanExpression containsMethod(SQLExpression expr)
56     {
57         /* st... = "set table" */
58         /* et... = "element table" */
59         String JavaDoc stJavaName = ownerQsc.te.getRangeVariable().getJavaName() + '.' + fieldName;
60         String JavaDoc etJavaNameBase = qs.getDefaultTableExpression().getRangeVariable().getJavaName();
61
62         if (expr instanceof UnboundVariable)
63         {
64             UnboundVariable var = (UnboundVariable)expr;
65             String JavaDoc etJavaName = qs.getDefaultTableExpression().getRangeVariable().getJavaName() + '.' + var.getVariableName();
66
67             TableIdentifier stRangeVar = new TableIdentifier(dba, stJavaName);
68             TableIdentifier etRangeVar = new TableIdentifier(dba, etJavaName);
69
70             var.bindTo(setStore.joinElementsTo(qs,
71                                                ownerQsc,
72                                                stRangeVar,
73                                                var.getVariableType(),
74                                                etRangeVar));
75             qs.setDistinctResults(true);
76
77             return new BooleanLiteral(qs, true);
78         }
79         else
80         {
81             TableIdentifier stRangeVar = new TableIdentifier(dba, stJavaName);
82             TableIdentifier etRangeVar;
83             int n = 0;
84
85             do
86             {
87                 String JavaDoc etJavaName = stJavaName + '.' + (++n);
88
89                 etRangeVar = new TableIdentifier(dba, etJavaName);
90             } while (qs.getTableExpression(etRangeVar) != null);
91
92             QueryStatement.QueryColumn qsc =
93                 setStore.joinElementsTo(qs,
94                                         ownerQsc,
95                                         stRangeVar,
96                                         setStore.getElementType(),
97                                         etRangeVar);
98             qs.setDistinctResults(true);
99
100             SQLExpression elemExpr = dba.getMapping(qsc.column).newSQLExpression(qs, qsc, "" + n);
101
102             return elemExpr.eq(expr);
103         }
104     }
105
106
107     /**
108      * Return the BooleanExpression for a query filter in the form "set.isEmpty()".
109      *
110      * @return The BooleanExpression for a query filter in the form "set.isEmpty()".
111      */

112     public BooleanExpression isEmptyMethod()
113     {
114         /* st... = "set table" */
115         String JavaDoc stJavaName = ownerQsc.te.getRangeVariable().getJavaName() + '.' + fieldName;
116         TableIdentifier stRangeVar = new TableIdentifier(dba, stJavaName);
117
118         return new ExistsExpression(qs, setStore.getExistsSubquery(ownerQsc, stRangeVar), false);
119     }
120
121     public StatementText toStatementText()
122     {
123         throw new JDOUserException("Cannot reference set object directly: field name = " + fieldName);
124     }
125 }
126
Popular Tags