Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef SC_DPTABLECACHE_HXX
20 : #define SC_DPTABLECACHE_HXX
21 :
22 : #include "global.hxx"
23 : #include "dpnumgroupinfo.hxx"
24 : #include "dpmacros.hxx"
25 : #include "tools/date.hxx"
26 :
27 : #include <boost/noncopyable.hpp>
28 : #include <boost/scoped_ptr.hpp>
29 : #include <boost/ptr_container/ptr_vector.hpp>
30 : #include <boost/unordered_set.hpp>
31 : #include <mdds/flat_segment_tree.hpp>
32 :
33 : #include <vector>
34 : #include <set>
35 :
36 : struct ScQueryParam;
37 : class ScDPObject;
38 : class ScDPItemData;
39 : struct ScDPNumGroupInfo;
40 :
41 : /**
42 : * This class represents the cached data part of the datapilot cache table
43 : * implementation.
44 : */
45 : class SC_DLLPUBLIC ScDPCache : boost::noncopyable
46 : {
47 : typedef boost::unordered_set<rtl::OUString, rtl::OUStringHash> StringSetType;
48 :
49 : public:
50 : typedef std::vector<ScDPItemData> ItemsType;
51 : typedef std::set<ScDPObject*> ObjectSetType;
52 : typedef std::vector<rtl::OUString> LabelsType;
53 : typedef std::vector<SCROW> IndexArrayType;
54 :
55 6 : struct GroupItems : boost::noncopyable
56 : {
57 : ItemsType maItems;
58 : ScDPNumGroupInfo maInfo;
59 : sal_Int32 mnGroupType;
60 :
61 : GroupItems();
62 : GroupItems(const ScDPNumGroupInfo& rInfo, sal_Int32 nGroupType);
63 : };
64 :
65 55 : struct Field : boost::noncopyable
66 : {
67 : /**
68 : * Optional items for grouped field.
69 : */
70 : boost::scoped_ptr<GroupItems> mpGroup;
71 :
72 : /**
73 : * Unique values in the field, stored in ascending order.
74 : */
75 : ItemsType maItems;
76 :
77 : /**
78 : * Original source data represented as indices to the unique value
79 : * list. The order of the data is as they appear in the original
80 : * data source.
81 : */
82 : IndexArrayType maData;
83 :
84 : sal_uLong mnNumFormat;
85 :
86 : Field();
87 : };
88 :
89 : /**
90 : * Interface for connecting to database source. Column index is 0-based.
91 : */
92 0 : class DBConnector
93 : {
94 : public:
95 : virtual long getColumnCount() const = 0;
96 : virtual rtl::OUString getColumnLabel(long nCol) const = 0;
97 : virtual bool first() = 0;
98 : virtual bool next() = 0;
99 : virtual void finish() = 0;
100 : virtual void getValue(long nCol, ScDPItemData& rData, short& rNumType) const = 0;
101 0 : virtual ~DBConnector() {}
102 : };
103 :
104 : private:
105 :
106 : ScDocument* mpDoc;
107 : long mnColumnCount;
108 :
109 : /**
110 : * All pivot table objects that references this cache.
111 : */
112 : mutable ObjectSetType maRefObjects;
113 :
114 : typedef boost::ptr_vector<Field> FieldsType;
115 : typedef boost::ptr_vector<GroupItems> GroupFieldsType;
116 :
117 : FieldsType maFields;
118 : GroupFieldsType maGroupFields;
119 : mutable StringSetType maStringPool;
120 :
121 : LabelsType maLabelNames; // Stores dimension names.
122 : mdds::flat_segment_tree<SCROW, bool> maEmptyRows;
123 : SCROW mnDataSize;
124 : SCROW mnRowCount;
125 :
126 : bool mbDisposing;
127 :
128 : public:
129 : const rtl::OUString* InternString(const rtl::OUString& rStr) const;
130 : void AddReference(ScDPObject* pObj) const;
131 : void RemoveReference(ScDPObject* pObj) const;
132 : const ObjectSetType& GetAllReferences() const;
133 :
134 : SCROW GetIdByItemData(long nDim, const ScDPItemData& rItem) const;
135 : rtl::OUString GetFormattedString(long nDim, const ScDPItemData& rItem) const;
136 : long AppendGroupField();
137 : void ResetGroupItems(long nDim, const ScDPNumGroupInfo& rNumInfo, sal_Int32 nGroupType);
138 : SCROW SetGroupItem(long nDim, const ScDPItemData& rData);
139 : void GetGroupDimMemberIds(long nDim, std::vector<SCROW>& rIds) const;
140 : void ClearGroupFields();
141 : const ScDPNumGroupInfo* GetNumGroupInfo(long nDim) const;
142 : sal_Int32 GetGroupType(long nDim) const;
143 :
144 : SCCOL GetDimensionIndex(const rtl::OUString& sName) const;
145 : sal_uLong GetNumberFormat( long nDim ) const;
146 : bool IsDateDimension( long nDim ) const ;
147 : long GetDimMemberCount(long nDim) const;
148 : SCROW GetOrder( long nDim, SCROW nIndex ) const;
149 :
150 : const ItemsType& GetDimMemberValues( SCCOL nDim ) const;
151 : bool InitFromDoc(ScDocument* pDoc, const ScRange& rRange);
152 : bool InitFromDataBase(DBConnector& rDB);
153 :
154 : SCROW GetRowCount() const;
155 : SCROW GetDataSize() const;
156 : SCROW GetItemDataId( sal_uInt16 nDim, SCROW nRow, bool bRepeatIfEmpty ) const;
157 : rtl::OUString GetDimensionName(LabelsType::size_type nDim) const;
158 : bool IsRowEmpty(SCROW nRow) const;
159 : bool ValidQuery(SCROW nRow, const ScQueryParam& rQueryParam) const;
160 :
161 : ScDocument* GetDoc() const;
162 : long GetColumnCount() const;
163 :
164 : const ScDPItemData* GetItemDataById( long nDim, SCROW nId ) const;
165 :
166 : ScDPCache(ScDocument* pDoc);
167 : ~ScDPCache();
168 :
169 : #if DEBUG_PIVOT_TABLE
170 : void Dump() const;
171 : #endif
172 :
173 : private:
174 : void PostInit();
175 : void Clear();
176 : void AddLabel(const rtl::OUString& rLabel);
177 : const GroupItems* GetGroupItems(long nDim) const;
178 : };
179 :
180 : #endif
181 :
182 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|