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

Generated by: LCOV version 1.10