LCOV - code coverage report
Current view: top level - connectivity/source/drivers/mork - MQueryHelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 121 166 72.9 %
Date: 2015-06-13 12:38:46 Functions: 15 17 88.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11