1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "MColumnAlias.hxx"
#include "MQueryHelper.hxx"
#include "MConnection.hxx"
#include "MorkParser.hxx"
#include <string>
#include <vector>
#include <algorithm>
#include <strings.hrc>
#include <unotools/textsearch.hxx>
#include <sal/log.hxx>
using namespace connectivity::mork;
using namespace connectivity;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdbc;
static
std::vector<bool> entryMatchedByExpression(MQueryHelper* _aQuery, MQueryExpression const * _aExpr, MQueryHelperResultEntry* entry);
MQueryHelperResultEntry::MQueryHelperResultEntry()
{
}
MQueryHelperResultEntry::~MQueryHelperResultEntry()
{
}
OUString MQueryHelperResultEntry::getValue( const OString &key ) const
{
FieldMap::const_iterator iter = m_Fields.find( key );
if ( iter == m_Fields.end() )
{
return OUString();
}
else
{
return iter->second;
}
}
void MQueryHelperResultEntry::setValue( const OString &key, const OUString & rValue)
{
m_Fields[ key ] = rValue;
}
MQueryHelper::MQueryHelper(const OColumnAlias& _ca)
:m_rColumnAlias( _ca )
,m_aError()
{
m_aResults.clear();
}
MQueryHelper::~MQueryHelper()
{
clear_results();
}
void MQueryHelper::setAddressbook(OUString const &ab)
{
::osl::MutexGuard aGuard(m_aMutex);
m_aAddressbook = ab;
}
void MQueryHelper::append(std::unique_ptr<MQueryHelperResultEntry> resEnt)
{
assert(resEnt);
m_aResults.push_back( std::move(resEnt) );
}
void MQueryHelper::clear_results()
{
m_aResults.clear();
}
void MQueryHelper::reset()
{
clear_results();
m_aError.reset();
}
MQueryHelperResultEntry*
MQueryHelper::getByIndex(sal_uInt32 nRow)
{
// Row numbers are from 1 to N, need to ensure this, and then
// subtract 1
if ( nRow < 1 ) {
return nullptr;
}
return m_aResults[nRow -1].get();
}
sal_Int32 MQueryHelper::getResultCount() const
{
sal_Int32 result = static_cast<sal_Int32>(m_aResults.size());
return result;
}
bool MQueryHelper::getRowValue( ORowSetValue& rValue, sal_Int32 nDBRow,const OUString& aDBColumnName, sal_Int32 nType )
{
MQueryHelperResultEntry* pResEntry = getByIndex( nDBRow );
OSL_ENSURE( pResEntry != nullptr, "xResEntry == NULL");
if (pResEntry == nullptr )
{
rValue.setNull();
return false;
}
switch ( nType )
{
case DataType::VARCHAR:
rValue = pResEntry->getValue( m_rColumnAlias.getProgrammaticNameOrFallbackToUTF8Alias( aDBColumnName ) );
break;
default:
rValue.setNull();
break;
}
return true;
}
sal_Int32 MQueryHelper::executeQuery(OConnection* xConnection, MQueryExpression & expr)
{
reset();
OString oStringTable = OUStringToOString( m_aAddressbook, RTL_TEXTENCODING_UTF8 );
std::set<int> listRecords;
bool handleListTable = false;
MorkParser* pMork;
// check if we are retrieving the default table
if (oStringTable == "AddressBook" || oStringTable == "CollectedAddressBook")
{
pMork = xConnection->getMorkParser(oStringTable);
}
else
{
// Let's try to retrieve the list in Collected Addresses book
pMork = xConnection->getMorkParser("CollectedAddressBook");
if (std::find(pMork->lists_.begin(), pMork->lists_.end(), m_aAddressbook) == pMork->lists_.end())
{
// so the list is in Address book
// TODO : manage case where an address book has been created
pMork = xConnection->getMorkParser("AddressBook");
}
handleListTable = true;
// retrieve row ids for that list table
std::string listTable = oStringTable.getStr();
pMork->getRecordKeysForListTable(listTable, listRecords);
}
MorkTableMap *Tables = pMork->getTables( 0x80 );
if (!Tables)
return -1;
MorkRowMap *Rows = nullptr;
// Iterate all tables
for (auto & table : Tables->map)
{
if (table.first != 1) break;
Rows = MorkParser::getRows( 0x80, &table.second );
if ( Rows )
{
// Iterate all rows
for (auto const& row : Rows->map)
{
// list specific table
// only retrieve rowIds that belong to that list table.
if (handleListTable)
{
int rowId = row.first;
// belongs this row id to the list table?
if (listRecords.end() == listRecords.find(rowId))
{
// no, skip it
continue;
}
}
std::unique_ptr<MQueryHelperResultEntry> entry(new MQueryHelperResultEntry());
for (auto const& cell : row.second)
{
std::string column = pMork->getColumn(cell.first);
std::string value = pMork->getValue(cell.second);
OString key(column.c_str(), static_cast<sal_Int32>(column.size()));
OString valueOString(value.c_str(), static_cast<sal_Int32>(value.size()));
OUString valueOUString = OStringToOUString( valueOString, RTL_TEXTENCODING_UTF8 );
entry->setValue(key, valueOUString);
}
bool result = true;
for (bool elem : entryMatchedByExpression(this, &expr, entry.get()))
{
result = result && elem;<--- Consider using std::any_of, std::all_of, std::none_of, or std::accumulate algorithm instead of a raw loop.
}
if (result)
{
append(std::move(entry));
}
}
}
}
return 0;
}
std::vector<bool> entryMatchedByExpression(MQueryHelper* _aQuery, MQueryExpression const * _aExpr, MQueryHelperResultEntry* entry)
{
std::vector<bool> resultVector;
for (auto const& expr : _aExpr->getExpressions())
{
if ( expr->isStringExpr() ) {
MQueryExpressionString* evStr = static_cast<MQueryExpressionString*> (expr);
// Set the 'name' property of the boolString.
OString attrName = _aQuery->getColumnAlias().getProgrammaticNameOrFallbackToUTF8Alias( evStr->getName() );
SAL_INFO("connectivity.mork", "Name = " << attrName);
bool bRequiresValue = true;
OUString currentValue = entry->getValue(attrName);
if (evStr->getCond() == MQueryOp::Exists || evStr->getCond() == MQueryOp::DoesNotExist)
{
bRequiresValue = false;
}
if (bRequiresValue)
{
SAL_INFO("connectivity.mork", "Value = " << evStr->getValue() );
const OUString& searchedValue = evStr->getValue();
if (evStr->getCond() == MQueryOp::Is) {
SAL_INFO("connectivity.mork", "MQueryOp::Is; done");
resultVector.push_back(currentValue == searchedValue);
} else if (evStr->getCond() == MQueryOp::IsNot) {
SAL_INFO("connectivity.mork", "MQueryOp::IsNot; done");
resultVector.push_back(currentValue != searchedValue);
} else if (evStr->getCond() == MQueryOp::EndsWith) {
SAL_INFO("connectivity.mork", "MQueryOp::EndsWith; done");
resultVector.push_back(currentValue.endsWith(searchedValue));
} else if (evStr->getCond() == MQueryOp::BeginsWith) {
SAL_INFO("connectivity.mork", "MQueryOp::BeginsWith; done");
resultVector.push_back(currentValue.startsWith(searchedValue));
} else if (evStr->getCond() == MQueryOp::Contains) {
SAL_INFO("connectivity.mork", "MQueryOp::Contains; done");
resultVector.push_back(currentValue.indexOf(searchedValue) != -1);
} else if (evStr->getCond() == MQueryOp::DoesNotContain) {
SAL_INFO("connectivity.mork", "MQueryOp::DoesNotContain; done");
resultVector.push_back(currentValue.indexOf(searchedValue) == -1);
} else if (evStr->getCond() == MQueryOp::RegExp) {
SAL_INFO("connectivity.mork", "MQueryOp::RegExp; done");
utl::SearchParam param(
searchedValue, utl::SearchParam::SearchType::Regexp);
utl::TextSearch ts(param, LANGUAGE_DONTKNOW);
sal_Int32 start = 0;
sal_Int32 end = currentValue.getLength();
resultVector.push_back(
ts.SearchForward(currentValue, &start, &end));
}
} else if (evStr->getCond() == MQueryOp::Exists) {
SAL_INFO("connectivity.mork", "MQueryOp::Exists; done");
resultVector.push_back(!currentValue.isEmpty());
} else if (evStr->getCond() == MQueryOp::DoesNotExist) {
SAL_INFO("connectivity.mork", "MQueryOp::DoesNotExist; done");
resultVector.push_back(currentValue.isEmpty());
}
}
else if ( expr->isExpr() ) {
SAL_INFO("connectivity.mork", "Appending Subquery Expression");
MQueryExpression* queryExpression = static_cast<MQueryExpression*> (expr);
// recursive call
std::vector<bool> subquery_result = entryMatchedByExpression(_aQuery, queryExpression, entry);
MQueryExpression::bool_cond condition = queryExpression->getExpressionCondition();
if (condition == MQueryExpression::OR) {
bool result = false;
for (bool elem : subquery_result)
{
result = result || elem;<--- Consider using std::any_of, std::all_of, std::none_of, or std::accumulate algorithm instead of a raw loop.
}
resultVector.push_back(result);
} else {
assert(condition == MQueryExpression::AND && "only OR or AND should exist");
bool result = true;
for (bool elem : subquery_result)
{
result = result && elem;<--- Consider using std::any_of, std::all_of, std::none_of, or std::accumulate algorithm instead of a raw loop.
}
resultVector.push_back(result);
}
}
else {
// Should never see this...
SAL_WARN("connectivity.mork", "Unknown Expression Type!");
_aQuery->getError().setResId(STR_ERROR_GET_ROW);
return resultVector;
}
}
return resultVector;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|