Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_DBACCESS_SOURCE_CORE_API_KEYSET_HXX
21 : #define INCLUDED_DBACCESS_SOURCE_CORE_API_KEYSET_HXX
22 :
23 : #include "CacheSet.hxx"
24 :
25 : #include <cppuhelper/implbase1.hxx>
26 : #include <memory>
27 : #include <map>
28 : #include <vector>
29 :
30 : #include <com/sun/star/lang/XUnoTunnel.hpp>
31 : #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
32 : #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
33 : #include <comphelper/stl_types.hxx>
34 :
35 : namespace dbaccess
36 : {
37 0 : struct SelectColumnDescription
38 : {
39 : OUString sRealName; // may be empty
40 : OUString sTableName; // may be empty
41 : OUString sDefaultValue;
42 : sal_Int32 nPosition;
43 : sal_Int32 nType;
44 : sal_Int32 nScale;
45 : sal_Bool bNullable;
46 :
47 0 : SelectColumnDescription()
48 : :nPosition( 0 )
49 : ,nType( 0 )
50 : ,nScale( 0 )
51 0 : ,bNullable(sal_False)
52 : {
53 0 : }
54 :
55 0 : SelectColumnDescription( sal_Int32 _nPosition, sal_Int32 _nType, sal_Int32 _nScale,sal_Bool _bNullable, const OUString& _rDefaultValue )
56 : :sDefaultValue( _rDefaultValue )
57 : ,nPosition( _nPosition )
58 : ,nType( _nType )
59 : ,nScale( _nScale )
60 0 : ,bNullable(_bNullable)
61 : {
62 0 : }
63 : };
64 : typedef ::std::map< OUString, SelectColumnDescription, ::comphelper::UStringMixLess > SelectColumnsMetaData;
65 :
66 : // the elements of _rxQueryColumns must have the properties PROPERTY_REALNAME and PROPERTY_TABLENAME
67 : void getColumnPositions(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& _rxQueryColumns,
68 : const ::com::sun::star::uno::Sequence< OUString >& _rColumnNames,
69 : const OUString& _rsUpdateTableName,
70 : SelectColumnsMetaData& o_rColumnNames /* out */,
71 : bool i_bAppendTableName = false);
72 :
73 : typedef ::std::pair<ORowSetRow,::std::pair<sal_Int32,::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow> > > OKeySetValue;
74 : typedef ::std::map<sal_Int32,OKeySetValue > OKeySetMatrix;
75 : typedef ::std::map<sal_Int32,ORowSetValueVector > OUpdatedParameter;
76 : // is used when the source supports keys
77 : class OKeySet : public OCacheSet
78 : {
79 : protected:
80 : OKeySetMatrix m_aKeyMap;
81 : OKeySetMatrix::iterator m_aKeyIter;
82 :
83 : ::std::vector< OUString > m_aAutoColumns; // contains all columns which are autoincrement ones
84 :
85 : OUpdatedParameter m_aUpdatedParameter; // contains all parameter which have been updated and are needed for refetching
86 : ORowSetValueVector m_aParameterValueForCache;
87 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
88 : ::std::auto_ptr<SelectColumnsMetaData> m_pKeyColumnNames; // contains all key column names
89 : ::std::auto_ptr<SelectColumnsMetaData> m_pColumnNames; // contains all column names
90 : ::std::auto_ptr<SelectColumnsMetaData> m_pParameterNames; // contains all parameter names
91 : ::std::auto_ptr<SelectColumnsMetaData> m_pForeignColumnNames; // contains all column names of the rest
92 : SAL_WNODEPRECATED_DECLARATIONS_POP
93 : connectivity::OSQLTable m_xTable; // reference to our table
94 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess> m_xTableKeys;
95 : // we need a different SQL (statement) for each different combination
96 : // of NULLness of key & foreign columns;
97 : // each subclause is either "colName = ?" or "colName IS NULL"
98 : // (we avoid the standard "colName IS NOT DISTINCT FROM ?" because it is not widely supported)
99 : typedef ::std::vector< bool > FilterColumnsNULL_t;
100 : typedef ::std::map< FilterColumnsNULL_t,
101 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > >
102 : vStatements_t;
103 : vStatements_t m_vStatements;
104 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement> m_xStatement;
105 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet> m_xSet;
106 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow> m_xRow;
107 : ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer > m_xComposer;
108 : const OUString m_sUpdateTableName;
109 : ::std::vector< OUString > m_aFilterColumns;
110 : sal_Int32& m_rRowCount;
111 :
112 : sal_Bool m_bRowCountFinal;
113 :
114 : /** copies the values from the insert row into the key row
115 : *
116 : * \param _rInsertRow the row which was inserted
117 : * \param _rKeyRow The current key row of the row set.
118 : + \param i_nBookmark The bookmark is used to update the parameter
119 : */
120 : void copyRowValue(const ORowSetRow& _rInsertRow,ORowSetRow& _rKeyRow,sal_Int32 i_nBookmark);
121 :
122 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > getKeyColumns() const;
123 : // returns true if it did any work
124 : bool fillAllRows();
125 : sal_Bool fetchRow();
126 : void invalidateRow();
127 :
128 : void impl_convertValue_throw(const ORowSetRow& _rInsertRow,const SelectColumnDescription& i_aMetaData);
129 : void initColumns();
130 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
131 : void findTableColumnsMatching_throw( const ::com::sun::star::uno::Any& i_aTable,
132 : const OUString& i_rUpdateTableName,
133 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData>& i_xMeta,
134 : const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>& i_xQueryColumns,
135 : ::std::auto_ptr<SelectColumnsMetaData>& o_pKeyColumnNames);
136 : SAL_WNODEPRECATED_DECLARATIONS_POP
137 : void ensureStatement( );
138 : virtual void makeNewStatement( );
139 : void setOneKeyColumnParameter( sal_Int32 &nPos,
140 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XParameters > &_xParameter,
141 : const connectivity::ORowSetValue &_rValue,
142 : sal_Int32 _nType,
143 : sal_Int32 _nScale ) const;
144 : OUStringBuffer createKeyFilter( );
145 : bool doTryRefetch_throw() throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);;
146 : void tryRefetch(const ORowSetRow& _rInsertRow,bool bRefetch);
147 : void executeUpdate(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOriginalRow,const OUString& i_sSQL,const OUString& i_sTableName,const ::std::vector<sal_Int32>& _aIndexColumnPositions = ::std::vector<sal_Int32>());
148 : void executeInsert( const ORowSetRow& _rInsertRow,const OUString& i_sSQL,const OUString& i_sTableName = OUString(),bool bRefetch = false);
149 : void executeStatement(OUStringBuffer& io_aFilter, ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryComposer>& io_xAnalyzer);
150 :
151 : virtual ~OKeySet();
152 : public:
153 : OKeySet(const connectivity::OSQLTable& _xTable,
154 : const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>& _xTableKeys,
155 : const OUString& _rUpdateTableName,
156 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer >& _xComposer,
157 : const ORowSetValueVector& _aParameterValueForCache,
158 : sal_Int32 i_nMaxRows,
159 : sal_Int32& o_nRowCount);
160 :
161 : // late ctor which can throw exceptions
162 : virtual void construct(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& _xDriverSet,const OUString& i_sRowSetFilter) SAL_OVERRIDE;
163 : virtual void reset(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& _xDriverSet) SAL_OVERRIDE;
164 :
165 : // ::com::sun::star::sdbc::XRow
166 : virtual sal_Bool SAL_CALL wasNull( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
167 : virtual OUString SAL_CALL getString( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
168 : virtual sal_Bool SAL_CALL getBoolean( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
169 : virtual sal_Int8 SAL_CALL getByte( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
170 : virtual sal_Int16 SAL_CALL getShort( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
171 : virtual sal_Int32 SAL_CALL getInt( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
172 : virtual sal_Int64 SAL_CALL getLong( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
173 : virtual float SAL_CALL getFloat( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
174 : virtual double SAL_CALL getDouble( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
175 : 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, std::exception) SAL_OVERRIDE;
176 : virtual ::com::sun::star::util::Date SAL_CALL getDate( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
177 : virtual ::com::sun::star::util::Time SAL_CALL getTime( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
178 : virtual ::com::sun::star::util::DateTime SAL_CALL getTimestamp( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
179 : 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, std::exception) SAL_OVERRIDE;
180 : 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, std::exception) SAL_OVERRIDE;
181 : 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, std::exception) SAL_OVERRIDE;
182 : 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, std::exception) SAL_OVERRIDE;
183 : 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, std::exception) SAL_OVERRIDE;
184 : 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, std::exception) SAL_OVERRIDE;
185 : 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, std::exception) SAL_OVERRIDE;
186 :
187 :
188 : virtual sal_Bool SAL_CALL rowUpdated( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
189 : virtual sal_Bool SAL_CALL rowInserted( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
190 : virtual sal_Bool SAL_CALL rowDeleted( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
191 : // ::com::sun::star::sdbc::XResultSet
192 : virtual sal_Bool SAL_CALL next( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
193 : virtual sal_Bool SAL_CALL isBeforeFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
194 : virtual sal_Bool SAL_CALL isAfterLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
195 : virtual sal_Bool SAL_CALL isFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
196 : virtual sal_Bool SAL_CALL isLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
197 : virtual void SAL_CALL beforeFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
198 : virtual void SAL_CALL afterLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
199 : virtual sal_Bool SAL_CALL first( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
200 : virtual sal_Bool SAL_CALL last( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
201 : virtual sal_Int32 SAL_CALL getRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
202 : virtual sal_Bool SAL_CALL absolute( sal_Int32 row ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
203 : virtual sal_Bool SAL_CALL relative( sal_Int32 rows ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
204 : virtual sal_Bool SAL_CALL previous( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
205 : virtual void SAL_CALL ensureRowForData( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
206 : virtual void SAL_CALL refreshRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
207 : // ::com::sun::star::sdbcx::XRowLocate
208 : virtual ::com::sun::star::uno::Any SAL_CALL getBookmark() throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
209 :
210 : virtual sal_Bool SAL_CALL moveToBookmark( const ::com::sun::star::uno::Any& bookmark ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
211 :
212 : 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) SAL_OVERRIDE;
213 :
214 : 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) SAL_OVERRIDE;
215 :
216 : virtual sal_Bool SAL_CALL hasOrderedBookmarks( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
217 :
218 : virtual sal_Int32 SAL_CALL hashBookmark( const ::com::sun::star::uno::Any& bookmark ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
219 :
220 : // ::com::sun::star::sdbcx::XDeleteRows
221 : 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) SAL_OVERRIDE;
222 : // ::com::sun::star::sdbc::XResultSetUpdate
223 : virtual void SAL_CALL updateRow(const ORowSetRow& _rInsertRow,const ORowSetRow& _rOriginalRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
224 : virtual void SAL_CALL deleteRow(const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
225 : virtual void SAL_CALL insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
226 : virtual void SAL_CALL cancelRowUpdates( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
227 : virtual void SAL_CALL moveToInsertRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
228 : virtual void SAL_CALL moveToCurrentRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
229 :
230 :
231 : virtual sal_Bool previous_checked( sal_Bool i_bFetchRow ) SAL_OVERRIDE;
232 : virtual sal_Bool absolute_checked( sal_Int32 row,sal_Bool i_bFetchRow ) SAL_OVERRIDE;
233 : virtual sal_Bool last_checked( sal_Bool i_bFetchRow) SAL_OVERRIDE;
234 : };
235 : }
236 : #endif // INCLUDED_DBACCESS_SOURCE_CORE_API_KEYSET_HXX
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|