Branch libreoffice-24-8-3
[LibreOffice.git] / sc / inc / dpcache.hxx
blobada1d7cddc88ecea5a8ce3a52a92c780f41c8ac8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #pragma once
21 #include "address.hxx"
22 #include "calcmacros.hxx"
23 #include "dpitemdata.hxx"
24 #include "dpnumgroupinfo.hxx"
25 #include "scdllapi.h"
26 #include "types.hxx"
28 #include <mdds/flat_segment_tree.hpp>
29 #include <tools/long.hxx>
30 #include <o3tl/sorted_vector.hxx>
32 #include <memory>
33 #include <unordered_set>
34 #include <vector>
36 struct ScQueryParam;
37 class ScDPObject;
38 class ScDocument;
39 class SvNumberFormatter;
40 struct ScInterpreterContext;
42 enum class SvNumFormatType : sal_Int16;
44 /**
45 * This class represents the cached data part of the datapilot cache table
46 * implementation.
48 class ScDPCache
50 public:
51 typedef std::unordered_set<OUString> StringSetType;
52 typedef mdds::flat_segment_tree<SCROW, bool> EmptyRowsType;
53 typedef std::vector<ScDPItemData> ScDPItemDataVec;
54 typedef o3tl::sorted_vector<ScDPObject*> ScDPObjectSet;
55 typedef std::vector<SCROW> IndexArrayType;
57 struct GroupItems
59 ScDPItemDataVec maItems;
60 ScDPNumGroupInfo maInfo;
61 sal_Int32 mnGroupType;
63 GroupItems();
64 GroupItems(const GroupItems&) = delete;
65 const GroupItems& operator=(const GroupItems&) = delete;
66 GroupItems(const ScDPNumGroupInfo& rInfo, sal_Int32 nGroupType);
69 struct Field
71 /**
72 * Optional items for grouped field.
74 std::unique_ptr<GroupItems> mpGroup;
76 /**
77 * Unique values in the field, stored in ascending order.
79 ScDPItemDataVec maItems;
81 /**
82 * Original source data represented as indices to the unique value
83 * list. The order of the data is as they appear in the original
84 * data source.
86 IndexArrayType maData;
88 sal_uInt32 mnNumFormat;
90 Field();
91 Field(const Field&) = delete;
92 const Field& operator=(const Field&) = delete;
95 /**
96 * Interface for connecting to database source. Column index is 0-based.
98 class DBConnector
100 public:
101 virtual tools::Long getColumnCount() const = 0;
102 virtual OUString getColumnLabel(tools::Long nCol) const = 0;
103 virtual bool first() = 0;
104 virtual bool next() = 0;
105 virtual void finish() = 0;
106 virtual void getValue(tools::Long nCol, ScDPItemData& rData, SvNumFormatType& rNumType) const = 0;
107 virtual ~DBConnector() {}
110 private:
112 ScDocument& mrDoc;
113 SCCOL mnColumnCount;
116 * All pivot table objects that references this cache.
118 mutable ScDPObjectSet maRefObjects;
120 typedef std::vector< std::unique_ptr<Field> > FieldsType;
121 typedef std::vector< std::unique_ptr<GroupItems> > GroupFieldsType;
123 FieldsType maFields;
124 GroupFieldsType maGroupFields;
125 std::vector<StringSetType> maStringPools; // one for each field.
127 std::vector<OUString> maLabelNames; // Stores dimension names and the data layout dimension name at position 0.
128 EmptyRowsType maEmptyRows;
129 SCROW mnDataSize;
130 SCROW mnRowCount;
132 bool mbDisposing;
134 public:
135 rtl_uString* InternString( size_t nDim, const OUString& rStr );
136 void AddReference(ScDPObject* pObj) const;
137 void RemoveReference(ScDPObject* pObj) const;
138 SC_DLLPUBLIC const ScDPObjectSet& GetAllReferences() const;
140 SCROW GetIdByItemData(tools::Long nDim, const ScDPItemData& rItem) const;
142 static sal_uInt32 GetLocaleIndependentFormat( ScInterpreterContext& rContext, sal_uInt32 nNumFormat );
143 static OUString GetLocaleIndependentFormattedNumberString( double fValue );
144 static OUString GetLocaleIndependentFormattedString( double fValue, ScInterpreterContext& rContext, sal_uInt32 nNumFormat );
145 SC_DLLPUBLIC OUString GetFormattedString(tools::Long nDim, const ScDPItemData& rItem, bool bLocaleIndependent) const;
146 ScInterpreterContext& GetInterpreterContext() const;
148 tools::Long AppendGroupField();
149 void ResetGroupItems(tools::Long nDim, const ScDPNumGroupInfo& rNumInfo, sal_Int32 nGroupType);
150 SCROW SetGroupItem(tools::Long nDim, const ScDPItemData& rData);
151 SC_DLLPUBLIC void GetGroupDimMemberIds(tools::Long nDim, std::vector<SCROW>& rIds) const;
152 void ClearGroupFields();
153 void ClearAllFields();
154 SC_DLLPUBLIC const ScDPNumGroupInfo* GetNumGroupInfo(tools::Long nDim) const;
157 * Return a group type identifier. The values correspond with
158 * css::sheet::DataPilotFieldGroupBy constant values.
160 * @param nDim 0-based dimension index.
162 * @return group type identifier, or 0 on failure.
164 SC_DLLPUBLIC sal_Int32 GetGroupType(tools::Long nDim) const;
166 SC_DLLPUBLIC SCCOL GetDimensionIndex(std::u16string_view sName) const;
167 sal_uInt32 GetNumberFormat( tools::Long nDim ) const;
168 SC_DLLPUBLIC bool IsDateDimension( tools::Long nDim ) const ;
169 tools::Long GetDimMemberCount(tools::Long nDim) const;
171 SC_DLLPUBLIC const IndexArrayType* GetFieldIndexArray( size_t nDim ) const;
172 SC_DLLPUBLIC const ScDPItemDataVec& GetDimMemberValues( SCCOL nDim ) const;
173 void InitFromDoc(ScDocument& rDoc, const ScRange& rRange);
174 bool InitFromDataBase(DBConnector& rDB);
177 * Row count is the number of records plus any trailing empty rows in case
178 * the source data is sheet and contains trailing empty rows.
180 SCROW GetRowCount() const;
183 * Data size is the number of records without any trailing empty rows for
184 * sheet source data. For any other source type, this should equal the
185 * row count.
187 SC_DLLPUBLIC SCROW GetDataSize() const;
188 SCROW GetItemDataId( sal_uInt16 nDim, SCROW nRow, bool bRepeatIfEmpty ) const;
189 SC_DLLPUBLIC OUString GetDimensionName(std::vector<OUString>::size_type nDim) const;
190 bool IsRowEmpty(SCROW nRow) const;
191 bool ValidQuery(SCROW nRow, const ScQueryParam& rQueryParam) const;
193 ScDocument& GetDoc() const;
194 tools::Long GetColumnCount() const;
196 SC_DLLPUBLIC const ScDPItemData* GetItemDataById( tools::Long nDim, SCROW nId ) const;
198 SC_DLLPUBLIC size_t GetFieldCount() const;
199 SC_DLLPUBLIC size_t GetGroupFieldCount() const;
201 ScDPCache(const ScDPCache&) = delete;
202 const ScDPCache& operator=(const ScDPCache&) = delete;
203 ScDPCache(ScDocument& rDoc);
204 ~ScDPCache();
206 #if DUMP_PIVOT_TABLE
207 void Dump() const;
208 #endif
210 private:
211 void PostInit();
212 void Clear();
213 const GroupItems* GetGroupItems(tools::Long nDim) const;
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */