001: /**********************************************************************
002: Copyright (c) 2004 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.store.expression;
019:
020: import java.util.Map;
021:
022: import org.jpox.store.mapping.JavaTypeMapping;
023:
024: /**
025: * An SQL expression that will test if a column of a table falls within the
026: * given Map. This is used for Querys where a Map is passed in as a parameter.
027: * @version $Revision: 1.6 $
028: */
029: public class MapLiteral extends ScalarExpression {
030: private final boolean isEmpty;
031: private final boolean containsNull;
032: private final MapValueLiteral mapValueLiteral;
033: private final MapKeyLiteral mapKeyLiteral;
034:
035: /**
036: * Constructor.
037: * @param qs The QueryStatement the MapLiteral will be used in.
038: * @param mapping The mapping to the Map
039: * @param map The Map that is the value.
040: */
041: public MapLiteral(QueryExpression qs, JavaTypeMapping mapping,
042: Map map) {
043: super (qs);
044: this .mapping = mapping;
045: containsNull = (map != null) && map.containsValue(null);
046: mapValueLiteral = new MapValueLiteral(qs, mapping, map);
047: mapKeyLiteral = new MapKeyLiteral(qs, mapping, map);
048:
049: // We'll consider the Map to be empty if it is null, is really
050: // empty, or only contains null.
051: // If it contains null we need a special case when creating the SQL.
052: isEmpty = (map == null) || (map.isEmpty())
053: || (map.size() == 1 && containsNull);
054: }
055:
056: /**
057: * Method to check the containing of a value in the Map.
058: * Return the BooleanExpression that results from
059: * MapValueLiteral.contains(SQLExpression).
060: * @param expr The SQLExpression that is checked for membership in the Map.
061: * @return The BooleanExpression that results from
062: * MapValueLiteral.contains(SQLExpression).
063: */
064: public BooleanExpression containsMethod(ScalarExpression expr) {
065: return mapValueLiteral.containsMethod(expr);
066: }
067:
068: /**
069: * Method to check the containing of a value in the Map.
070: * Return the BooleanExpression that results from
071: * MapValueLiteral.contains(SQLExpression).
072: * @param expr The SQLExpression that is checked for membership in the Map.
073: * @return The BooleanExpression that results from
074: * MapValueLiteral.contains(SQLExpression).
075: */
076: public BooleanExpression containsValueMethod(ScalarExpression expr) {
077: return mapValueLiteral.containsMethod(expr);
078: }
079:
080: /**
081: * Method to check the containing of a key in the Map.
082: * Return the BooleanExpression that results from
083: * MapKeyLiteral.contains(SQLExpression).
084: * @param expr The SQLExpression that is checked for membership in the Map.
085: * @return The BooleanExpression that results from
086: * MapKeyLiteral.contains(SQLExpression).
087: */
088: public BooleanExpression containsKeyMethod(ScalarExpression expr) {
089: return mapKeyLiteral.containsMethod(expr);
090: }
091:
092: /**
093: * Method to check the containing of an entry in the Map.
094: * Return the BooleanExpression that results from
095: * MapValueLiteral.contains(SQLExpression) and MapKeyLiteral.contains(SQLExpression).
096: * @param expr The SQLExpression that is checked for membership in the Map.
097: * @return The BooleanExpression that results from
098: * MapValueLiteral.contains(SQLExpression) and MapKeyLiteral.contains(SQLExpression).
099: */
100: public BooleanExpression containsEntryMethod(ScalarExpression expr) {
101: return mapKeyLiteral.containsMethod(expr).and(
102: mapValueLiteral.containsMethod(expr));
103: }
104:
105: /**
106: * Method to get a value from the Map for a key
107: * @param expr The key argument expression
108: * @return The statement
109: **/
110: public ScalarExpression getMethod(ScalarExpression expr) {
111: return mapKeyLiteral.getMethod(expr);
112: }
113:
114: /**
115: * Method to check for emptiness of the collection.
116: * @return The BooleanExpression.
117: **/
118: public BooleanExpression isEmptyMethod() {
119: return new BooleanLiteral(qs, mapping, isEmpty);
120: }
121: }
|