xref: /aoo42x/main/sc/source/core/tool/queryparam.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 // INCLUDE ---------------------------------------------------------------
28 
29 #include "queryparam.hxx"
30 
31 using ::std::vector;
32 
33 // ============================================================================
34 
ScQueryParamBase()35 ScQueryParamBase::ScQueryParamBase()
36 {
37     Resize( MAXQUERY );
38     for (sal_uInt16 i=0; i<MAXQUERY; i++)
39         maEntries[i].Clear();
40 }
41 
ScQueryParamBase(const ScQueryParamBase & r)42 ScQueryParamBase::ScQueryParamBase(const ScQueryParamBase& r) :
43     bHasHeader(r.bHasHeader), bByRow(r.bByRow), bInplace(r.bInplace), bCaseSens(r.bCaseSens),
44     bRegExp(r.bRegExp), bDuplicate(r.bDuplicate), bMixedComparison(r.bMixedComparison),
45     maEntries(r.maEntries)
46 {
47 }
48 
~ScQueryParamBase()49 ScQueryParamBase::~ScQueryParamBase()
50 {
51 }
52 
GetEntryCount() const53 SCSIZE ScQueryParamBase::GetEntryCount() const
54 {
55     return maEntries.size();
56 }
57 
GetEntry(SCSIZE n) const58 ScQueryEntry& ScQueryParamBase::GetEntry(SCSIZE n) const
59 {
60     return maEntries[n];
61 }
62 
Resize(SCSIZE nNew)63 void ScQueryParamBase::Resize(SCSIZE nNew)
64 {
65     if ( nNew < MAXQUERY )
66         nNew = MAXQUERY;                // nie weniger als MAXQUERY
67 
68     vector<ScQueryEntry> aNewEntries(nNew);
69     SCSIZE nCopy = ::std::min(maEntries.size(), nNew);
70     for (SCSIZE i=0; i<nCopy; i++)
71         aNewEntries[i] = maEntries[i];
72 
73     maEntries.swap(aNewEntries);
74 }
75 
DeleteQuery(SCSIZE nPos)76 void ScQueryParamBase::DeleteQuery( SCSIZE nPos )
77 {
78     if (nPos >= maEntries.size())
79         return;
80 
81     size_t n = maEntries.size();
82     vector<ScQueryEntry> aNewEntries;
83     aNewEntries.reserve(n);
84     for (size_t i = 0; i < n; ++i)
85         if (i != nPos)
86             aNewEntries.push_back(maEntries[i]);
87 
88     // Don't forget to append an empty entry to make up for the removed one.
89     // The size of the entries is not supposed to change.
90     aNewEntries.push_back(ScQueryEntry());
91 
92     maEntries.swap(aNewEntries);
93 }
94 
FillInExcelSyntax(String & aCellStr,SCSIZE nIndex)95 void ScQueryParamBase::FillInExcelSyntax(String& aCellStr, SCSIZE nIndex)
96 {
97     if (aCellStr.Len() > 0)
98     {
99         if ( nIndex >= maEntries.size() )
100             Resize( nIndex+1 );
101 
102         ScQueryEntry& rEntry = GetEntry(nIndex);
103 
104         rEntry.bDoQuery = sal_True;
105         // Operatoren herausfiltern
106         if (aCellStr.GetChar(0) == '<')
107         {
108             if (aCellStr.GetChar(1) == '>')
109             {
110                 *rEntry.pStr = aCellStr.Copy(2);
111                 rEntry.eOp   = SC_NOT_EQUAL;
112             }
113             else if (aCellStr.GetChar(1) == '=')
114             {
115                 *rEntry.pStr = aCellStr.Copy(2);
116                 rEntry.eOp   = SC_LESS_EQUAL;
117             }
118             else
119             {
120                 *rEntry.pStr = aCellStr.Copy(1);
121                 rEntry.eOp   = SC_LESS;
122             }
123         }
124         else if (aCellStr.GetChar(0) == '>')
125         {
126             if (aCellStr.GetChar(1) == '=')
127             {
128                 *rEntry.pStr = aCellStr.Copy(2);
129                 rEntry.eOp   = SC_GREATER_EQUAL;
130             }
131             else
132             {
133                 *rEntry.pStr = aCellStr.Copy(1);
134                 rEntry.eOp   = SC_GREATER;
135             }
136         }
137         else
138         {
139             if (aCellStr.GetChar(0) == '=')
140                 *rEntry.pStr = aCellStr.Copy(1);
141             else
142                 *rEntry.pStr = aCellStr;
143             rEntry.eOp = SC_EQUAL;
144         }
145     }
146 }
147 
148 // ============================================================================
149 
ScQueryParamTable()150 ScQueryParamTable::ScQueryParamTable()
151 {
152 }
153 
ScQueryParamTable(const ScQueryParamTable & r)154 ScQueryParamTable::ScQueryParamTable(const ScQueryParamTable& r) :
155     nCol1(r.nCol1),nRow1(r.nRow1),nCol2(r.nCol2),nRow2(r.nRow2),nTab(r.nTab)
156 {
157 }
158 
~ScQueryParamTable()159 ScQueryParamTable::~ScQueryParamTable()
160 {
161 }
162 
163 // ============================================================================
164 
ScQueryParam()165 ScQueryParam::ScQueryParam() :
166     ScQueryParamBase(),
167     ScQueryParamTable(),
168     bDestPers(true),
169     nDestTab(0),
170     nDestCol(0),
171     nDestRow(0)
172 {
173     Clear();
174 }
175 
176 //------------------------------------------------------------------------
177 
ScQueryParam(const ScQueryParam & r)178 ScQueryParam::ScQueryParam( const ScQueryParam& r ) :
179     ScQueryParamBase(r),
180     ScQueryParamTable(r),
181     bDestPers(r.bDestPers), nDestTab(r.nDestTab), nDestCol(r.nDestCol), nDestRow(r.nDestRow)
182 {
183 }
184 
ScQueryParam(const ScDBQueryParamInternal & r)185 ScQueryParam::ScQueryParam( const ScDBQueryParamInternal& r ) :
186     ScQueryParamBase(r),
187     ScQueryParamTable(r),
188     bDestPers(true),
189     nDestTab(0),
190     nDestCol(0),
191     nDestRow(0)
192 {
193 }
194 
195 
196 //------------------------------------------------------------------------
197 
~ScQueryParam()198 ScQueryParam::~ScQueryParam()
199 {
200 }
201 
202 //------------------------------------------------------------------------
203 
Clear()204 void ScQueryParam::Clear()
205 {
206     nCol1=nCol2 = 0;
207     nRow1=nRow2 = 0;
208     nTab = SCTAB_MAX;
209     bHasHeader = bCaseSens = bRegExp = bMixedComparison = sal_False;
210     bInplace = bByRow = bDuplicate = sal_True;
211 
212     Resize( MAXQUERY );
213     for (sal_uInt16 i=0; i<MAXQUERY; i++)
214         maEntries[i].Clear();
215 
216     ClearDestParams();
217 }
218 
ClearDestParams()219 void ScQueryParam::ClearDestParams()
220 {
221     bDestPers = true;
222     nDestTab = 0;
223     nDestCol = 0;
224     nDestRow = 0;
225 }
226 
227 //------------------------------------------------------------------------
228 
operator =(const ScQueryParam & r)229 ScQueryParam& ScQueryParam::operator=( const ScQueryParam& r )
230 {
231     nCol1       = r.nCol1;
232     nRow1       = r.nRow1;
233     nCol2       = r.nCol2;
234     nRow2       = r.nRow2;
235     nTab        = r.nTab;
236     nDestTab    = r.nDestTab;
237     nDestCol    = r.nDestCol;
238     nDestRow    = r.nDestRow;
239     bHasHeader  = r.bHasHeader;
240     bInplace    = r.bInplace;
241     bCaseSens   = r.bCaseSens;
242     bRegExp     = r.bRegExp;
243     bMixedComparison = r.bMixedComparison;
244     bDuplicate  = r.bDuplicate;
245     bByRow      = r.bByRow;
246     bDestPers   = r.bDestPers;
247 
248     maEntries = r.maEntries;
249 
250     return *this;
251 }
252 
253 //------------------------------------------------------------------------
254 
operator ==(const ScQueryParam & rOther) const255 sal_Bool ScQueryParam::operator==( const ScQueryParam& rOther ) const
256 {
257     sal_Bool bEqual = sal_False;
258 
259     // Anzahl der Queries gleich?
260     SCSIZE nUsed      = 0;
261     SCSIZE nOtherUsed = 0;
262     SCSIZE nEntryCount = GetEntryCount();
263     SCSIZE nOtherEntryCount = rOther.GetEntryCount();
264 
265     while ( nUsed<nEntryCount && maEntries[nUsed].bDoQuery ) ++nUsed;
266     while ( nOtherUsed<nOtherEntryCount && rOther.maEntries[nOtherUsed].bDoQuery )
267         ++nOtherUsed;
268 
269     if (   (nUsed       == nOtherUsed)
270         && (nCol1       == rOther.nCol1)
271         && (nRow1       == rOther.nRow1)
272         && (nCol2       == rOther.nCol2)
273         && (nRow2       == rOther.nRow2)
274         && (nTab        == rOther.nTab)
275         && (bHasHeader  == rOther.bHasHeader)
276         && (bByRow      == rOther.bByRow)
277         && (bInplace    == rOther.bInplace)
278         && (bCaseSens   == rOther.bCaseSens)
279         && (bRegExp     == rOther.bRegExp)
280         && (bMixedComparison == rOther.bMixedComparison)
281         && (bDuplicate  == rOther.bDuplicate)
282         && (bDestPers   == rOther.bDestPers)
283         && (nDestTab    == rOther.nDestTab)
284         && (nDestCol    == rOther.nDestCol)
285         && (nDestRow    == rOther.nDestRow) )
286     {
287         bEqual = sal_True;
288         for ( SCSIZE i=0; i<nUsed && bEqual; i++ )
289             bEqual = maEntries[i] == rOther.maEntries[i];
290     }
291     return bEqual;
292 }
293 
294 //------------------------------------------------------------------------
295 
MoveToDest()296 void ScQueryParam::MoveToDest()
297 {
298     if (!bInplace)
299     {
300         SCsCOL nDifX = ((SCsCOL) nDestCol) - ((SCsCOL) nCol1);
301         SCsROW nDifY = ((SCsROW) nDestRow) - ((SCsROW) nRow1);
302         SCsTAB nDifZ = ((SCsTAB) nDestTab) - ((SCsTAB) nTab);
303 
304         nCol1 = sal::static_int_cast<SCCOL>( nCol1 + nDifX );
305         nRow1 = sal::static_int_cast<SCROW>( nRow1 + nDifY );
306         nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nDifX );
307         nRow2 = sal::static_int_cast<SCROW>( nRow2 + nDifY );
308         nTab  = sal::static_int_cast<SCTAB>( nTab  + nDifZ );
309         size_t n = maEntries.size();
310         for (size_t i=0; i<n; i++)
311             maEntries[i].nField += nDifX;
312 
313         bInplace = sal_True;
314     }
315     else
316     {
317         DBG_ERROR("MoveToDest, bInplace == TRUE");
318     }
319 }
320 
321 // ============================================================================
322 
ScDBQueryParamBase(DataType eType)323 ScDBQueryParamBase::ScDBQueryParamBase(DataType eType) :
324     ScQueryParamBase(),
325     mnField(-1),
326     mbSkipString(true),
327     meType(eType)
328 {
329 }
330 
~ScDBQueryParamBase()331 ScDBQueryParamBase::~ScDBQueryParamBase()
332 {
333 }
334 
GetType() const335 ScDBQueryParamBase::DataType ScDBQueryParamBase::GetType() const
336 {
337     return meType;
338 }
339 
340 // ============================================================================
341 
ScDBQueryParamInternal()342 ScDBQueryParamInternal::ScDBQueryParamInternal() :
343     ScDBQueryParamBase(ScDBQueryParamBase::INTERNAL),
344     ScQueryParamTable()
345 {
346 }
347 
~ScDBQueryParamInternal()348 ScDBQueryParamInternal::~ScDBQueryParamInternal()
349 {
350 }
351 
352 // ============================================================================
353 
ScDBQueryParamMatrix()354 ScDBQueryParamMatrix::ScDBQueryParamMatrix() :
355     ScDBQueryParamBase(ScDBQueryParamBase::MATRIX)
356 {
357 }
358 
~ScDBQueryParamMatrix()359 ScDBQueryParamMatrix::~ScDBQueryParamMatrix()
360 {
361 }
362 
363