1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
24 #include "connectivity/sdbcx/VColumn.hxx"
25 #include "file/FResultSet.hxx"
26 #include "file/FResultSetMetaData.hxx"
27 #include <com/sun/star/sdbc/DataType.hpp>
28 #include <com/sun/star/sdbc/ColumnValue.hpp>
29 #include <comphelper/property.hxx>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/beans/PropertyAttribute.hpp>
32 #include <com/sun/star/container/XIndexAccess.hpp>
33 #include <comphelper/sequence.hxx>
34 #include <cppuhelper/typeprovider.hxx>
35 #include "connectivity/dbconversion.hxx"
36 #include "connectivity/dbtools.hxx"
37 #include <cppuhelper/propshlp.hxx>
39 #include <tools/debug.hxx>
40 #include <com/sun/star/sdbc/ResultSetType.hpp>
41 #include <com/sun/star/sdbc/FetchDirection.hpp>
42 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
43 #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
46 #include <comphelper/extract.hxx>
47 #include "connectivity/dbexception.hxx"
48 #include <comphelper/types.hxx>
49 #include "resource/file_res.hrc"
50 #include "resource/sharedresources.hxx"
53 using namespace ::comphelper
;
54 using namespace connectivity
;
55 using namespace connectivity::file
;
56 using namespace ::cppu
;
57 using namespace dbtools
;
58 using namespace com::sun::star::uno
;
59 using namespace com::sun::star::lang
;
60 using namespace com::sun::star::beans
;
61 using namespace com::sun::star::sdbc
;
62 using namespace com::sun::star::sdbcx
;
63 using namespace com::sun::star::container
;
67 void lcl_throwError(sal_uInt16 _nErrorId
,const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _xContext
)
69 ::connectivity::SharedResources aResources
;
70 const OUString sMessage
= aResources
.getResourceString(_nErrorId
);
71 ::dbtools::throwGenericSQLException(sMessage
,_xContext
);
75 IMPLEMENT_SERVICE_INFO(OResultSet
,"com.sun.star.sdbcx.drivers.file.ResultSet","com.sun.star.sdbc.ResultSet");
77 OResultSet::OResultSet(OStatement_Base
* pStmt
,OSQLParseTreeIterator
& _aSQLIterator
) : OResultSet_BASE(m_aMutex
)
78 ,::comphelper::OPropertyContainer(OResultSet_BASE::rBHelper
)
79 ,m_aAssignValues(NULL
)
80 ,m_pEvaluationKeySet(NULL
)
81 ,m_aSkipDeletedSet(this)
85 ,m_pParseTree(pStmt
->getParseTree())
87 ,m_aSQLIterator(_aSQLIterator
)
89 ,m_nResultSetType(ResultSetType::SCROLL_INSENSITIVE
)
90 ,m_nFetchDirection(FetchDirection::FORWARD
)
91 ,m_nResultSetConcurrency(ResultSetConcurrency::UPDATABLE
)
94 ,m_xDBMetaData(pStmt
->getOwnConnection()->getMetaData())
95 ,m_nTextEncoding(pStmt
->getOwnConnection()->getTextEncoding())
98 ,m_nLastVisitedPos(-1)
99 ,m_nRowCountResult(-1)
103 ,m_bLastRecord(false)
105 ,m_bRowUpdated(false)
106 ,m_bRowInserted(false)
107 ,m_bRowDeleted(false)
108 ,m_bShowDeleted(pStmt
->getOwnConnection()->showDeleted())
111 osl_atomic_increment( &m_refCount
);
112 m_bIsCount
= (m_pParseTree
&&
113 m_pParseTree
->count() > 2 &&
114 SQL_ISRULE(m_pParseTree
->getChild(2),scalar_exp_commalist
) &&
115 SQL_ISRULE(m_pParseTree
->getChild(2)->getChild(0),derived_column
) &&
116 SQL_ISRULE(m_pParseTree
->getChild(2)->getChild(0)->getChild(0),general_set_fct
) &&
117 m_pParseTree
->getChild(2)->getChild(0)->getChild(0)->count() == 4
120 m_nResultSetConcurrency
= isCount() ? ResultSetConcurrency::READ_ONLY
: ResultSetConcurrency::UPDATABLE
;
122 m_aSkipDeletedSet
.SetDeletedVisible(m_bShowDeleted
);
123 osl_atomic_decrement( &m_refCount
);
127 OResultSet::~OResultSet()
129 osl_atomic_increment( &m_refCount
);
133 void OResultSet::construct()
135 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE
), PROPERTY_ID_FETCHSIZE
, 0,&m_nFetchSize
, ::cppu::UnoType
<sal_Int32
>::get());
136 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE
), PROPERTY_ID_RESULTSETTYPE
, PropertyAttribute::READONLY
,&m_nResultSetType
, ::cppu::UnoType
<sal_Int32
>::get());
137 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION
), PROPERTY_ID_FETCHDIRECTION
, 0,&m_nFetchDirection
, ::cppu::UnoType
<sal_Int32
>::get());
138 registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY
), PROPERTY_ID_RESULTSETCONCURRENCY
,PropertyAttribute::READONLY
,&m_nResultSetConcurrency
, ::cppu::UnoType
<sal_Int32
>::get());
141 void OResultSet::disposing(void)
143 OPropertySetHelper::disposing();
145 ::osl::MutexGuard
aGuard(m_aMutex
);
146 m_xStatement
.clear();
151 m_xParamColumns
= NULL
;
154 Reference
<XComponent
> xComp
= m_pTable
;
156 xComp
->removeEventListener(this);
164 DELETEZ(m_pSortIndex
);
166 if(m_aInsertRow
.is())
167 m_aInsertRow
->get().clear();
169 m_aSkipDeletedSet
.clear();
172 Any SAL_CALL
OResultSet::queryInterface( const Type
& rType
) throw(RuntimeException
, std::exception
)
174 Any aRet
= OPropertySetHelper::queryInterface(rType
);
175 return aRet
.hasValue() ? aRet
: OResultSet_BASE::queryInterface(rType
);
178 Sequence
< Type
> SAL_CALL
OResultSet::getTypes( ) throw(RuntimeException
, std::exception
)
180 ::osl::MutexGuard
aGuard( m_aMutex
);
182 OTypeCollection
aTypes( ::getCppuType( (const Reference
< ::com::sun::star::beans::XMultiPropertySet
> *)0 ),
183 ::getCppuType( (const Reference
< ::com::sun::star::beans::XPropertySet
> *)0 ),
184 ::getCppuType( (const Reference
< ::com::sun::star::beans::XPropertySet
> *)0 ));
186 return ::comphelper::concatSequences(aTypes
.getTypes(),OResultSet_BASE::getTypes());
190 sal_Int32 SAL_CALL
OResultSet::findColumn( const OUString
& columnName
) throw(SQLException
, RuntimeException
, std::exception
)
192 ::osl::MutexGuard
aGuard( m_aMutex
);
193 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
196 Reference
< XResultSetMetaData
> xMeta
= getMetaData();
197 sal_Int32 nLen
= xMeta
->getColumnCount();
201 if(xMeta
->isCaseSensitive(i
) ? columnName
== xMeta
->getColumnName(i
) :
202 columnName
.equalsIgnoreAsciiCase(xMeta
->getColumnName(i
)))
206 ::dbtools::throwInvalidColumnException( columnName
, *this );
208 return 0; // Never reached
211 const ORowSetValue
& OResultSet::getValue(sal_Int32 columnIndex
) throw(::com::sun::star::sdbc::SQLException
)
213 ::osl::MutexGuard
aGuard( m_aMutex
);
214 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
216 checkIndex(columnIndex
);
219 m_bWasNull
= (m_aSelectRow
->get())[columnIndex
]->getValue().isNull();
220 return *(m_aSelectRow
->get())[columnIndex
];
223 void OResultSet::checkIndex(sal_Int32 columnIndex
) throw(::com::sun::star::sdbc::SQLException
)
225 if ( columnIndex
<= 0
226 || columnIndex
>= m_nColumnCount
)
227 ::dbtools::throwInvalidIndexException(*this);
230 Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
OResultSet::getBinaryStream( sal_Int32
/*columnIndex*/ ) throw(SQLException
, RuntimeException
, std::exception
)
235 Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
OResultSet::getCharacterStream( sal_Int32
/*columnIndex*/ ) throw(SQLException
, RuntimeException
, std::exception
)
241 sal_Bool SAL_CALL
OResultSet::getBoolean( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
243 return getValue(columnIndex
);
247 sal_Int8 SAL_CALL
OResultSet::getByte( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
249 return getValue(columnIndex
);
253 Sequence
< sal_Int8
> SAL_CALL
OResultSet::getBytes( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
255 return getValue(columnIndex
);
259 ::com::sun::star::util::Date SAL_CALL
OResultSet::getDate( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
261 return getValue(columnIndex
);
265 double SAL_CALL
OResultSet::getDouble( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
267 return getValue(columnIndex
);
271 float SAL_CALL
OResultSet::getFloat( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
273 return getValue(columnIndex
);
277 sal_Int32 SAL_CALL
OResultSet::getInt( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
279 return getValue(columnIndex
);
283 sal_Int32 SAL_CALL
OResultSet::getRow( ) throw(SQLException
, RuntimeException
, std::exception
)
285 ::osl::MutexGuard
aGuard( m_aMutex
);
286 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
288 OSL_ENSURE((m_bShowDeleted
|| !m_aRow
->isDeleted()),"getRow called for deleted row");
290 return m_aSkipDeletedSet
.getMappedPosition((m_aRow
->get())[0]->getValue());
294 sal_Int64 SAL_CALL
OResultSet::getLong( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
296 return getValue(columnIndex
);
300 Reference
< XResultSetMetaData
> SAL_CALL
OResultSet::getMetaData( ) throw(SQLException
, RuntimeException
, std::exception
)
302 ::osl::MutexGuard
aGuard( m_aMutex
);
303 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
306 if(!m_xMetaData
.is())
307 m_xMetaData
= new OResultSetMetaData(m_xColumns
,m_aSQLIterator
.getTables().begin()->first
,m_pTable
);
311 Reference
< XArray
> SAL_CALL
OResultSet::getArray( sal_Int32
/*columnIndex*/ ) throw(SQLException
, RuntimeException
, std::exception
)
318 Reference
< XClob
> SAL_CALL
OResultSet::getClob( sal_Int32
/*columnIndex*/ ) throw(SQLException
, RuntimeException
, std::exception
)
323 Reference
< XBlob
> SAL_CALL
OResultSet::getBlob( sal_Int32
/*columnIndex*/ ) throw(SQLException
, RuntimeException
, std::exception
)
329 Reference
< XRef
> SAL_CALL
OResultSet::getRef( sal_Int32
/*columnIndex*/ ) throw(SQLException
, RuntimeException
, std::exception
)
335 Any SAL_CALL
OResultSet::getObject( sal_Int32 columnIndex
, const Reference
< ::com::sun::star::container::XNameAccess
>& /*typeMap*/ ) throw(SQLException
, RuntimeException
, std::exception
)
337 return getValue(columnIndex
).makeAny();
341 sal_Int16 SAL_CALL
OResultSet::getShort( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
343 return getValue(columnIndex
);
346 OUString SAL_CALL
OResultSet::getString( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
348 return getValue(columnIndex
);
351 ::com::sun::star::util::Time SAL_CALL
OResultSet::getTime( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
353 return getValue(columnIndex
);
356 ::com::sun::star::util::DateTime SAL_CALL
OResultSet::getTimestamp( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
358 return getValue(columnIndex
);
362 sal_Bool SAL_CALL
OResultSet::isAfterLast( ) throw(SQLException
, RuntimeException
, std::exception
)
364 ::osl::MutexGuard
aGuard( m_aMutex
);
365 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
368 return m_nRowPos
== sal_Int32(m_pFileSet
->get().size());
371 sal_Bool SAL_CALL
OResultSet::isFirst( ) throw(SQLException
, RuntimeException
, std::exception
)
373 ::osl::MutexGuard
aGuard( m_aMutex
);
374 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
377 return m_nRowPos
== 0;
380 sal_Bool SAL_CALL
OResultSet::isLast( ) throw(SQLException
, RuntimeException
, std::exception
)
382 ::osl::MutexGuard
aGuard( m_aMutex
);
383 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
386 return m_nRowPos
== sal_Int32(m_pFileSet
->get().size() - 1);
389 void SAL_CALL
OResultSet::beforeFirst( ) throw(SQLException
, RuntimeException
, std::exception
)
391 ::osl::MutexGuard
aGuard( m_aMutex
);
392 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
399 void SAL_CALL
OResultSet::afterLast( ) throw(SQLException
, RuntimeException
, std::exception
)
401 ::osl::MutexGuard
aGuard( m_aMutex
);
402 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
411 void SAL_CALL
OResultSet::close( ) throw(SQLException
, RuntimeException
, std::exception
)
417 sal_Bool SAL_CALL
OResultSet::first( ) throw(SQLException
, RuntimeException
, std::exception
)
419 ::osl::MutexGuard
aGuard( m_aMutex
);
420 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
421 return m_pTable
? m_aSkipDeletedSet
.skipDeleted(IResultSetHelper::FIRST
,1,true) : sal_False
;
425 sal_Bool SAL_CALL
OResultSet::last( ) throw(SQLException
, RuntimeException
, std::exception
)
427 // here I know definitely that I stand on the last record
428 ::osl::MutexGuard
aGuard( m_aMutex
);
429 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
430 return m_pTable
? m_aSkipDeletedSet
.skipDeleted(IResultSetHelper::LAST
,1,true) : sal_False
;
433 sal_Bool SAL_CALL
OResultSet::absolute( sal_Int32 row
) throw(SQLException
, RuntimeException
, std::exception
)
435 ::osl::MutexGuard
aGuard( m_aMutex
);
436 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
437 return m_pTable
? m_aSkipDeletedSet
.skipDeleted(IResultSetHelper::ABSOLUTE
,row
,true) : sal_False
;
440 sal_Bool SAL_CALL
OResultSet::relative( sal_Int32 row
) throw(SQLException
, RuntimeException
, std::exception
)
442 ::osl::MutexGuard
aGuard( m_aMutex
);
443 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
444 return m_pTable
? m_aSkipDeletedSet
.skipDeleted(IResultSetHelper::RELATIVE
,row
,true) : sal_False
;
447 sal_Bool SAL_CALL
OResultSet::previous( ) throw(SQLException
, RuntimeException
, std::exception
)
449 ::osl::MutexGuard
aGuard( m_aMutex
);
450 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
451 return m_pTable
? m_aSkipDeletedSet
.skipDeleted(IResultSetHelper::PRIOR
,0,true) : sal_False
;
454 Reference
< XInterface
> SAL_CALL
OResultSet::getStatement( ) throw(SQLException
, RuntimeException
, std::exception
)
456 ::osl::MutexGuard
aGuard( m_aMutex
);
457 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
464 sal_Bool SAL_CALL
OResultSet::rowDeleted( ) throw(SQLException
, RuntimeException
, std::exception
)
466 ::osl::MutexGuard
aGuard( m_aMutex
);
467 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
470 return m_bRowDeleted
;
473 sal_Bool SAL_CALL
OResultSet::rowInserted( ) throw(SQLException
, RuntimeException
, std::exception
)
474 { ::osl::MutexGuard
aGuard( m_aMutex
);
475 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
478 return m_bRowInserted
;
481 sal_Bool SAL_CALL
OResultSet::rowUpdated( ) throw(SQLException
, RuntimeException
, std::exception
)
483 ::osl::MutexGuard
aGuard( m_aMutex
);
484 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
487 return m_bRowUpdated
;
491 sal_Bool SAL_CALL
OResultSet::isBeforeFirst( ) throw(SQLException
, RuntimeException
, std::exception
)
493 ::osl::MutexGuard
aGuard( m_aMutex
);
494 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
497 return m_nRowPos
== -1;
500 // sal_Bool OResultSet::evaluate()
502 // OSL_ENSURE(m_pSQLAnalyzer,"OResultSet::evaluate: Analyzer isn't set!");
503 // sal_Bool bRet = sal_True;
504 // while(!m_pSQLAnalyzer->evaluateRestriction())
506 // if(m_pEvaluationKeySet)
508 // if(m_aEvaluateIter == m_pEvaluationKeySet->end())
510 // bRet = m_pTable->seekRow(IResultSetHelper::BOOKMARK,(*m_aEvaluateIter),m_nRowPos);
511 // ++m_aEvaluateIter;
514 // bRet = m_pTable->seekRow(IResultSetHelper::NEXT,1,m_nRowPos);
517 // if(m_pEvaluationKeySet)
519 // bRet = m_pTable->fetchRow(m_aEvaluateRow,*(m_pTable->getTableColumns()),sal_True,sal_True);
524 // bRet = m_pTable->fetchRow(m_aRow,*m_xColumns,sal_False,sal_True);
532 sal_Bool SAL_CALL
OResultSet::next( ) throw(SQLException
, RuntimeException
, std::exception
)
534 ::osl::MutexGuard
aGuard( m_aMutex
);
535 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
537 return m_pTable
? m_aSkipDeletedSet
.skipDeleted(IResultSetHelper::NEXT
,1,true) : sal_False
;
541 sal_Bool SAL_CALL
OResultSet::wasNull( ) throw(SQLException
, RuntimeException
, std::exception
)
543 ::osl::MutexGuard
aGuard( m_aMutex
);
544 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
550 void SAL_CALL
OResultSet::cancel( ) throw(RuntimeException
, std::exception
)
554 void SAL_CALL
OResultSet::clearWarnings( ) throw(SQLException
, RuntimeException
, std::exception
)
558 Any SAL_CALL
OResultSet::getWarnings( ) throw(SQLException
, RuntimeException
, std::exception
)
563 void SAL_CALL
OResultSet::insertRow( ) throw(SQLException
, RuntimeException
, std::exception
)
565 ::osl::MutexGuard
aGuard( m_aMutex
);
566 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
569 if(!m_bInserted
|| !m_pTable
)
570 throwFunctionSequenceException(*this);
572 // we know that we append new rows at the end
573 // so we have to know where the end is
574 m_aSkipDeletedSet
.skipDeleted(IResultSetHelper::LAST
,1,false);
575 m_bRowInserted
= m_pTable
->InsertRow(*m_aInsertRow
, true, m_xColsIdx
);
576 if(m_bRowInserted
&& m_pFileSet
.is())
578 sal_Int32 nPos
= (m_aInsertRow
->get())[0]->getValue();
579 m_pFileSet
->get().push_back(nPos
);
580 *(m_aInsertRow
->get())[0] = sal_Int32(m_pFileSet
->get().size());
583 m_aSkipDeletedSet
.insertNewPosition((m_aRow
->get())[0]->getValue());
587 void SAL_CALL
OResultSet::updateRow( ) throw(SQLException
, RuntimeException
, std::exception
)
589 ::osl::MutexGuard
aGuard( m_aMutex
);
590 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
592 if(!m_pTable
|| m_pTable
->isReadOnly())
593 lcl_throwError(STR_TABLE_READONLY
,*this);
595 m_bRowUpdated
= m_pTable
->UpdateRow(*m_aInsertRow
, m_aRow
,m_xColsIdx
);
596 *(m_aInsertRow
->get())[0] = (sal_Int32
)(m_aRow
->get())[0]->getValue();
601 void SAL_CALL
OResultSet::deleteRow() throw(SQLException
, RuntimeException
, std::exception
)
603 ::osl::MutexGuard
aGuard( m_aMutex
);
604 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
607 if(!m_pTable
|| m_pTable
->isReadOnly())
608 lcl_throwError(STR_TABLE_READONLY
,*this);
610 lcl_throwError(STR_DELETE_ROW
,*this);
611 if(m_aRow
->isDeleted())
612 lcl_throwError(STR_ROW_ALREADY_DELETED
,*this);
614 sal_Int32 nPos
= (sal_Int32
)(m_aRow
->get())[0]->getValue();
615 m_bRowDeleted
= m_pTable
->DeleteRow(*m_xColumns
);
616 if(m_bRowDeleted
&& m_pFileSet
.is())
618 m_aRow
->setDeleted(true);
619 // don't touch the m_pFileSet member here
620 m_aSkipDeletedSet
.deletePosition(nPos
);
624 void SAL_CALL
OResultSet::cancelRowUpdates( ) throw(SQLException
, RuntimeException
, std::exception
)
626 ::osl::MutexGuard
aGuard( m_aMutex
);
627 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
631 m_bRowUpdated
= false;
632 m_bRowInserted
= false;
633 m_bRowDeleted
= false;
635 if(m_aInsertRow
.is())
637 OValueRefVector::Vector::iterator aIter
= m_aInsertRow
->get().begin()+1;
638 for(;aIter
!= m_aInsertRow
->get().end();++aIter
)
640 (*aIter
)->setBound(false);
647 void SAL_CALL
OResultSet::moveToInsertRow( ) throw(SQLException
, RuntimeException
, std::exception
)
649 ::osl::MutexGuard
aGuard( m_aMutex
);
650 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
652 if(!m_pTable
|| m_pTable
->isReadOnly())
653 lcl_throwError(STR_TABLE_READONLY
,*this);
657 OValueRefVector::Vector::iterator aIter
= m_aInsertRow
->get().begin()+1;
658 for(;aIter
!= m_aInsertRow
->get().end();++aIter
)
660 (*aIter
)->setBound(false);
666 void SAL_CALL
OResultSet::moveToCurrentRow( ) throw(SQLException
, RuntimeException
, std::exception
)
670 void OResultSet::updateValue(sal_Int32 columnIndex
,const ORowSetValue
& x
) throw(SQLException
, RuntimeException
)
672 ::osl::MutexGuard
aGuard( m_aMutex
);
673 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
675 checkIndex(columnIndex
);
676 columnIndex
= mapColumn(columnIndex
);
678 (m_aInsertRow
->get())[columnIndex
]->setBound(true);
679 *(m_aInsertRow
->get())[columnIndex
] = x
;
683 void SAL_CALL
OResultSet::updateNull( sal_Int32 columnIndex
) throw(SQLException
, RuntimeException
, std::exception
)
686 updateValue(columnIndex
,aEmpty
);
690 void SAL_CALL
OResultSet::updateBoolean( sal_Int32 columnIndex
, sal_Bool x
) throw(SQLException
, RuntimeException
, std::exception
)
692 updateValue(columnIndex
, static_cast<bool>(x
));
695 void SAL_CALL
OResultSet::updateByte( sal_Int32 columnIndex
, sal_Int8 x
) throw(SQLException
, RuntimeException
, std::exception
)
697 updateValue(columnIndex
,x
);
701 void SAL_CALL
OResultSet::updateShort( sal_Int32 columnIndex
, sal_Int16 x
) throw(SQLException
, RuntimeException
, std::exception
)
703 updateValue(columnIndex
,x
);
706 void SAL_CALL
OResultSet::updateInt( sal_Int32 columnIndex
, sal_Int32 x
) throw(SQLException
, RuntimeException
, std::exception
)
708 updateValue(columnIndex
,x
);
711 void SAL_CALL
OResultSet::updateLong( sal_Int32
/*columnIndex*/, sal_Int64
/*x*/ ) throw(SQLException
, RuntimeException
, std::exception
)
713 ::dbtools::throwFeatureNotImplementedException( "XRowUpdate::updateLong", *this );
716 void SAL_CALL
OResultSet::updateFloat( sal_Int32 columnIndex
, float x
) throw(SQLException
, RuntimeException
, std::exception
)
718 updateValue(columnIndex
,x
);
722 void SAL_CALL
OResultSet::updateDouble( sal_Int32 columnIndex
, double x
) throw(SQLException
, RuntimeException
, std::exception
)
724 updateValue(columnIndex
,x
);
727 void SAL_CALL
OResultSet::updateString( sal_Int32 columnIndex
, const OUString
& x
) throw(SQLException
, RuntimeException
, std::exception
)
729 updateValue(columnIndex
,x
);
732 void SAL_CALL
OResultSet::updateBytes( sal_Int32 columnIndex
, const Sequence
< sal_Int8
>& x
) throw(SQLException
, RuntimeException
, std::exception
)
734 updateValue(columnIndex
,x
);
737 void SAL_CALL
OResultSet::updateDate( sal_Int32 columnIndex
, const ::com::sun::star::util::Date
& x
) throw(SQLException
, RuntimeException
, std::exception
)
739 updateValue(columnIndex
,x
);
743 void SAL_CALL
OResultSet::updateTime( sal_Int32 columnIndex
, const ::com::sun::star::util::Time
& x
) throw(SQLException
, RuntimeException
, std::exception
)
745 updateValue(columnIndex
,x
);
749 void SAL_CALL
OResultSet::updateTimestamp( sal_Int32 columnIndex
, const ::com::sun::star::util::DateTime
& x
) throw(SQLException
, RuntimeException
, std::exception
)
751 updateValue(columnIndex
,x
);
755 void SAL_CALL
OResultSet::updateBinaryStream( sal_Int32 columnIndex
, const Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
) throw(SQLException
, RuntimeException
, std::exception
)
757 ::osl::MutexGuard
aGuard( m_aMutex
);
758 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
761 ::dbtools::throwFunctionSequenceException(*this);
763 Sequence
<sal_Int8
> aSeq
;
764 x
->readBytes(aSeq
,length
);
765 updateValue(columnIndex
,aSeq
);
768 void SAL_CALL
OResultSet::updateCharacterStream( sal_Int32 columnIndex
, const Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
) throw(SQLException
, RuntimeException
, std::exception
)
770 updateBinaryStream(columnIndex
,x
,length
);
773 void SAL_CALL
OResultSet::refreshRow( ) throw(SQLException
, RuntimeException
, std::exception
)
775 ::osl::MutexGuard
aGuard( m_aMutex
);
776 checkDisposed(OResultSet_BASE::rBHelper
.bDisposed
);
779 void SAL_CALL
OResultSet::updateObject( sal_Int32 columnIndex
, const Any
& x
) throw(SQLException
, RuntimeException
, std::exception
)
781 if (!::dbtools::implUpdateObject(this, columnIndex
, x
))
782 throw SQLException();
786 void SAL_CALL
OResultSet::updateNumericObject( sal_Int32 columnIndex
, const Any
& x
, sal_Int32
/*scale*/ ) throw(SQLException
, RuntimeException
, std::exception
)
788 if (!::dbtools::implUpdateObject(this, columnIndex
, x
))
789 throw SQLException();
792 IPropertyArrayHelper
* OResultSet::createArrayHelper( ) const
794 Sequence
< Property
> aProps
;
795 describeProperties(aProps
);
796 return new ::cppu::OPropertyArrayHelper(aProps
);
799 IPropertyArrayHelper
& OResultSet::getInfoHelper()
801 return *const_cast<OResultSet
*>(this)->getArrayHelper();
805 bool OResultSet::ExecuteRow(IResultSetHelper::Movement eFirstCursorPosition
,
806 sal_Int32 nFirstOffset
,
810 OSL_ENSURE(m_pSQLAnalyzer
,"OResultSet::ExecuteRow: Analyzer isn't set!");
812 // For further Fetch-Operations this information may possibly be changed ...
813 IResultSetHelper::Movement eCursorPosition
= eFirstCursorPosition
;
814 sal_Int32 nOffset
= nFirstOffset
;
819 const OSQLColumns
& rTableCols
= *(m_pTable
->getTableColumns());
820 bool bHasRestriction
= m_pSQLAnalyzer
->hasRestriction();
823 // protect from reading over the end when someboby is inserting while we are reading
824 // this method works only for dBase at the moment !!!!
825 if (eCursorPosition
== IResultSetHelper::NEXT
&& m_nFilePos
== m_nLastVisitedPos
)
830 if (!m_pTable
|| !m_pTable
->seekRow(eCursorPosition
, nOffset
, m_nFilePos
))
835 if (!bEvaluate
) // If no evaluation runs, then just fill the results-row
837 m_pTable
->fetchRow(m_aRow
,rTableCols
, true,bRetrieveData
);
841 m_pTable
->fetchRow(m_aEvaluateRow
, rTableCols
, true,bRetrieveData
|| bHasRestriction
);
843 if ( ( !m_bShowDeleted
844 && m_aEvaluateRow
->isDeleted()
847 && !m_pSQLAnalyzer
->evaluateRestriction()
850 { // Evaluate the next record
851 // delete current row in Keyset
852 if (m_pEvaluationKeySet
)
855 if (m_pEvaluationKeySet
->end() != m_aEvaluateIter
)
856 nOffset
= (*m_aEvaluateIter
);
862 else if (m_pFileSet
.is())
864 OSL_ENSURE(eCursorPosition
== IResultSetHelper::NEXT
, "Falsche CursorPosition!");
865 eCursorPosition
= IResultSetHelper::NEXT
;
868 else if (eCursorPosition
== IResultSetHelper::FIRST
||
869 eCursorPosition
== IResultSetHelper::NEXT
||
870 eCursorPosition
== IResultSetHelper::ABSOLUTE
)
872 eCursorPosition
= IResultSetHelper::NEXT
;
875 else if (eCursorPosition
== IResultSetHelper::LAST
||
876 eCursorPosition
== IResultSetHelper::PRIOR
)
878 eCursorPosition
= IResultSetHelper::PRIOR
;
881 else if (eCursorPosition
== IResultSetHelper::RELATIVE
)
883 eCursorPosition
= (nOffset
>= 0) ? IResultSetHelper::NEXT
: IResultSetHelper::PRIOR
;
894 // Evaluate may only be set,
895 // if the Keyset will be constructed further
896 if ( ( m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
)
903 OKeyValue
* pKeyValue
= GetOrderbyKeyValue( m_aSelectRow
);
904 m_pSortIndex
->AddKeyValue(pKeyValue
);
906 else if (m_pFileSet
.is())
908 sal_uInt32 nBookmarkValue
= std::abs((sal_Int32
)(m_aEvaluateRow
->get())[0]->getValue());
909 m_pFileSet
->get().push_back(nBookmarkValue
);
912 else if (m_aSQLIterator
.getStatementType() == SQL_STATEMENT_UPDATE
)
917 // read the actual result-row
918 bOK
= m_pTable
->fetchRow(m_aEvaluateRow
, *(m_pTable
->getTableColumns()), true,true);
923 // just give the values to be changed:
924 if(!m_pTable
->UpdateRow(*m_aAssignValues
,m_aEvaluateRow
, m_xColsIdx
))
928 else if (m_aSQLIterator
.getStatementType() == SQL_STATEMENT_DELETE
)
933 bOK
= m_pTable
->fetchRow(m_aEvaluateRow
, *(m_pTable
->getTableColumns()), true,true);
937 if(!m_pTable
->DeleteRow(*m_xColumns
))
945 bool OResultSet::Move(IResultSetHelper::Movement eCursorPosition
, sal_Int32 nOffset
, bool bRetrieveData
)
947 sal_Int32 nTempPos
= m_nRowPos
;
949 if (m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
&&
952 if (!m_pFileSet
.is()) //no Index available
955 ExecuteRow(eCursorPosition
,nOffset
,false,bRetrieveData
);
957 // now set the bookmark for outside this is the logical pos and not the file pos
958 *(*m_aRow
->get().begin()) = sal_Int32(m_nRowPos
+ 1);
962 switch(eCursorPosition
)
964 case IResultSetHelper::NEXT
:
967 case IResultSetHelper::PRIOR
:
971 case IResultSetHelper::FIRST
:
974 case IResultSetHelper::LAST
:
975 m_nRowPos
= m_pFileSet
->get().size() - 1;
977 case IResultSetHelper::RELATIVE
:
978 m_nRowPos
+= nOffset
;
980 case IResultSetHelper::ABSOLUTE
:
981 case IResultSetHelper::BOOKMARK
:
982 if ( m_nRowPos
== (nOffset
-1) )
984 m_nRowPos
= nOffset
-1;
989 // The FileCursor is outside of the valid range, if:
991 // b.) a KeySet exists and m_nRowPos > m_pFileSet->size()
992 if (m_nRowPos
< 0 || (m_pFileSet
->isFrozen() && eCursorPosition
!= IResultSetHelper::BOOKMARK
&& m_nRowPos
>= (sal_Int32
)m_pFileSet
->get().size() )) // && m_pFileSet->IsFrozen()
998 if (m_nRowPos
< (sal_Int32
)m_pFileSet
->get().size())
1001 ExecuteRow(IResultSetHelper::BOOKMARK
,(m_pFileSet
->get())[m_nRowPos
],false,bRetrieveData
);
1003 // now set the bookmark for outside
1004 *(*m_aRow
->get().begin()) = sal_Int32(m_nRowPos
+ 1);
1005 if ( (bRetrieveData
|| m_pSQLAnalyzer
->hasRestriction()) && m_pSQLAnalyzer
->hasFunctions() )
1007 m_pSQLAnalyzer
->setSelectionEvaluationResult(m_aSelectRow
,m_aColMapping
);
1010 else // Index must be further constructed
1012 // set first on the last known row
1013 if (m_pFileSet
->get().empty())
1015 m_pTable
->seekRow(IResultSetHelper::ABSOLUTE
, 0, m_nFilePos
);
1019 m_aFileSetIter
= m_pFileSet
->get().end()-1;
1020 m_pTable
->seekRow(IResultSetHelper::BOOKMARK
, *m_aFileSetIter
, m_nFilePos
);
1023 // Determine the number of further Fetches
1024 while (bOK
&& m_nRowPos
>= (sal_Int32
)m_pFileSet
->get().size())
1026 if (m_pEvaluationKeySet
)
1028 if (m_nRowPos
>= (sal_Int32
)m_pEvaluationKeySet
->size())
1030 else if (m_nRowPos
== 0)
1032 m_aEvaluateIter
= m_pEvaluationKeySet
->begin();
1033 bOK
= ExecuteRow(IResultSetHelper::BOOKMARK
,*m_aEvaluateIter
,true, bRetrieveData
);
1038 bOK
= ExecuteRow(IResultSetHelper::BOOKMARK
,*m_aEvaluateIter
,true, bRetrieveData
);
1042 bOK
= ExecuteRow(IResultSetHelper::NEXT
,1,true, false);//bRetrieveData);
1047 // read the results again
1048 m_pTable
->fetchRow(m_aRow
, *(m_pTable
->getTableColumns()), true,bRetrieveData
);
1050 // now set the bookmark for outside
1051 *(*m_aRow
->get().begin()) = sal_Int32(m_nRowPos
+ 1);
1053 if ( (bRetrieveData
|| m_pSQLAnalyzer
->hasRestriction()) && m_pSQLAnalyzer
->hasFunctions() )
1055 m_pSQLAnalyzer
->setSelectionEvaluationResult(m_aSelectRow
,m_aColMapping
);
1058 else if (!m_pFileSet
->isFrozen()) // no valid record found
1060 m_pFileSet
->setFrozen();
1061 m_pEvaluationKeySet
= NULL
;
1068 else if (m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
&& isCount())
1070 // Fetch the COUNT(*)
1071 switch (eCursorPosition
)
1073 case IResultSetHelper::NEXT
:
1076 case IResultSetHelper::PRIOR
:
1079 case IResultSetHelper::FIRST
:
1082 case IResultSetHelper::LAST
:
1085 case IResultSetHelper::RELATIVE
:
1086 m_nRowPos
+= nOffset
;
1088 case IResultSetHelper::ABSOLUTE
:
1089 case IResultSetHelper::BOOKMARK
:
1090 m_nRowPos
= nOffset
- 1;
1094 if ( m_nRowPos
< 0 )
1096 else if (m_nRowPos
== 0)
1098 // put COUNT(*) in result-row
1099 // (must be the first and only variable in the row)
1100 if (m_aRow
->get().size() >= 2)
1102 *(m_aRow
->get())[1] = m_nRowCountResult
;
1103 *(m_aRow
->get())[0] = sal_Int32(1);
1104 (m_aRow
->get())[1]->setBound(true);
1105 (m_aSelectRow
->get())[1] = (m_aRow
->get())[1];
1116 // Fetch only possible at SELECT!
1122 // is the Cursor positioned before the first row
1123 // then the position will be maintained
1125 m_nRowPos
= nTempPos
;
1128 switch(eCursorPosition
)
1130 case IResultSetHelper::PRIOR
:
1131 case IResultSetHelper::FIRST
:
1134 case IResultSetHelper::LAST
:
1135 case IResultSetHelper::NEXT
:
1136 case IResultSetHelper::ABSOLUTE
:
1137 case IResultSetHelper::RELATIVE
:
1139 m_nRowPos
= m_pFileSet
.is() ? (sal_Int32
)m_pFileSet
->get().size() : -1;
1140 else if (nOffset
< 0)
1143 case IResultSetHelper::BOOKMARK
:
1144 m_nRowPos
= nTempPos
; // last Position
1150 void OResultSet::sortRows()
1152 if (!m_pSQLAnalyzer
->hasRestriction() && m_aOrderbyColumnNumber
.size() == 1)
1154 // is just one field given for sorting
1155 // and this field is indexed, then the Index will be used
1156 Reference
<XIndexesSupplier
> xIndexSup
;
1157 m_pTable
->queryInterface(::getCppuType((const Reference
<XIndexesSupplier
>*)0)) >>= xIndexSup
;
1159 Reference
<XIndexAccess
> xIndexes
;
1162 xIndexes
.set(xIndexSup
->getIndexes(),UNO_QUERY
);
1163 Reference
<XPropertySet
> xColProp
;
1164 if(m_aOrderbyColumnNumber
[0] < xIndexes
->getCount())
1166 xColProp
.set(xIndexes
->getByIndex(m_aOrderbyColumnNumber
[0]),UNO_QUERY
);
1167 // iterate through the indexes to find the matching column
1168 const sal_Int32 nCount
= xIndexes
->getCount();
1169 for(sal_Int32 i
=0; i
< nCount
;++i
)
1171 Reference
<XColumnsSupplier
> xIndex(xIndexes
->getByIndex(i
),UNO_QUERY
);
1172 Reference
<XNameAccess
> xIndexCols
= xIndex
->getColumns();
1173 if(xIndexCols
->hasByName(comphelper::getString(xColProp
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
)))))
1175 m_pFileSet
= new OKeySet();
1177 if(fillIndexValues(xIndex
))
1185 OSortIndex::TKeyTypeVector
eKeyType(m_aOrderbyColumnNumber
.size());
1186 ::std::vector
<sal_Int32
>::iterator aOrderByIter
= m_aOrderbyColumnNumber
.begin();
1187 for (::std::vector
<sal_Int16
>::size_type i
=0;aOrderByIter
!= m_aOrderbyColumnNumber
.end(); ++aOrderByIter
,++i
)
1189 OSL_ENSURE((sal_Int32
)m_aSelectRow
->get().size() > *aOrderByIter
,"Invalid Index");
1190 switch ((*(m_aSelectRow
->get().begin()+*aOrderByIter
))->getValue().getTypeKind())
1192 case DataType::CHAR
:
1193 case DataType::VARCHAR
:
1194 case DataType::LONGVARCHAR
:
1195 eKeyType
[i
] = SQL_ORDERBYKEY_STRING
;
1198 case DataType::OTHER
:
1199 case DataType::TINYINT
:
1200 case DataType::SMALLINT
:
1201 case DataType::INTEGER
:
1202 case DataType::DECIMAL
:
1203 case DataType::NUMERIC
:
1204 case DataType::REAL
:
1205 case DataType::DOUBLE
:
1206 case DataType::DATE
:
1207 case DataType::TIME
:
1208 case DataType::TIMESTAMP
:
1210 eKeyType
[i
] = SQL_ORDERBYKEY_DOUBLE
;
1213 // Other types aren't implemented (so they are always FALSE)
1215 eKeyType
[i
] = SQL_ORDERBYKEY_NONE
;
1216 SAL_WARN( "connectivity.drivers","OFILECursor::Execute: Datentyp nicht implementiert");
1219 (m_aSelectRow
->get())[*aOrderByIter
]->setBound(true);
1222 m_pSortIndex
= new OSortIndex(eKeyType
,m_aOrderbyAscending
);
1224 if (m_pEvaluationKeySet
)
1226 m_aEvaluateIter
= m_pEvaluationKeySet
->begin();
1228 while (m_aEvaluateIter
!= m_pEvaluationKeySet
->end())
1230 ExecuteRow(IResultSetHelper::BOOKMARK
,(*m_aEvaluateIter
),true);
1236 while ( ExecuteRow( IResultSetHelper::NEXT
, 1, false, true ) )
1238 m_aSelectRow
->get()[0]->setValue( m_aRow
->get()[0]->getValue() );
1239 if ( m_pSQLAnalyzer
->hasFunctions() )
1240 m_pSQLAnalyzer
->setSelectionEvaluationResult( m_aSelectRow
, m_aColMapping
);
1241 const sal_Int32 nBookmark
= (*m_aRow
->get().begin())->getValue();
1242 ExecuteRow( IResultSetHelper::BOOKMARK
, nBookmark
, true, false );
1246 // create sorted Keyset
1247 m_pEvaluationKeySet
= NULL
;
1249 m_pFileSet
= m_pSortIndex
->CreateKeySet();
1250 DELETEZ(m_pSortIndex
);
1251 // now access to a sorted set is possible via Index
1256 bool OResultSet::OpenImpl()
1258 OSL_ENSURE(m_pSQLAnalyzer
,"No analyzer set with setSqlAnalyzer!");
1261 const OSQLTables
& xTabs
= m_aSQLIterator
.getTables();
1262 if ((xTabs
.begin() == xTabs
.end()) || !xTabs
.begin()->second
.is())
1263 lcl_throwError(STR_QUERY_TOO_COMPLEX
,*this);
1265 if ( xTabs
.size() > 1 || m_aSQLIterator
.hasErrors() )
1266 lcl_throwError(STR_QUERY_MORE_TABLES
,*this);
1268 OSQLTable xTable
= xTabs
.begin()->second
;
1269 m_xColumns
= m_aSQLIterator
.getSelectColumns();
1271 m_xColNames
= xTable
->getColumns();
1272 m_xColsIdx
.set(m_xColNames
,UNO_QUERY
);
1273 doTableSpecials(xTable
);
1274 Reference
<XComponent
> xComp(xTable
,UNO_QUERY
);
1276 xComp
->addEventListener(this);
1279 m_pTable
->refreshHeader();
1281 sal_Int32 nColumnCount
= m_xColsIdx
->getCount();
1283 initializeRow(m_aRow
,nColumnCount
);
1284 initializeRow(m_aEvaluateRow
,nColumnCount
);
1285 initializeRow(m_aInsertRow
,nColumnCount
);
1288 m_nResultSetConcurrency
= (m_pTable
->isReadOnly() || isCount()) ? ResultSetConcurrency::READ_ONLY
: ResultSetConcurrency::UPDATABLE
;
1290 // create new Index:
1293 // position at the beginning
1296 m_nRowCountResult
= -1;
1298 m_nLastVisitedPos
= m_pTable
->getCurrentLastPos();
1300 switch(m_aSQLIterator
.getStatementType())
1302 case SQL_STATEMENT_SELECT
:
1306 if(m_xColumns
->get().size() > 1)
1307 lcl_throwError(STR_QUERY_COMPLEX_COUNT
,*this);
1309 m_nRowCountResult
= 0;
1310 // for now simply iterate over all rows and
1311 // do all actions (or just count)
1314 if (m_pEvaluationKeySet
)
1316 m_aEvaluateIter
= m_pEvaluationKeySet
->begin();
1317 bOK
= m_aEvaluateIter
== m_pEvaluationKeySet
->end();
1322 if (m_pEvaluationKeySet
)
1323 ExecuteRow(IResultSetHelper::BOOKMARK
,(*m_aEvaluateIter
),true);
1325 bOK
= ExecuteRow(IResultSetHelper::NEXT
,1,true);
1329 m_nRowCountResult
++;
1330 if(m_pEvaluationKeySet
)
1333 bOK
= m_aEvaluateIter
== m_pEvaluationKeySet
->end();
1338 // save result of COUNT(*) in m_nRowCountResult.
1339 // nRowCount (number of Rows in the result) = 1 for this request!
1340 m_pEvaluationKeySet
= NULL
;
1345 bool bDistinct
= false;
1346 assert(m_pParseTree
!= 0);
1347 OSQLParseNode
*pDistinct
= m_pParseTree
->getChild(1);
1349 assert(m_aOrderbyColumnNumber
.size() ==
1350 m_aOrderbyAscending
.size());
1351 if (pDistinct
&& pDistinct
->getTokenID() == SQL_TOKEN_DISTINCT
)
1353 // To eliminate duplicates we need to sort on all columns.
1354 // This is not a problem because the SQL spec says that the
1355 // order of columns that are not specified in ORDER BY
1356 // clause is undefined, so it doesn't hurt to sort on
1357 // these; pad the vectors to include them.
1358 for (sal_Int32 i
= 1; // 0: bookmark (see setBoundedColumns)
1359 static_cast<size_t>(i
) < m_aColMapping
.size(); ++i
)
1361 if (::std::find(m_aOrderbyColumnNumber
.begin(),
1362 m_aOrderbyColumnNumber
.end(), i
)
1363 == m_aOrderbyColumnNumber
.end())
1365 m_aOrderbyColumnNumber
.push_back(i
);
1366 // ASC or DESC doesn't matter
1367 m_aOrderbyAscending
.push_back(SQL_ASC
);
1376 if (!m_pFileSet
.is())
1378 m_pFileSet
= new OKeySet();
1380 if (!m_pSQLAnalyzer
->hasRestriction())
1381 // now the Keyset can be filled!
1382 // But be careful: It is assumed, that the FilePositions will be stored as sequence 1..n
1384 if ( m_nLastVisitedPos
> 0)
1385 m_pFileSet
->get().reserve( m_nLastVisitedPos
);
1386 for (sal_Int32 i
= 0; i
< m_nLastVisitedPos
; i
++)
1387 m_pFileSet
->get().push_back(i
+ 1);
1390 OSL_ENSURE(m_pFileSet
.is(),"Kein KeySet vorhanden! :-(");
1392 if(bDistinct
&& m_pFileSet
.is())
1394 OValueRow aSearchRow
= new OValueVector(m_aRow
->get().size());
1395 OValueRefVector::Vector::iterator aRowIter
= m_aRow
->get().begin();
1396 OValueVector::Vector::iterator aSearchIter
= aSearchRow
->get().begin();
1397 for ( ++aRowIter
,++aSearchIter
; // the first column is the bookmark column
1398 aRowIter
!= m_aRow
->get().end();
1399 ++aRowIter
,++aSearchIter
)
1400 aSearchIter
->setBound((*aRowIter
)->isBound());
1402 size_t nMaxRow
= m_pFileSet
->get().size();
1406 #if OSL_DEBUG_LEVEL > 1
1412 for( size_t j
= nMaxRow
-1; j
> 0; --j
)
1414 nPos
= (m_pFileSet
->get())[j
];
1415 ExecuteRow(IResultSetHelper::BOOKMARK
,nPos
,false);
1416 m_pSQLAnalyzer
->setSelectionEvaluationResult(m_aSelectRow
,m_aColMapping
);
1417 { // copy row values
1418 OValueRefVector::Vector::iterator copyFrom
= m_aSelectRow
->get().begin();
1419 OValueVector::Vector::iterator copyTo
= aSearchRow
->get().begin();
1420 for ( ++copyFrom
,++copyTo
; // the first column is the bookmark column
1421 copyFrom
!= m_aSelectRow
->get().end();
1422 ++copyFrom
,++copyTo
)
1423 *copyTo
= *(*copyFrom
);
1426 // compare with next row
1427 nKey
= (m_pFileSet
->get())[j
-1];
1428 ExecuteRow(IResultSetHelper::BOOKMARK
,nKey
,false);
1429 m_pSQLAnalyzer
->setSelectionEvaluationResult(m_aSelectRow
,m_aColMapping
);
1430 OValueRefVector::Vector::iterator loopInRow
= m_aSelectRow
->get().begin();
1431 OValueVector::Vector::iterator existentInSearchRow
= aSearchRow
->get().begin();
1432 for ( ++loopInRow
,++existentInSearchRow
; // the first column is the bookmark column
1433 loopInRow
!= m_aSelectRow
->get().end();
1434 ++loopInRow
,++existentInSearchRow
)
1436 if ( (*loopInRow
)->isBound() && !( *(*loopInRow
) == *existentInSearchRow
) )
1440 if(loopInRow
== m_aSelectRow
->get().end())
1441 (m_pFileSet
->get())[j
] = 0; // Rows match -- Mark for deletion by setting key to 0
1442 #if OSL_DEBUG_LEVEL > 1
1448 m_pFileSet
->get().erase(::std::remove_if(m_pFileSet
->get().begin(),m_pFileSet
->get().end(),
1449 ::std::bind2nd(::std::equal_to
<sal_Int32
>(),0))
1450 ,m_pFileSet
->get().end());
1456 case SQL_STATEMENT_UPDATE
:
1457 case SQL_STATEMENT_DELETE
:
1458 // during processing count the number of processed Rows
1459 m_nRowCountResult
= 0;
1460 // for now simply iterate over all rows and
1461 // run the actions (or simply count):
1465 if (m_pEvaluationKeySet
)
1467 m_aEvaluateIter
= m_pEvaluationKeySet
->begin();
1468 bOK
= m_aEvaluateIter
== m_pEvaluationKeySet
->end();
1473 if (m_pEvaluationKeySet
)
1474 ExecuteRow(IResultSetHelper::BOOKMARK
,(*m_aEvaluateIter
),true);
1476 bOK
= ExecuteRow(IResultSetHelper::NEXT
,1,true);
1480 m_nRowCountResult
++;
1481 if(m_pEvaluationKeySet
)
1484 bOK
= m_aEvaluateIter
== m_pEvaluationKeySet
->end();
1489 // save result of COUNT(*) in nRowCountResult.
1490 // nRowCount (number of rows in the result-set) = 1 for this request!
1491 m_pEvaluationKeySet
= NULL
;
1494 case SQL_STATEMENT_INSERT
:
1495 m_nRowCountResult
= 0;
1497 OSL_ENSURE(m_aAssignValues
.is(),"No assign values set!");
1498 if(!m_pTable
->InsertRow(*m_aAssignValues
, true,m_xColsIdx
))
1504 m_nRowCountResult
= 1;
1507 SAL_WARN( "connectivity.drivers", "OResultSet::OpenImpl: unsupported statement type!" );
1517 Sequence
< sal_Int8
> OResultSet::getUnoTunnelImplementationId()
1519 static ::cppu::OImplementationId
* pId
= 0;
1522 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
1525 static ::cppu::OImplementationId aId
;
1529 return pId
->getImplementationId();
1532 // com::sun::star::lang::XUnoTunnel
1534 sal_Int64
OResultSet::getSomething( const Sequence
< sal_Int8
> & rId
) throw (RuntimeException
, std::exception
)
1536 return (rId
.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId
.getConstArray(), 16 ) )
1537 ? reinterpret_cast< sal_Int64
>( this )
1541 void OResultSet::setBoundedColumns(const OValueRefRow
& _rRow
,
1542 const OValueRefRow
& _rSelectRow
,
1543 const ::rtl::Reference
<connectivity::OSQLColumns
>& _rxColumns
,
1544 const Reference
<XIndexAccess
>& _xNames
,
1545 bool _bSetColumnMapping
,
1546 const Reference
<XDatabaseMetaData
>& _xMetaData
,
1547 ::std::vector
<sal_Int32
>& _rColMapping
)
1549 ::comphelper::UStringMixEqual
aCase(_xMetaData
->supportsMixedCaseQuotedIdentifiers());
1551 Reference
<XPropertySet
> xTableColumn
;
1552 OUString sTableColumnName
, sSelectColumnRealName
;
1554 const OUString sName
= OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
);
1555 const OUString sRealName
= OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME
);
1556 const OUString sType
= OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
);
1558 typedef ::std::map
<OSQLColumns::Vector::iterator
,sal_Bool
> IterMap
;
1559 IterMap aSelectIters
;
1560 OValueRefVector::Vector::iterator aRowIter
= _rRow
->get().begin()+1;
1561 for (sal_Int32 i
=0; // the first column is the bookmark column
1562 aRowIter
!= _rRow
->get().end();
1566 (*aRowIter
)->setBound(false);
1569 // get the table column and its name
1570 _xNames
->getByIndex(i
) >>= xTableColumn
;
1571 OSL_ENSURE(xTableColumn
.is(), "OResultSet::setBoundedColumns: invalid table column!");
1572 if (xTableColumn
.is())
1573 xTableColumn
->getPropertyValue(sName
) >>= sTableColumnName
;
1575 sTableColumnName
= OUString();
1577 // look if we have such a select column
1578 // TODO: would like to have a O(log n) search here ...
1579 for ( OSQLColumns::Vector::iterator aIter
= _rxColumns
->get().begin();
1580 aIter
!= _rxColumns
->get().end();
1584 if((*aIter
)->getPropertySetInfo()->hasPropertyByName(sRealName
))
1585 (*aIter
)->getPropertyValue(sRealName
) >>= sSelectColumnRealName
;
1587 (*aIter
)->getPropertyValue(sName
) >>= sSelectColumnRealName
;
1589 if ( aCase(sTableColumnName
, sSelectColumnRealName
) && !(*aRowIter
)->isBound() && aSelectIters
.end() == aSelectIters
.find(aIter
) )
1591 aSelectIters
.insert(IterMap::value_type(aIter
,sal_True
));
1592 if(_bSetColumnMapping
)
1594 sal_Int32 nSelectColumnPos
= aIter
- _rxColumns
->get().begin() + 1;
1595 // the getXXX methods are 1-based ...
1596 sal_Int32 nTableColumnPos
= i
+ 1;
1597 // get first table column is the bookmark column ...
1598 _rColMapping
[nSelectColumnPos
] = nTableColumnPos
;
1599 (_rSelectRow
->get())[nSelectColumnPos
] = *aRowIter
;
1602 (*aRowIter
)->setBound(true);
1603 sal_Int32 nType
= DataType::OTHER
;
1604 if (xTableColumn
.is())
1605 xTableColumn
->getPropertyValue(sType
) >>= nType
;
1606 (*aRowIter
)->setTypeKind(nType
);
1614 SAL_WARN( "connectivity.drivers","OResultSet::setBoundedColumns: caught an Exception!");
1617 // in this case we got more select columns as columns exist in the table
1618 if ( _bSetColumnMapping
&& aSelectIters
.size() != _rColMapping
.size() )
1620 Reference
<XNameAccess
> xNameAccess(_xNames
,UNO_QUERY
);
1621 Sequence
< OUString
> aSelectColumns
= xNameAccess
->getElementNames();
1623 for ( OSQLColumns::Vector::iterator aIter
= _rxColumns
->get().begin();
1624 aIter
!= _rxColumns
->get().end();
1628 if ( aSelectIters
.end() == aSelectIters
.find(aIter
) )
1630 if ( (*aIter
)->getPropertySetInfo()->hasPropertyByName(sRealName
) )
1631 (*aIter
)->getPropertyValue(sRealName
) >>= sSelectColumnRealName
;
1633 (*aIter
)->getPropertyValue(sName
) >>= sSelectColumnRealName
;
1635 if ( xNameAccess
->hasByName( sSelectColumnRealName
) )
1637 aSelectIters
.insert(IterMap::value_type(aIter
,sal_True
));
1638 sal_Int32 nSelectColumnPos
= aIter
- _rxColumns
->get().begin() + 1;
1639 const OUString
* pBegin
= aSelectColumns
.getConstArray();
1640 const OUString
* pEnd
= pBegin
+ aSelectColumns
.getLength();
1641 for(sal_Int32 i
=0;pBegin
!= pEnd
;++pBegin
,++i
)
1643 if ( aCase(*pBegin
, sSelectColumnRealName
) )
1645 // the getXXX methods are 1-based ...
1646 sal_Int32 nTableColumnPos
= i
+ 1;
1647 // get first table column is the bookmark column ...
1648 _rColMapping
[nSelectColumnPos
] = nTableColumnPos
;
1649 (_rSelectRow
->get())[nSelectColumnPos
] = (_rRow
->get())[nTableColumnPos
];
1659 void SAL_CALL
OResultSet::acquire() throw()
1661 OResultSet_BASE::acquire();
1664 void SAL_CALL
OResultSet::release() throw()
1666 OResultSet_BASE::release();
1669 Reference
< ::com::sun::star::beans::XPropertySetInfo
> SAL_CALL
OResultSet::getPropertySetInfo( ) throw(RuntimeException
, std::exception
)
1671 return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
1674 void OResultSet::doTableSpecials(const OSQLTable
& _xTable
)
1676 Reference
< ::com::sun::star::lang::XUnoTunnel
> xTunnel(_xTable
,UNO_QUERY
);
1679 m_pTable
= reinterpret_cast< OFileTable
* >( xTunnel
->getSomething(OFileTable::getUnoTunnelImplementationId()) );
1681 m_pTable
->acquire();
1685 void OResultSet::clearInsertRow()
1687 m_aRow
->setDeleted(false); // set to false here because this is the new row
1688 OValueRefVector::Vector::iterator aIter
= m_aInsertRow
->get().begin();
1689 const OValueRefVector::Vector::iterator aEnd
= m_aInsertRow
->get().end();
1690 for(sal_Int32 nPos
= 0;aIter
!= aEnd
;++aIter
,++nPos
)
1692 ORowSetValueDecoratorRef
& rValue
= (*aIter
);
1693 if ( rValue
->isBound() )
1695 (m_aRow
->get())[nPos
]->setValue( (*aIter
)->getValue() );
1697 rValue
->setBound(nPos
== 0);
1698 rValue
->setModified(false);
1703 void OResultSet::initializeRow(OValueRefRow
& _rRow
,sal_Int32 _nColumnCount
)
1707 _rRow
= new OValueRefVector(_nColumnCount
);
1708 (_rRow
->get())[0]->setBound(true);
1709 ::std::for_each(_rRow
->get().begin()+1,_rRow
->get().end(),TSetRefBound(false));
1713 bool OResultSet::fillIndexValues(const Reference
< XColumnsSupplier
> &/*_xIndex*/)
1718 bool OResultSet::move(IResultSetHelper::Movement _eCursorPosition
, sal_Int32 _nOffset
, bool _bRetrieveData
)
1720 return Move(_eCursorPosition
,_nOffset
,_bRetrieveData
);
1723 sal_Int32
OResultSet::getDriverPos() const
1725 return (m_aRow
->get())[0]->getValue();
1728 bool OResultSet::deletedVisible() const
1730 return m_bShowDeleted
;
1733 bool OResultSet::isRowDeleted() const
1735 return m_aRow
->isDeleted();
1738 void SAL_CALL
OResultSet::disposing( const EventObject
& Source
) throw (RuntimeException
, std::exception
)
1740 Reference
<XPropertySet
> xProp
= m_pTable
;
1741 if(m_pTable
&& Source
.Source
== xProp
)
1743 m_pTable
->release();
1749 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */