1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "queryparam.hxx"
21 #include "queryentry.hxx"
22 #include "scmatrix.hxx"
24 #include "svl/sharedstringpool.hxx"
28 const size_t MAXQUERY
= 8;
30 class FindByField
: public std::unary_function
<ScQueryEntry
, bool>
34 FindByField(SCCOLROW nField
) : mnField(nField
) {}
35 bool operator() (const ScQueryEntry
& rEntry
) const
37 return rEntry
.bDoQuery
&& rEntry
.nField
== mnField
;
41 struct FindUnused
: public std::unary_function
<ScQueryEntry
, bool>
43 bool operator() (const ScQueryEntry
& rEntry
) const
45 return !rEntry
.bDoQuery
;
51 ScQueryParamBase::const_iterator
ScQueryParamBase::begin() const
53 return maEntries
.begin();
56 ScQueryParamBase::const_iterator
ScQueryParamBase::end() const
58 return maEntries
.end();
61 ScQueryParamBase::ScQueryParamBase() :
70 for (size_t i
= 0; i
< MAXQUERY
; ++i
)
71 maEntries
.push_back(new ScQueryEntry
);
74 ScQueryParamBase::ScQueryParamBase(const ScQueryParamBase
& r
) :
75 bHasHeader(r
.bHasHeader
), bByRow(r
.bByRow
), bInplace(r
.bInplace
), bCaseSens(r
.bCaseSens
),
76 bRegExp(r
.bRegExp
), bDuplicate(r
.bDuplicate
), mbRangeLookup(r
.mbRangeLookup
),
77 maEntries(r
.maEntries
)
81 ScQueryParamBase::~ScQueryParamBase()
85 bool ScQueryParamBase::IsValidFieldIndex() const
90 SCSIZE
ScQueryParamBase::GetEntryCount() const
92 return maEntries
.size();
95 const ScQueryEntry
& ScQueryParamBase::GetEntry(SCSIZE n
) const
100 ScQueryEntry
& ScQueryParamBase::GetEntry(SCSIZE n
)
105 ScQueryEntry
& ScQueryParamBase::AppendEntry()
107 // Find the first unused entry.
108 EntriesType::iterator itr
= std::find_if(
109 maEntries
.begin(), maEntries
.end(), FindUnused());
111 if (itr
!= maEntries
.end())
115 // Add a new entry to the end.
116 maEntries
.push_back(new ScQueryEntry
);
117 return maEntries
.back();
120 ScQueryEntry
* ScQueryParamBase::FindEntryByField(SCCOLROW nField
, bool bNew
)
122 EntriesType::iterator itr
= std::find_if(
123 maEntries
.begin(), maEntries
.end(), FindByField(nField
));
125 if (itr
!= maEntries
.end())
127 // existing entry found!
132 // no existing entry found, and we are not creating a new one.
135 return &AppendEntry();
138 void ScQueryParamBase::RemoveEntryByField(SCCOLROW nField
)
140 EntriesType::iterator itr
= std::find_if(
141 maEntries
.begin(), maEntries
.end(), FindByField(nField
));
143 if (itr
!= maEntries
.end())
145 maEntries
.erase(itr
);
146 if (maEntries
.size() < MAXQUERY
)
147 // Make sure that we have at least MAXQUERY number of entries at
149 maEntries
.push_back(new ScQueryEntry
);
153 void ScQueryParamBase::Resize(size_t nNew
)
156 nNew
= MAXQUERY
; // never less than MAXQUERY
158 if (nNew
< maEntries
.size())
160 size_t n
= maEntries
.size() - nNew
;
161 for (size_t i
= 0; i
< n
; ++i
)
162 maEntries
.pop_back();
164 else if (nNew
> maEntries
.size())
166 size_t n
= nNew
- maEntries
.size();
167 for (size_t i
= 0; i
< n
; ++i
)
168 maEntries
.push_back(new ScQueryEntry
);
172 void ScQueryParamBase::FillInExcelSyntax(
173 svl::SharedStringPool
& rPool
, const OUString
& rStr
, SCSIZE nIndex
)
175 const OUString aCellStr
= rStr
;
176 if (!aCellStr
.isEmpty())
178 if ( nIndex
>= maEntries
.size() )
181 ScQueryEntry
& rEntry
= GetEntry(nIndex
);
182 ScQueryEntry::Item
& rItem
= rEntry
.GetQueryItem();
184 rEntry
.bDoQuery
= sal_True
;
185 // Operatoren herausfiltern
186 if (aCellStr
[0] == '<')
188 if (aCellStr
[1] == '>')
190 rItem
.maString
= rPool
.intern(aCellStr
.copy(2));
191 rEntry
.eOp
= SC_NOT_EQUAL
;
193 else if (aCellStr
[1] == '=')
195 rItem
.maString
= rPool
.intern(aCellStr
.copy(2));
196 rEntry
.eOp
= SC_LESS_EQUAL
;
200 rItem
.maString
= rPool
.intern(aCellStr
.copy(1));
201 rEntry
.eOp
= SC_LESS
;
204 else if (aCellStr
[0]== '>')
206 if (aCellStr
[1] == '=')
208 rItem
.maString
= rPool
.intern(aCellStr
.copy(2));
209 rEntry
.eOp
= SC_GREATER_EQUAL
;
213 rItem
.maString
= rPool
.intern(aCellStr
.copy(1));
214 rEntry
.eOp
= SC_GREATER
;
219 if (aCellStr
[0] == '=')
220 rItem
.maString
= rPool
.intern(aCellStr
.copy(1));
222 rItem
.maString
= rPool
.intern(aCellStr
);
223 rEntry
.eOp
= SC_EQUAL
;
228 ScQueryParamTable::ScQueryParamTable()
232 ScQueryParamTable::ScQueryParamTable(const ScQueryParamTable
& r
) :
233 nCol1(r
.nCol1
),nRow1(r
.nRow1
),nCol2(r
.nCol2
),nRow2(r
.nRow2
),nTab(r
.nTab
)
237 ScQueryParamTable::~ScQueryParamTable()
241 ScQueryParam::ScQueryParam() :
252 ScQueryParam::ScQueryParam( const ScQueryParam
& r
) :
254 ScQueryParamTable(r
),
255 bDestPers(r
.bDestPers
), nDestTab(r
.nDestTab
), nDestCol(r
.nDestCol
), nDestRow(r
.nDestRow
)
259 ScQueryParam::ScQueryParam( const ScDBQueryParamInternal
& r
) :
261 ScQueryParamTable(r
),
269 ScQueryParam::~ScQueryParam()
273 void ScQueryParam::Clear()
278 bHasHeader
= bCaseSens
= bRegExp
= false;
279 bInplace
= bByRow
= bDuplicate
= sal_True
;
281 boost::ptr_vector
<ScQueryEntry
>::iterator itr
= maEntries
.begin(), itrEnd
= maEntries
.end();
282 for (; itr
!= itrEnd
; ++itr
)
288 void ScQueryParam::ClearDestParams()
296 ScQueryParam
& ScQueryParam::operator=( const ScQueryParam
& r
)
303 nDestTab
= r
.nDestTab
;
304 nDestCol
= r
.nDestCol
;
305 nDestRow
= r
.nDestRow
;
306 bHasHeader
= r
.bHasHeader
;
307 bInplace
= r
.bInplace
;
308 bCaseSens
= r
.bCaseSens
;
310 bDuplicate
= r
.bDuplicate
;
312 bDestPers
= r
.bDestPers
;
314 maEntries
= r
.maEntries
.clone();
319 bool ScQueryParam::operator==( const ScQueryParam
& rOther
) const
323 // Anzahl der Queries gleich?
325 SCSIZE nOtherUsed
= 0;
326 SCSIZE nEntryCount
= GetEntryCount();
327 SCSIZE nOtherEntryCount
= rOther
.GetEntryCount();
329 while ( nUsed
<nEntryCount
&& maEntries
[nUsed
].bDoQuery
) ++nUsed
;
330 while ( nOtherUsed
<nOtherEntryCount
&& rOther
.maEntries
[nOtherUsed
].bDoQuery
)
333 if ( (nUsed
== nOtherUsed
)
334 && (nCol1
== rOther
.nCol1
)
335 && (nRow1
== rOther
.nRow1
)
336 && (nCol2
== rOther
.nCol2
)
337 && (nRow2
== rOther
.nRow2
)
338 && (nTab
== rOther
.nTab
)
339 && (bHasHeader
== rOther
.bHasHeader
)
340 && (bByRow
== rOther
.bByRow
)
341 && (bInplace
== rOther
.bInplace
)
342 && (bCaseSens
== rOther
.bCaseSens
)
343 && (bRegExp
== rOther
.bRegExp
)
344 && (bDuplicate
== rOther
.bDuplicate
)
345 && (bDestPers
== rOther
.bDestPers
)
346 && (nDestTab
== rOther
.nDestTab
)
347 && (nDestCol
== rOther
.nDestCol
)
348 && (nDestRow
== rOther
.nDestRow
) )
351 for ( SCSIZE i
=0; i
<nUsed
&& bEqual
; i
++ )
352 bEqual
= maEntries
[i
] == rOther
.maEntries
[i
];
357 void ScQueryParam::MoveToDest()
361 SCsCOL nDifX
= ((SCsCOL
) nDestCol
) - ((SCsCOL
) nCol1
);
362 SCsROW nDifY
= ((SCsROW
) nDestRow
) - ((SCsROW
) nRow1
);
363 SCsTAB nDifZ
= ((SCsTAB
) nDestTab
) - ((SCsTAB
) nTab
);
365 nCol1
= sal::static_int_cast
<SCCOL
>( nCol1
+ nDifX
);
366 nRow1
= sal::static_int_cast
<SCROW
>( nRow1
+ nDifY
);
367 nCol2
= sal::static_int_cast
<SCCOL
>( nCol2
+ nDifX
);
368 nRow2
= sal::static_int_cast
<SCROW
>( nRow2
+ nDifY
);
369 nTab
= sal::static_int_cast
<SCTAB
>( nTab
+ nDifZ
);
370 size_t n
= maEntries
.size();
371 for (size_t i
=0; i
<n
; i
++)
372 maEntries
[i
].nField
+= nDifX
;
378 OSL_FAIL("MoveToDest, bInplace == TRUE");
382 ScDBQueryParamBase::ScDBQueryParamBase(DataType eType
) :
390 ScDBQueryParamBase::~ScDBQueryParamBase()
394 ScDBQueryParamBase::DataType
ScDBQueryParamBase::GetType() const
399 ScDBQueryParamInternal::ScDBQueryParamInternal() :
400 ScDBQueryParamBase(ScDBQueryParamBase::INTERNAL
),
405 ScDBQueryParamInternal::~ScDBQueryParamInternal()
409 bool ScDBQueryParamInternal::IsValidFieldIndex() const
411 return nCol1
<= mnField
&& mnField
<= nCol2
;
414 ScDBQueryParamMatrix::ScDBQueryParamMatrix() :
415 ScDBQueryParamBase(ScDBQueryParamBase::MATRIX
)
419 bool ScDBQueryParamMatrix::IsValidFieldIndex() const
422 mpMatrix
->GetDimensions(nC
, nR
);
423 return 0 <= mnField
&& mnField
<= static_cast<SCCOL
>(nC
);
426 ScDBQueryParamMatrix::~ScDBQueryParamMatrix()
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */