xref: /trunk/main/connectivity/source/inc/odbc/OResultSet.hxx (revision e8df90a080f9dc7518c4b937d7dcd461d7b4d178)
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 #ifndef _CONNECTIVITY_ODBC_ORESULTSET_HXX_
25 #define _CONNECTIVITY_ODBC_ORESULTSET_HXX_
26 
27 #include <com/sun/star/sdbc/XResultSet.hpp>
28 #include <com/sun/star/sdbc/XRow.hpp>
29 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
30 #include <com/sun/star/sdbc/XCloseable.hpp>
31 #include <com/sun/star/sdbc/XColumnLocate.hpp>
32 #include <com/sun/star/util/XCancellable.hpp>
33 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
34 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
35 #include <com/sun/star/sdbc/XRowUpdate.hpp>
36 #include <com/sun/star/sdbcx/XRowLocate.hpp>
37 #include <com/sun/star/sdbcx/XDeleteRows.hpp>
38 #include <cppuhelper/compbase12.hxx>
39 #include <comphelper/proparrhlp.hxx>
40 #include "odbc/OFunctions.hxx"
41 #include "odbc/OStatement.hxx"
42 #include "odbc/odbcbasedllapi.hxx"
43 #include "connectivity/CommonTools.hxx"
44 #include "connectivity/FValue.hxx"
45 #include "TSkipDeletedSet.hxx"
46 
47 namespace connectivity
48 {
49     namespace odbc
50     {
51 
52         /*
53         **  java_sql_ResultSet
54         */
55         typedef ::cppu::WeakComponentImplHelper12<      ::com::sun::star::sdbc::XResultSet,
56                                                         ::com::sun::star::sdbc::XRow,
57                                                         ::com::sun::star::sdbc::XResultSetMetaDataSupplier,
58                                                         ::com::sun::star::util::XCancellable,
59                                                         ::com::sun::star::sdbc::XWarningsSupplier,
60                                                         ::com::sun::star::sdbc::XResultSetUpdate,
61                                                         ::com::sun::star::sdbc::XRowUpdate,
62                                                         ::com::sun::star::sdbcx::XRowLocate,
63                                                         ::com::sun::star::sdbcx::XDeleteRows,
64                                                         ::com::sun::star::sdbc::XCloseable,
65                                                         ::com::sun::star::sdbc::XColumnLocate,
66                                                         ::com::sun::star::lang::XServiceInfo> OResultSet_BASE;
67 
68 
69         typedef ::std::pair<sal_Int64,sal_Int32> TVoidPtr;
70         typedef ::std::allocator< TVoidPtr > TVoidAlloc;
71         typedef ::std::vector<TVoidPtr> TVoidVector;
72         //  typedef ::com::sun::star::uno::Sequence<TVoidPtr> TVoidVector;
73         /// unary_function Functor object for class ZZ returntype is void
74         struct OOO_DLLPUBLIC_ODBCBASE TBookmarkPosMapCompare
75         {
76             typedef ::com::sun::star::uno::Sequence<sal_Int8> first_argument_type;
77             typedef ::com::sun::star::uno::Sequence<sal_Int8> second_argument_type;
78             typedef bool result_type;
operator ()connectivity::odbc::TBookmarkPosMapCompare79             inline bool operator()( const ::com::sun::star::uno::Sequence<sal_Int8>& _rLH,
80                                     const ::com::sun::star::uno::Sequence<sal_Int8>& _rRH) const
81             {
82                 if(_rLH.getLength() == _rRH.getLength())
83                 {
84                     sal_Int32 nCount = _rLH.getLength();
85                     if(nCount != 4)
86                     {
87                         const sal_Int8* pLHBack = _rLH.getConstArray() + nCount - 1;
88                         const sal_Int8* pRHBack = _rRH.getConstArray() + nCount - 1;
89 
90                         sal_Int32 i;
91                         for(i=0;i < nCount;++i,--pLHBack,--pRHBack)
92                         {
93                             if(!(*pLHBack) && *pRHBack)
94                                 return sal_True;
95                             else if(*pLHBack && !(*pRHBack))
96                                 return sal_False;
97                         }
98                         for(i=0,++pLHBack,++pRHBack;i < nCount;++pLHBack,++pRHBack,++i)
99                             if(*pLHBack < *pRHBack)
100                                 return sal_True;
101                         return sal_False;
102                     }
103                     else
104                         return *reinterpret_cast<const sal_Int32*>(_rLH.getConstArray()) < *reinterpret_cast<const sal_Int32*>(_rRH.getConstArray());
105 
106                 }
107                 else
108                     return _rLH.getLength() < _rRH.getLength();
109             }
110         };
111 
112         typedef ::std::map< ::com::sun::star::uno::Sequence<sal_Int8>, sal_Int32,TBookmarkPosMapCompare > TBookmarkPosMap;
113 
114         class OOO_DLLPUBLIC_ODBCBASE OResultSet :
115                             public  comphelper::OBaseMutex,
116                             public  ::connectivity::IResultSetHelper,
117                             public  OResultSet_BASE,
118                             public  ::cppu::OPropertySetHelper,
119                             public  ::comphelper::OPropertyArrayUsageHelper<OResultSet>
120         {
121         protected:
122             TBookmarkPosMap                             m_aPosToBookmarks;
123             // used top hold the information about the value and the datatype to save calls to metadata
124             typedef ::std::vector<ORowSetValue>         TDataRow;
125 
126             TVoidVector                                 m_aBindVector;
127             ::std::vector<SQLLEN>                       m_aLengthVector;
128             ::std::map<sal_Int32,SWORD>                 m_aODBCColumnTypes;
129             ::com::sun::star::uno::Sequence<sal_Int8>   m_aBookmark;
130 
131             TDataRow                                    m_aRow; // only used when SQLGetData can't be called in any order
132             ORowSetValue                                m_aEmptyValue;  // needed for the getValue method when no prefetch is used
133             SQLHANDLE                                   m_aStatementHandle;
134             SQLHANDLE                                   m_aConnectionHandle;
135             OStatement_Base*                            m_pStatement;
136             OSkipDeletedSet*                            m_pSkipDeletedSet;
137             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>    m_xStatement;
138             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData>        m_xMetaData;
139             SQLUSMALLINT*                               m_pRowStatusArray;
140             rtl_TextEncoding                            m_nTextEncoding;
141             sal_Int32                                   m_nRowPos;
142             sal_Int32                                   m_nLastColumnPos;       // used for m_aRow just to know where we are
143             mutable SQLULEN                            m_nUseBookmarks;
144             SQLRETURN                                   m_nCurrentFetchState;
145             sal_Bool                                    m_bWasNull;
146             sal_Bool                                    m_bEOF;                 // after last record
147             sal_Bool                                    m_bLastRecord;
148             sal_Bool                                    m_bFreeHandle;
149             sal_Bool                                    m_bInserting;
150             sal_Bool                                    m_bFetchData;           // true when SQLGetaData can be called in any order or when fetching data for m_aRow
151             sal_Bool                                    m_bRowInserted;
152             sal_Bool                                    m_bRowDeleted;
153             sal_Bool                                    m_bUseFetchScroll;
154 
155             sal_Bool  isBookmarkable()          const;
156             sal_Int32 getResultSetConcurrency() const;
157             sal_Int32 getResultSetType()        const;
158             sal_Int32 getFetchDirection()       const;
159             sal_Int32 getFetchSize()            const;
160             ::rtl::OUString getCursorName()     const;
161 
162             void setFetchDirection(sal_Int32 _par0);
163             void setFetchSize(sal_Int32 _par0);
164 
165 
166             void fillRow(sal_Int32 _nToColumn);
167             void allocBuffer();
168             void releaseBuffer();
169             void updateValue(sal_Int32 columnIndex,SQLSMALLINT _nType,void* _pValue);
170             void fillNeededData(SQLRETURN _nRet);
171             const ORowSetValue& getValue(sal_Int32 _nColumnIndex,SQLSMALLINT _nType,void* _pValue,SQLINTEGER _rSize);
172             sal_Bool moveImpl(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, sal_Bool _bRetrieveData);
173             TVoidPtr allocBindColumn(sal_Int32 _nType,sal_Int32 _nColumnIndex);
174             SQLRETURN unbind(sal_Bool _bUnbindHandle = sal_True);
175             SWORD impl_getColumnType_nothrow(sal_Int32 columnIndex);
176 
177             // OPropertyArrayUsageHelper
178             virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
179             // OPropertySetHelper
180             virtual ::cppu::IPropertyArrayHelper & SAL_CALL getInfoHelper();
181 
182             virtual sal_Bool SAL_CALL convertFastPropertyValue(
183                                 ::com::sun::star::uno::Any & rConvertedValue,
184                                 ::com::sun::star::uno::Any & rOldValue,
185                                 sal_Int32 nHandle,
186                                 const ::com::sun::star::uno::Any& rValue );
187             virtual void SAL_CALL setFastPropertyValue_NoBroadcast(
188                                     sal_Int32 nHandle,
189                                     const ::com::sun::star::uno::Any& rValue
190                                      );
191             virtual void SAL_CALL getFastPropertyValue(
192                                     ::com::sun::star::uno::Any& rValue,
193                                     sal_Int32 nHandle
194                                          ) const;
195         public:
196             DECLARE_SERVICE_INFO();
197             // ein Konstruktor, der fuer das Returnen des Objektes benoetigt wird:
198             OResultSet( SQLHANDLE _pStatementHandle,OStatement_Base* pStmt);
199             virtual ~OResultSet();
200 
201             void construct();
202 
getOdbcFunction(sal_Int32 _nIndex) const203             inline oslGenericFunction getOdbcFunction(sal_Int32 _nIndex)  const
204             {
205                 return m_pStatement->getOdbcFunction(_nIndex);
206             }
207 
operator *()208             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > operator *()
209             {
210                 return ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >(*(OResultSet_BASE*)this);
211             }
212 
setMetaData(const::com::sun::star::uno::Reference<::com::sun::star::sdbc::XResultSetMetaData> & _xMetaData)213             inline void setMetaData(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData>& _xMetaData) { m_xMetaData = _xMetaData;}
214 
215             // ::cppu::OComponentHelper
216             virtual void SAL_CALL disposing(void);
217             // XInterface
218             virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType );
219             virtual void SAL_CALL acquire() throw();
220             virtual void SAL_CALL release() throw();
221             //XTypeProvider
222             virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  );
223             // XPropertySet
224             virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  );
225             // XResultSet
226             virtual sal_Bool SAL_CALL next(  );
227             virtual sal_Bool SAL_CALL isBeforeFirst(  );
228             virtual sal_Bool SAL_CALL isAfterLast(  );
229             virtual sal_Bool SAL_CALL isFirst(  );
230             virtual sal_Bool SAL_CALL isLast(  );
231             virtual void SAL_CALL beforeFirst(  );
232             virtual void SAL_CALL afterLast(  );
233             virtual sal_Bool SAL_CALL first(  );
234             virtual sal_Bool SAL_CALL last(  );
235             virtual sal_Int32 SAL_CALL getRow(  );
236             virtual sal_Bool SAL_CALL absolute( sal_Int32 row );
237             virtual sal_Bool SAL_CALL relative( sal_Int32 rows );
238             virtual sal_Bool SAL_CALL previous(  );
239             virtual void SAL_CALL refreshRow(  );
240             virtual sal_Bool SAL_CALL rowUpdated(  );
241             virtual sal_Bool SAL_CALL rowInserted(  );
242             virtual sal_Bool SAL_CALL rowDeleted(  );
243             virtual ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL getStatement(  );
244             // XRow
245             virtual sal_Bool SAL_CALL wasNull(  );
246             virtual ::rtl::OUString SAL_CALL getString( sal_Int32 columnIndex );
247             virtual sal_Bool SAL_CALL getBoolean( sal_Int32 columnIndex );
248             virtual sal_Int8 SAL_CALL getByte( sal_Int32 columnIndex );
249             virtual sal_Int16 SAL_CALL getShort( sal_Int32 columnIndex );
250             virtual sal_Int32 SAL_CALL getInt( sal_Int32 columnIndex );
251             virtual sal_Int64 SAL_CALL getLong( sal_Int32 columnIndex );
252             virtual float SAL_CALL getFloat( sal_Int32 columnIndex );
253             virtual double SAL_CALL getDouble( sal_Int32 columnIndex );
254             virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getBytes( sal_Int32 columnIndex );
255             virtual ::com::sun::star::util::Date SAL_CALL getDate( sal_Int32 columnIndex );
256             virtual ::com::sun::star::util::Time SAL_CALL getTime( sal_Int32 columnIndex );
257             virtual ::com::sun::star::util::DateTime SAL_CALL getTimestamp( sal_Int32 columnIndex );
258             virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getBinaryStream( sal_Int32 columnIndex );
259             virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getCharacterStream( sal_Int32 columnIndex );
260             virtual ::com::sun::star::uno::Any SAL_CALL getObject( sal_Int32 columnIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& typeMap );
261             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRef > SAL_CALL getRef( sal_Int32 columnIndex );
262             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XBlob > SAL_CALL getBlob( sal_Int32 columnIndex );
263             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XClob > SAL_CALL getClob( sal_Int32 columnIndex );
264             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XArray > SAL_CALL getArray( sal_Int32 columnIndex );
265             // XResultSetMetaDataSupplier
266             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSetMetaData > SAL_CALL getMetaData(  );
267             // XCancellable
268             virtual void SAL_CALL cancel(  );
269             // XCloseable
270             virtual void SAL_CALL close(  );
271             // XWarningsSupplier
272             virtual ::com::sun::star::uno::Any SAL_CALL getWarnings(  );
273             virtual void SAL_CALL clearWarnings(  );
274             // XResultSetUpdate
275             virtual void SAL_CALL insertRow(  );
276             virtual void SAL_CALL updateRow(  );
277             virtual void SAL_CALL deleteRow(  );
278             virtual void SAL_CALL cancelRowUpdates(  );
279             virtual void SAL_CALL moveToInsertRow(  );
280             virtual void SAL_CALL moveToCurrentRow(  );
281             // XRowUpdate
282             virtual void SAL_CALL updateNull( sal_Int32 columnIndex );
283             virtual void SAL_CALL updateBoolean( sal_Int32 columnIndex, sal_Bool x );
284             virtual void SAL_CALL updateByte( sal_Int32 columnIndex, sal_Int8 x );
285             virtual void SAL_CALL updateShort( sal_Int32 columnIndex, sal_Int16 x );
286             virtual void SAL_CALL updateInt( sal_Int32 columnIndex, sal_Int32 x );
287             virtual void SAL_CALL updateLong( sal_Int32 columnIndex, sal_Int64 x );
288             virtual void SAL_CALL updateFloat( sal_Int32 columnIndex, float x );
289             virtual void SAL_CALL updateDouble( sal_Int32 columnIndex, double x );
290             virtual void SAL_CALL updateString( sal_Int32 columnIndex, const ::rtl::OUString& x );
291             virtual void SAL_CALL updateBytes( sal_Int32 columnIndex, const ::com::sun::star::uno::Sequence< sal_Int8 >& x );
292             virtual void SAL_CALL updateDate( sal_Int32 columnIndex, const ::com::sun::star::util::Date& x );
293             virtual void SAL_CALL updateTime( sal_Int32 columnIndex, const ::com::sun::star::util::Time& x );
294             virtual void SAL_CALL updateTimestamp( sal_Int32 columnIndex, const ::com::sun::star::util::DateTime& x );
295             virtual void SAL_CALL updateBinaryStream( sal_Int32 columnIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length );
296             virtual void SAL_CALL updateCharacterStream( sal_Int32 columnIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length );
297             virtual void SAL_CALL updateObject( sal_Int32 columnIndex, const ::com::sun::star::uno::Any& x );
298             virtual void SAL_CALL updateNumericObject( sal_Int32 columnIndex, const ::com::sun::star::uno::Any& x, sal_Int32 scale );
299             // XColumnLocate
300             virtual sal_Int32 SAL_CALL findColumn( const ::rtl::OUString& columnName );
301             // XRowLocate
302             virtual ::com::sun::star::uno::Any SAL_CALL getBookmark(  );
303             virtual sal_Bool SAL_CALL moveToBookmark( const ::com::sun::star::uno::Any& bookmark );
304             virtual sal_Bool SAL_CALL moveRelativeToBookmark( const ::com::sun::star::uno::Any& bookmark, sal_Int32 rows );
305             virtual sal_Int32 SAL_CALL compareBookmarks( const ::com::sun::star::uno::Any& first, const ::com::sun::star::uno::Any& second );
306             virtual sal_Bool SAL_CALL hasOrderedBookmarks(  );
307             virtual sal_Int32 SAL_CALL hashBookmark( const ::com::sun::star::uno::Any& bookmark );
308             // XDeleteRows
309             virtual ::com::sun::star::uno::Sequence< sal_Int32 > SAL_CALL deleteRows( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rows );
310 
311             // IResultSetHelper
312             virtual sal_Bool move(IResultSetHelper::Movement _eCursorPosition, sal_Int32 _nOffset, sal_Bool _bRetrieveData);
313             virtual sal_Int32 getDriverPos() const;
314             virtual sal_Bool deletedVisible() const;
315             virtual sal_Bool isRowDeleted() const;
316 
317         protected:
318             using OPropertySetHelper::getFastPropertyValue;
319         };
320     }
321 }
322 #endif // _CONNECTIVITY_ODBC_ORESULTSET_HXX_
323