LCOV - code coverage report
Current view: top level - connectivity/source/drivers/mork - MQueryHelper.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 170 0.0 %
Date: 2014-04-14 Functions: 0 19 0.0 %
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           0 : MQueryHelperResultEntry::MQueryHelperResultEntry()
      49             : {
      50           0 : }
      51             : 
      52           0 : MQueryHelperResultEntry::~MQueryHelperResultEntry()
      53             : {
      54           0 : }
      55             : 
      56           0 : OUString MQueryHelperResultEntry::getValue( const OString &key ) const
      57             : {
      58           0 :     FieldMap::const_iterator iter = m_Fields.find( key );
      59           0 :     if ( iter == m_Fields.end() )
      60             :     {
      61           0 :         return OUString();
      62             :     }
      63             :     else
      64             :     {
      65           0 :         return iter->second;
      66             :     }
      67             : }
      68             : 
      69           0 : 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           0 :     m_Fields[ key ] = rValue;
      75           0 : }
      76             : 
      77           0 : MQueryHelper::MQueryHelper(const OColumnAlias& _ca)
      78             :     :m_nIndex( 0 )
      79             :     ,m_bHasMore( sal_True )
      80             :     ,m_bAtEnd( sal_False )
      81             :     ,m_rColumnAlias( _ca )
      82           0 :     ,m_aError()
      83             : {
      84           0 :     m_aResults.clear();
      85           0 : }
      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           0 : void MQueryHelper::setAddressbook(OUString &ab)
      97             : {
      98             :     SAL_INFO("connectivity.mork", "MQueryHelper::setAddressbook()");
      99             : 
     100           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     101             : 
     102           0 :     m_aAddressbook = ab;
     103             : 
     104           0 :     OSL_TRACE("\tOUT MQuery::setAddressbook()");
     105           0 : }
     106             : 
     107           0 : void MQueryHelper::setExpression( MQueryExpression &_expr )
     108             : {
     109             :     SAL_INFO("connectivity.mork", "MQueryHelper::setExpression()");
     110             :     OSL_TRACE("IN MQueryHelper::setExpression()");
     111           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     112             : 
     113           0 :     m_aExpr = _expr;
     114             : 
     115           0 :     OSL_TRACE("\tOUT MQuery::setExpression()");
     116           0 : }
     117             : 
     118           0 : void MQueryHelper::append(MQueryHelperResultEntry* resEnt)
     119             : {
     120             : //    SAL_INFO("connectivity.mork", "MQueryHelper::append()");
     121             : 
     122           0 :     if ( resEnt != NULL ) {
     123           0 :         m_aResults.push_back( resEnt );
     124           0 :         m_bAtEnd   = sal_False;
     125             :     }
     126           0 : }
     127             : 
     128           0 : void MQueryHelper::clear_results()
     129             : {
     130           0 :     resultsArray::iterator iter = m_aResults.begin();
     131           0 :     while ( iter != m_aResults.end() ) {
     132           0 :         delete (*iter);
     133           0 :         ++iter;
     134             :     }
     135           0 :     m_aResults.clear();
     136           0 : }
     137             : 
     138           0 : void MQueryHelper::reset()
     139             : {
     140           0 :     m_nIndex = 0;
     141           0 :     m_bHasMore = sal_True;
     142           0 :     m_bAtEnd = sal_False;
     143           0 :     clear_results();
     144           0 :     m_aError.reset();
     145           0 : }
     146             : 
     147             : MQueryHelperResultEntry*
     148           0 : MQueryHelper::getByIndex(sal_uInt32 nRow)
     149             : {
     150             :     // Row numbers are from 1 to N, need to ensure this, and then
     151             :     // substract 1
     152           0 :     if ( nRow < 1 ) {
     153           0 :         return( NULL );
     154             :     }
     155           0 :     return m_aResults[nRow -1];
     156             : }
     157             : 
     158           0 : sal_Int32 MQueryHelper::getResultCount() const
     159             : {
     160             : //    SAL_INFO("connectivity.mork", "MQueryHelper::getResultCount()" );
     161           0 :     sal_Int32 result = static_cast<sal_Int32>(m_aResults.size());
     162             : //    SAL_INFO("connectivity.mork", "result: " << result);
     163             : 
     164           0 :     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           0 : sal_Bool MQueryHelper::getRowValue( ORowSetValue& rValue, sal_Int32 nDBRow,const OUString& aDBColumnName, sal_Int32 nType )
     190             : {
     191             :     SAL_INFO("connectivity.mork", "MQueryHelper::getRowValue()" );
     192           0 :     MQueryHelperResultEntry* xResEntry = getByIndex( nDBRow );
     193             : 
     194             :     OSL_ENSURE( xResEntry != NULL, "xResEntry == NULL");
     195           0 :     if (xResEntry == NULL )
     196             :     {
     197           0 :         rValue.setNull();
     198           0 :         return sal_False;
     199             :     }
     200           0 :     switch ( nType )
     201             :     {
     202             :         case DataType::VARCHAR:
     203           0 :             rValue = xResEntry->getValue( m_rColumnAlias.getProgrammaticNameOrFallbackToUTF8Alias( aDBColumnName ) );
     204           0 :             break;
     205             : 
     206             :         default:
     207           0 :             rValue.setNull();
     208           0 :             break;
     209             :     }
     210             : 
     211           0 :     return sal_True;
     212             : }
     213             : 
     214           0 : sal_Int32 MQueryHelper::executeQuery(OConnection* xConnection)
     215             : {
     216             :     SAL_INFO("connectivity.mork", "MQueryHelper::executeQuery()");
     217           0 :     reset();
     218             : 
     219           0 :     OString oStringTable = OUStringToOString( m_aAddressbook, RTL_TEXTENCODING_UTF8 );
     220           0 :     std::set<int> listRecords;
     221           0 :     bool handleListTable = false;
     222           0 :     MorkParser* xMork = xConnection->getMorkParser(oStringTable);
     223             : 
     224             :     // check if we are retrieving the default table
     225           0 :     if (oStringTable != "AddressBook" && oStringTable != "CollectedAddressBook")
     226             :     {
     227           0 :         handleListTable = true;
     228             :         // retrieve row ids for that list table
     229           0 :         std::string listTable = oStringTable.getStr();
     230           0 :         xMork->getRecordKeysForListTable(listTable, listRecords);
     231             :     }
     232           0 :     MorkTableMap::iterator tableIter;
     233           0 :     MorkTableMap *Tables = xMork->getTables( 0x80 );
     234           0 :     if (!Tables)
     235           0 :         return -1;
     236           0 :     MorkRowMap *Rows = 0;
     237           0 :     MorkRowMap::iterator rowIter;
     238             : 
     239             :     // Iterate all tables
     240           0 :     for ( tableIter = Tables->begin(); tableIter != Tables->end(); ++tableIter )
     241             :     {
     242           0 :         if (tableIter->first != 1) break;
     243           0 :         Rows = xMork->getRows( 0x80, &tableIter->second );
     244           0 :         if ( Rows )
     245             :         {
     246             :             // Iterate all rows
     247           0 :             for ( rowIter = Rows->begin(); rowIter != Rows->end(); ++rowIter )
     248             :             {
     249             :                 // list specific table
     250             :                 // only retrieve rowIds that belong to that list table.
     251           0 :                 if (handleListTable)
     252             :                 {
     253           0 :                     int rowId = rowIter->first;
     254             :                     // belongs this row id to the list table?
     255           0 :                     if (listRecords.end() == std::find(listRecords.begin(), listRecords.end(), rowId))
     256             :                     {
     257             :                         // no, skip it
     258           0 :                         continue;
     259             :                     }
     260             :                 }
     261             : 
     262           0 :                 MQueryHelperResultEntry* entry = new MQueryHelperResultEntry();
     263           0 :                 for (MorkCells::iterator CellsIter = rowIter->second.begin();
     264           0 :                      CellsIter != rowIter->second.end(); ++CellsIter )
     265             :                 {
     266           0 :                     std::string column = xMork->getColumn(CellsIter->first);
     267           0 :                     std::string value = xMork->getValue(CellsIter->second);
     268           0 :                     OString key(column.c_str(), static_cast<sal_Int32>(column.size()));
     269           0 :                     OString valueOString(value.c_str(), static_cast<sal_Int32>(value.size()));
     270           0 :                     OUString valueOUString = OStringToOUString( valueOString, RTL_TEXTENCODING_UTF8 );
     271           0 :                     entry->setValue(key, valueOUString);
     272           0 :                 }
     273           0 :                 ::std::vector< sal_Bool > vector = entryMatchedByExpression(this, &m_aExpr, entry);
     274           0 :                 sal_Bool result = sal_True;
     275           0 :                 for (::std::vector<sal_Bool>::iterator iter = vector.begin(); iter != vector.end(); ++iter)
     276             :                 {
     277           0 :                     result = result && *iter;
     278             :                 }
     279           0 :                 if (result)
     280             :                 {
     281           0 :                     append(entry);
     282             :                 }
     283             :                 else
     284             :                 {
     285           0 :                     delete entry;
     286             :                 }
     287           0 :             }
     288             :         }
     289             :     }
     290           0 :     return 0;
     291             : }
     292             : 
     293           0 : ::std::vector< sal_Bool > entryMatchedByExpression(MQueryHelper* _aQuery, MQueryExpression* _aExpr, MQueryHelperResultEntry* entry)
     294             : {
     295           0 :     ::std::vector< sal_Bool > resultVector;
     296           0 :     MQueryExpression::ExprVector::iterator evIter;
     297           0 :     for( evIter = _aExpr->getExpressions().begin();
     298           0 :          evIter != _aExpr->getExpressions().end();
     299             :          ++evIter )
     300             :     {
     301           0 :         if ( (*evIter)->isStringExpr() ) {
     302           0 :             MQueryExpressionString* evStr = static_cast<MQueryExpressionString*> (*evIter);
     303             :             // Set the 'name' property of the boolString.
     304           0 :             OString attrName = _aQuery->getColumnAlias().getProgrammaticNameOrFallbackToUTF8Alias( evStr->getName() );
     305             :             SAL_INFO("connectivity.mork", "Name = " << attrName.getStr());
     306           0 :             sal_Bool requiresValue = sal_True;
     307           0 :             OUString currentValue = entry->getValue(attrName);
     308           0 :             if (evStr->getCond() == MQueryOp::Exists || evStr->getCond() == MQueryOp::DoesNotExist)
     309             :             {
     310           0 :                 requiresValue = sal_False;
     311             :             }
     312           0 :             if (requiresValue)
     313             :             {
     314             :                 SAL_INFO("connectivity.mork", "Value = " << evStr->getValue() );
     315           0 :                 OUString searchedValue = evStr->getValue();
     316           0 :                 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           0 :                 } 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           0 :                 } else if (evStr->getCond() == MQueryOp::EndsWith) {
     323             :                     SAL_INFO("connectivity.mork", "MQueryOp::EndsWith; done");
     324           0 :                     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           0 :                 }
     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           0 :             }
     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           0 :     return resultVector;
     382             : }
     383             : 
     384             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10