xref: /trunk/main/dbaccess/source/core/api/KeySet.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef DBACCESS_CORE_API_KEYSET_HXX
29 #define DBACCESS_CORE_API_KEYSET_HXX
30 
31 #ifndef DBACCESS_CORE_API_CACHESET_HXX
32 #include "CacheSet.hxx"
33 #endif
34 
35 #ifndef _CPPUHELPER_IMPLBASE1_HXX_
36 #include <cppuhelper/implbase1.hxx>
37 #endif
38 #include <memory>
39 #include <map>
40 
41 #ifndef _COM_SUN_STAR_LANG_XUNOTUNNEL_HPP_
42 #include <com/sun/star/lang/XUnoTunnel.hpp>
43 #endif
44 #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
45 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
46 #ifndef _COMPHELPER_STLTYPES_HXX_
47 #include <comphelper/stl_types.hxx>
48 #endif
49 
50 namespace dbaccess
51 {
52     struct SelectColumnDescription
53     {
54         ::rtl::OUString sRealName;      // may be empty
55         ::rtl::OUString sTableName;      // may be empty
56         ::rtl::OUString sDefaultValue;
57         sal_Int32       nPosition;
58         sal_Int32       nType;
59         sal_Int32       nScale;
60         sal_Bool        bNullable;
61 
62 
63 
64         SelectColumnDescription()
65             :nPosition( 0 )
66             ,nType( 0 )
67             ,nScale( 0 )
68             ,bNullable(sal_False)
69         {
70         }
71 
72         SelectColumnDescription( sal_Int32 _nPosition, sal_Int32 _nType, sal_Int32 _nScale,sal_Bool _bNullable, const ::rtl::OUString& _rDefaultValue )
73             :sDefaultValue( _rDefaultValue )
74             ,nPosition( _nPosition )
75             ,nType( _nType )
76             ,nScale( _nScale )
77             ,bNullable(_bNullable)
78         {
79         }
80     };
81     typedef ::std::map< ::rtl::OUString, SelectColumnDescription, ::comphelper::UStringMixLess >    SelectColumnsMetaData;
82 
83 	// the elements of _rxQueryColumns must have the properties PROPERTY_REALNAME and PROPERTY_TABLENAME
84 	void getColumnPositions(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& _rxQueryColumns,
85                             const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rColumnNames,
86 							const ::rtl::OUString& _rsUpdateTableName,
87 							SelectColumnsMetaData& o_rColumnNames /* out */,
88                             bool i_bAppendTableName = false);
89 
90     typedef ::std::pair<ORowSetRow,::std::pair<sal_Int32,::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow> > > OKeySetValue;
91 	typedef ::std::map<sal_Int32,OKeySetValue > OKeySetMatrix;
92     typedef ::std::map<sal_Int32,ORowSetValueVector > OUpdatedParameter;
93 	// is used when the source supports keys
94 	class OKeySet : public OCacheSet
95 	{
96     protected:
97 		OKeySetMatrix											m_aKeyMap;
98 		OKeySetMatrix::iterator									m_aKeyIter;
99 
100 		::std::vector< ::rtl::OUString >						m_aAutoColumns;	 // contains all columns which are autoincrement ones
101 
102         OUpdatedParameter                                       m_aUpdatedParameter;    // contains all parameter which have been updated and are needed for refetching
103         ORowSetValueVector                                      m_aParameterValueForCache;
104         ::std::auto_ptr<SelectColumnsMetaData>                  m_pKeyColumnNames;      // contains all key column names
105         ::std::auto_ptr<SelectColumnsMetaData>                  m_pColumnNames;         // contains all column names
106         ::std::auto_ptr<SelectColumnsMetaData>                  m_pParameterNames;      // contains all parameter names
107         ::std::auto_ptr<SelectColumnsMetaData>                  m_pForeignColumnNames;  // contains all column names of the rest
108 		connectivity::OSQLTable									m_xTable; // reference to our table
109         ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>    m_xTableKeys;
110 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement>	m_xStatement;
111 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>			m_xSet;
112 		::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow>					m_xRow;
113 		::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer >	m_xComposer;
114 		::rtl::OUString																	m_sUpdateTableName;
115         ::std::vector< ::rtl::OUString >						m_aFilterColumns;
116         sal_Int32&                                              m_rRowCount;
117 
118 		sal_Bool m_bRowCountFinal;
119 
120 		/**
121 			getComposedTableName return the composed table name for the query
122 			@param _sCatalog	the catalogname may be empty
123 			@param _sSchema		the schemaname may be empty
124 			@param _sTable		the tablename
125 
126 			@return the composed name
127 		*/
128 		::rtl::OUString getComposedTableName( const ::rtl::OUString& _sCatalog,
129 											  const ::rtl::OUString& _sSchema,
130 											  const ::rtl::OUString& _sTable);
131 
132         /** copies the values from the insert row into the key row
133         *
134         * \param _rInsertRow the row which was inserted
135         * \param _rKeyRow The current key row of the row set.
136         + \param i_nBookmark The bookmark is used to update the parameter
137         */
138         void copyRowValue(const ORowSetRow& _rInsertRow,ORowSetRow& _rKeyRow,sal_Int32 i_nBookmark);
139 
140 		::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > getKeyColumns() const;
141 		void fillAllRows();
142 		sal_Bool fetchRow();
143 
144         void impl_convertValue_throw(const ORowSetRow& _rInsertRow,const SelectColumnDescription& i_aMetaData);
145         void initColumns();
146         void findTableColumnsMatching_throw( const ::com::sun::star::uno::Any& i_aTable,
147                                              const ::rtl::OUString& i_rUpdateTableName,
148                                              const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData>& i_xMeta,
149                                              const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>& i_xQueryColumns,
150                                              ::std::auto_ptr<SelectColumnsMetaData>& o_pKeyColumnNames);
151         ::rtl::OUStringBuffer createKeyFilter();
152         void tryRefetch(const ORowSetRow& _rInsertRow,bool bRefetch);
153         void executeUpdate(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOrginalRow,const ::rtl::OUString& i_sSQL,const ::rtl::OUString& i_sTableName,const ::std::vector<sal_Int32>& _aIndexColumnPositions = ::std::vector<sal_Int32>());
154         void executeInsert( const ORowSetRow& _rInsertRow,const ::rtl::OUString& i_sSQL,const ::rtl::OUString& i_sTableName = ::rtl::OUString(),bool bRefetch = false);
155         void executeStatement(::rtl::OUStringBuffer& io_aFilter,const ::rtl::OUString& i_sRowSetFilter,::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryComposer>& io_xAnalyzer);
156 
157 		virtual ~OKeySet();
158 	public:
159 		OKeySet(const connectivity::OSQLTable& _xTable,
160                 const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>& _xTableKeys,
161 				const ::rtl::OUString& _rUpdateTableName,
162 				const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer >& _xComposer,
163                 const ORowSetValueVector& _aParameterValueForCache,
164                 sal_Int32 i_nMaxRows,
165                 sal_Int32& o_nRowCount);
166 
167 		// late ctor which can throw exceptions
168 		virtual void construct(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& _xDriverSet,const ::rtl::OUString& i_sRowSetFilter);
169 
170 		// ::com::sun::star::sdbc::XRow
171 		virtual sal_Bool SAL_CALL wasNull(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
172         virtual ::rtl::OUString SAL_CALL getString( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
173         virtual sal_Bool SAL_CALL getBoolean( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
174         virtual sal_Int8 SAL_CALL getByte( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
175         virtual sal_Int16 SAL_CALL getShort( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
176         virtual sal_Int32 SAL_CALL getInt( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
177         virtual sal_Int64 SAL_CALL getLong( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
178         virtual float SAL_CALL getFloat( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
179         virtual double SAL_CALL getDouble( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
180         virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getBytes( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
181         virtual ::com::sun::star::util::Date SAL_CALL getDate( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
182         virtual ::com::sun::star::util::Time SAL_CALL getTime( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
183         virtual ::com::sun::star::util::DateTime SAL_CALL getTimestamp( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
184         virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getBinaryStream( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
185         virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getCharacterStream( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
186         virtual ::com::sun::star::uno::Any SAL_CALL getObject( sal_Int32 columnIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& typeMap ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
187         virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRef > SAL_CALL getRef( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
188         virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XBlob > SAL_CALL getBlob( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
189         virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XClob > SAL_CALL getClob( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
190         virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XArray > SAL_CALL getArray( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
191 
192 
193 		virtual sal_Bool SAL_CALL rowUpdated(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
194         virtual sal_Bool SAL_CALL rowInserted(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
195         virtual sal_Bool SAL_CALL rowDeleted(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
196 		// ::com::sun::star::sdbc::XResultSet
197 		virtual sal_Bool SAL_CALL next(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
198 		virtual sal_Bool SAL_CALL isBeforeFirst(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
199 		virtual sal_Bool SAL_CALL isAfterLast(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
200 		virtual sal_Bool SAL_CALL isFirst(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
201 		virtual sal_Bool SAL_CALL isLast(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
202 		virtual void SAL_CALL beforeFirst(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
203 		virtual void SAL_CALL afterLast(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
204 		virtual sal_Bool SAL_CALL first(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
205 		virtual sal_Bool SAL_CALL last(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
206 		virtual sal_Int32 SAL_CALL getRow(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
207 		virtual sal_Bool SAL_CALL absolute( sal_Int32 row ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
208 		virtual sal_Bool SAL_CALL relative( sal_Int32 rows ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
209         virtual sal_Bool SAL_CALL previous(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
210 		virtual void SAL_CALL refreshRow(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
211 		// ::com::sun::star::sdbcx::XRowLocate
212 		virtual ::com::sun::star::uno::Any SAL_CALL getBookmark() throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
213 		// -------------------------------------------------------------------------
214 		virtual sal_Bool SAL_CALL moveToBookmark( const ::com::sun::star::uno::Any& bookmark ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
215 		// -------------------------------------------------------------------------
216 		virtual sal_Bool SAL_CALL moveRelativeToBookmark( const ::com::sun::star::uno::Any& bookmark, sal_Int32 rows ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
217 		// -------------------------------------------------------------------------
218 		virtual sal_Int32 SAL_CALL compareBookmarks( const ::com::sun::star::uno::Any& first, const ::com::sun::star::uno::Any& second ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
219 		// -------------------------------------------------------------------------
220 		virtual sal_Bool SAL_CALL hasOrderedBookmarks(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
221 		// -------------------------------------------------------------------------
222 		virtual sal_Int32 SAL_CALL hashBookmark( const ::com::sun::star::uno::Any& bookmark ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
223 		// -------------------------------------------------------------------------
224 		// ::com::sun::star::sdbcx::XDeleteRows
225 		virtual ::com::sun::star::uno::Sequence< sal_Int32 > SAL_CALL deleteRows( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rows ,const connectivity::OSQLTable& _xTable) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
226 		// ::com::sun::star::sdbc::XResultSetUpdate
227 		virtual void SAL_CALL updateRow(const ORowSetRow& _rInsertRow,const ORowSetRow& _rOrginalRow,const connectivity::OSQLTable& _xTable   ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
228 		virtual void SAL_CALL deleteRow(const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable   ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
229 		virtual void SAL_CALL insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
230 		virtual void SAL_CALL cancelRowUpdates(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
231 		virtual void SAL_CALL moveToInsertRow(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
232 		virtual void SAL_CALL moveToCurrentRow(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
233 
234 
235         virtual sal_Bool previous_checked( sal_Bool i_bFetchRow );
236         virtual sal_Bool absolute_checked( sal_Int32 row,sal_Bool i_bFetchRow );
237         virtual sal_Bool last_checked( sal_Bool i_bFetchRow);
238 	};
239 }
240 #endif // DBACCESS_CORE_API_KEYSET_HXX
241 
242