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 
20 #include <dpsdbtab.hxx>
21 #include <globstr.hrc>
22 #include <scresid.hxx>
23 #include <dpfilteredcache.hxx>
24 #include <document.hxx>
25 #include <dpobject.hxx>
26 
27 #include <com/sun/star/sdb/CommandType.hpp>
28 
29 using namespace com::sun::star;
30 
31 using ::std::vector;
32 using ::com::sun::star::uno::Sequence;
33 using ::com::sun::star::uno::Any;
34 
GetCommandType() const35 sal_Int32 ScImportSourceDesc::GetCommandType() const
36 {
37     sal_Int32 nSdbType = -1;
38 
39     switch ( nType )
40     {
41         case sheet::DataImportMode_SQL:   nSdbType = sdb::CommandType::COMMAND; break;
42         case sheet::DataImportMode_TABLE: nSdbType = sdb::CommandType::TABLE;   break;
43         case sheet::DataImportMode_QUERY: nSdbType = sdb::CommandType::QUERY;   break;
44         default:
45             ;
46     }
47     return nSdbType;
48 }
49 
CreateCache(const ScDPDimensionSaveData * pDimData) const50 const ScDPCache* ScImportSourceDesc::CreateCache(const ScDPDimensionSaveData* pDimData) const
51 {
52     if (!mpDoc)
53         return nullptr;
54 
55     sal_Int32 nSdbType = GetCommandType();
56     if (nSdbType < 0)
57         return nullptr;
58 
59     ScDPCollection::DBCaches& rCaches = mpDoc->GetDPCollection()->GetDBCaches();
60     return rCaches.getCache(nSdbType, aDBName, aObject, pDimData);
61 }
62 
ScDatabaseDPData(const ScDocument * pDoc,const ScDPCache & rCache)63 ScDatabaseDPData::ScDatabaseDPData(
64     const ScDocument* pDoc, const ScDPCache& rCache) :
65     ScDPTableData(pDoc),
66     aCacheTable(rCache)
67 {
68 }
69 
~ScDatabaseDPData()70 ScDatabaseDPData::~ScDatabaseDPData()
71 {
72 }
73 
DisposeData()74 void ScDatabaseDPData::DisposeData()
75 {
76     //TODO: use OpenDatabase here?
77     aCacheTable.clear();
78 }
79 
GetColumnCount()80 sal_Int32 ScDatabaseDPData::GetColumnCount()
81 {
82     CreateCacheTable();
83     return GetCacheTable().getColSize();
84 }
85 
getDimensionName(sal_Int32 nColumn)86 OUString ScDatabaseDPData::getDimensionName(sal_Int32 nColumn)
87 {
88     if (getIsDataLayoutDimension(nColumn))
89     {
90         //TODO: different internal and display names?
91         //return "Data";
92         return ScResId(STR_PIVOT_DATA);
93     }
94 
95     CreateCacheTable();
96     return aCacheTable.getFieldName(static_cast<SCCOL>(nColumn));
97 }
98 
getIsDataLayoutDimension(sal_Int32 nColumn)99 bool ScDatabaseDPData::getIsDataLayoutDimension(sal_Int32 nColumn)
100 {
101     return ( nColumn == GetCacheTable().getColSize());
102 }
103 
IsDateDimension(sal_Int32)104 bool ScDatabaseDPData::IsDateDimension(sal_Int32 /* nDim */)
105 {
106     //TODO: later...
107     return false;
108 }
109 
SetEmptyFlags(bool,bool)110 void ScDatabaseDPData::SetEmptyFlags( bool /* bIgnoreEmptyRows */, bool /* bRepeatIfEmpty */ )
111 {
112     //  not used for database data
113     //TODO: disable flags
114 }
115 
CreateCacheTable()116 void ScDatabaseDPData::CreateCacheTable()
117 {
118     if (!aCacheTable.empty())
119         // cache table already created.
120         return;
121 
122     aCacheTable.fillTable();
123 }
124 
FilterCacheTable(const vector<ScDPFilteredCache::Criterion> & rCriteria,const std::unordered_set<sal_Int32> & rCatDims)125 void ScDatabaseDPData::FilterCacheTable(const vector<ScDPFilteredCache::Criterion>& rCriteria, const std::unordered_set<sal_Int32>& rCatDims)
126 {
127     CreateCacheTable();
128     aCacheTable.filterByPageDimension(
129         rCriteria, (IsRepeatIfEmpty() ? rCatDims : std::unordered_set<sal_Int32>()));
130 }
131 
GetDrillDownData(const vector<ScDPFilteredCache::Criterion> & rCriteria,const std::unordered_set<sal_Int32> & rCatDims,Sequence<Sequence<Any>> & rData)132 void ScDatabaseDPData::GetDrillDownData(const vector<ScDPFilteredCache::Criterion>& rCriteria, const std::unordered_set<sal_Int32>& rCatDims, Sequence< Sequence<Any> >& rData)
133 {
134     CreateCacheTable();
135     sal_Int32 nRowSize = aCacheTable.getRowSize();
136     if (!nRowSize)
137         return;
138 
139     aCacheTable.filterTable(
140         rCriteria, rData, IsRepeatIfEmpty() ? rCatDims : std::unordered_set<sal_Int32>());
141 }
142 
CalcResults(CalcInfo & rInfo,bool bAutoShow)143 void ScDatabaseDPData::CalcResults(CalcInfo& rInfo, bool bAutoShow)
144 {
145     CreateCacheTable();
146     CalcResultsFromCacheTable( aCacheTable, rInfo, bAutoShow);
147 }
148 
GetCacheTable() const149 const ScDPFilteredCache& ScDatabaseDPData::GetCacheTable() const
150 {
151     return aCacheTable;
152 }
153 
ReloadCacheTable()154 void ScDatabaseDPData::ReloadCacheTable()
155 {
156     aCacheTable.clear();
157     CreateCacheTable();
158 }
159 
160 #if DUMP_PIVOT_TABLE
161 
Dump() const162 void ScDatabaseDPData::Dump() const
163 {
164     // TODO : Implement this.
165 }
166 
167 #endif
168 
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
170