build fix
[LibreOffice.git] / connectivity / source / drivers / mork / MResultSet.cxx
blobc6206668301e23c7716df33c5465287a398e532a
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 .
20 #include <com/sun/star/sdbc/ResultSetType.hpp>
21 #include <com/sun/star/sdbc/FetchDirection.hpp>
22 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
23 #include <com/sun/star/sdbcx/CompareBookmark.hpp>
24 #include <connectivity/dbtools.hxx>
26 #include <vector>
27 #include <algorithm>
28 #include "MResultSet.hxx"
29 #include "sqlbison.hxx"
30 #include "MResultSetMetaData.hxx"
31 #include "FDatabaseMetaDataResultSet.hxx"
33 #include "resource/mork_res.hrc"
34 #include "resource/common_res.hrc"
36 using namespace ::comphelper;
37 using namespace connectivity;
38 using namespace connectivity::mork;
39 using namespace ::cppu;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::beans;
43 using namespace com::sun::star::sdbc;
44 using namespace com::sun::star::sdbcx;
45 using namespace com::sun::star::container;
46 using namespace com::sun::star::io;
47 using namespace com::sun::star::util;
50 // IMPLEMENT_SERVICE_INFO(OResultSet,"com.sun.star.sdbcx.OResultSet","com.sun.star.sdbc.ResultSet");
51 OUString SAL_CALL OResultSet::getImplementationName( ) throw ( RuntimeException, std::exception) \
53 return OUString("com.sun.star.sdbcx.mork.ResultSet");
56 Sequence< OUString > SAL_CALL OResultSet::getSupportedServiceNames( ) throw( RuntimeException, std::exception)
58 return {"com.sun.star.sdbc.ResultSet","com.sun.star.sdbcx.ResultSet"};
61 sal_Bool SAL_CALL OResultSet::supportsService( const OUString& _rServiceName ) throw( RuntimeException, std::exception)
63 return cppu::supportsService(this, _rServiceName);
67 OResultSet::OResultSet(OCommonStatement* pStmt, const std::shared_ptr< connectivity::OSQLParseTreeIterator >& _pSQLIterator )
68 : OResultSet_BASE(m_aMutex)
69 ,OPropertySetHelper(OResultSet_BASE::rBHelper)
70 ,m_pStatement(pStmt)
71 ,m_xStatement(*pStmt)
72 ,m_xMetaData(nullptr)
73 ,m_nRowPos(0)
74 ,m_bWasNull(false)
75 ,m_nFetchSize(0)
76 ,m_nResultSetType(ResultSetType::SCROLL_INSENSITIVE)
77 ,m_nFetchDirection(FetchDirection::FORWARD)
78 ,m_pSQLIterator( _pSQLIterator )
79 ,m_pParseTree( _pSQLIterator->getParseTree() )
80 ,m_aQueryHelper(pStmt->getOwnConnection()->getColumnAlias())
81 ,m_pTable(nullptr)
82 ,m_CurrentRowCount(0)
83 ,m_nParamIndex(0)
84 ,m_bIsAlwaysFalseQuery(false)
85 ,m_pKeySet(nullptr)
86 ,m_nUpdatedRow(0)
87 ,m_bIsReadOnly(TRISTATE_INDET)
89 //m_aQuery.setMaxNrOfReturns(pStmt->getOwnConnection()->getMaxResultRecords());
92 OResultSet::~OResultSet()
97 void OResultSet::disposing()
99 OPropertySetHelper::disposing();
101 ::osl::MutexGuard aGuard(m_aMutex);
103 m_xStatement.clear();
104 m_xMetaData.clear();
105 m_pParseTree = nullptr;
106 m_xColumns = nullptr;
107 m_xParamColumns = nullptr;
108 m_pKeySet = nullptr;
109 if(m_pTable)
111 m_pTable->release();
112 m_pTable = nullptr;
116 Any SAL_CALL OResultSet::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
118 Any aRet = OPropertySetHelper::queryInterface(rType);
119 if(!aRet.hasValue())
120 aRet = OResultSet_BASE::queryInterface(rType);
121 return aRet;
124 Sequence< Type > SAL_CALL OResultSet::getTypes( ) throw( RuntimeException, std::exception)
126 OTypeCollection aTypes( cppu::UnoType<css::beans::XMultiPropertySet>::get(),
127 cppu::UnoType<css::beans::XFastPropertySet>::get(),
128 cppu::UnoType<css::beans::XPropertySet>::get());
130 return ::comphelper::concatSequences(aTypes.getTypes(),OResultSet_BASE::getTypes());
133 void OResultSet::methodEntry()
135 checkDisposed(OResultSet_BASE::rBHelper.bDisposed);
136 if ( !m_pTable )
138 OSL_FAIL( "OResultSet::methodEntry: looks like we're disposed, but how is this possible?" );
139 throw DisposedException( OUString(), *this );
144 sal_Int32 SAL_CALL OResultSet::findColumn( const OUString& columnName ) throw(SQLException, RuntimeException, std::exception)
146 ResultSetEntryGuard aGuard( *this );
148 // find the first column with the name columnName
149 Reference< XResultSetMetaData > xMeta = getMetaData();
150 sal_Int32 nLen = xMeta->getColumnCount();
151 sal_Int32 i = 1;
152 for(;i<=nLen;++i)
154 if(xMeta->isCaseSensitive(i) ? columnName == xMeta->getColumnName(i) :
155 columnName.equalsIgnoreAsciiCase(xMeta->getColumnName(i)))
156 return i;
159 ::dbtools::throwInvalidColumnException( columnName, *this );
160 assert(false);
161 return 0; // Never reached
164 Reference< XInputStream > SAL_CALL OResultSet::getBinaryStream( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
166 return nullptr;
169 Reference< XInputStream > SAL_CALL OResultSet::getCharacterStream( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
171 return nullptr;
175 sal_Bool SAL_CALL OResultSet::getBoolean( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
177 ResultSetEntryGuard aGuard( *this );
178 m_bWasNull = true;
179 return false;
183 sal_Int8 SAL_CALL OResultSet::getByte( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
185 ResultSetEntryGuard aGuard( *this );
186 return 0;
190 Sequence< sal_Int8 > SAL_CALL OResultSet::getBytes( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
192 ResultSetEntryGuard aGuard( *this );
193 return Sequence< sal_Int8 >();
197 Date SAL_CALL OResultSet::getDate( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
199 ResultSetEntryGuard aGuard( *this );
200 return Date();
204 double SAL_CALL OResultSet::getDouble( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
206 ResultSetEntryGuard aGuard( *this );
207 return 0.0;
211 float SAL_CALL OResultSet::getFloat( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
213 ResultSetEntryGuard aGuard( *this );
214 return 0;
218 sal_Int32 SAL_CALL OResultSet::getInt( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
220 ResultSetEntryGuard aGuard( *this );
221 return 0;
225 sal_Int32 SAL_CALL OResultSet::getRow( ) throw(SQLException, RuntimeException, std::exception)
227 ResultSetEntryGuard aGuard( *this );
229 SAL_INFO("connectivity.mork", "return = " << m_nRowPos);
230 return m_nRowPos;
234 sal_Int64 SAL_CALL OResultSet::getLong( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
236 ResultSetEntryGuard aGuard( *this );
237 return sal_Int64();
241 Reference< XResultSetMetaData > SAL_CALL OResultSet::getMetaData( ) throw(SQLException, RuntimeException, std::exception)
243 ResultSetEntryGuard aGuard( *this );
245 if(!m_xMetaData.is())
246 m_xMetaData = new OResultSetMetaData(
247 m_pSQLIterator->getSelectColumns(), m_pSQLIterator->getTables().begin()->first ,m_pTable,determineReadOnly());
248 return m_xMetaData;
251 Reference< XArray > SAL_CALL OResultSet::getArray( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
253 return nullptr;
257 Reference< XClob > SAL_CALL OResultSet::getClob( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
259 return nullptr;
262 Reference< XBlob > SAL_CALL OResultSet::getBlob( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
264 return nullptr;
268 Reference< XRef > SAL_CALL OResultSet::getRef( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
270 return nullptr;
274 Any SAL_CALL OResultSet::getObject( sal_Int32 /*columnIndex*/, const Reference< css::container::XNameAccess >& /*typeMap*/ ) throw(SQLException, RuntimeException, std::exception)
276 return Any();
280 sal_Int16 SAL_CALL OResultSet::getShort( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
282 return 0;
286 void OResultSet::checkIndex(sal_Int32 columnIndex ) throw(css::sdbc::SQLException)
288 if(columnIndex <= 0 || columnIndex > (sal_Int32)m_xColumns->get().size())
289 ::dbtools::throwInvalidIndexException(*this);
292 sal_uInt32 OResultSet::currentRowCount()
294 if ( m_bIsAlwaysFalseQuery )
295 return 0;
296 //return 0;//m_aQuery.getRealRowCount() - deletedCount();
297 // new implementation
298 return m_aQueryHelper.getResultCount();
302 bool OResultSet::fetchCurrentRow( ) throw(SQLException, RuntimeException)
304 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
305 return fetchRow(getCurrentCardNumber());
309 bool OResultSet::fetchRow(sal_Int32 cardNumber,bool bForceReload) throw(SQLException, RuntimeException)
311 SAL_INFO("connectivity.mork", "cardNumber = " << cardNumber);
312 if (!bForceReload)
314 // Check whether we've already fetched the row...
315 if ( !(m_aRow->get())[0].isNull() && (sal_Int32)(m_aRow->get())[0] == (sal_Int32)cardNumber )
316 return true;
317 //Check whether the old row has been changed
318 if (cardNumber == m_nUpdatedRow)
320 //write back the changes first
321 if (!pushCard(cardNumber)) //error write back the changes
322 throw SQLException();
325 // else
326 // m_aQuery.resyncRow(cardNumber);
328 if ( !validRow( cardNumber ) )
329 return false;
331 (m_aRow->get())[0] = (sal_Int32)cardNumber;
332 sal_Int32 nCount = m_aColumnNames.getLength();
333 //m_RowStates = m_aQuery.getRowStates(cardNumber);
334 for( sal_Int32 i = 1; i <= nCount; i++ )
336 if ( (m_aRow->get())[i].isBound() )
339 // Everything in the addressbook is a string!
341 if ( !m_aQueryHelper.getRowValue( (m_aRow->get())[i], cardNumber, m_aColumnNames[i-1], DataType::VARCHAR ))
343 m_pStatement->getOwnConnection()->throwSQLException( m_aQueryHelper.getError(), *this );
347 return true;
352 const ORowSetValue& OResultSet::getValue(sal_Int32 cardNumber, sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
354 if ( !fetchRow( cardNumber ) )
356 OSL_FAIL("fetchRow() returned False" );
357 m_bWasNull = true;
358 return *ODatabaseMetaDataResultSet::getEmptyValue();
361 m_bWasNull = (m_aRow->get())[columnIndex].isNull();
362 return (m_aRow->get())[columnIndex];
367 OUString SAL_CALL OResultSet::getString( sal_Int32 columnIndex ) throw(SQLException, RuntimeException, std::exception)
369 ResultSetEntryGuard aGuard( *this );
371 OSL_ENSURE(m_xColumns.is(), "Need the Columns!!");
372 OSL_ENSURE(columnIndex <= (sal_Int32)m_xColumns->get().size(), "Trying to access invalid columns number");
373 checkIndex( columnIndex );
375 // If this query was sorted then we should have a valid KeySet, so use it
376 return getValue(getCurrentCardNumber(), mapColumn( columnIndex ) );
381 Time SAL_CALL OResultSet::getTime( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
383 ResultSetEntryGuard aGuard( *this );
384 return Time();
388 DateTime SAL_CALL OResultSet::getTimestamp( sal_Int32 /*columnIndex*/ ) throw(SQLException, RuntimeException, std::exception)
390 ResultSetEntryGuard aGuard( *this );
391 return DateTime();
395 sal_Bool SAL_CALL OResultSet::isBeforeFirst( ) throw(SQLException, RuntimeException, std::exception)
397 ResultSetEntryGuard aGuard( *this );
399 // here you have to implement your movements
400 // return true means there is no data
401 OSL_TRACE("In/Out: OResultSet::isBeforeFirst" );
402 return( m_nRowPos < 1 );
405 sal_Bool SAL_CALL OResultSet::isAfterLast( ) throw(SQLException, RuntimeException, std::exception)
407 SAL_WARN("connectivity.mork", "OResultSet::isAfterLast() NOT IMPLEMENTED!");
408 ResultSetEntryGuard aGuard( *this );
410 OSL_TRACE("In/Out: OResultSet::isAfterLast" );
411 // return sal_True;
412 return m_nRowPos > currentRowCount() && MQueryHelper::queryComplete();
415 sal_Bool SAL_CALL OResultSet::isFirst( ) throw(SQLException, RuntimeException, std::exception)
417 ResultSetEntryGuard aGuard( *this );
419 OSL_TRACE("In/Out: OResultSet::isFirst" );
420 return m_nRowPos == 1;
423 sal_Bool SAL_CALL OResultSet::isLast( ) throw(SQLException, RuntimeException, std::exception)
425 SAL_WARN("connectivity.mork", "OResultSet::isLast() NOT IMPLEMENTED!");
426 ResultSetEntryGuard aGuard( *this );
428 OSL_TRACE("In/Out: OResultSet::isLast" );
429 // return sal_True;
430 return m_nRowPos == currentRowCount() && MQueryHelper::queryComplete();
433 void SAL_CALL OResultSet::beforeFirst( ) throw(SQLException, RuntimeException, std::exception)
435 ResultSetEntryGuard aGuard( *this );
437 // move before the first row so that isBeforeFirst returns false
438 OSL_TRACE("In/Out: OResultSet::beforeFirst" );
439 if ( first() )
440 previous();
443 void SAL_CALL OResultSet::afterLast( ) throw(SQLException, RuntimeException, std::exception)
445 ResultSetEntryGuard aGuard( *this );
446 OSL_TRACE("In/Out: OResultSet::afterLast" );
448 if(last())
449 next();
453 void SAL_CALL OResultSet::close() throw(SQLException, RuntimeException, std::exception)
455 OSL_TRACE("In/Out: OResultSet::close" );
456 dispose();
460 sal_Bool SAL_CALL OResultSet::first( ) throw(SQLException, RuntimeException, std::exception)
462 OSL_TRACE("In/Out: OResultSet::first" );
463 return seekRow( FIRST_POS );
467 sal_Bool SAL_CALL OResultSet::last( ) throw(SQLException, RuntimeException, std::exception)
469 OSL_TRACE("In/Out: OResultSet::last" );
470 return seekRow( LAST_POS );
473 sal_Bool SAL_CALL OResultSet::absolute( sal_Int32 row ) throw(SQLException, RuntimeException, std::exception)
475 OSL_TRACE("In/Out: OResultSet::absolute" );
476 return seekRow( ABSOLUTE_POS, row );
479 sal_Bool SAL_CALL OResultSet::relative( sal_Int32 row ) throw(SQLException, RuntimeException, std::exception)
481 OSL_TRACE("In/Out: OResultSet::relative" );
482 return seekRow( RELATIVE_POS, row );
485 sal_Bool SAL_CALL OResultSet::previous( ) throw(SQLException, RuntimeException, std::exception)
487 ResultSetEntryGuard aGuard( *this );
488 OSL_TRACE("In/Out: OResultSet::previous" );
489 return seekRow( PRIOR_POS );
492 Reference< XInterface > SAL_CALL OResultSet::getStatement( ) throw(SQLException, RuntimeException, std::exception)
494 ResultSetEntryGuard aGuard( *this );
496 OSL_TRACE("In/Out: OResultSet::getStatement" );
497 return m_xStatement;
501 sal_Bool SAL_CALL OResultSet::rowDeleted( ) throw(SQLException, RuntimeException, std::exception)
503 SAL_WARN("connectivity.mork", "OResultSet::rowDeleted() NOT IMPLEMENTED!");
504 ResultSetEntryGuard aGuard( *this );
505 return true;//return ((m_RowStates & RowStates_Deleted) == RowStates_Deleted) ;
508 sal_Bool SAL_CALL OResultSet::rowInserted( ) throw(SQLException, RuntimeException, std::exception)
510 SAL_WARN("connectivity.mork", "OResultSet::rowInserted() NOT IMPLEMENTED!");
511 ResultSetEntryGuard aGuard( *this );
512 return true;//return ((m_RowStates & RowStates_Inserted) == RowStates_Inserted);
515 sal_Bool SAL_CALL OResultSet::rowUpdated( ) throw(SQLException, RuntimeException, std::exception)
517 SAL_WARN("connectivity.mork", "OResultSet::rowUpdated() NOT IMPLEMENTED!");
518 ResultSetEntryGuard aGuard( *this );
519 return true;// return ((m_RowStates & RowStates_Updated) == RowStates_Updated) ;
523 sal_Bool SAL_CALL OResultSet::next( ) throw(SQLException, RuntimeException, std::exception)
525 return seekRow( NEXT_POS );
529 sal_Bool SAL_CALL OResultSet::wasNull( ) throw(SQLException, RuntimeException, std::exception)
531 ResultSetEntryGuard aGuard( *this );
533 return m_bWasNull;
537 void SAL_CALL OResultSet::cancel( ) throw(RuntimeException, std::exception)
539 ResultSetEntryGuard aGuard( *this );
540 OSL_TRACE("In/Out: OResultSet::cancel" );
544 void SAL_CALL OResultSet::clearWarnings( ) throw(SQLException, RuntimeException, std::exception)
546 OSL_TRACE("In/Out: OResultSet::clearWarnings" );
549 Any SAL_CALL OResultSet::getWarnings( ) throw(SQLException, RuntimeException, std::exception)
551 OSL_TRACE("In/Out: OResultSet::getWarnings" );
552 return Any();
555 void SAL_CALL OResultSet::refreshRow( ) throw(SQLException, RuntimeException, std::exception)
557 OSL_TRACE("In/Out: OResultSet::refreshRow" );
558 if (fetchRow(getCurrentCardNumber(),true)) {
559 //force fetch current row will cause we lose all change to the current row
560 m_pStatement->getOwnConnection()->throwSQLException( STR_ERROR_REFRESH_ROW, *this );
564 IPropertyArrayHelper* OResultSet::createArrayHelper( ) const
566 Sequence< Property > aProps(5);
567 Property* pProperties = aProps.getArray();
568 sal_Int32 nPos = 0;
569 pProperties[nPos++] = css::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION),
570 PROPERTY_ID_FETCHDIRECTION, cppu::UnoType<sal_Int32>::get(), 0);
572 pProperties[nPos++] = css::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE),
573 PROPERTY_ID_FETCHSIZE, cppu::UnoType<sal_Int32>::get(), 0);
575 pProperties[nPos++] = css::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISBOOKMARKABLE),
576 PROPERTY_ID_ISBOOKMARKABLE, cppu::UnoType<bool>::get(), PropertyAttribute::READONLY);
578 pProperties[nPos++] = css::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY),
579 PROPERTY_ID_RESULTSETCONCURRENCY, cppu::UnoType<sal_Int32>::get(), PropertyAttribute::READONLY);
581 pProperties[nPos++] = css::beans::Property(::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE),
582 PROPERTY_ID_RESULTSETTYPE, cppu::UnoType<sal_Int32>::get(), PropertyAttribute::READONLY);
584 return new OPropertyArrayHelper(aProps);
587 IPropertyArrayHelper & OResultSet::getInfoHelper()
589 return *getArrayHelper();
592 sal_Bool OResultSet::convertFastPropertyValue(
593 Any & /*rConvertedValue*/,
594 Any & /*rOldValue*/,
595 sal_Int32 nHandle,
596 const Any& /*rValue*/ )
597 throw (css::lang::IllegalArgumentException)
599 OSL_FAIL( "OResultSet::convertFastPropertyValue: not implemented!" );
600 switch(nHandle)
602 case PROPERTY_ID_ISBOOKMARKABLE:
603 case PROPERTY_ID_RESULTSETCONCURRENCY:
604 case PROPERTY_ID_RESULTSETTYPE:
605 throw css::lang::IllegalArgumentException();
606 case PROPERTY_ID_FETCHDIRECTION:
607 case PROPERTY_ID_FETCHSIZE:
608 default:
611 return false;
614 void OResultSet::setFastPropertyValue_NoBroadcast(
615 sal_Int32 nHandle,
616 const Any& /*rValue*/
618 throw (Exception, std::exception)
620 OSL_FAIL( "OResultSet::setFastPropertyValue_NoBroadcast: not implemented!" );
621 switch(nHandle)
623 case PROPERTY_ID_ISBOOKMARKABLE:
624 case PROPERTY_ID_RESULTSETCONCURRENCY:
625 case PROPERTY_ID_RESULTSETTYPE:
626 throw Exception();
627 case PROPERTY_ID_FETCHDIRECTION:
628 break;
629 case PROPERTY_ID_FETCHSIZE:
630 break;
631 default:
636 void OResultSet::getFastPropertyValue(
637 Any& rValue,
638 sal_Int32 nHandle
639 ) const
641 switch(nHandle)
643 case PROPERTY_ID_RESULTSETCONCURRENCY:
644 rValue <<= (sal_Int32)ResultSetConcurrency::UPDATABLE;
645 break;
646 case PROPERTY_ID_RESULTSETTYPE:
647 rValue <<= m_nResultSetType;
648 break;
649 case PROPERTY_ID_FETCHDIRECTION:
650 rValue <<= m_nFetchDirection;
651 break;
652 case PROPERTY_ID_FETCHSIZE:
653 rValue <<= m_nFetchSize;
654 break;
655 case PROPERTY_ID_ISBOOKMARKABLE:
656 const_cast< OResultSet* >( this )->determineReadOnly();
657 rValue <<= (m_bIsReadOnly == TRISTATE_FALSE);
658 break;
662 void SAL_CALL OResultSet::acquire() throw()
664 OResultSet_BASE::acquire();
667 void SAL_CALL OResultSet::release() throw()
669 OResultSet_BASE::release();
672 css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL OResultSet::getPropertySetInfo( ) throw(css::uno::RuntimeException, std::exception)
674 return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
678 void OResultSet::parseParameter( const OSQLParseNode* pNode, OUString& rMatchString )
680 OSL_ENSURE(pNode->count() > 0,"Error parsing parameter in Parse Tree");
681 OSQLParseNode *pMark = pNode->getChild(0);
683 // Initialize to empty string
684 rMatchString.clear();
686 OUString aParameterName;
687 if (SQL_ISPUNCTUATION(pMark,"?")) {
688 aParameterName = "?";
690 else if (SQL_ISPUNCTUATION(pMark,":")) {
691 aParameterName = pNode->getChild(1)->getTokenValue();
693 // XXX - Now we know name, what's value????
694 m_nParamIndex ++;
695 SAL_INFO(
696 "connectivity.mork",
697 "Parameter name [" << m_nParamIndex << "]: " << aParameterName);
699 if ( m_aParameterRow.is() ) {
700 OSL_ENSURE( m_nParamIndex < (sal_Int32)m_aParameterRow->get().size() + 1, "More parameters than values found" );
701 rMatchString = (m_aParameterRow->get())[(sal_uInt16)m_nParamIndex];
702 SAL_INFO("connectivity.mork", "Prop Value: " << rMatchString);
704 else {
705 SAL_INFO("connectivity.mork", "Prop Value: Invalid ParameterRow!");
709 #define WILDCARD "%"
710 #define ALT_WILDCARD "*"
711 static const sal_Unicode MATCHCHAR = '_';
713 void OResultSet::analyseWhereClause( const OSQLParseNode* parseTree,
714 MQueryExpression &queryExpression)
716 OUString columnName;
717 MQueryOp::cond_type op( MQueryOp::Is );
718 OUString matchString;
720 if ( parseTree == nullptr )
721 return;
723 if ( m_pSQLIterator->getParseTree() != nullptr ) {
724 ::rtl::Reference<OSQLColumns> xColumns = m_pSQLIterator->getParameters();
725 if(xColumns.is())
727 OUString aColName, aParameterValue;
728 OSQLColumns::Vector::const_iterator aIter = xColumns->get().begin();
729 sal_Int32 i = 1;
730 for(;aIter != xColumns->get().end();++aIter)
732 (*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
733 SAL_INFO("connectivity.mork", "Prop Column Name: " << aColName);
734 if ( m_aParameterRow.is() ) {
735 aParameterValue = (m_aParameterRow->get())[(sal_uInt16)i];
736 SAL_INFO("connectivity.mork", "Prop Value: " << aParameterValue);
738 else {
739 SAL_INFO("connectivity.mork", "Prop Value: Invalid ParameterRow!");
741 i++;
747 if ( SQL_ISRULE(parseTree,where_clause) )
749 OSL_TRACE("analyseSQL : Got WHERE clause");
750 // Reset Parameter Counter
751 resetParameters();
752 analyseWhereClause( parseTree->getChild( 1 ), queryExpression );
754 else if ( parseTree->count() == 3 && // Handle ()'s
755 SQL_ISPUNCTUATION(parseTree->getChild(0),"(") &&
756 SQL_ISPUNCTUATION(parseTree->getChild(2),")"))
759 OSL_TRACE("analyseSQL : Got Punctuation ()");
760 MQueryExpression *subExpression = new MQueryExpression();
761 analyseWhereClause( parseTree->getChild( 1 ), *subExpression );
762 queryExpression.addExpression( subExpression );
764 else if ((SQL_ISRULE(parseTree,search_condition) || (SQL_ISRULE(parseTree,boolean_term)))
765 && parseTree->count() == 3) // Handle AND/OR
768 OSL_TRACE("analyseSQL : Got AND/OR clause");
770 // TODO - Need to take care or AND, for now match is always OR
771 analyseWhereClause( parseTree->getChild( 0 ), queryExpression );
772 analyseWhereClause( parseTree->getChild( 2 ), queryExpression );
774 if (SQL_ISTOKEN(parseTree->getChild(1),OR)) { // OR-Operator
775 queryExpression.setExpressionCondition( MQueryExpression::OR );
777 else if (SQL_ISTOKEN(parseTree->getChild(1),AND)) { // AND-Operator
778 queryExpression.setExpressionCondition( MQueryExpression::AND );
780 else {
781 OSL_FAIL("analyseSQL: Error in Parse Tree");
784 else if (SQL_ISRULE(parseTree,comparison_predicate))
786 OSL_ENSURE(parseTree->count() == 3, "Error parsing COMPARE predicate");
787 if (!(SQL_ISRULE(parseTree->getChild(0),column_ref) ||
788 parseTree->getChild(2)->getNodeType() == SQLNodeType::String ||
789 parseTree->getChild(2)->getNodeType() == SQLNodeType::IntNum ||
790 parseTree->getChild(2)->getNodeType() == SQLNodeType::ApproxNum ||
791 SQL_ISTOKEN(parseTree->getChild(2),TRUE) ||
792 SQL_ISTOKEN(parseTree->getChild(2),FALSE) ||
793 SQL_ISRULE(parseTree->getChild(2),parameter) ||
794 // odbc date
795 (SQL_ISRULE(parseTree->getChild(2),set_fct_spec) && SQL_ISPUNCTUATION(parseTree->getChild(2)->getChild(0),"{"))))
797 m_pStatement->getOwnConnection()->throwSQLException( STR_QUERY_TOO_COMPLEX, *this );
800 OSQLParseNode *pPrec = parseTree->getChild(1);
801 if (pPrec->getNodeType() == SQLNodeType::Equal)
802 op = MQueryOp::Is;
803 else if (pPrec->getNodeType() == SQLNodeType::NotEqual)
804 op = MQueryOp::IsNot;
806 OUString sTableRange;
807 if(SQL_ISRULE(parseTree->getChild(0),column_ref))
808 m_pSQLIterator->getColumnRange(parseTree->getChild(0),columnName,sTableRange);
809 else if(parseTree->getChild(0)->isToken())
810 columnName = parseTree->getChild(0)->getTokenValue();
812 if ( SQL_ISRULE(parseTree->getChild(2),parameter) ) {
813 parseParameter( parseTree->getChild(2), matchString );
815 else {
816 matchString = parseTree->getChild(2)->getTokenValue();
819 if ( columnName == "0" && op == MQueryOp::Is && matchString == "1" ) {
820 OSL_TRACE("Query always evaluates to FALSE");
821 m_bIsAlwaysFalseQuery = true;
823 queryExpression.addExpression( new MQueryExpressionString( columnName, op, matchString ));
825 else if (SQL_ISRULE(parseTree,like_predicate))
827 OSL_ENSURE(parseTree->count() == 2, "Error parsing LIKE predicate");
829 OSL_TRACE("analyseSQL : Got LIKE rule");
831 if ( !(SQL_ISRULE(parseTree->getChild(0), column_ref)) )
833 m_pStatement->getOwnConnection()->throwSQLException( STR_QUERY_INVALID_LIKE_COLUMN, *this );
837 OSQLParseNode *pColumn;
838 OSQLParseNode *pAtom;
839 OSQLParseNode *pOptEscape;
840 const OSQLParseNode* pPart2 = parseTree->getChild(1);
841 pColumn = parseTree->getChild(0); // Match Item
842 pAtom = pPart2->getChild(static_cast<sal_uInt32>(pPart2->count()-2)); // Match String
843 pOptEscape = pPart2->getChild(static_cast<sal_uInt32>(pPart2->count()-1)); // Opt Escape Rule
844 (void)pOptEscape;
845 const bool bNot = SQL_ISTOKEN(pPart2->getChild(0), NOT);
847 if (!(pAtom->getNodeType() == SQLNodeType::String ||
848 pAtom->getNodeType() == SQLNodeType::Name ||
849 SQL_ISRULE(pAtom,parameter) ||
850 ( pAtom->getChild(0) && pAtom->getChild(0)->getNodeType() == SQLNodeType::Name ) ||
851 ( pAtom->getChild(0) && pAtom->getChild(0)->getNodeType() == SQLNodeType::String )
854 OSL_TRACE("analyseSQL : pAtom->count() = %zu", pAtom->count() );
856 m_pStatement->getOwnConnection()->throwSQLException( STR_QUERY_INVALID_LIKE_STRING, *this );
859 OUString sTableRange;
860 if(SQL_ISRULE(pColumn,column_ref))
861 m_pSQLIterator->getColumnRange(pColumn,columnName,sTableRange);
863 SAL_INFO("connectivity.mork", "ColumnName = " << columnName);
865 if ( SQL_ISRULE(pAtom,parameter) ) {
866 parseParameter( pAtom, matchString );
867 // Replace all '*' with '%' : UI Usually does this but not with
868 // Parameters for some reason.
869 matchString = matchString.replaceAll( ALT_WILDCARD, WILDCARD );
871 else
873 matchString = pAtom->getTokenValue();
876 // Determine where '%' character is...
878 if ( matchString == WILDCARD )
880 // String containing only a '%' and nothing else
881 op = MQueryOp::Exists;
882 // Will be ignored for Exists case, but clear anyway.
883 matchString.clear();
885 else if ( matchString.indexOf ( WILDCARD ) == -1 &&
886 matchString.indexOf ( MATCHCHAR ) == -1 )
888 // Simple string , eg. "to match"
889 if ( bNot )
890 op = MQueryOp::DoesNotContain;
891 else
892 op = MQueryOp::Contains;
894 else if ( matchString.startsWith( WILDCARD )
895 && matchString.endsWith( WILDCARD )
896 && matchString.indexOf ( WILDCARD, 1 ) == matchString.lastIndexOf ( WILDCARD )
897 && matchString.indexOf( MATCHCHAR ) == -1
900 // Relatively simple "%string%" - ie, contains...
901 // Cut '%' from front and rear
902 matchString = matchString.replaceAt( 0, 1, OUString() );
903 matchString = matchString.replaceAt( matchString.getLength() -1 , 1, OUString() );
905 if (bNot)
906 op = MQueryOp::DoesNotContain;
907 else
908 op = MQueryOp::Contains;
910 else if ( bNot )
912 // We currently can't handle a 'NOT LIKE' when there are '%' or
913 // '_' dispersed throughout
914 m_pStatement->getOwnConnection()->throwSQLException( STR_QUERY_NOT_LIKE_TOO_COMPLEX, *this );
916 else
918 if ( (matchString.indexOf ( WILDCARD ) == matchString.lastIndexOf ( WILDCARD ))
919 && matchString.indexOf( MATCHCHAR ) == -1
922 // One occurrence of '%' - no '_' matches...
923 if ( matchString.startsWith( WILDCARD ) )
925 op = MQueryOp::EndsWith;
926 matchString = matchString.replaceAt( 0, 1, OUString());
928 else if ( matchString.indexOf ( WILDCARD ) == matchString.getLength() -1 )
930 op = MQueryOp::BeginsWith;
931 matchString = matchString.replaceAt( matchString.getLength() -1 , 1, OUString() );
933 else
935 sal_Int32 pos = matchString.indexOf ( WILDCARD );
936 matchString = matchString.replaceAt( pos, 1, ".*" );
937 op = MQueryOp::RegExp;
941 else
943 // Most Complex, need to use an RE
944 sal_Int32 pos;
945 while ( (pos = matchString.indexOf ( WILDCARD )) != -1 )
947 matchString = matchString.replaceAt( pos, 1, ".*" );
950 while ( (pos = matchString.indexOf( MATCHCHAR )) != -1 )
952 matchString = matchString.replaceAt( pos, 1, "." );
955 op = MQueryOp::RegExp;
959 queryExpression.addExpression( new MQueryExpressionString( columnName, op, matchString ));
961 else if (SQL_ISRULE(parseTree,test_for_null))
963 OSL_ENSURE(parseTree->count() == 2,"Error in ParseTree");
964 const OSQLParseNode* pPart2 = parseTree->getChild(1);
965 OSL_ENSURE(SQL_ISTOKEN(pPart2->getChild(0),IS),"Error in ParseTree");
967 if (!SQL_ISRULE(parseTree->getChild(0),column_ref))
969 m_pStatement->getOwnConnection()->throwSQLException( STR_QUERY_INVALID_IS_NULL_COLUMN, *this );
972 if (SQL_ISTOKEN(pPart2->getChild(1),NOT))
974 op = MQueryOp::Exists;
976 else
978 op = MQueryOp::DoesNotExist;
981 OUString sTableRange;
982 m_pSQLIterator->getColumnRange(parseTree->getChild(0),columnName,sTableRange);
984 queryExpression.addExpression( new MQueryExpressionString( columnName, op ));
986 else
988 OSL_TRACE( "Unexpected statement!!!" );
990 m_pStatement->getOwnConnection()->throwSQLException( STR_QUERY_TOO_COMPLEX, *this );
994 void OResultSet::fillRowData()
995 throw (css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
997 OSL_ENSURE( m_pStatement, "Require a statement" );
999 MQueryExpression queryExpression;
1001 OConnection* pConnection = static_cast<OConnection*>(m_pStatement->getConnection().get());
1002 m_xColumns = m_pSQLIterator->getSelectColumns();
1004 OSL_ENSURE(m_xColumns.is(), "Need the Columns!!");
1006 OSQLColumns::Vector::const_iterator aIter = m_xColumns->get().begin();
1007 const OUString sProprtyName = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME);
1008 OUString sName;
1009 m_aAttributeStrings.clear();
1010 m_aAttributeStrings.reserve(m_xColumns->get().size());
1011 for (sal_Int32 i = 1; aIter != m_xColumns->get().end();++aIter, i++)
1013 (*aIter)->getPropertyValue(sProprtyName) >>= sName;
1014 SAL_INFO(
1015 "connectivity.mork", "Query Columns : (" << i << ") " << sName);
1016 m_aAttributeStrings.push_back( sName );
1019 // Generate Match Conditions for Query
1020 const OSQLParseNode* pParseTree = m_pSQLIterator->getWhereTree();
1022 m_bIsAlwaysFalseQuery = false;
1023 if ( pParseTree != nullptr )
1025 // Extract required info
1027 OSL_TRACE("\tHave a Where Clause");
1029 analyseWhereClause( pParseTree, queryExpression );
1031 // If the query is a 0=1 then set Row count to 0 and return
1032 if ( m_bIsAlwaysFalseQuery )
1034 m_bIsReadOnly = TRISTATE_TRUE;
1035 return;
1038 OUString aStr( m_pTable->getName() );
1039 m_aQueryHelper.setAddressbook( aStr );
1041 sal_Int32 rv = m_aQueryHelper.executeQuery(pConnection, queryExpression);
1042 if ( rv == -1 ) {
1043 m_pStatement->getOwnConnection()->throwSQLException( STR_ERR_EXECUTING_QUERY, *this );
1046 if (m_aQueryHelper.hadError())
1048 m_pStatement->getOwnConnection()->throwSQLException( m_aQueryHelper.getError(), *this );
1051 //determine whether the address book is readonly
1052 determineReadOnly();
1054 SAL_INFO("connectivity.mork", "executeQuery returned " << rv);
1056 OSL_TRACE( "\tOUT OResultSet::fillRowData()" );
1060 static bool matchRow( OValueRow& row1, OValueRow& row2 )
1062 OValueVector::Vector::const_iterator row1Iter = row1->get().begin();
1063 OValueVector::Vector::const_iterator row2Iter = row2->get().begin();
1064 for ( ++row1Iter,++row2Iter; // the first column is the bookmark column
1065 row1Iter != row1->get().end(); ++row1Iter,++row2Iter)
1067 if ( row1Iter->isBound())
1069 // Compare values, if at anytime there's a mismatch return false
1070 if ( !( (*row1Iter) == (*row2Iter) ) )
1071 return false;
1075 // If we get to here the rows match
1076 return true;
1079 sal_Int32 OResultSet::getRowForCardNumber(sal_Int32 nCardNum)
1081 SAL_INFO("connectivity.mork", "nCardNum = " << nCardNum);
1083 if ( m_pKeySet.is() )
1085 sal_Int32 nPos;
1086 for(nPos=0;nPos < (sal_Int32)m_pKeySet->get().size();nPos++)
1088 if (nCardNum == (m_pKeySet->get())[nPos])
1090 SAL_INFO("connectivity.mork", "return = " << nPos+1);
1091 return nPos+1;
1096 m_pStatement->getOwnConnection()->throwSQLException( STR_INVALID_BOOKMARK, *this );
1098 return 0;
1101 void SAL_CALL OResultSet::executeQuery() throw(css::sdbc::SQLException,
1102 css::uno::RuntimeException,
1103 std::exception)
1105 ResultSetEntryGuard aGuard( *this );
1107 OSL_ENSURE( m_pTable, "Need a Table object");
1108 if(!m_pTable)
1110 const OSQLTables& rTabs = m_pSQLIterator->getTables();
1111 if (rTabs.empty() || !rTabs.begin()->second.is())
1112 m_pStatement->getOwnConnection()->throwSQLException( STR_QUERY_TOO_COMPLEX, *this );
1114 m_pTable = static_cast< OTable* > ((rTabs.begin()->second).get());
1118 m_nRowPos = 0;
1120 fillRowData();
1122 OSL_ENSURE(m_xColumns.is(), "Need the Columns!!");
1124 switch( m_pSQLIterator->getStatementType() )
1126 case OSQLStatementType::Select:
1128 if(m_bIsAlwaysFalseQuery) {
1129 break;
1131 else if(isCount())
1133 m_pStatement->getOwnConnection()->throwSQLException( STR_NO_COUNT_SUPPORT, *this );
1135 else
1137 bool bDistinct = false;
1138 OSQLParseNode *pDistinct = m_pParseTree->getChild(1);
1139 if (pDistinct && pDistinct->getTokenID() == SQL_TOKEN_DISTINCT)
1141 if(!IsSorted())
1143 m_aOrderbyColumnNumber.push_back(m_aColMapping[1]);
1144 m_aOrderbyAscending.push_back(TAscendingOrder::DESC);
1146 bDistinct = true;
1149 OSortIndex::TKeyTypeVector eKeyType(m_aOrderbyColumnNumber.size());
1150 ::std::vector<sal_Int32>::const_iterator aOrderByIter = m_aOrderbyColumnNumber.begin();
1151 for ( ::std::vector<sal_Int16>::size_type i = 0; aOrderByIter != m_aOrderbyColumnNumber.end(); ++aOrderByIter,++i)
1153 OSL_ENSURE((sal_Int32)m_aRow->get().size() > *aOrderByIter,"Invalid Index");
1154 switch ((m_aRow->get().begin()+*aOrderByIter)->getTypeKind())
1156 case DataType::CHAR:
1157 case DataType::VARCHAR:
1158 eKeyType[i] = OKeyType::String;
1159 break;
1161 case DataType::OTHER:
1162 case DataType::TINYINT:
1163 case DataType::SMALLINT:
1164 case DataType::INTEGER:
1165 case DataType::DECIMAL:
1166 case DataType::NUMERIC:
1167 case DataType::REAL:
1168 case DataType::DOUBLE:
1169 case DataType::DATE:
1170 case DataType::TIME:
1171 case DataType::TIMESTAMP:
1172 case DataType::BIT:
1173 eKeyType[i] = OKeyType::Double;
1174 break;
1176 // Other types aren't implemented (so they are always FALSE)
1177 default:
1178 eKeyType[i] = OKeyType::NONE;
1179 OSL_FAIL("MResultSet::executeQuery: Order By Data Type not implemented");
1180 break;
1184 if (IsSorted())
1186 // Implement Sorting
1188 // So that we can sort we need to wait until the executed
1189 // query to the mozilla addressbooks has returned all
1190 // values.
1192 OSL_TRACE("Query is to be sorted");
1194 OSL_ENSURE( MQueryHelper::queryComplete(), "Query not complete!!");
1196 OSortIndex aSortIndex(eKeyType,m_aOrderbyAscending);
1198 OSL_TRACE("OrderbyColumnNumber->size() = %zu",m_aOrderbyColumnNumber.size());
1199 #if OSL_DEBUG_LEVEL > 0
1200 for ( ::std::vector<sal_Int32>::size_type i = 0; i < m_aColMapping.size(); i++ )
1201 SAL_INFO(
1202 "connectivity.mork",
1203 "Mapped: " << i << " -> " << m_aColMapping[i]);
1204 #endif
1205 for ( sal_Int32 nRow = 1; nRow <= m_aQueryHelper.getResultCount(); nRow++ ) {
1207 OKeyValue* pKeyValue = OKeyValue::createKeyValue((nRow));
1209 ::std::vector<sal_Int32>::const_iterator aIter = m_aOrderbyColumnNumber.begin();
1210 for (;aIter != m_aOrderbyColumnNumber.end(); ++aIter)
1212 const ORowSetValue& value = getValue(nRow, *aIter);
1214 SAL_INFO(
1215 "connectivity.mork",
1216 "Adding Value: (" << nRow << "," << *aIter
1217 << ") : " << value.getString());
1219 pKeyValue->pushKey(new ORowSetValueDecorator(value));
1222 aSortIndex.AddKeyValue( pKeyValue );
1225 m_pKeySet = aSortIndex.CreateKeySet();
1226 m_CurrentRowCount = static_cast<sal_Int32>(m_pKeySet->get().size());
1227 #if OSL_DEBUG_LEVEL > 0
1228 for( OKeySet::Vector::size_type i = 0; i < m_pKeySet->get().size(); i++ )
1229 SAL_INFO(
1230 "connectivity.mork",
1231 "Sorted: " << i << " -> " << (m_pKeySet->get())[i]);
1232 #endif
1234 beforeFirst(); // Go back to start
1236 else //we always need m_pKeySet now
1237 m_pKeySet = new OKeySet();
1239 // Handle the DISTINCT case
1240 if ( bDistinct && m_pKeySet.is() )
1242 OValueRow aSearchRow = new OValueVector( m_aRow->get().size() );
1244 for(sal_Int32 & i : m_pKeySet->get())
1246 fetchRow( i ); // Fills m_aRow
1247 if ( matchRow( m_aRow, aSearchRow ) )
1249 i = 0; // Marker for later to be removed
1251 else
1253 // They don't match, so it's not a duplicate.
1254 // Use the current Row as the next one to match against
1255 *aSearchRow = *m_aRow;
1258 // Now remove any keys marked with a 0
1259 m_pKeySet->get().erase(::std::remove_if(m_pKeySet->get().begin(),m_pKeySet->get().end()
1260 ,::std::bind2nd(::std::equal_to<sal_Int32>(),0))
1261 ,m_pKeySet->get().end());
1265 } break;
1267 case OSQLStatementType::Update:
1268 case OSQLStatementType::Delete:
1269 case OSQLStatementType::Insert:
1270 break;
1271 default:
1272 m_pStatement->getOwnConnection()->throwSQLException( STR_STMT_TYPE_NOT_SUPPORTED, *this );
1273 break;
1277 void OResultSet::setBoundedColumns(const OValueRow& _rRow,
1278 const ::rtl::Reference<connectivity::OSQLColumns>& _rxColumns,
1279 const Reference<XIndexAccess>& _xNames,
1280 bool _bSetColumnMapping,
1281 const Reference<XDatabaseMetaData>& _xMetaData,
1282 ::std::vector<sal_Int32>& _rColMapping)
1284 ::comphelper::UStringMixEqual aCase(_xMetaData->supportsMixedCaseQuotedIdentifiers());
1286 Reference<XPropertySet> xTableColumn;
1287 OUString sTableColumnName, sSelectColumnRealName;
1289 const OUString sName = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME);
1290 const OUString sRealName = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME);
1292 ::std::vector< OUString> aColumnNames;
1293 aColumnNames.reserve(_rxColumns->get().size());
1294 OValueVector::Vector::iterator aRowIter = _rRow->get().begin()+1;
1295 for (sal_Int32 i=0; // the first column is the bookmark column
1296 aRowIter != _rRow->get().end();
1297 ++i, ++aRowIter
1302 // get the table column and its name
1303 _xNames->getByIndex(i) >>= xTableColumn;
1304 OSL_ENSURE(xTableColumn.is(), "OResultSet::setBoundedColumns: invalid table column!");
1305 if (xTableColumn.is())
1306 xTableColumn->getPropertyValue(sName) >>= sTableColumnName;
1307 else
1308 sTableColumnName.clear();
1310 // look if we have such a select column
1311 // TODO: would like to have a O(log n) search here ...
1312 sal_Int32 nColumnPos = 0;
1313 for ( OSQLColumns::Vector::const_iterator aIter = _rxColumns->get().begin();
1314 aIter != _rxColumns->get().end();
1315 ++aIter,++nColumnPos
1318 if ( nColumnPos < (sal_Int32)aColumnNames.size() )
1319 sSelectColumnRealName = aColumnNames[nColumnPos];
1320 else
1322 if((*aIter)->getPropertySetInfo()->hasPropertyByName(sRealName))
1323 (*aIter)->getPropertyValue(sRealName) >>= sSelectColumnRealName;
1324 else
1325 (*aIter)->getPropertyValue(sName) >>= sSelectColumnRealName;
1326 aColumnNames.push_back(sSelectColumnRealName);
1329 if (aCase(sTableColumnName, sSelectColumnRealName))
1331 if(_bSetColumnMapping)
1333 sal_Int32 nSelectColumnPos = static_cast<sal_Int32>(aIter - _rxColumns->get().begin() + 1);
1334 // the getXXX methods are 1-based ...
1335 sal_Int32 nTableColumnPos = i + 1;
1336 // get first table column is the bookmark column
1338 SAL_INFO(
1339 "connectivity.mork",
1340 "Set Col Mapping: " << nSelectColumnPos << " -> "
1341 << nTableColumnPos);
1342 _rColMapping[nSelectColumnPos] = nTableColumnPos;
1345 aRowIter->setBound(true);
1346 aRowIter->setTypeKind(DataType::VARCHAR);
1350 catch (Exception&)
1352 OSL_FAIL("OResultSet::setBoundedColumns: caught an Exception!");
1358 bool OResultSet::isCount() const
1360 return (m_pParseTree &&
1361 m_pParseTree->count() > 2 &&
1362 SQL_ISRULE(m_pParseTree->getChild(2),scalar_exp_commalist) &&
1363 SQL_ISRULE(m_pParseTree->getChild(2)->getChild(0),derived_column) &&
1364 SQL_ISRULE(m_pParseTree->getChild(2)->getChild(0)->getChild(0),general_set_fct) &&
1365 m_pParseTree->getChild(2)->getChild(0)->getChild(0)->count() == 4
1370 // Check for valid row in m_aQuery
1372 bool OResultSet::validRow( sal_uInt32 nRow)
1374 sal_Int32 nNumberOfRecords = m_aQueryHelper.getResultCount();
1376 while ( nRow > (sal_uInt32)nNumberOfRecords && !MQueryHelper::queryComplete() ) {
1377 #if OSL_DEBUG_LEVEL > 0
1378 OSL_TRACE("validRow: waiting...");
1379 #endif
1380 if (!m_aQueryHelper.checkRowAvailable( nRow ))
1382 SAL_INFO(
1383 "connectivity.mork",
1384 "validRow(" << nRow << "): return False");
1385 return false;
1388 if ( m_aQueryHelper.hadError() )
1390 m_pStatement->getOwnConnection()->throwSQLException( m_aQueryHelper.getError(), *this );
1393 nNumberOfRecords = m_aQueryHelper.getResultCount();
1396 if (( nRow == 0 ) ||
1397 ( nRow > (sal_uInt32)nNumberOfRecords && MQueryHelper::queryComplete()) ){
1398 SAL_INFO("connectivity.mork", "validRow(" << nRow << "): return False");
1399 return false;
1401 SAL_INFO("connectivity.mork", "validRow(" << nRow << "): return True");
1403 return true;
1405 bool OResultSet::fillKeySet(sal_Int32 nMaxCardNumber)
1407 impl_ensureKeySet();
1408 if (m_CurrentRowCount < nMaxCardNumber)
1410 sal_Int32 nKeyValue;
1411 if ( (sal_Int32)m_pKeySet->get().capacity() < nMaxCardNumber )
1412 m_pKeySet->get().reserve(nMaxCardNumber + 20 );
1414 for (nKeyValue = m_CurrentRowCount+1; nKeyValue <= nMaxCardNumber; nKeyValue ++)
1415 m_pKeySet->get().push_back( nKeyValue );
1416 m_CurrentRowCount = nMaxCardNumber;
1418 return true;
1421 sal_Int32 OResultSet::deletedCount()
1423 impl_ensureKeySet();
1424 return m_CurrentRowCount - static_cast<sal_Int32>(m_pKeySet->get().size());
1428 bool OResultSet::seekRow( eRowPosition pos, sal_Int32 nOffset )
1430 ResultSetEntryGuard aGuard( *this );
1431 if ( !m_pKeySet.is() )
1432 m_pStatement->getOwnConnection()->throwSQLException( STR_ILLEGAL_MOVEMENT, *this );
1434 sal_Int32 nNumberOfRecords = m_aQueryHelper.getResultCount();
1435 sal_Int32 nRetrievedRows = currentRowCount();
1436 sal_Int32 nCurPos = m_nRowPos;
1438 SAL_INFO("connectivity.mork", "nCurPos = " << nCurPos);
1439 switch( pos ) {
1440 case NEXT_POS:
1441 OSL_TRACE("seekRow: NEXT");
1442 nCurPos++;
1443 break;
1444 case PRIOR_POS:
1445 OSL_TRACE("seekRow: PRIOR");
1446 if ( nCurPos > 0 )
1447 nCurPos--;
1448 break;
1450 case FIRST_POS:
1451 OSL_TRACE("seekRow: FIRST");
1452 nCurPos = 1;
1453 break;
1455 case LAST_POS:
1456 OSL_TRACE("seekRow: LAST");
1457 nCurPos = nRetrievedRows;
1458 break;
1459 case ABSOLUTE_POS:
1460 SAL_INFO("connectivity.mork", "ABSOLUTE : " << nOffset);
1461 nCurPos = nOffset;
1462 break;
1463 case RELATIVE_POS:
1464 SAL_INFO("connectivity.mork", "RELATIVE : " << nOffset);
1465 nCurPos += sal_uInt32( nOffset );
1466 break;
1469 if ( nCurPos <= 0 ) {
1470 m_nRowPos = 0;
1471 SAL_INFO(
1472 "connectivity.mork", "return False, m_nRowPos = " << m_nRowPos);
1473 return false;
1475 sal_Int32 nCurCard;
1476 if ( nCurPos < (sal_Int32)m_pKeySet->get().size() ) //The requested row is exist in m_pKeySet, so we just use it
1478 nCurCard = (m_pKeySet->get())[nCurPos-1];
1480 else //The requested row has not been retrieved until now. We should get the right card for it.
1481 nCurCard = nCurPos + deletedCount();
1483 if ( nCurCard > nNumberOfRecords) {
1484 fillKeySet(nNumberOfRecords);
1485 m_nRowPos = static_cast<sal_uInt32>(m_pKeySet->get().size() + 1);
1486 SAL_INFO(
1487 "connectivity.mork", "return False, m_nRowPos = " << m_nRowPos);
1488 return false;
1490 //Insert new retrieved items for later use
1491 fillKeySet(nNumberOfRecords);
1492 m_nRowPos = (sal_uInt32)nCurPos;
1493 SAL_INFO("connectivity.mork", "return True, m_nRowPos = " << m_nRowPos);
1494 fetchCurrentRow();
1495 return true;
1498 void OResultSet::setColumnMapping(const ::std::vector<sal_Int32>& _aColumnMapping)
1500 m_aColMapping = _aColumnMapping;
1501 #if OSL_DEBUG_LEVEL > 0
1502 for ( size_t i = 0; i < m_aColMapping.size(); i++ )
1503 SAL_INFO(
1504 "connectivity.mork",
1505 "Set Mapped: " << i << " -> " << m_aColMapping[i]);
1506 #endif
1510 css::uno::Any OResultSet::getBookmark( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1512 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
1513 ResultSetEntryGuard aGuard( *this );
1514 if ( !fetchCurrentRow() ) {
1515 m_pStatement->getOwnConnection()->throwSQLException( STR_ERROR_GET_ROW, *this );
1518 OSL_ENSURE((!m_aRow->isDeleted()),"getBookmark called for deleted row");
1519 return makeAny((sal_Int32)(m_aRow->get())[0]);
1521 sal_Bool OResultSet::moveToBookmark( const css::uno::Any& bookmark ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1523 ResultSetEntryGuard aGuard( *this );
1524 SAL_INFO(
1525 "connectivity.mork", "bookmark = " << comphelper::getINT32(bookmark));
1526 sal_Int32 nCardNum = comphelper::getINT32(bookmark);
1527 m_nRowPos = getRowForCardNumber(nCardNum);
1528 fetchCurrentRow();
1529 return true;
1531 sal_Bool OResultSet::moveRelativeToBookmark( const css::uno::Any& bookmark, sal_Int32 rows ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1533 ResultSetEntryGuard aGuard( *this );
1534 SAL_INFO(
1535 "connectivity.mork",
1536 "bookmark = " << comphelper::getINT32(bookmark) << " rows= " << rows);
1537 sal_Int32 nCardNum = comphelper::getINT32(bookmark);
1538 m_nRowPos = getRowForCardNumber(nCardNum);
1539 return seekRow(RELATIVE_POS,rows );
1541 sal_Int32 OResultSet::compareBookmarks( const css::uno::Any& lhs, const css::uno::Any& rhs ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1543 ResultSetEntryGuard aGuard( *this );
1544 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
1545 sal_Int32 nFirst=0;
1546 sal_Int32 nSecond=0;
1547 sal_Int32 nResult=0;
1549 if ( !( lhs >>= nFirst ) || !( rhs >>= nSecond ) ) {
1550 m_pStatement->getOwnConnection()->throwSQLException( STR_INVALID_BOOKMARK, *this );
1553 if(nFirst < nSecond)
1554 nResult = CompareBookmark::LESS;
1555 else if(nFirst > nSecond)
1556 nResult = CompareBookmark::GREATER;
1557 else
1558 nResult = CompareBookmark::EQUAL;
1560 return nResult;
1562 sal_Bool OResultSet::hasOrderedBookmarks( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1564 ResultSetEntryGuard aGuard( *this );
1565 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
1566 return true;
1568 sal_Int32 OResultSet::hashBookmark( const css::uno::Any& bookmark ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1570 ResultSetEntryGuard aGuard( *this );
1571 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
1572 return comphelper::getINT32(bookmark);
1575 sal_Int32 OResultSet::getCurrentCardNumber()
1577 if ( ( m_nRowPos == 0 ) || !m_pKeySet.is() )
1578 return 0;
1579 if (m_pKeySet->get().size() < m_nRowPos)
1580 return 0;
1581 return (m_pKeySet->get())[m_nRowPos-1];
1583 void OResultSet::checkPendingUpdate() throw(SQLException, RuntimeException)
1585 OSL_FAIL( "OResultSet::checkPendingUpdate() not implemented" );
1587 OSL_TRACE("checkPendingUpdate, m_nRowPos = %u", m_nRowPos );
1588 const sal_Int32 nCurrentRow = getCurrentCardNumber();
1590 if ((m_nNewRow && nCurrentRow != m_nNewRow)
1591 || ( m_nUpdatedRow && m_nUpdatedRow != nCurrentRow))
1593 const OUString sError( m_pStatement->getOwnConnection()->getResources().getResourceStringWithSubstitution(
1594 STR_COMMIT_ROW,
1595 "$position$", OUString::valueOf(nCurrentRow)
1596 ) );
1597 ::dbtools::throwGenericSQLException(sError,*this);
1602 void OResultSet::updateValue(sal_Int32 columnIndex ,const ORowSetValue& x) throw(SQLException, RuntimeException)
1604 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
1605 ResultSetEntryGuard aGuard( *this );
1606 if ( !fetchCurrentRow() ) {
1607 m_pStatement->getOwnConnection()->throwSQLException( STR_ERROR_GET_ROW, *this );
1610 checkPendingUpdate();
1612 checkIndex(columnIndex );
1613 columnIndex = mapColumn(columnIndex);
1615 (m_aRow->get())[columnIndex].setBound(true);
1616 (m_aRow->get())[columnIndex] = x;
1617 m_nUpdatedRow = getCurrentCardNumber();
1618 // m_RowStates = m_RowStates | RowStates_Updated;
1622 void SAL_CALL OResultSet::updateNull( sal_Int32 columnIndex ) throw(SQLException, RuntimeException, std::exception)
1624 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
1625 ResultSetEntryGuard aGuard( *this );
1626 if ( !fetchCurrentRow() )
1627 m_pStatement->getOwnConnection()->throwSQLException( STR_ERROR_GET_ROW, *this );
1629 checkPendingUpdate();
1630 checkIndex(columnIndex );
1631 columnIndex = mapColumn(columnIndex);
1633 (m_aRow->get())[columnIndex].setBound(true);
1634 (m_aRow->get())[columnIndex].setNull();
1635 m_nUpdatedRow = getCurrentCardNumber();
1636 // m_RowStates = m_RowStates | RowStates_Updated;
1640 void SAL_CALL OResultSet::updateBoolean( sal_Int32 columnIndex, sal_Bool x ) throw(SQLException, RuntimeException, std::exception)
1642 updateValue(columnIndex, static_cast<bool>(x));
1645 void SAL_CALL OResultSet::updateByte( sal_Int32 columnIndex, sal_Int8 x ) throw(SQLException, RuntimeException, std::exception)
1647 updateValue(columnIndex,x);
1651 void SAL_CALL OResultSet::updateShort( sal_Int32 columnIndex, sal_Int16 x ) throw(SQLException, RuntimeException, std::exception)
1653 updateValue(columnIndex,x);
1656 void SAL_CALL OResultSet::updateInt( sal_Int32 columnIndex, sal_Int32 x ) throw(SQLException, RuntimeException, std::exception)
1658 updateValue(columnIndex,x);
1661 void SAL_CALL OResultSet::updateLong( sal_Int32 /*columnIndex*/, sal_Int64 /*x*/ ) throw(SQLException, RuntimeException, std::exception)
1663 ::dbtools::throwFeatureNotImplementedSQLException( "XRowUpdate::updateLong", *this );
1666 void SAL_CALL OResultSet::updateFloat( sal_Int32 columnIndex, float x ) throw(SQLException, RuntimeException, std::exception)
1668 updateValue(columnIndex,x);
1672 void SAL_CALL OResultSet::updateDouble( sal_Int32 columnIndex, double x ) throw(SQLException, RuntimeException, std::exception)
1674 updateValue(columnIndex,x);
1677 void SAL_CALL OResultSet::updateString( sal_Int32 columnIndex, const OUString& x ) throw(SQLException, RuntimeException, std::exception)
1679 updateValue(columnIndex,x);
1682 void SAL_CALL OResultSet::updateBytes( sal_Int32 columnIndex, const Sequence< sal_Int8 >& x ) throw(SQLException, RuntimeException, std::exception)
1684 updateValue(columnIndex,x);
1687 void SAL_CALL OResultSet::updateDate( sal_Int32 columnIndex, const css::util::Date& x ) throw(SQLException, RuntimeException, std::exception)
1689 updateValue(columnIndex,x);
1693 void SAL_CALL OResultSet::updateTime( sal_Int32 columnIndex, const css::util::Time& x ) throw(SQLException, RuntimeException, std::exception)
1695 updateValue(columnIndex,x);
1699 void SAL_CALL OResultSet::updateTimestamp( sal_Int32 columnIndex, const css::util::DateTime& x ) throw(SQLException, RuntimeException, std::exception)
1701 updateValue(columnIndex,x);
1705 void SAL_CALL OResultSet::updateBinaryStream( sal_Int32 columnIndex, const Reference< css::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
1707 ResultSetEntryGuard aGuard( *this );
1709 if(!x.is())
1710 ::dbtools::throwFunctionSequenceException(*this);
1712 Sequence<sal_Int8> aSeq;
1713 x->readBytes(aSeq,length);
1714 updateValue(columnIndex,aSeq);
1717 void SAL_CALL OResultSet::updateCharacterStream( sal_Int32 columnIndex, const Reference< css::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
1719 updateBinaryStream(columnIndex,x,length);
1722 void SAL_CALL OResultSet::updateObject( sal_Int32 columnIndex, const Any& x ) throw(SQLException, RuntimeException, std::exception)
1724 if (!::dbtools::implUpdateObject(this, columnIndex, x))
1726 const OUString sError( m_pStatement->getOwnConnection()->getResources().getResourceStringWithSubstitution(
1727 STR_COLUMN_NOT_UPDATEABLE,
1728 "$position$", OUString::number(columnIndex)
1729 ) );
1730 ::dbtools::throwGenericSQLException(sError,*this);
1731 } // if (!::dbtools::implUpdateObject(this, columnIndex, x))
1735 void SAL_CALL OResultSet::updateNumericObject( sal_Int32 columnIndex, const Any& x, sal_Int32 /*scale*/ ) throw(SQLException, RuntimeException, std::exception)
1737 if (!::dbtools::implUpdateObject(this, columnIndex, x))
1739 const OUString sError( m_pStatement->getOwnConnection()->getResources().getResourceStringWithSubstitution(
1740 STR_COLUMN_NOT_UPDATEABLE,
1741 "$position$", OUString::number(columnIndex)
1742 ) );
1743 ::dbtools::throwGenericSQLException(sError,*this);
1747 // XResultSetUpdate
1749 void SAL_CALL OResultSet::insertRow( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1751 ResultSetEntryGuard aGuard( *this );
1752 SAL_INFO("connectivity.mork", "in, m_nRowPos = " << m_nRowPos);
1753 // m_RowStates = RowStates_Inserted;
1754 updateRow();
1755 //m_aQueryHelper.setRowStates(getCurrentCardNumber(),m_RowStates);
1756 SAL_INFO("connectivity.mork", "out, m_nRowPos = " << m_nRowPos);
1759 void SAL_CALL OResultSet::updateRow( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1761 OSL_FAIL( "OResultSet::updateRow( ) not implemented" );
1764 void SAL_CALL OResultSet::deleteRow( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1766 OSL_FAIL( "OResultSet::deleteRow( ) not implemented" );
1769 void SAL_CALL OResultSet::cancelRowUpdates( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1771 OSL_FAIL( "OResultSet::cancelRowUpdates( ) not implemented" );
1774 void SAL_CALL OResultSet::moveToInsertRow( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1776 OSL_FAIL( "OResultSet::moveToInsertRow( ) not implemented" );
1779 void SAL_CALL OResultSet::moveToCurrentRow( ) throw(css::sdbc::SQLException, css::uno::RuntimeException, std::exception)
1781 ResultSetEntryGuard aGuard( *this );
1782 SAL_INFO("connectivity.mork", "m_nRowPos = " << m_nRowPos);
1783 if (rowInserted())
1785 m_nRowPos = 0;
1786 fetchCurrentRow();
1790 bool OResultSet::determineReadOnly()
1792 // OSL_FAIL( "OResultSet::determineReadOnly( ) not implemented" );
1794 if (m_bIsReadOnly == TRISTATE_INDET)
1796 m_bIsReadOnly = TRISTATE_TRUE;
1797 // OConnection* xConnection = static_cast<OConnection*>(m_pStatement->getConnection().get());
1798 // m_bIsReadOnly = !m_aQueryHelper.isWritable(xConnection) || m_bIsAlwaysFalseQuery;
1801 return m_bIsReadOnly != TRISTATE_FALSE;
1804 void OResultSet::setTable(OTable* _rTable)
1806 OSL_TRACE("In : setTable");
1807 m_pTable = _rTable;
1808 m_pTable->acquire();
1809 m_xTableColumns = m_pTable->getColumns();
1810 if(m_xTableColumns.is())
1811 m_aColumnNames = m_xTableColumns->getElementNames();
1812 OSL_TRACE("Out : setTable");
1815 void OResultSet::setOrderByColumns(const ::std::vector<sal_Int32>& _aColumnOrderBy)
1817 m_aOrderbyColumnNumber = _aColumnOrderBy;
1820 void OResultSet::setOrderByAscending(const ::std::vector<TAscendingOrder>& _aOrderbyAsc)
1822 m_aOrderbyAscending = _aOrderbyAsc;
1824 Sequence< sal_Int32 > SAL_CALL OResultSet::deleteRows( const Sequence< Any >& /*rows*/ ) throw(SQLException, RuntimeException, std::exception)
1826 ::dbtools::throwFeatureNotImplementedSQLException( "XDeleteRows::deleteRows", *this );
1827 return Sequence< sal_Int32 >();
1830 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */