LibreOffice Module dbaccess (master) 1
KeySet.hxx
Go to the documentation of this file.
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#pragma once
21
22#include "CacheSet.hxx"
23
24#include <memory>
25#include <map>
26#include <utility>
27#include <vector>
28
29#include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
30#include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
32
33namespace dbaccess
34{
36 {
37 OUString sRealName; // may be empty
38 OUString sTableName; // may be empty
39 OUString sDefaultValue;
40 sal_Int32 nPosition;
41 sal_Int32 nType;
42 sal_Int32 nScale;
44
46 :nPosition( 0 )
47 ,nType( 0 )
48 ,nScale( 0 )
49 ,bNullable(false)
50 {
51 }
52
53 SelectColumnDescription( sal_Int32 _nPosition, sal_Int32 _nType, sal_Int32 _nScale, bool _bNullable, OUString _sDefaultValue )
54 :sDefaultValue(std::move( _sDefaultValue ))
55 ,nPosition( _nPosition )
56 ,nType( _nType )
57 ,nScale( _nScale )
58 ,bNullable(_bNullable)
59 {
60 }
61 };
62 typedef std::map< OUString, SelectColumnDescription, ::comphelper::UStringMixLess > SelectColumnsMetaData;
63
64 // the elements of _rxQueryColumns must have the properties PROPERTY_REALNAME and PROPERTY_TABLENAME
65 void getColumnPositions(const css::uno::Reference< css::container::XNameAccess >& _rxQueryColumns,
66 const css::uno::Sequence< OUString >& _rColumnNames,
67 std::u16string_view _rsUpdateTableName,
68 SelectColumnsMetaData& o_rColumnNames /* out */,
69 bool i_bAppendTableName = false);
70
71 typedef std::pair<ORowSetRow,std::pair<sal_Int32,css::uno::Reference< css::sdbc::XRow> > > OKeySetValue;
72 typedef std::map<sal_Int32,OKeySetValue > OKeySetMatrix;
73 typedef std::map<sal_Int32, rtl::Reference<ORowSetValueVector> > OUpdatedParameter;
74 // is used when the source supports keys
75 class OKeySet : public OCacheSet
76 {
77 protected:
79 OKeySetMatrix::iterator m_aKeyIter;
80
81 std::vector< OUString > m_aAutoColumns; // contains all columns which are autoincrement ones
82
83 OUpdatedParameter m_aUpdatedParameter; // contains all parameter which have been updated and are needed for refetching
85 std::unique_ptr<SelectColumnsMetaData> m_pKeyColumnNames; // contains all key column names
86 std::unique_ptr<SelectColumnsMetaData> m_pColumnNames; // contains all column names
87 std::unique_ptr<SelectColumnsMetaData> m_pParameterNames; // contains all parameter names
88 std::unique_ptr<SelectColumnsMetaData> m_pForeignColumnNames; // contains all column names of the rest
89 connectivity::OSQLTable m_xTable; // reference to our table
90 // we need a different SQL (statement) for each different combination
91 // of NULLness of key & foreign columns;
92 // each subclause is either "colName = ?" or "colName IS NULL"
93 // (we avoid the standard "colName IS NOT DISTINCT FROM ?" because it is not widely supported)
94 typedef std::map< std::vector<bool>,
95 css::uno::Reference< css::sdbc::XPreparedStatement > >
98 css::uno::Reference< css::sdbc::XPreparedStatement> m_xStatement;
99 css::uno::Reference< css::sdbc::XResultSet> m_xSet;
100 css::uno::Reference< css::sdbc::XRow> m_xRow;
101 css::uno::Reference< css::sdb::XSingleSelectQueryAnalyzer > m_xComposer;
102 const OUString m_sUpdateTableName;
103 std::vector< OUString > m_aFilterColumns;
104 sal_Int32& m_rRowCount;
105
107
114 void copyRowValue(const ORowSetRow& _rInsertRow, ORowSetRow const & _rKeyRow, sal_Int32 i_nBookmark);
115
116 // returns true if it did any work
117 bool fillAllRows();
118 bool fetchRow();
119 void invalidateRow();
120
121 static void impl_convertValue_throw(const ORowSetRow& _rInsertRow,const SelectColumnDescription& i_aMetaData);
122 void initColumns();
123 void findTableColumnsMatching_throw( const css::uno::Any& i_aTable,
124 const OUString& i_rUpdateTableName,
125 const css::uno::Reference< css::sdbc::XDatabaseMetaData>& i_xMeta,
126 const css::uno::Reference< css::container::XNameAccess>& i_xQueryColumns,
127 std::unique_ptr<SelectColumnsMetaData> const & o_pKeyColumnNames);
128 void ensureStatement( );
129 virtual void makeNewStatement( );
130 static void setOneKeyColumnParameter( sal_Int32 &nPos,
131 const css::uno::Reference< css::sdbc::XParameters > &_xParameter,
132 const connectivity::ORowSetValue &_rValue,
133 sal_Int32 _nType,
134 sal_Int32 _nScale );
135 OUStringBuffer createKeyFilter( );
138 bool doTryRefetch_throw();
139 void tryRefetch(const ORowSetRow& _rInsertRow,bool bRefetch);
140 void executeUpdate(const ORowSetRow& _rInsertRow, const ORowSetRow& _rOriginalRow, const OUString& i_sSQL, std::u16string_view i_sTableName,const std::vector<sal_Int32>& _aIndexColumnPositions = std::vector<sal_Int32>());
141 void executeInsert( const ORowSetRow& _rInsertRow, const OUString& i_sSQL, std::u16string_view i_sTableName, bool bRefetch = false);
142 void executeStatement(OUStringBuffer& io_aFilter, css::uno::Reference< css::sdb::XSingleSelectQueryComposer>& io_xAnalyzer);
143
144 virtual ~OKeySet() override;
145 public:
147 OUString _sUpdateTableName,
148 const css::uno::Reference< css::sdb::XSingleSelectQueryAnalyzer >& _xComposer,
149 const ORowSetValueVector& _aParameterValueForCache,
150 sal_Int32 i_nMaxRows,
151 sal_Int32& o_nRowCount);
152
153 // late ctor which can throw exceptions
154 virtual void construct(const css::uno::Reference< css::sdbc::XResultSet>& _xDriverSet,const OUString& i_sRowSetFilter) override;
155 virtual void reset(const css::uno::Reference< css::sdbc::XResultSet>& _xDriverSet) override;
156
157 // css::sdbc::XRow
158 virtual sal_Bool SAL_CALL wasNull( ) override;
159 virtual OUString SAL_CALL getString( sal_Int32 columnIndex ) override;
160 virtual sal_Bool SAL_CALL getBoolean( sal_Int32 columnIndex ) override;
161 virtual sal_Int8 SAL_CALL getByte( sal_Int32 columnIndex ) override;
162 virtual sal_Int16 SAL_CALL getShort( sal_Int32 columnIndex ) override;
163 virtual sal_Int32 SAL_CALL getInt( sal_Int32 columnIndex ) override;
164 virtual sal_Int64 SAL_CALL getLong( sal_Int32 columnIndex ) override;
165 virtual float SAL_CALL getFloat( sal_Int32 columnIndex ) override;
166 virtual double SAL_CALL getDouble( sal_Int32 columnIndex ) override;
167 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getBytes( sal_Int32 columnIndex ) override;
168 virtual css::util::Date SAL_CALL getDate( sal_Int32 columnIndex ) override;
169 virtual css::util::Time SAL_CALL getTime( sal_Int32 columnIndex ) override;
170 virtual css::util::DateTime SAL_CALL getTimestamp( sal_Int32 columnIndex ) override;
171 virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getBinaryStream( sal_Int32 columnIndex ) override;
172 virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getCharacterStream( sal_Int32 columnIndex ) override;
173 virtual css::uno::Any SAL_CALL getObject( sal_Int32 columnIndex, const css::uno::Reference< css::container::XNameAccess >& typeMap ) override;
174 virtual css::uno::Reference< css::sdbc::XRef > SAL_CALL getRef( sal_Int32 columnIndex ) override;
175 virtual css::uno::Reference< css::sdbc::XBlob > SAL_CALL getBlob( sal_Int32 columnIndex ) override;
176 virtual css::uno::Reference< css::sdbc::XClob > SAL_CALL getClob( sal_Int32 columnIndex ) override;
177 virtual css::uno::Reference< css::sdbc::XArray > SAL_CALL getArray( sal_Int32 columnIndex ) override;
178
179
180 virtual bool rowUpdated( ) override;
181 virtual bool rowInserted( ) override;
182 virtual bool rowDeleted( ) override;
183 bool isBeforeFirst( );
184 bool isAfterLast( );
185
186 // css::sdbc::XResultSet
187 virtual bool next() override;
188 virtual void beforeFirst( ) override;
189 virtual void afterLast( ) override;
190 virtual bool first() override;
191 virtual bool last( ) override;
192 virtual sal_Int32 getRow( ) override;
193 virtual bool absolute( sal_Int32 row ) override;
194 virtual bool previous( ) override;
197 void ensureRowForData( );
198 virtual void refreshRow( ) override;
199 // css::sdbcx::XRowLocate
200 virtual css::uno::Any getBookmark() override;
201
202 virtual bool moveToBookmark( const css::uno::Any& bookmark ) override;
203
204 virtual sal_Int32 compareBookmarks( const css::uno::Any& first, const css::uno::Any& second ) override;
205
206 virtual bool hasOrderedBookmarks( ) override;
207
208 virtual sal_Int32 hashBookmark( const css::uno::Any& bookmark ) override;
209
210 // css::sdbc::XResultSetUpdate
211 virtual void updateRow(const ORowSetRow& _rInsertRow,const ORowSetRow& _rOriginalRow,const connectivity::OSQLTable& _xTable ) override;
212 virtual void deleteRow(const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) override;
213 virtual void insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) override;
214 };
215}
216
217/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Bool SAL_CALL wasNull() override
Definition: KeySet.cxx:1228
void ensureStatement()
Definition: KeySet.cxx:332
void executeUpdate(const ORowSetRow &_rInsertRow, const ORowSetRow &_rOriginalRow, const OUString &i_sSQL, std::u16string_view i_sTableName, const std::vector< sal_Int32 > &_aIndexColumnPositions=std::vector< sal_Int32 >())
Definition: KeySet.cxx:535
std::unique_ptr< SelectColumnsMetaData > m_pKeyColumnNames
Definition: KeySet.hxx:85
void initColumns()
Definition: KeySet.cxx:136
void executeStatement(OUStringBuffer &io_aFilter, css::uno::Reference< css::sdb::XSingleSelectQueryComposer > &io_xAnalyzer)
Definition: KeySet.cxx:366
virtual css::uno::Reference< css::sdbc::XBlob > SAL_CALL getBlob(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1343
std::vector< OUString > m_aAutoColumns
Definition: KeySet.hxx:81
virtual void makeNewStatement()
Definition: KeySet.cxx:355
bool isBeforeFirst()
Definition: KeySet.cxx:949
virtual double SAL_CALL getDouble(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1289
OUpdatedParameter m_aUpdatedParameter
Definition: KeySet.hxx:83
virtual void insertRow(const ORowSetRow &_rInsertRow, const connectivity::OSQLTable &_xTable) override
Definition: KeySet.cxx:593
virtual sal_Int32 SAL_CALL getInt(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1271
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getCharacterStream(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1325
css::uno::Reference< css::sdbc::XPreparedStatement > m_xStatement
Definition: KeySet.hxx:98
virtual sal_Bool SAL_CALL getBoolean(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1253
void findTableColumnsMatching_throw(const css::uno::Any &i_aTable, const OUString &i_rUpdateTableName, const css::uno::Reference< css::sdbc::XDatabaseMetaData > &i_xMeta, const css::uno::Reference< css::container::XNameAccess > &i_xQueryColumns, std::unique_ptr< SelectColumnsMetaData > const &o_pKeyColumnNames)
Definition: KeySet.cxx:146
virtual css::util::DateTime SAL_CALL getTimestamp(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1313
virtual css::uno::Any SAL_CALL getObject(sal_Int32 columnIndex, const css::uno::Reference< css::container::XNameAccess > &typeMap) override
Definition: KeySet.cxx:1331
virtual css::util::Date SAL_CALL getDate(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1301
void invalidateRow()
Definition: KeySet.cxx:400
css::uno::Reference< css::sdbc::XRow > m_xRow
Definition: KeySet.hxx:100
sal_Int32 & m_rRowCount
Definition: KeySet.hxx:104
void tryRefetch(const ORowSetRow &_rInsertRow, bool bRefetch)
Definition: KeySet.cxx:770
virtual void reset(const css::uno::Reference< css::sdbc::XResultSet > &_xDriverSet) override
Definition: KeySet.cxx:322
static void setOneKeyColumnParameter(sal_Int32 &nPos, const css::uno::Reference< css::sdbc::XParameters > &_xParameter, const connectivity::ORowSetValue &_rValue, sal_Int32 _nType, sal_Int32 _nScale)
Definition: KeySet.cxx:234
virtual sal_Int32 getRow() override
Definition: KeySet.cxx:1006
virtual css::uno::Any getBookmark() override
Definition: KeySet.cxx:406
virtual sal_Int32 compareBookmarks(const css::uno::Any &first, const css::uno::Any &second) override
Definition: KeySet.cxx:421
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getBytes(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1295
connectivity::OSQLTable m_xTable
Definition: KeySet.hxx:89
bool isAfterLast()
Definition: KeySet.cxx:954
virtual bool absolute(sal_Int32 row) override
Definition: KeySet.cxx:1014
OKeySetMatrix::iterator m_aKeyIter
Definition: KeySet.hxx:79
void ensureRowForData()
Definition: KeySet.cxx:1237
OUStringBuffer createKeyFilter()
Definition: KeySet.cxx:247
virtual void construct(const css::uno::Reference< css::sdbc::XResultSet > &_xDriverSet, const OUString &i_sRowSetFilter) override
Definition: KeySet.cxx:277
virtual bool rowUpdated() override
Definition: KeySet.cxx:1361
bool doTryRefetch_throw()
Definition: KeySet.cxx:1082
virtual float SAL_CALL getFloat(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1283
virtual bool hasOrderedBookmarks() override
Definition: KeySet.cxx:430
virtual bool rowInserted() override
Definition: KeySet.cxx:1366
void executeInsert(const ORowSetRow &_rInsertRow, const OUString &i_sSQL, std::u16string_view i_sTableName, bool bRefetch=false)
Definition: KeySet.cxx:629
virtual bool moveToBookmark(const css::uno::Any &bookmark) override
Definition: KeySet.cxx:413
std::unique_ptr< SelectColumnsMetaData > m_pParameterNames
Definition: KeySet.hxx:87
std::unique_ptr< SelectColumnsMetaData > m_pColumnNames
Definition: KeySet.hxx:86
virtual sal_Int32 hashBookmark(const css::uno::Any &bookmark) override
Definition: KeySet.cxx:435
const OUString m_sUpdateTableName
Definition: KeySet.hxx:102
OKeySetMatrix m_aKeyMap
Definition: KeySet.hxx:78
rtl::Reference< ORowSetValueVector > m_aParameterValueForCache
Definition: KeySet.hxx:84
virtual bool last() override
Definition: KeySet.cxx:992
vStatements_t m_vStatements
Definition: KeySet.hxx:97
OKeySet(connectivity::OSQLTable _aTable, OUString _sUpdateTableName, const css::uno::Reference< css::sdb::XSingleSelectQueryAnalyzer > &_xComposer, const ORowSetValueVector &_aParameterValueForCache, sal_Int32 i_nMaxRows, sal_Int32 &o_nRowCount)
std::unique_ptr< SelectColumnsMetaData > m_pForeignColumnNames
Definition: KeySet.hxx:88
virtual void afterLast() override
Definition: KeySet.cxx:966
virtual css::uno::Reference< css::sdbc::XClob > SAL_CALL getClob(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1349
std::vector< OUString > m_aFilterColumns
Definition: KeySet.hxx:103
static void impl_convertValue_throw(const ORowSetRow &_rInsertRow, const SelectColumnDescription &i_aMetaData)
Definition: KeySet.cxx:1443
std::map< std::vector< bool >, css::uno::Reference< css::sdbc::XPreparedStatement > > vStatements_t
Definition: KeySet.hxx:96
css::uno::Reference< css::sdb::XSingleSelectQueryAnalyzer > m_xComposer
Definition: KeySet.hxx:101
virtual sal_Int64 SAL_CALL getLong(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1277
virtual void beforeFirst() override
Definition: KeySet.cxx:959
bool fillAllRows()
Definition: KeySet.cxx:1213
virtual sal_Int8 SAL_CALL getByte(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1259
virtual sal_Int16 SAL_CALL getShort(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1265
virtual void updateRow(const ORowSetRow &_rInsertRow, const ORowSetRow &_rOriginalRow, const connectivity::OSQLTable &_xTable) override
Definition: KeySet.cxx:441
virtual css::uno::Reference< css::sdbc::XRef > SAL_CALL getRef(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1337
virtual void deleteRow(const ORowSetRow &_rInsertRow, const connectivity::OSQLTable &_xTable) override
Definition: KeySet.cxx:829
virtual bool first() override
Definition: KeySet.cxx:974
virtual css::util::Time SAL_CALL getTime(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1307
bool m_bRowCountFinal
Definition: KeySet.hxx:106
virtual bool next() override
Definition: KeySet.cxx:921
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getBinaryStream(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1319
virtual void refreshRow() override
Definition: KeySet.cxx:1122
void copyRowValue(const ORowSetRow &_rInsertRow, ORowSetRow const &_rKeyRow, sal_Int32 i_nBookmark)
copies the values from the insert row into the key row
Definition: KeySet.cxx:789
virtual bool previous() override
Definition: KeySet.cxx:1071
virtual css::uno::Reference< css::sdbc::XArray > SAL_CALL getArray(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1355
css::uno::Reference< css::sdbc::XResultSet > m_xSet
Definition: KeySet.hxx:99
virtual OUString SAL_CALL getString(sal_Int32 columnIndex) override
Definition: KeySet.cxx:1247
virtual bool rowDeleted() override
Definition: KeySet.cxx:1371
css::uno::Reference< css::sdbcx::XColumnsSupplier > OSQLTable
std::map< OUString, SelectColumnDescription, ::comphelper::UStringMixLess > SelectColumnsMetaData
Definition: KeySet.hxx:62
std::map< sal_Int32, rtl::Reference< ORowSetValueVector > > OUpdatedParameter
Definition: KeySet.hxx:73
std::pair< ORowSetRow, std::pair< sal_Int32, css::uno::Reference< css::sdbc::XRow > > > OKeySetValue
Definition: KeySet.hxx:71
void getColumnPositions(const Reference< XNameAccess > &_rxQueryColumns, const css::uno::Sequence< OUString > &_aColumnNames, std::u16string_view _rsUpdateTableName, SelectColumnsMetaData &o_rColumnNames, bool i_bAppendTableName)
Definition: KeySet.cxx:1381
std::map< sal_Int32, OKeySetValue > OKeySetMatrix
Definition: KeySet.hxx:72
SelectColumnDescription(sal_Int32 _nPosition, sal_Int32 _nType, sal_Int32 _nScale, bool _bNullable, OUString _sDefaultValue)
Definition: KeySet.hxx:53
unsigned char sal_Bool
signed char sal_Int8