KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > HasVariantValueNodeVisitor


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.HasVariantValueNodeVisitor
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.sql.compile.Visitable;
26 import org.apache.derby.iapi.sql.compile.Visitor;
27
28
29 import org.apache.derby.iapi.store.access.Qualifier;
30 import org.apache.derby.iapi.error.StandardException;
31
32 /**
33  * Find out if we have a value node with variant type less than what the
34  * caller desires, anywhere below us. Stop traversal as soon as we find one.
35  * This is used in two places: one to check the values clause of an insert
36  * statement; i.e
37  * <pre>
38  * insert into <table> values (?, 1, foobar());
39  * </pre>
40  * If all the expressions in the values clause are QUERY_INVARIANT (and an
41  * exception is made for parameters) then we can cache the results in the
42  * RowResultNode. This is useful when we have a prepared insert statement which
43  * is repeatedly executed.
44  * <p>
45  * The second place where this is used is to check if a subquery can be
46  * materialized or not.
47  * @see org.apache.derby.iapi.store.access.Qualifier
48  *
49  * @author jamie
50  */

51 public class HasVariantValueNodeVisitor implements Visitor
52 {
53     private boolean hasVariant;
54     private int variantType;
55     private boolean ignoreParameters;
56
57
58     /**
59      * Construct a visitor
60      */

61     public HasVariantValueNodeVisitor()
62     {
63         this.variantType = Qualifier.VARIANT;
64         this.ignoreParameters = false;
65         if (SanityManager.DEBUG)
66         {
67             SanityManager.ASSERT(Qualifier.VARIANT < Qualifier.SCAN_INVARIANT, "qualifier constants not ordered as expected");
68             SanityManager.ASSERT(Qualifier.SCAN_INVARIANT < Qualifier.QUERY_INVARIANT, "qualifier constants not ordered as expected");
69         }
70     }
71
72     
73     /**
74      * Construct a visitor. Pass in the variant
75      * type. We look for nodes that are less
76      * than or equal to this variant type. E.g.,
77      * if the variantType is Qualifier.SCAN_VARIANT,
78      * then any node that is either VARIANT or
79      * SCAN_VARIANT will cause the visitor to
80      * consider it variant.
81      *
82      * @param variantType the type of variance we consider
83      * variant
84      * @param ignoreParameters should I ignore parameter nodes?
85      */

86     public HasVariantValueNodeVisitor(int variantType, boolean ignoreParameters)
87     {
88         this.variantType = variantType;
89         this.ignoreParameters = ignoreParameters;
90
91         if (SanityManager.DEBUG)
92         {
93             SanityManager.ASSERT(variantType >= Qualifier.VARIANT, "bad variantType");
94             // note: there is no point in (variantType == Qualifier.CONSTANT) so throw an
95
// exception for that case too
96
SanityManager.ASSERT(variantType <= Qualifier.QUERY_INVARIANT, "bad variantType");
97         }
98     }
99     
100     ////////////////////////////////////////////////
101
//
102
// VISITOR INTERFACE
103
//
104
////////////////////////////////////////////////
105

106     /**
107      * If we have found the target node, we are done.
108      *
109      * @param node the node to process
110      *
111      * @return me
112      *
113      * @exception StandardException on error
114      */

115     public Visitable visit(Visitable node) throws StandardException
116     {
117         if (node instanceof ValueNode)
118         {
119             if (ignoreParameters && ((ValueNode)node).requiresTypeFromContext())
120                 return node;
121                 
122             if (((ValueNode)node).getOrderableVariantType() <= variantType)
123             {
124                 hasVariant = true;
125             }
126         }
127         return node;
128     }
129
130     public boolean skipChildren(Visitable node)
131     {
132         return false;
133     }
134
135     /**
136      * Stop traversal if we found the target node
137      *
138      * @return true/false
139      */

140     public boolean stopTraversal()
141     {
142         return hasVariant;
143     }
144
145     ////////////////////////////////////////////////
146
//
147
// CLASS INTERFACE
148
//
149
////////////////////////////////////////////////
150
/**
151      * Indicate whether we found the node in
152      * question
153      *
154      * @return true/false
155      */

156     public boolean hasVariant()
157     {
158         return hasVariant;
159     }
160 }
161
Popular Tags