001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2004-2005 TONBELLER AG
008// Copyright (C) 2006-2012 Pentaho
009// All Rights Reserved.
010*/
011package mondrian.rolap;
012
013import mondrian.olap.Id;
014import mondrian.rolap.aggmatcher.AggStar;
015import mondrian.rolap.sql.SqlQuery;
016
017import java.util.Arrays;
018
019/**
020 * Constraint which optimizes the search for a child by name. This is used
021 * whenever the string representation of a member is parsed, e.g.
022 * [Customers].[USA].[CA]. Restricts the result to
023 * the member we are searching for.
024 *
025 * @author avix
026 */
027class ChildByNameConstraint extends DefaultMemberChildrenConstraint {
028    private final String childName;
029    private final Object cacheKey;
030
031    /**
032     * Creates a <code>ChildByNameConstraint</code>.
033     *
034     * @param childName Name of child
035     */
036    public ChildByNameConstraint(Id.NameSegment childName) {
037        this.childName = childName.name;
038        this.cacheKey = Arrays.asList(ChildByNameConstraint.class, childName);
039    }
040
041    @Override
042    public int hashCode() {
043        return getCacheKey().hashCode();
044    }
045
046    @Override
047    public boolean equals(Object obj) {
048        return obj instanceof ChildByNameConstraint
049            && getCacheKey().equals(
050                ((ChildByNameConstraint) obj).getCacheKey());
051    }
052
053    public void addLevelConstraint(
054        SqlQuery query,
055        RolapCube baseCube,
056        AggStar aggStar,
057        RolapLevel level)
058    {
059        super.addLevelConstraint(query, baseCube, aggStar, level);
060        query.addWhere(
061            SqlConstraintUtils.constrainLevel(
062                level, query, baseCube, aggStar, childName, true));
063    }
064
065    public String toString() {
066        return "ChildByNameConstraint(" + childName + ")";
067    }
068
069    public Object getCacheKey() {
070        return cacheKey;
071    }
072
073}
074
075// End ChildByNameConstraint.java