1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_xmlhelp.hxx" 26 #include <ucbhelper/contentidentifier.hxx> 27 #include <com/sun/star/ucb/OpenMode.hpp> 28 #include <com/sun/star/uno/Reference.h> 29 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBBUTE_HPP_ 30 #include <com/sun/star/beans/PropertyAttribute.hpp> 31 #endif 32 #include <com/sun/star/ucb/ListActionType.hpp> 33 #include <com/sun/star/ucb/XSourceInitialization.hpp> 34 #include <ucbhelper/resultsetmetadata.hxx> 35 36 #include "resultsetbase.hxx" 37 38 using namespace chelp; 39 using namespace com::sun::star; 40 41 ResultSetBase::ResultSetBase( const uno::Reference< lang::XMultiServiceFactory >& xMSF, 42 const uno::Reference< ucb::XContentProvider >& xProvider, 43 sal_Int32 nOpenMode, 44 const uno::Sequence< beans::Property >& seq, 45 const uno::Sequence< ucb::NumberedSortingInfo >& seqSort ) 46 : m_xMSF( xMSF ), 47 m_xProvider( xProvider ), 48 m_nRow( -1 ), 49 m_nWasNull( true ), 50 m_nOpenMode( nOpenMode ), 51 m_bRowCountFinal( true ), 52 m_sProperty( seq ), 53 m_sSortingInfo( seqSort ), 54 m_pDisposeEventListeners( 0 ), 55 m_pRowCountListeners( 0 ), 56 m_pIsFinalListeners( 0 ) 57 { 58 } 59 60 ResultSetBase::~ResultSetBase() 61 { 62 delete m_pIsFinalListeners; 63 delete m_pRowCountListeners; 64 delete m_pDisposeEventListeners; 65 } 66 67 68 // XInterface 69 70 void SAL_CALL 71 ResultSetBase::acquire( 72 void ) 73 throw() 74 { 75 OWeakObject::acquire(); 76 } 77 78 79 void SAL_CALL 80 ResultSetBase::release( 81 void ) 82 throw() 83 { 84 OWeakObject::release(); 85 } 86 87 88 89 uno::Any SAL_CALL 90 ResultSetBase::queryInterface( 91 const uno::Type& rType ) 92 throw( uno::RuntimeException ) 93 { 94 uno::Any aRet = cppu::queryInterface( rType, 95 SAL_STATIC_CAST( lang::XComponent*, this), 96 SAL_STATIC_CAST( sdbc::XRow*, this), 97 SAL_STATIC_CAST( sdbc::XResultSet*, this), 98 SAL_STATIC_CAST( sdbc::XResultSetMetaDataSupplier*, this), 99 SAL_STATIC_CAST( beans::XPropertySet*, this ), 100 SAL_STATIC_CAST( ucb::XContentAccess*, this) ); 101 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); 102 } 103 104 105 106 // XComponent 107 108 109 void SAL_CALL 110 ResultSetBase::addEventListener( 111 const uno::Reference< lang::XEventListener >& Listener ) 112 throw( uno::RuntimeException ) 113 { 114 osl::MutexGuard aGuard( m_aMutex ); 115 116 if ( ! m_pDisposeEventListeners ) 117 m_pDisposeEventListeners = 118 new cppu::OInterfaceContainerHelper( m_aMutex ); 119 120 m_pDisposeEventListeners->addInterface( Listener ); 121 } 122 123 124 void SAL_CALL 125 ResultSetBase::removeEventListener( 126 const uno::Reference< lang::XEventListener >& Listener ) 127 throw( uno::RuntimeException ) 128 { 129 osl::MutexGuard aGuard( m_aMutex ); 130 131 if ( m_pDisposeEventListeners ) 132 m_pDisposeEventListeners->removeInterface( Listener ); 133 } 134 135 136 137 void SAL_CALL 138 ResultSetBase::dispose() 139 throw( uno::RuntimeException ) 140 { 141 osl::MutexGuard aGuard( m_aMutex ); 142 143 lang::EventObject aEvt; 144 aEvt.Source = static_cast< lang::XComponent * >( this ); 145 146 if ( m_pDisposeEventListeners && m_pDisposeEventListeners->getLength() ) 147 { 148 m_pDisposeEventListeners->disposeAndClear( aEvt ); 149 } 150 if( m_pRowCountListeners && m_pRowCountListeners->getLength() ) 151 { 152 m_pRowCountListeners->disposeAndClear( aEvt ); 153 } 154 if( m_pIsFinalListeners && m_pIsFinalListeners->getLength() ) 155 { 156 m_pIsFinalListeners->disposeAndClear( aEvt ); 157 } 158 } 159 160 161 162 // XResultSet 163 164 sal_Bool SAL_CALL 165 ResultSetBase::next( 166 void ) 167 throw( sdbc::SQLException, 168 uno::RuntimeException ) 169 { 170 sal_Bool test; 171 m_nRow++; 172 if( sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() ) 173 test = true; 174 else 175 test = false; 176 return test; 177 } 178 179 180 sal_Bool SAL_CALL 181 ResultSetBase::isBeforeFirst( 182 void ) 183 throw( sdbc::SQLException, 184 uno::RuntimeException ) 185 { 186 return m_nRow == -1; 187 } 188 189 190 sal_Bool SAL_CALL 191 ResultSetBase::isAfterLast( 192 void ) 193 throw( sdbc::SQLException, 194 uno::RuntimeException ) 195 { 196 return sal::static_int_cast<sal_uInt32>( m_nRow ) >= m_aItems.size(); // Cannot happen, if m_aFolder.isOpen() 197 } 198 199 200 sal_Bool SAL_CALL 201 ResultSetBase::isFirst( 202 void ) 203 throw( sdbc::SQLException, 204 uno::RuntimeException ) 205 { 206 return m_nRow == 0; 207 } 208 209 210 sal_Bool SAL_CALL 211 ResultSetBase::isLast( 212 void ) 213 throw( sdbc::SQLException, 214 uno::RuntimeException) 215 { 216 if( sal::static_int_cast<sal_uInt32>( m_nRow ) == m_aItems.size() - 1 ) 217 return true; 218 else 219 return false; 220 } 221 222 223 void SAL_CALL 224 ResultSetBase::beforeFirst( 225 void ) 226 throw( sdbc::SQLException, 227 uno::RuntimeException) 228 { 229 m_nRow = -1; 230 } 231 232 233 void SAL_CALL 234 ResultSetBase::afterLast( 235 void ) 236 throw( sdbc::SQLException, 237 uno::RuntimeException ) 238 { 239 m_nRow = m_aItems.size(); 240 } 241 242 243 sal_Bool SAL_CALL 244 ResultSetBase::first( 245 void ) 246 throw( sdbc::SQLException, 247 uno::RuntimeException) 248 { 249 m_nRow = -1; 250 return next(); 251 } 252 253 254 sal_Bool SAL_CALL 255 ResultSetBase::last( 256 void ) 257 throw( sdbc::SQLException, 258 uno::RuntimeException ) 259 { 260 m_nRow = m_aItems.size() - 1; 261 return true; 262 } 263 264 265 sal_Int32 SAL_CALL 266 ResultSetBase::getRow( 267 void ) 268 throw( sdbc::SQLException, 269 uno::RuntimeException) 270 { 271 // Test, whether behind last row 272 if( -1 == m_nRow || sal::static_int_cast<sal_uInt32>( m_nRow ) >= m_aItems.size() ) 273 return 0; 274 else 275 return m_nRow+1; 276 } 277 278 279 sal_Bool SAL_CALL ResultSetBase::absolute( sal_Int32 row ) 280 throw( sdbc::SQLException, uno::RuntimeException) 281 { 282 if( row >= 0 ) 283 m_nRow = row - 1; 284 else 285 { 286 last(); 287 m_nRow += ( row + 1 ); 288 if( m_nRow < -1 ) 289 m_nRow = -1; 290 } 291 292 return 0<= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size(); 293 } 294 295 296 297 298 sal_Bool SAL_CALL 299 ResultSetBase::relative( 300 sal_Int32 row ) 301 throw( sdbc::SQLException, 302 uno::RuntimeException) 303 { 304 if( isAfterLast() || isBeforeFirst() ) 305 throw sdbc::SQLException(); 306 307 if( row > 0 ) 308 while( row-- ) 309 next(); 310 else if( row < 0 ) 311 while( row++ && m_nRow > -1 ) 312 previous(); 313 314 return 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size(); 315 } 316 317 318 319 sal_Bool SAL_CALL 320 ResultSetBase::previous( 321 void ) 322 throw( sdbc::SQLException, 323 uno::RuntimeException) 324 { 325 if( sal::static_int_cast<sal_uInt32>( m_nRow ) > m_aItems.size() ) 326 m_nRow = m_aItems.size(); // Correct Handling of afterLast 327 if( 0 <= m_nRow ) -- m_nRow; 328 329 return 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size(); 330 } 331 332 333 void SAL_CALL 334 ResultSetBase::refreshRow( 335 void ) 336 throw( sdbc::SQLException, 337 uno::RuntimeException) 338 { 339 } 340 341 342 sal_Bool SAL_CALL 343 ResultSetBase::rowUpdated( 344 void ) 345 throw( sdbc::SQLException, 346 uno::RuntimeException ) 347 { 348 return false; 349 } 350 351 sal_Bool SAL_CALL 352 ResultSetBase::rowInserted( 353 void ) 354 throw( sdbc::SQLException, 355 uno::RuntimeException ) 356 { 357 return false; 358 } 359 360 sal_Bool SAL_CALL 361 ResultSetBase::rowDeleted( 362 void ) 363 throw( sdbc::SQLException, 364 uno::RuntimeException ) 365 { 366 return false; 367 } 368 369 370 uno::Reference< uno::XInterface > SAL_CALL 371 ResultSetBase::getStatement( 372 void ) 373 throw( sdbc::SQLException, 374 uno::RuntimeException ) 375 { 376 uno::Reference< uno::XInterface > test( 0 ); 377 return test; 378 } 379 380 381 // XCloseable 382 383 void SAL_CALL 384 ResultSetBase::close( 385 void ) 386 throw( sdbc::SQLException, 387 uno::RuntimeException) 388 { 389 } 390 391 392 rtl::OUString SAL_CALL 393 ResultSetBase::queryContentIdentifierString( 394 void ) 395 throw( uno::RuntimeException ) 396 { 397 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() ) 398 return m_aPath[m_nRow]; 399 else 400 return rtl::OUString(); 401 } 402 403 404 uno::Reference< ucb::XContentIdentifier > SAL_CALL 405 ResultSetBase::queryContentIdentifier( 406 void ) 407 throw( uno::RuntimeException ) 408 { 409 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() ) 410 { 411 rtl::OUString url = queryContentIdentifierString(); 412 if( ! m_aIdents[m_nRow].is() && url.getLength() ) 413 m_aIdents[m_nRow] = uno::Reference< ucb::XContentIdentifier >( 414 new ::ucbhelper::ContentIdentifier( m_xMSF,url ) ); 415 return m_aIdents[m_nRow]; 416 } 417 418 return uno::Reference< ucb::XContentIdentifier >(); 419 } 420 421 422 uno::Reference< ucb::XContent > SAL_CALL 423 ResultSetBase::queryContent( 424 void ) 425 throw( uno::RuntimeException ) 426 { 427 if( 0 <= m_nRow && sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aItems.size() ) 428 return m_xProvider->queryContent( queryContentIdentifier() ); 429 else 430 return uno::Reference< ucb::XContent >(); 431 } 432 433 434 435 class XPropertySetInfoImpl 436 : public cppu::OWeakObject, 437 public beans::XPropertySetInfo 438 { 439 public: 440 441 XPropertySetInfoImpl( const uno::Sequence< beans::Property >& aSeq ) 442 : m_aSeq( aSeq ) 443 { 444 } 445 446 void SAL_CALL acquire( void ) 447 throw() 448 { 449 OWeakObject::acquire(); 450 } 451 452 453 void SAL_CALL release( void ) 454 throw() 455 { 456 OWeakObject::release(); 457 } 458 459 uno::Any SAL_CALL queryInterface( const uno::Type& rType ) 460 throw( uno::RuntimeException ) 461 { 462 uno::Any aRet = cppu::queryInterface( rType, 463 SAL_STATIC_CAST( beans::XPropertySetInfo*, this ) ); 464 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); 465 } 466 467 uno::Sequence< beans::Property > SAL_CALL getProperties() 468 throw( uno::RuntimeException ) 469 { 470 return m_aSeq; 471 } 472 473 beans::Property SAL_CALL getPropertyByName( const ::rtl::OUString& aName ) 474 throw( beans::UnknownPropertyException, 475 uno::RuntimeException) 476 { 477 for( int i = 0; i < m_aSeq.getLength(); ++i ) 478 if( aName == m_aSeq[i].Name ) 479 return m_aSeq[i]; 480 throw beans::UnknownPropertyException(); 481 } 482 483 sal_Bool SAL_CALL hasPropertyByName( const ::rtl::OUString& Name ) 484 throw( uno::RuntimeException ) 485 { 486 for( int i = 0; i < m_aSeq.getLength(); ++i ) 487 if( Name == m_aSeq[i].Name ) 488 return true; 489 return false; 490 } 491 492 private: 493 494 uno::Sequence< beans::Property > m_aSeq; 495 }; 496 497 498 499 // XPropertySet 500 uno::Reference< beans::XPropertySetInfo > SAL_CALL 501 ResultSetBase::getPropertySetInfo() 502 throw( uno::RuntimeException) 503 { 504 uno::Sequence< beans::Property > seq(2); 505 seq[0].Name = rtl::OUString::createFromAscii( "RowCount" ); 506 seq[0].Handle = -1; 507 seq[0].Type = getCppuType( static_cast< sal_Int32* >(0) ); 508 seq[0].Attributes = beans::PropertyAttribute::READONLY; 509 510 seq[1].Name = rtl::OUString::createFromAscii( "IsRowCountFinal" ); 511 seq[1].Handle = -1; 512 seq[1].Type = getCppuType( static_cast< sal_Bool* >(0) ); 513 seq[1].Attributes = beans::PropertyAttribute::READONLY; 514 515 //t 516 return uno::Reference< beans::XPropertySetInfo > ( new XPropertySetInfoImpl( seq ) ); 517 } 518 519 520 521 void SAL_CALL ResultSetBase::setPropertyValue( 522 const rtl::OUString& aPropertyName, const uno::Any& aValue ) 523 throw( beans::UnknownPropertyException, 524 beans::PropertyVetoException, 525 lang::IllegalArgumentException, 526 lang::WrappedTargetException, 527 uno::RuntimeException) 528 { 529 (void)aValue; 530 531 if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) || 532 aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) ) 533 return; 534 535 throw beans::UnknownPropertyException(); 536 } 537 538 539 uno::Any SAL_CALL ResultSetBase::getPropertyValue( 540 const rtl::OUString& PropertyName ) 541 throw( beans::UnknownPropertyException, 542 lang::WrappedTargetException, 543 uno::RuntimeException) 544 { 545 if( PropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) ) 546 { 547 uno::Any aAny; 548 aAny <<= m_bRowCountFinal; 549 return aAny; 550 } 551 else if ( PropertyName == rtl::OUString::createFromAscii( "RowCount" ) ) 552 { 553 uno::Any aAny; 554 sal_Int32 count = m_aItems.size(); 555 aAny <<= count; 556 return aAny; 557 } 558 else 559 throw beans::UnknownPropertyException(); 560 } 561 562 563 void SAL_CALL ResultSetBase::addPropertyChangeListener( 564 const rtl::OUString& aPropertyName, 565 const uno::Reference< beans::XPropertyChangeListener >& xListener ) 566 throw( beans::UnknownPropertyException, 567 lang::WrappedTargetException, 568 uno::RuntimeException) 569 { 570 if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) ) 571 { 572 osl::MutexGuard aGuard( m_aMutex ); 573 if ( ! m_pIsFinalListeners ) 574 m_pIsFinalListeners = 575 new cppu::OInterfaceContainerHelper( m_aMutex ); 576 577 m_pIsFinalListeners->addInterface( xListener ); 578 } 579 else if ( aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) ) 580 { 581 osl::MutexGuard aGuard( m_aMutex ); 582 if ( ! m_pRowCountListeners ) 583 m_pRowCountListeners = 584 new cppu::OInterfaceContainerHelper( m_aMutex ); 585 m_pRowCountListeners->addInterface( xListener ); 586 } 587 else 588 throw beans::UnknownPropertyException(); 589 } 590 591 592 void SAL_CALL ResultSetBase::removePropertyChangeListener( 593 const rtl::OUString& aPropertyName, 594 const uno::Reference< beans::XPropertyChangeListener >& aListener ) 595 throw( beans::UnknownPropertyException, 596 lang::WrappedTargetException, 597 uno::RuntimeException) 598 { 599 if( aPropertyName == rtl::OUString::createFromAscii( "IsRowCountFinal" ) && 600 m_pIsFinalListeners ) 601 { 602 osl::MutexGuard aGuard( m_aMutex ); 603 m_pIsFinalListeners->removeInterface( aListener ); 604 } 605 else if ( aPropertyName == rtl::OUString::createFromAscii( "RowCount" ) && 606 m_pRowCountListeners ) 607 { 608 osl::MutexGuard aGuard( m_aMutex ); 609 m_pRowCountListeners->removeInterface( aListener ); 610 } 611 else 612 throw beans::UnknownPropertyException(); 613 } 614 615 616 void SAL_CALL ResultSetBase::addVetoableChangeListener( 617 const rtl::OUString& PropertyName, 618 const uno::Reference< beans::XVetoableChangeListener >& aListener ) 619 throw( beans::UnknownPropertyException, 620 lang::WrappedTargetException, 621 uno::RuntimeException) 622 { 623 (void)PropertyName; 624 (void)aListener; 625 } 626 627 628 void SAL_CALL ResultSetBase::removeVetoableChangeListener( 629 const rtl::OUString& PropertyName, 630 const uno::Reference< beans::XVetoableChangeListener >& aListener ) 631 throw( beans::UnknownPropertyException, 632 lang::WrappedTargetException, 633 uno::RuntimeException) 634 { 635 (void)PropertyName; 636 (void)aListener; 637 } 638 639 640 641 // XResultSetMetaDataSupplier 642 uno::Reference< sdbc::XResultSetMetaData > SAL_CALL 643 ResultSetBase::getMetaData( 644 void ) 645 throw( sdbc::SQLException, 646 uno::RuntimeException ) 647 { 648 ::ucbhelper::ResultSetMetaData* p = 649 new ::ucbhelper::ResultSetMetaData( 650 m_xMSF, m_sProperty ); 651 return uno::Reference< sdbc::XResultSetMetaData >( p ); 652 } 653