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