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) 2009-2011 Pentaho and others
008// All Rights Reserved.
009*/
010package mondrian.rolap;
011
012import mondrian.olap.Member;
013import mondrian.rolap.sql.TupleConstraint;
014
015import java.sql.SQLException;
016import java.util.List;
017import java.util.RandomAccess;
018
019/**
020 * Base helper class for the SQL tuple readers
021 * {@link mondrian.rolap.HighCardSqlTupleReader} and
022 * {@link mondrian.rolap.SqlTupleReader}.
023 *
024 * <p>Keeps track of target levels and constraints for adding to the SQL query.
025 * The real work is done in the extending classes,
026 * {@link Target} and
027 * {@link mondrian.rolap.SqlTupleReader.Target}.
028 *
029 * @author Kurtis Walker
030 * @since July 23, 2009
031 */
032public abstract class TargetBase {
033    final List<RolapMember> srcMembers;
034    final RolapLevel level;
035    private RolapMember currMember;
036    private List<RolapMember> list;
037    final Object cacheLock;
038    final TupleReader.MemberBuilder memberBuilder;
039
040    public TargetBase(
041        List<RolapMember> srcMembers,
042        RolapLevel level,
043        TupleReader.MemberBuilder memberBuilder)
044    {
045        this.srcMembers = srcMembers;
046        this.level = level;
047        cacheLock = memberBuilder.getMemberCacheLock();
048        this.memberBuilder = memberBuilder;
049    }
050
051    public void setList(final List<RolapMember> list) {
052        assert list instanceof RandomAccess;
053        this.list = list;
054    }
055
056    public List<RolapMember> getSrcMembers() {
057        return srcMembers;
058    }
059
060    public RolapLevel getLevel() {
061        return level;
062    }
063
064    public RolapMember getCurrMember() {
065        return this.currMember;
066    }
067
068    public void removeCurrMember() {
069        this.currMember = null;
070    }
071
072    public void setCurrMember(final RolapMember m) {
073        this.currMember = m;
074    }
075
076    public List<RolapMember> getList() {
077        return list;
078    }
079
080    public String toString() {
081        return level.getUniqueName();
082    }
083
084    /**
085     * Adds a row to the collection.
086     *
087     * @param stmt Statement
088     * @param column Column ordinal (0-based)
089     * @return Ordinal of next unconsumed column
090     * @throws SQLException On error
091     */
092    public final int addRow(SqlStatement stmt, int column) throws SQLException {
093        synchronized (cacheLock) {
094            return internalAddRow(stmt, column);
095        }
096    }
097
098    public abstract void open();
099
100    public abstract List<Member> close();
101
102    abstract int internalAddRow(SqlStatement stmt, int column)
103        throws SQLException;
104
105    public void add(final RolapMember member) {
106        this.getList().add(member);
107    }
108
109    RolapNativeCrossJoin.NonEmptyCrossJoinConstraint
110    castToNonEmptyCJConstraint(TupleConstraint constraint) {
111        return (RolapNativeCrossJoin.NonEmptyCrossJoinConstraint) constraint;
112    }
113}
114
115// End TargetBase.java