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 336 : 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 : bool bNullable;
46 :
47 112 : SelectColumnDescription()
48 : :nPosition( 0 )
49 : ,nType( 0 )
50 : ,nScale( 0 )
51 112 : ,bNullable(false)
52 : {
53 112 : }
54 :
55 112 : SelectColumnDescription( sal_Int32 _nPosition, sal_Int32 _nType, sal_Int32 _nScale,bool _bNullable, const OUString& _rDefaultValue )
56 : :sDefaultValue( _rDefaultValue )
57 : ,nPosition( _nPosition )
58 : ,nType( _nType )
59 : ,nScale( _nScale )
60 112 : ,bNullable(_bNullable)
61 : {
62 112 : }
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 : ::std::unique_ptr<SelectColumnsMetaData> m_pKeyColumnNames; // contains all key column names
88 : ::std::unique_ptr<SelectColumnsMetaData> m_pColumnNames; // contains all column names
89 : ::std::unique_ptr<SelectColumnsMetaData> m_pParameterNames; // contains all parameter names
90 : ::std::unique_ptr<SelectColumnsMetaData> m_pForeignColumnNames; // contains all column names of the rest
91 : connectivity::OSQLTable m_xTable; // reference to our table
92 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess> m_xTableKeys;
93 : // we need a different SQL (statement) for each different combination
94 : // of NULLness of key & foreign columns;
95 : // each subclause is either "colName = ?" or "colName IS NULL"
96 : // (we avoid the standard "colName IS NOT DISTINCT FROM ?" because it is not widely supported)
97 : typedef ::std::vector< bool > FilterColumnsNULL_t;
98 : typedef ::std::map< FilterColumnsNULL_t,
99 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > >
100 : vStatements_t;
101 : vStatements_t m_vStatements;
102 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement> m_xStatement;
103 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet> m_xSet;
104 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow> m_xRow;
105 : ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer > m_xComposer;
106 : const OUString m_sUpdateTableName;
107 : ::std::vector< OUString > m_aFilterColumns;
108 : sal_Int32& m_rRowCount;
109 :
110 : bool m_bRowCountFinal;
111 :
112 : /** copies the values from the insert row into the key row
113 : *
114 : * \param _rInsertRow the row which was inserted
115 : * \param _rKeyRow The current key row of the row set.
116 : + \param i_nBookmark The bookmark is used to update the parameter
117 : */
118 : void copyRowValue(const ORowSetRow& _rInsertRow,ORowSetRow& _rKeyRow,sal_Int32 i_nBookmark);
119 :
120 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > getKeyColumns() const;
121 : // returns true if it did any work
122 : bool fillAllRows();
123 : bool fetchRow();
124 : void invalidateRow();
125 :
126 : static void impl_convertValue_throw(const ORowSetRow& _rInsertRow,const SelectColumnDescription& i_aMetaData);
127 : void initColumns();
128 : void findTableColumnsMatching_throw( const ::com::sun::star::uno::Any& i_aTable,
129 : const OUString& i_rUpdateTableName,
130 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData>& i_xMeta,
131 : const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>& i_xQueryColumns,
132 : ::std::unique_ptr<SelectColumnsMetaData>& o_pKeyColumnNames);
133 : void ensureStatement( );
134 : virtual void makeNewStatement( );
135 : static void setOneKeyColumnParameter( sal_Int32 &nPos,
136 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XParameters > &_xParameter,
137 : const connectivity::ORowSetValue &_rValue,
138 : sal_Int32 _nType,
139 : sal_Int32 _nScale );
140 : OUStringBuffer createKeyFilter( );
141 : bool doTryRefetch_throw() throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);;
142 : void tryRefetch(const ORowSetRow& _rInsertRow,bool bRefetch);
143 : 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>());
144 : void executeInsert( const ORowSetRow& _rInsertRow,const OUString& i_sSQL,const OUString& i_sTableName = OUString(),bool bRefetch = false);
145 : void executeStatement(OUStringBuffer& io_aFilter, ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryComposer>& io_xAnalyzer);
146 :
147 : virtual ~OKeySet();
148 : public:
149 : OKeySet(const connectivity::OSQLTable& _xTable,
150 : const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>& _xTableKeys,
151 : const OUString& _rUpdateTableName,
152 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer >& _xComposer,
153 : const ORowSetValueVector& _aParameterValueForCache,
154 : sal_Int32 i_nMaxRows,
155 : sal_Int32& o_nRowCount);
156 :
157 : // late ctor which can throw exceptions
158 : virtual void construct(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& _xDriverSet,const OUString& i_sRowSetFilter) SAL_OVERRIDE;
159 : virtual void reset(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& _xDriverSet) SAL_OVERRIDE;
160 :
161 : // ::com::sun::star::sdbc::XRow
162 : virtual sal_Bool SAL_CALL wasNull( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
163 : virtual OUString SAL_CALL getString( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
164 : virtual sal_Bool SAL_CALL getBoolean( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
165 : virtual sal_Int8 SAL_CALL getByte( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
166 : virtual sal_Int16 SAL_CALL getShort( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
167 : virtual sal_Int32 SAL_CALL getInt( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
168 : virtual sal_Int64 SAL_CALL getLong( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
169 : virtual float SAL_CALL getFloat( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
170 : virtual double SAL_CALL getDouble( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
171 : 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;
172 : 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;
173 : 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;
174 : 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;
175 : 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;
176 : 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;
177 : 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;
178 : 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;
179 : 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;
180 : 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;
181 : 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;
182 :
183 :
184 : virtual bool SAL_CALL rowUpdated( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
185 : virtual bool SAL_CALL rowInserted( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
186 : virtual bool SAL_CALL rowDeleted( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
187 : // ::com::sun::star::sdbc::XResultSet
188 : virtual bool SAL_CALL next( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
189 : virtual bool SAL_CALL isBeforeFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
190 : virtual bool SAL_CALL isAfterLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
191 : virtual bool SAL_CALL isFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
192 : virtual bool SAL_CALL isLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
193 : virtual void SAL_CALL beforeFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
194 : virtual void SAL_CALL afterLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
195 : virtual bool SAL_CALL first( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
196 : virtual bool SAL_CALL last( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
197 : virtual sal_Int32 SAL_CALL getRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
198 : virtual bool SAL_CALL absolute( sal_Int32 row ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
199 : virtual bool SAL_CALL relative( sal_Int32 rows ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
200 : virtual bool SAL_CALL previous( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
201 : void SAL_CALL ensureRowForData( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
202 : virtual void SAL_CALL refreshRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
203 : // ::com::sun::star::sdbcx::XRowLocate
204 : virtual ::com::sun::star::uno::Any SAL_CALL getBookmark() throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
205 :
206 : virtual bool SAL_CALL moveToBookmark( const ::com::sun::star::uno::Any& bookmark ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
207 :
208 : virtual 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;
209 :
210 : 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;
211 :
212 : virtual bool SAL_CALL hasOrderedBookmarks( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
213 :
214 : 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;
215 :
216 : // ::com::sun::star::sdbcx::XDeleteRows
217 : 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;
218 : // ::com::sun::star::sdbc::XResultSetUpdate
219 : 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;
220 : 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;
221 : 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;
222 : virtual void SAL_CALL cancelRowUpdates( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
223 : virtual void SAL_CALL moveToInsertRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
224 : virtual void SAL_CALL moveToCurrentRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) SAL_OVERRIDE;
225 :
226 :
227 : virtual bool previous_checked( bool i_bFetchRow ) SAL_OVERRIDE;
228 : virtual bool absolute_checked( sal_Int32 row,bool i_bFetchRow ) SAL_OVERRIDE;
229 : virtual bool last_checked( bool i_bFetchRow) SAL_OVERRIDE;
230 : };
231 : }
232 : #endif // INCLUDED_DBACCESS_SOURCE_CORE_API_KEYSET_HXX
233 :
234 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|