2 Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package com.mysql.clusterj.core.query;
20 import com.mysql.clusterj.core.spi.QueryExecutionContext;
21 import com.mysql.clusterj.core.store.Operation;
22 import com.mysql.clusterj.core.store.ScanFilter;
23 import com.mysql.clusterj.core.store.ScanOperation;
24 import com.mysql.clusterj.ClusterJException;
25 import com.mysql.clusterj.ClusterJFatalInternalException;
26 import com.mysql.clusterj.query.Predicate;
27 import java.util.ArrayList;
28 import java.util.List;
30 public class AndPredicateImpl extends PredicateImpl {
32 List<PredicateImpl> predicates = new ArrayList<PredicateImpl>();
34 public AndPredicateImpl(QueryDomainTypeImpl<?> dobj,
35 PredicateImpl left, PredicateImpl right) {
38 predicates.add(right);
42 public Predicate and(Predicate predicate) {
43 if (predicate instanceof ComparativePredicateImpl) {
44 predicates.add((PredicateImpl)predicate);
46 } else if (predicate instanceof AndPredicateImpl) {
47 predicates.addAll(((AndPredicateImpl)predicate).predicates);
49 } else if (predicate instanceof OrPredicateImpl) {
50 predicates.add((PredicateImpl)predicate);
52 } else if (predicate instanceof InPredicateImpl) {
53 predicates.add((PredicateImpl)predicate);
55 } else if (predicate instanceof NotPredicateImpl) {
56 predicates.add((PredicateImpl)predicate);
59 throw new UnsupportedOperationException(
60 local.message("ERR_NotImplemented"));
64 public Predicate or(Predicate predicate) {
65 throw new UnsupportedOperationException(
66 local.message("ERR_NotImplemented"));
69 public Predicate not() {
70 throw new UnsupportedOperationException(
71 local.message("ERR_NotImplemented"));
75 public void markParameters() {
76 for (PredicateImpl predicateImpl: predicates) {
77 predicateImpl.markParameters();
82 public void unmarkParameters() {
83 for (PredicateImpl predicateImpl: predicates) {
84 predicateImpl.unmarkParameters();
88 /** Create a filter for the operation. Set the conditions into the
89 * new filter, one for each predicate.
90 * @param context the query execution context with the parameter values
91 * @param op the operation
94 public void filterCmpValue(QueryExecutionContext context,
97 ScanFilter filter = op.getScanFilter(context);
98 filter.begin(ScanFilter.Group.GROUP_AND);
99 for (PredicateImpl predicate: predicates) {
100 predicate.filterCmpValue(context, op, filter);
103 } catch (Exception ex) {
104 throw new ClusterJException(
105 local.message("ERR_Get_NdbFilter"), ex);
109 /** Set the keys into the operation for each predicate.
110 * Each predicate must be an equal predicate for a primary or unique key.
112 public void operationEqual(QueryExecutionContext context,
114 for (PredicateImpl predicate: predicates) {
115 if (!(predicate instanceof EqualPredicateImpl)) {
116 throw new ClusterJFatalInternalException(
117 local.message("ERR_Implementation_Should_Not_Occur"));
119 predicate.operationEqual(context, op);
123 /** Get the best index for the operation. Delegate to the method
124 * in the superclass, passing the array of predicates.
126 * @return the best index
129 public CandidateIndexImpl getBestCandidateIndex(QueryExecutionContext context) {
130 return getBestCandidateIndexFor(context, predicates.toArray(
131 new PredicateImpl[predicates.size()]));
134 /** Get the number of conditions in the top level predicate.
135 * This is used to determine whether a hash index can be used. If there
136 * are exactly the number of conditions as index columns, then the
137 * hash index might be used.
138 * For AND predicates, there is one condition for each predicate included
140 * AndPredicateImpl overrides this method.
141 * @return the number of conditions
144 protected int getNumberOfConditionsInPredicate() {
145 return predicates.size();