1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include "CacheSet.hxx"
28 #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
29 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
30 #include <comphelper/stl_types.hxx>
34 struct SelectColumnDescription
36 OUString sRealName
; // may be empty
37 OUString sTableName
; // may be empty
38 OUString sDefaultValue
;
44 SelectColumnDescription()
52 SelectColumnDescription( sal_Int32 _nPosition
, sal_Int32 _nType
, sal_Int32 _nScale
,bool _bNullable
, const OUString
& _rDefaultValue
)
53 :sDefaultValue( _rDefaultValue
)
54 ,nPosition( _nPosition
)
57 ,bNullable(_bNullable
)
61 typedef std::map
< OUString
, SelectColumnDescription
, ::comphelper::UStringMixLess
> SelectColumnsMetaData
;
63 // the elements of _rxQueryColumns must have the properties PROPERTY_REALNAME and PROPERTY_TABLENAME
64 void getColumnPositions(const css::uno::Reference
< css::container::XNameAccess
>& _rxQueryColumns
,
65 const css::uno::Sequence
< OUString
>& _rColumnNames
,
66 const OUString
& _rsUpdateTableName
,
67 SelectColumnsMetaData
& o_rColumnNames
/* out */,
68 bool i_bAppendTableName
= false);
70 typedef std::pair
<ORowSetRow
,std::pair
<sal_Int32
,css::uno::Reference
< css::sdbc::XRow
> > > OKeySetValue
;
71 typedef std::map
<sal_Int32
,OKeySetValue
> OKeySetMatrix
;
72 typedef std::map
<sal_Int32
, rtl::Reference
<ORowSetValueVector
> > OUpdatedParameter
;
73 // is used when the source supports keys
74 class OKeySet
: public OCacheSet
77 OKeySetMatrix m_aKeyMap
;
78 OKeySetMatrix::iterator m_aKeyIter
;
80 std::vector
< OUString
> m_aAutoColumns
; // contains all columns which are autoincrement ones
82 OUpdatedParameter m_aUpdatedParameter
; // contains all parameter which have been updated and are needed for refetching
83 rtl::Reference
<ORowSetValueVector
> m_aParameterValueForCache
;
84 std::unique_ptr
<SelectColumnsMetaData
> m_pKeyColumnNames
; // contains all key column names
85 std::unique_ptr
<SelectColumnsMetaData
> m_pColumnNames
; // contains all column names
86 std::unique_ptr
<SelectColumnsMetaData
> m_pParameterNames
; // contains all parameter names
87 std::unique_ptr
<SelectColumnsMetaData
> m_pForeignColumnNames
; // contains all column names of the rest
88 connectivity::OSQLTable m_xTable
; // reference to our table
89 // we need a different SQL (statement) for each different combination
90 // of NULLness of key & foreign columns;
91 // each subclause is either "colName = ?" or "colName IS NULL"
92 // (we avoid the standard "colName IS NOT DISTINCT FROM ?" because it is not widely supported)
93 typedef std::map
< std::vector
<bool>,
94 css::uno::Reference
< css::sdbc::XPreparedStatement
> >
96 vStatements_t m_vStatements
;
97 css::uno::Reference
< css::sdbc::XPreparedStatement
> m_xStatement
;
98 css::uno::Reference
< css::sdbc::XResultSet
> m_xSet
;
99 css::uno::Reference
< css::sdbc::XRow
> m_xRow
;
100 css::uno::Reference
< css::sdb::XSingleSelectQueryAnalyzer
> m_xComposer
;
101 const OUString m_sUpdateTableName
;
102 std::vector
< OUString
> m_aFilterColumns
;
103 sal_Int32
& m_rRowCount
;
105 bool m_bRowCountFinal
;
107 /** copies the values from the insert row into the key row
109 * \param _rInsertRow the row which was inserted
110 * \param _rKeyRow The current key row of the row set.
111 + \param i_nBookmark The bookmark is used to update the parameter
113 void copyRowValue(const ORowSetRow
& _rInsertRow
, ORowSetRow
const & _rKeyRow
, sal_Int32 i_nBookmark
);
115 // returns true if it did any work
118 void invalidateRow();
120 static void impl_convertValue_throw(const ORowSetRow
& _rInsertRow
,const SelectColumnDescription
& i_aMetaData
);
122 void findTableColumnsMatching_throw( const css::uno::Any
& i_aTable
,
123 const OUString
& i_rUpdateTableName
,
124 const css::uno::Reference
< css::sdbc::XDatabaseMetaData
>& i_xMeta
,
125 const css::uno::Reference
< css::container::XNameAccess
>& i_xQueryColumns
,
126 std::unique_ptr
<SelectColumnsMetaData
> const & o_pKeyColumnNames
);
127 void ensureStatement( );
128 virtual void makeNewStatement( );
129 static void setOneKeyColumnParameter( sal_Int32
&nPos
,
130 const css::uno::Reference
< css::sdbc::XParameters
> &_xParameter
,
131 const connectivity::ORowSetValue
&_rValue
,
134 OUStringBuffer
createKeyFilter( );
135 /// @throws css::sdbc::SQLException
136 /// @throws css::uno::RuntimeException
137 bool doTryRefetch_throw();
138 void tryRefetch(const ORowSetRow
& _rInsertRow
,bool bRefetch
);
139 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
>());
140 void executeInsert( const ORowSetRow
& _rInsertRow
, const OUString
& i_sSQL
, const OUString
& i_sTableName
, bool bRefetch
= false);
141 void executeStatement(OUStringBuffer
& io_aFilter
, css::uno::Reference
< css::sdb::XSingleSelectQueryComposer
>& io_xAnalyzer
);
143 virtual ~OKeySet() override
;
145 OKeySet(const connectivity::OSQLTable
& _xTable
,
146 const OUString
& _rUpdateTableName
,
147 const css::uno::Reference
< css::sdb::XSingleSelectQueryAnalyzer
>& _xComposer
,
148 const ORowSetValueVector
& _aParameterValueForCache
,
149 sal_Int32 i_nMaxRows
,
150 sal_Int32
& o_nRowCount
);
152 // late ctor which can throw exceptions
153 virtual void construct(const css::uno::Reference
< css::sdbc::XResultSet
>& _xDriverSet
,const OUString
& i_sRowSetFilter
) override
;
154 virtual void reset(const css::uno::Reference
< css::sdbc::XResultSet
>& _xDriverSet
) override
;
157 virtual sal_Bool SAL_CALL
wasNull( ) override
;
158 virtual OUString SAL_CALL
getString( sal_Int32 columnIndex
) override
;
159 virtual sal_Bool SAL_CALL
getBoolean( sal_Int32 columnIndex
) override
;
160 virtual sal_Int8 SAL_CALL
getByte( sal_Int32 columnIndex
) override
;
161 virtual sal_Int16 SAL_CALL
getShort( sal_Int32 columnIndex
) override
;
162 virtual sal_Int32 SAL_CALL
getInt( sal_Int32 columnIndex
) override
;
163 virtual sal_Int64 SAL_CALL
getLong( sal_Int32 columnIndex
) override
;
164 virtual float SAL_CALL
getFloat( sal_Int32 columnIndex
) override
;
165 virtual double SAL_CALL
getDouble( sal_Int32 columnIndex
) override
;
166 virtual css::uno::Sequence
< sal_Int8
> SAL_CALL
getBytes( sal_Int32 columnIndex
) override
;
167 virtual css::util::Date SAL_CALL
getDate( sal_Int32 columnIndex
) override
;
168 virtual css::util::Time SAL_CALL
getTime( sal_Int32 columnIndex
) override
;
169 virtual css::util::DateTime SAL_CALL
getTimestamp( sal_Int32 columnIndex
) override
;
170 virtual css::uno::Reference
< css::io::XInputStream
> SAL_CALL
getBinaryStream( sal_Int32 columnIndex
) override
;
171 virtual css::uno::Reference
< css::io::XInputStream
> SAL_CALL
getCharacterStream( sal_Int32 columnIndex
) override
;
172 virtual css::uno::Any SAL_CALL
getObject( sal_Int32 columnIndex
, const css::uno::Reference
< css::container::XNameAccess
>& typeMap
) override
;
173 virtual css::uno::Reference
< css::sdbc::XRef
> SAL_CALL
getRef( sal_Int32 columnIndex
) override
;
174 virtual css::uno::Reference
< css::sdbc::XBlob
> SAL_CALL
getBlob( sal_Int32 columnIndex
) override
;
175 virtual css::uno::Reference
< css::sdbc::XClob
> SAL_CALL
getClob( sal_Int32 columnIndex
) override
;
176 virtual css::uno::Reference
< css::sdbc::XArray
> SAL_CALL
getArray( sal_Int32 columnIndex
) override
;
179 virtual bool rowUpdated( ) override
;
180 virtual bool rowInserted( ) override
;
181 virtual bool rowDeleted( ) override
;
182 bool isBeforeFirst( );
185 // css::sdbc::XResultSet
186 virtual bool next() override
;
187 virtual void beforeFirst( ) override
;
188 virtual void afterLast( ) override
;
189 virtual bool first() override
;
190 virtual bool last( ) override
;
191 virtual sal_Int32
getRow( ) override
;
192 virtual bool absolute( sal_Int32 row
) override
;
193 virtual bool previous( ) override
;
194 /// @throws css::sdbc::SQLException
195 /// @throws css::uno::RuntimeException
196 void ensureRowForData( );
197 virtual void refreshRow( ) override
;
198 // css::sdbcx::XRowLocate
199 virtual css::uno::Any
getBookmark() override
;
201 virtual bool moveToBookmark( const css::uno::Any
& bookmark
) override
;
203 virtual sal_Int32
compareBookmarks( const css::uno::Any
& first
, const css::uno::Any
& second
) override
;
205 virtual bool hasOrderedBookmarks( ) override
;
207 virtual sal_Int32
hashBookmark( const css::uno::Any
& bookmark
) override
;
209 // css::sdbc::XResultSetUpdate
210 virtual void updateRow(const ORowSetRow
& _rInsertRow
,const ORowSetRow
& _rOriginalRow
,const connectivity::OSQLTable
& _xTable
) override
;
211 virtual void deleteRow(const ORowSetRow
& _rInsertRow
,const connectivity::OSQLTable
& _xTable
) override
;
212 virtual void insertRow( const ORowSetRow
& _rInsertRow
,const connectivity::OSQLTable
& _xTable
) override
;
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */