Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / mozab / mozillasrc / MQuery.cxx
blob2af3e55fb89aedd5327abde80f47a733b6e58e26
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <MQueryHelper.hxx>
22 #include <MNameMapper.hxx>
23 #include <MConnection.hxx>
24 #include <connectivity/dbexception.hxx>
25 #include "MQuery.hxx"
26 #include "MLdapAttributeMap.hxx"
27 #include "MTypeConverter.hxx"
28 #include "MNSMozabProxy.hxx"
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <com/sun/star/mozilla/XMozillaBootstrap.hpp>
32 #include <osl/thread.hxx>
34 #if OSL_DEBUG_LEVEL > 0
35 # define OUtoCStr( x ) ( OUStringToOString ( (x), RTL_TEXTENCODING_ASCII_US).getStr())
36 #else /* OSL_DEBUG_LEVEL */
37 # define OUtoCStr( x ) ("dummy")
38 #endif /* OSL_DEBUG_LEVEL */
40 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
41 static NS_DEFINE_CID(kAbDirectoryQueryArgumentsCID, NS_ABDIRECTORYQUERYARGUMENTS_CID);
42 static NS_DEFINE_CID(kBooleanConditionStringCID, NS_BOOLEANCONDITIONSTRING_CID);
43 static NS_DEFINE_CID(kBooleanExpressionCID, NS_BOOLEANEXPRESSION_CID);
44 static NS_DEFINE_CID(kAbDirectoryQueryProxyCID, NS_ABDIRECTORYQUERYPROXY_CID);
45 static NS_DEFINE_CID(kAbLDAPAttributeMap, NS_IABLDAPATTRIBUTEMAP_IID);
47 using namespace connectivity::mozab;
48 using namespace connectivity;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::sdbc;
51 using namespace connectivity;
54 // Used to store an nsIAbDirectoryQuery member without the need to use Mozilla
55 // types in the MQuery.hxx file.
57 namespace connectivity {
58 namespace mozab {
59 struct MQueryDirectory {
60 nsCOMPtr<nsIAbDirectory> directory;
61 nsCOMPtr<nsIAbDirectoryQuery> directoryQuery;
62 PRInt32 contextId;
63 MQueryDirectory() : contextId(-1) {}
69 MQuery::MQuery( const OColumnAlias& _ca )
70 :m_rColumnAlias( _ca )
72 OSL_TRACE( "IN MQuery::MQuery( ca )" );
74 construct();
76 #if OSL_DEBUG_LEVEL > 0
77 m_oThreadID = osl::Thread::getCurrentIdentifier();
78 #endif
80 OSL_TRACE( "\tOUT MQuery::MQuery( ca )" );
83 MQuery::~MQuery()
85 OSL_TRACE("IN MQuery::~MQuery()");
87 // MQueryHelper is reference counted, so we need to decrement the
88 // count here.
90 if ( m_aQueryDirectory->contextId != -1 && m_aQueryDirectory->directoryQuery !=
91 NULL )
92 m_aQueryDirectory->directoryQuery->StopQuery(m_aQueryDirectory->contextId);
94 if ( m_aQueryDirectory )
95 delete m_aQueryDirectory;
97 NS_IF_RELEASE( m_aQueryHelper);
99 OSL_TRACE("\tOUT MQuery::~MQuery()");
102 void MQuery::construct()
104 // Set default values. (For now just as a reminder).
105 m_aError.reset();
106 m_bQuerySubDirs = sal_True; // LDAP Queryies require this to be set!
107 m_nMaxNrOfReturns = -1; // Unlimited number of returns.
109 m_aQueryDirectory = new MQueryDirectory();
110 // MQueryHelper is reference counted, so we need to add to the
111 // count here to prevent accidental deletion else where...
113 m_aQueryHelper = new MQueryHelper();
114 NS_IF_ADDREF( m_aQueryHelper);
117 void MQuery::setAddressbook(OUString &ab)
119 OSL_TRACE("IN MQuery::setAddressbook()");
120 ::osl::MutexGuard aGuard(m_aMutex);
122 m_aAddressbook = ab;
124 OSL_TRACE("\tOUT MQuery::setAddressbook()");
127 void MQuery::setMaxNrOfReturns(const sal_Int32 mnr)
129 OSL_TRACE( "IN MQuery::setMaxNrOfReturns()" );
130 ::osl::MutexGuard aGuard(m_aMutex);
132 m_nMaxNrOfReturns = mnr;
133 OSL_TRACE("\tOUT MQuery::setMaxNrOfReturns()" );
136 void MQuery::setExpression( MQueryExpression &_expr )
138 OSL_TRACE("IN MQuery::setExpression()");
139 ::osl::MutexGuard aGuard(m_aMutex);
141 m_aExpr = _expr;
143 OSL_TRACE("\tOUT MQuery::setExpression()");
146 static sal_Int32 generateExpression( MQuery* _aQuery, MQueryExpression* _aExpr,
147 nsIAbBooleanExpression* queryExpression )
149 nsresult rv; // Store return values.
150 // Array that holds all matchItems, to be passed to DoQuery().
151 nsCOMPtr<nsISupportsArray> matchItems;
152 NS_NewISupportsArray(getter_AddRefs(matchItems));
154 // Add every individual boolString to matchItems array.
155 nsString matchValue;
156 // Initialise the matchItems container.
157 MQueryExpression::ExprVector::iterator evIter;
158 for( evIter = _aExpr->getExpressions().begin();
159 evIter != _aExpr->getExpressions().end();
160 ++evIter )
162 if ( (*evIter)->isStringExpr() ) {
163 nsCOMPtr<nsIAbBooleanConditionString> boolString = do_CreateInstance (kBooleanConditionStringCID, &rv);
164 NS_ENSURE_SUCCESS( rv, rv );
166 MQueryExpressionString* evStr = static_cast<MQueryExpressionString*> (*evIter);
168 // Set the 'name' property of the boolString.
169 // Check if it's an alias first...
170 OString attrName = _aQuery->getColumnAlias().getProgrammaticNameOrFallbackToUTF8Alias( evStr->getName() );
171 boolString->SetName( strdup( attrName.getStr() ) );
172 OSL_TRACE("Name = %s ;", attrName.getStr() );
173 // Set the 'matchType' property of the boolString. Check for equal length.
174 sal_Bool requiresValue = sal_True;
175 switch(evStr->getCond()) {
176 case MQueryOp::Exists:
177 OSL_TRACE("MQueryOp::Exists; ");
178 boolString->SetCondition(nsIAbBooleanConditionTypes::Exists);
179 requiresValue = sal_False;
180 break;
181 case MQueryOp::DoesNotExist:
182 OSL_TRACE("MQueryOp::DoesNotExist; ");
183 boolString->SetCondition(nsIAbBooleanConditionTypes::DoesNotExist);
184 requiresValue = sal_False;
185 break;
186 case MQueryOp::Contains:
187 OSL_TRACE("MQueryOp::Contains; ");
188 boolString->SetCondition(nsIAbBooleanConditionTypes::Contains);
189 break;
190 case MQueryOp::DoesNotContain:
191 OSL_TRACE("MQueryOp::DoesNotContain; ");
192 boolString->SetCondition(nsIAbBooleanConditionTypes::DoesNotContain);
193 break;
194 case MQueryOp::Is:
195 OSL_TRACE("MQueryOp::Is; ");
196 boolString->SetCondition(nsIAbBooleanConditionTypes::Is);
197 break;
198 case MQueryOp::IsNot:
199 OSL_TRACE("MQueryOp::IsNot; ");
200 boolString->SetCondition(nsIAbBooleanConditionTypes::IsNot);
201 break;
202 case MQueryOp::BeginsWith:
203 OSL_TRACE("MQueryOp::BeginsWith; ");
204 boolString->SetCondition(nsIAbBooleanConditionTypes::BeginsWith);
205 break;
206 case MQueryOp::EndsWith:
207 OSL_TRACE("MQueryOp::EndsWith; ");
208 boolString->SetCondition(nsIAbBooleanConditionTypes::EndsWith);
209 break;
210 case MQueryOp::SoundsLike:
211 OSL_TRACE("MQueryOp::SoundsLike; ");
212 boolString->SetCondition(nsIAbBooleanConditionTypes::SoundsLike);
213 break;
214 case MQueryOp::RegExp:
215 OSL_TRACE("MQueryOp::RegExp; ");
216 boolString->SetCondition(nsIAbBooleanConditionTypes::RegExp);
217 break;
218 default:
219 OSL_TRACE("(default) MQueryOp::Is; ");
220 boolString->SetCondition(nsIAbBooleanConditionTypes::Is);
221 break;
223 // Set the 'matchValue' property of the boolString. Value returned in unicode.
224 if ( requiresValue )
226 OSL_TRACE("Value = %s", OUtoCStr( evStr->getValue() ) );
227 MTypeConverter::ouStringToNsString( evStr->getValue(), matchValue);
228 boolString->SetValue(matchValue.get ());
230 // Add the individual boolString to the container of matchItems.
231 matchItems->AppendElement(boolString);
233 else if ( (*evIter)->isExpr() ) {
234 nsCOMPtr< nsIAbBooleanExpression > subQueryExpr = do_CreateInstance( kBooleanExpressionCID , &rv);
235 NS_ENSURE_SUCCESS( rv, rv );
236 rv = generateExpression( _aQuery, static_cast< MQueryExpression* >(*evIter),
237 subQueryExpr );
238 NS_ENSURE_SUCCESS( rv, rv );
239 matchItems->AppendElement(subQueryExpr);
241 else {
242 // Should never see this...
243 OSL_FAIL("Unknown Expression Type!");
244 return( NS_ERROR_UNEXPECTED );
248 queryExpression->SetExpressions(matchItems);
249 if ( _aExpr->getExpressionCondition() == MQueryExpression::AND )
250 queryExpression->SetOperation(nsIAbBooleanOperationTypes::AND);
251 else
252 queryExpression->SetOperation(nsIAbBooleanOperationTypes::OR);
254 return( NS_OK );
256 sal_uInt32 MQuery::InsertLoginInfo(OConnection* _pCon)
258 nsresult rv; // Store return values.
260 OUString nameAB = _pCon->getHost().replace('.','_');
261 OUString bindDN = _pCon->getBindDN();
262 OUString password = _pCon->getPassword();
263 sal_Bool useSSL = _pCon->getUseSSL();
265 nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
266 NS_ENSURE_SUCCESS(rv, rv);
268 // create the ldap maxHits entry for the preferences file.
269 // Note: maxHits is applicable to LDAP only in mozilla.
270 nsCAutoString prefName(NS_LITERAL_CSTRING("ldap_2.servers."));
271 const char *pAddressBook = MTypeConverter::ouStringToCCharStringAscii(nameAB.getStr());
272 prefName.Append(pAddressBook);
274 if (!bindDN.isEmpty())
276 nsCAutoString bindPrefName=prefName;
277 bindPrefName.Append(NS_LITERAL_CSTRING(".auth.dn"));
278 rv = prefs->SetCharPref (bindPrefName.get(),
279 MTypeConverter::ouStringToCCharStringAscii( bindDN.getStr() ) );
280 NS_ENSURE_SUCCESS(rv, rv);
282 nsCAutoString pwdPrefName=prefName;
283 pwdPrefName.Append(NS_LITERAL_CSTRING(".auth.pwd"));
284 rv = prefs->SetCharPref (pwdPrefName.get(),
285 MTypeConverter::ouStringToCCharStringAscii( password.getStr() ) );
286 NS_ENSURE_SUCCESS(rv, rv);
288 if (useSSL)
290 nsCAutoString sslPrefName=prefName;
291 sslPrefName.Append(NS_LITERAL_CSTRING(".UseSSL"));
292 rv = prefs->SetBoolPref (sslPrefName.get(),useSSL);
293 NS_ENSURE_SUCCESS(rv, rv);
295 return rv;
298 //determine whether current profile is locked,any error will lead to return true
299 bool isProfileLocked(OConnection* _pCon)
301 ::com::sun::star::uno::Reference< ::com::sun::star::mozilla::XMozillaBootstrap > xMozillaBootstrap;
302 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
303 OSL_ENSURE( xFactory.is(), "can't get service factory" );
305 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xInstance = xFactory->createInstance("com.sun.star.mozilla.MozillaBootstrap");
306 OSL_ENSURE( xInstance.is(), "failed to create instance" );
307 xMozillaBootstrap = ::com::sun::star::uno::Reference< ::com::sun::star::mozilla::XMozillaBootstrap >(xInstance,::com::sun::star::uno::UNO_QUERY);
308 if (_pCon)
309 return xMozillaBootstrap->isProfileLocked(_pCon->getProduct(),_pCon->getMozProfile());
310 else
311 return xMozillaBootstrap->isCurrentProfileLocked();
316 sal_Int32 getDirectoryType(const nsIAbDirectory* directory)
318 nsresult retCode;
319 nsCOMPtr<nsIRDFResource> rdfResource = do_QueryInterface((nsISupports *)directory, &retCode) ;
320 if (NS_FAILED(retCode)) { return SDBCAddress::Unknown; }
321 const char * uri;
322 retCode=rdfResource->GetValueConst(&uri);
323 if (NS_FAILED(retCode)) { return SDBCAddress::Unknown; }
324 const sal_Char *sUriPrefix = ::connectivity::mozab::getSchemeURI( ::connectivity::mozab::SCHEME_LDAP );
325 if (strncmp(uri,sUriPrefix,strlen(sUriPrefix)) == 0)
327 return SDBCAddress::LDAP;
329 sUriPrefix = ::connectivity::mozab::getSchemeURI( ::connectivity::mozab::SCHEME_MOZILLA );
330 if (strncmp(uri,sUriPrefix,strlen(sUriPrefix)) == 0)
332 return SDBCAddress::Mozilla;
334 sUriPrefix = ::connectivity::mozab::getSchemeURI( ::connectivity::mozab::SCHEME_MOZILLA_MDB );
335 if (strncmp(uri,sUriPrefix,strlen(sUriPrefix)) == 0)
337 return SDBCAddress::Mozilla;
339 sUriPrefix = ::connectivity::mozab::getSchemeURI( ::connectivity::mozab::SCHEME_OUTLOOK_EXPRESS );
340 if (strncmp(uri,sUriPrefix,strlen(sUriPrefix)) == 0)
342 return SDBCAddress::OutlookExp;
344 sUriPrefix = ::connectivity::mozab::getSchemeURI( ::connectivity::mozab::SCHEME_OUTLOOK_MAPI );
345 if (strncmp(uri,sUriPrefix,strlen(sUriPrefix)) == 0)
347 return SDBCAddress::Outlook;
349 return SDBCAddress::Unknown;
353 sal_Bool isForceQueryProxyUsed(const nsIAbDirectory* directory)
355 sal_Int32 nType = getDirectoryType(directory);
356 if (nType == SDBCAddress::Outlook || nType == SDBCAddress::OutlookExp)
357 return sal_True;
358 return sal_False;
361 sal_Int32 MQuery::commitRow(const sal_Int32 rowIndex)
363 if (!m_aQueryHelper || !m_aQueryDirectory || !m_aQueryDirectory->directoryQuery)
364 return sal_False;
365 MNSMozabProxy xMProxy;
366 RunArgs args;
367 args.funcIndex = ProxiedFunc::FUNC_QUERYHELPER_COMMIT_CARD;
368 args.argCount = 3;
369 args.arg1 = (void*)m_aQueryHelper;
370 args.arg2 = (void*)&rowIndex;
371 args.arg3 = (void*)m_aQueryDirectory->directory;
372 nsresult rv = xMProxy.StartProxy(&args,m_Product,m_Profile);
373 m_aError = m_aQueryHelper->getError();
374 return rv;
378 sal_Int32 MQuery::deleteRow(const sal_Int32 rowIndex)
380 if (!m_aQueryHelper || !m_aQueryDirectory || !m_aQueryDirectory->directoryQuery)
381 return sal_False;
382 MNSMozabProxy xMProxy;
383 RunArgs args;
384 args.funcIndex = ProxiedFunc::FUNC_QUERYHELPER_DELETE_CARD;
385 args.argCount = 3;
386 args.arg1 = (void*)m_aQueryHelper;
387 args.arg2 = (void*)&rowIndex;
388 args.arg3 = (void*)m_aQueryDirectory->directory;
389 nsresult rv = xMProxy.StartProxy(&args,m_Product,m_Profile);
390 m_aError = m_aQueryHelper->getError();
391 return rv;
396 sal_Int32 MQuery::executeQuery(OConnection* _pCon)
398 ::osl::MutexGuard aGuard(m_aMutex);
399 OSL_TRACE("IN MQuery::executeQuery()");
400 m_Product = _pCon->getProduct();
401 m_Profile = _pCon->getMozProfile();
403 nsresult rv;
404 MNSMozabProxy xMProxy;
405 RunArgs args;
406 args.funcIndex = ProxiedFunc::FUNC_EXECUTE_QUERY;
407 args.argCount = 2;
408 args.arg1 = (void*)this;
409 args.arg2 = (void*)_pCon;
410 rv = xMProxy.StartProxy(&args,m_Product,m_Profile);
411 return rv;
414 sal_Int32 MQuery::executeQueryProxied(OConnection* _pCon)
416 #if OSL_DEBUG_LEVEL > 0
417 OSL_TRACE("IN MQuery::executeQueryProxied() Caller thread: %4d", m_oThreadID);
418 #endif
420 nsresult rv; // Store return values.
421 // MTypeConverter aTypeConverter;
422 // Create a nsIAbDirectory object to initialise the nsIAbDirectoryQuery object.
423 nsCOMPtr<nsIRDFService> rdfService(do_GetService(kRDFServiceCID, &rv)) ;
424 if (NS_FAILED(rv))
425 return(-1);
427 nsCOMPtr<nsIAbDirectory> directory;
428 MNameMapper *nmap = _pCon->getNameMapper();
431 if ( nmap->getDir( m_aAddressbook, getter_AddRefs( directory ) ) == sal_False )
432 return( -1 );
435 //insert ldap bind info to mozilla profile(in memery,none saved),so we can use it in mozilla part codes
436 if (_pCon->isLDAP())
438 rv = InsertLoginInfo(_pCon);
439 NS_ENSURE_SUCCESS(rv, rv);
442 // Since Outlook Express and Outlook in OCL mode support a very limited query capability,
443 // we use the following bool to judge whether we need bypass any use of a DirectoryQuery
444 // interface and instead force the use of the QueryProxy.
445 sal_Bool forceQueryProxyUse = isForceQueryProxyUsed(directory);
447 m_aQueryDirectory->directory = directory;
448 // Initialize directory in cases of LDAP and Mozilla
449 if (!forceQueryProxyUse) m_aQueryDirectory->directoryQuery = do_QueryInterface(directory, &rv);
451 if ( NS_FAILED(rv) || forceQueryProxyUse)
453 // Create a nsIAbDirectoryQuery object which eventually will execute
454 // the query by calling DoQuery().
455 nsCOMPtr< nsIAbDirectoryQueryProxy > directoryQueryProxy = do_CreateInstance( kAbDirectoryQueryProxyCID, &rv);
457 // Need to turn this off for anything using the Query Proxy since it
458 // treats Mailing Lists as directories!
460 m_bQuerySubDirs = sal_False;
462 rv = directoryQueryProxy->Initiate (directory);
463 NS_ENSURE_SUCCESS(rv, rv);
465 m_aQueryDirectory->directoryQuery = do_QueryInterface (directoryQueryProxy, &rv);
466 NS_ENSURE_SUCCESS(rv, rv);
467 OSL_TRACE("Using the directoryQueryProxy");
469 #if OSL_DEBUG_LEVEL > 0
470 else
471 OSL_TRACE("Not using a Query Proxy, Query i/f supported by directory");
472 #endif /* OSL_DEBUG_LEVEL */
475 // The problem here is that an LDAP Address Book may exist as
476 // a Mozilla Address Book. So we need to limit the number of
477 // records returned by the Server:
478 // 1. Determine if this is an LDAP Address Book
479 // [LDAP overrides the default operations(write|read|search) of all types with search only].
480 // 2. Determine if the limit is already set by us.
481 // 3. Use the mozilla preferences to see if this value is set.
482 // 4. Use value or else default to 100.
484 PRBool isWriteable;
485 rv = directory->GetOperations (&isWriteable);
486 NS_ENSURE_SUCCESS(rv, rv);
487 if (!(isWriteable & nsIAbDirectory::opWrite))
489 if(m_nMaxNrOfReturns == -1)
491 // Determine if the limit maxHits has been set in the mozilla preferences
492 // if set, then use the value otherwise default to 100
493 nsCOMPtr<nsIPref> prefs = do_GetService(NS_PREF_CONTRACTID, &rv);
494 NS_ENSURE_SUCCESS(rv, rv);
496 // create the ldap maxHits entry for the preferences file.
497 // Note: maxHits is applicable to LDAP only in mozilla.
498 nsCAutoString prefName(NS_LITERAL_CSTRING("ldap_2.servers."));
499 const char *pAddressBook = MTypeConverter::ouStringToCCharStringAscii(m_aAddressbook);
500 prefName.Append(pAddressBook);
501 prefName.Append(NS_LITERAL_CSTRING(".maxHits"));
503 PRInt32 maxHits;
504 rv = prefs->GetIntPref(prefName.get(), &maxHits);
505 if (NS_FAILED(rv))
506 m_nMaxNrOfReturns = 100;
507 else
508 m_nMaxNrOfReturns = maxHits;
513 nsCOMPtr< nsIAbBooleanExpression > queryExpression = do_CreateInstance( kBooleanExpressionCID , &rv);
514 NS_ENSURE_SUCCESS( rv, rv );
515 rv = generateExpression( this, &m_aExpr, queryExpression );
516 NS_ENSURE_SUCCESS( rv, rv );
518 // Use the nsIAbCard to return the card properties.
519 const char *returnProperties[] = {"card:nsIAbCard"};
520 PRInt32 count=1;
522 nsCOMPtr< nsIAbDirectoryQueryArguments > arguments = do_CreateInstance( kAbDirectoryQueryArgumentsCID, &rv);
523 NS_ENSURE_SUCCESS( rv, rv );
525 rv = arguments->SetExpression(queryExpression);
526 NS_ENSURE_SUCCESS( rv, rv );
528 rv = arguments->SetReturnProperties(count, returnProperties);
529 NS_ENSURE_SUCCESS( rv, rv );
531 rv = arguments->SetQuerySubDirectories(m_bQuerySubDirs);
532 NS_ENSURE_SUCCESS( rv, rv );
534 nsCOMPtr< nsIAbLDAPAttributeMap > attributeMap( new MLdapAttributeMap );
535 rv = arguments->SetTypeSpecificArg( attributeMap );
536 NS_ENSURE_SUCCESS( rv, rv );
538 // Execute the query.
539 OSL_TRACE( "****** calling DoQuery");
541 m_aError.reset();
543 m_aQueryHelper->reset();
545 rv = m_aQueryDirectory->directoryQuery->DoQuery(arguments, m_aQueryHelper, m_nMaxNrOfReturns, -1, &m_aQueryDirectory->contextId);
548 if (NS_FAILED(rv)) {
549 m_aQueryDirectory->contextId = -1;
550 OSL_TRACE( "****** DoQuery failed");
551 OSL_TRACE("\tOUT MQuery::executeQueryProxied()\n");
552 m_aQueryHelper->notifyQueryError() ;
553 return(-1);
555 #if OSL_DEBUG_LEVEL > 0
556 else {
557 OSL_TRACE( "****** DoQuery succeeded");
559 #endif
561 OSL_TRACE("\tOUT MQuery::executeQueryProxied()");
563 return(0);
569 // If the query executed is being done asynchronously then this may return
570 // -1 as the count, ie. it's undetermined.
572 sal_Int32
573 MQuery::getRowCount()
575 return( m_aQueryHelper->getResultCount() );
581 // As opposed to getRowCount() this returns the actual number of rows fetched
582 // so far (if is an async query)
584 sal_uInt32
585 MQuery::getRealRowCount()
587 return( m_aQueryHelper->getRealCount() );
591 // If the query executed is being done asynchronously then this may be
592 // false
594 sal_Bool
595 MQuery::queryComplete( void )
597 return( hadError() || m_aQueryHelper->queryComplete() );
600 sal_Bool
601 MQuery::waitForQueryComplete( void )
603 if( m_aQueryHelper->waitForQueryComplete( ) )
604 return sal_True;
605 m_aError = m_aQueryHelper->getError();
606 return( sal_False );
611 sal_Bool
612 MQuery::checkRowAvailable( sal_Int32 nDBRow )
614 while( !queryComplete() && m_aQueryHelper->getRealCount() <= (sal_uInt32)nDBRow )
615 if ( !m_aQueryHelper->waitForRow( nDBRow ) ) {
616 m_aError = m_aQueryHelper->getError();
617 return( sal_False );
620 return( getRowCount() > nDBRow );
623 sal_Bool
624 MQuery::setRowValue( ORowSetValue& rValue, sal_Int32 nDBRow,const OUString& aDBColumnName, sal_Int32 nType ) const
626 MQueryHelperResultEntry* xResEntry = m_aQueryHelper->getByIndex( nDBRow );
628 OSL_ENSURE( xResEntry != NULL, "xResEntry == NULL");
629 if (xResEntry == NULL )
631 const_cast< MQuery* >( this )->m_aError = m_aQueryHelper->getError();
632 return sal_False;
634 switch ( nType )
636 case DataType::VARCHAR:
637 xResEntry->setValue( m_rColumnAlias.getProgrammaticNameOrFallbackToUTF8Alias( aDBColumnName ), rValue.getString() );
638 break;
639 default:
640 OSL_FAIL( "invalid data type!" );
641 break;
644 return sal_True;
648 sal_Bool
649 MQuery::getRowValue( ORowSetValue& rValue, sal_Int32 nDBRow,const OUString& aDBColumnName, sal_Int32 nType ) const
651 MQueryHelperResultEntry* xResEntry = m_aQueryHelper->getByIndex( nDBRow );
653 OSL_ENSURE( xResEntry != NULL, "xResEntry == NULL");
654 if (xResEntry == NULL )
656 const_cast< MQuery* >( this )->m_aError = m_aQueryHelper->getError();
657 rValue.setNull();
658 return sal_False;
660 switch ( nType )
662 case DataType::VARCHAR:
663 rValue = xResEntry->getValue( m_rColumnAlias.getProgrammaticNameOrFallbackToUTF8Alias( aDBColumnName ) );
664 break;
666 default:
667 rValue.setNull();
668 break;
671 return sal_True;
674 sal_Int32
675 MQuery::getRowStates(sal_Int32 nDBRow)
677 MQueryHelperResultEntry* xResEntry = m_aQueryHelper->getByIndex( nDBRow );
679 OSL_ENSURE( xResEntry != NULL, "xResEntry == NULL");
680 if (xResEntry == NULL )
682 m_aError = m_aQueryHelper->getError();
683 return RowStates_Error;
685 return xResEntry->getRowStates();
687 sal_Bool
688 MQuery::setRowStates(sal_Int32 nDBRow,sal_Int32 aState)
690 MQueryHelperResultEntry* xResEntry = m_aQueryHelper->getByIndex( nDBRow );
692 OSL_ENSURE( xResEntry != NULL, "xResEntry == NULL");
693 if (xResEntry == NULL )
695 m_aError = m_aQueryHelper->getError();
696 return sal_False;
698 return xResEntry->setRowStates(aState);
701 sal_Bool
702 MQuery::resyncRow(sal_Int32 nDBRow)
704 MNSMozabProxy xMProxy;
705 RunArgs args;
706 args.funcIndex = ProxiedFunc::FUNC_QUERYHELPER_RESYNC_CARD;
707 args.argCount = 2;
708 args.arg1 = (void*)m_aQueryHelper;
709 args.arg2 = (void*)&nDBRow;
710 nsresult rv = xMProxy.StartProxy(&args,m_Product,m_Profile);
711 m_aError = m_aQueryHelper->getError();
712 return NS_SUCCEEDED( rv ) ? sal_True : sal_False;
715 sal_Int32
716 MQuery::createNewCard()
718 sal_Int32 nNumber = 0;
719 MNSMozabProxy xMProxy;
720 RunArgs args;
721 args.funcIndex = ProxiedFunc::FUNC_QUERYHELPER_CREATE_NEW_CARD;
722 args.argCount = 2;
723 args.arg1 = (void*)m_aQueryHelper;
724 args.arg2 = (void*)&nNumber;
725 nsresult rv = xMProxy.StartProxy(&args,m_Product,m_Profile);
727 m_aError = m_aQueryHelper->getError();
728 NS_ENSURE_SUCCESS(rv,0);
729 return nNumber;
733 MNameMapper*
734 MQuery::CreateNameMapper()
736 return( new MNameMapper() );
740 void
741 MQuery::FreeNameMapper( MNameMapper* _ptr )
743 delete _ptr;
746 sal_Bool MQuery::isWritable(OConnection* _pCon)
748 if ( !m_aQueryDirectory )
749 return sal_False;
751 nsresult rv; // Store return values.
752 nsCOMPtr<nsIAbDirectory> directory = do_QueryInterface(m_aQueryDirectory->directory, &rv);;
753 if (NS_FAILED(rv))
754 return sal_False;
755 if (getDirectoryType(directory) == SDBCAddress::Mozilla && isProfileLocked(_pCon))
756 return sal_False;
758 PRBool isWriteable;
759 rv = directory->GetOperations (&isWriteable);
760 if (NS_FAILED(rv))
761 return sal_False;
762 sal_Bool bWritable = ( isWriteable & nsIAbDirectory::opWrite ) == nsIAbDirectory::opWrite;
763 return bWritable;
766 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */