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

Generated by: LCOV version 1.10