LibreOffice Module dbaccess (master) 1
KeySet.cxx
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#include <memory>
21#include <string_view>
22
23#include "KeySet.hxx"
24#include <sal/log.hxx>
25#include <core_resource.hxx>
26#include <strings.hrc>
27#include <strings.hxx>
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/lang/XMultiServiceFactory.hpp>
30#include <com/sun/star/sdb/XParametersSupplier.hpp>
31#include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
32#include <com/sun/star/sdbc/ColumnValue.hpp>
33#include <com/sun/star/sdbc/SQLException.hpp>
34#include <com/sun/star/sdbc/XPreparedStatement.hpp>
35#include <com/sun/star/sdbc/XParameters.hpp>
36#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
37#include <com/sun/star/sdbc/XGeneratedResultSet.hpp>
38#include <com/sun/star/container/XIndexAccess.hpp>
39#include <com/sun/star/sdbcx/CompareBookmark.hpp>
40#include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
41#include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
42#include <comphelper/types.hxx>
45#include <algorithm>
46#include <com/sun/star/io/XInputStream.hpp>
47#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
48#include <composertools.hxx>
49#include <utility>
50#include "PrivateRow.hxx"
51
52using namespace dbaccess;
53using namespace ::connectivity;
54using namespace ::dbtools;
55using namespace ::com::sun::star::uno;
56using namespace ::com::sun::star::beans;
57using namespace ::com::sun::star::sdbc;
58using namespace ::com::sun::star::sdb;
59using namespace ::com::sun::star::sdbcx;
60using namespace ::com::sun::star::container;
61using namespace ::com::sun::star::lang;
62using namespace ::com::sun::star::util;
63using namespace ::com::sun::star::io;
64using namespace ::com::sun::star;
65using namespace ::cppu;
66using namespace ::osl;
67using std::vector;
68
69namespace
70{
71 void lcl_fillIndexColumns(const Reference<XIndexAccess>& _xIndexes, std::vector< Reference<XNameAccess> >& _rAllIndexColumns)
72 {
73 if ( !_xIndexes.is() )
74 return;
75
76 Reference<XPropertySet> xIndexColsSup;
77 sal_Int32 nCount = _xIndexes->getCount();
78 for(sal_Int32 j = 0 ; j < nCount ; ++j)
79 {
80 xIndexColsSup.set(_xIndexes->getByIndex(j),UNO_QUERY);
81 if( xIndexColsSup.is()
82 && comphelper::getBOOL(xIndexColsSup->getPropertyValue(PROPERTY_ISUNIQUE))
83 && !comphelper::getBOOL(xIndexColsSup->getPropertyValue(PROPERTY_ISPRIMARYKEYINDEX))
84 )
85 _rAllIndexColumns.push_back(Reference<XColumnsSupplier>(xIndexColsSup,UNO_QUERY_THROW)->getColumns());
86 }
87 }
88
89 template < typename T > void tryDispose( Reference<T> &r )
90 {
91 try
92 {
93 ::comphelper::disposeComponent(r);
94 }
95 catch(const Exception&)
96 {
97 r = nullptr;
98 }
99 catch(...)
100 {
101 SAL_WARN("dbaccess", "Unknown Exception occurred");
102 }
103 }
104}
105
106
107OKeySet::OKeySet(connectivity::OSQLTable _xTable,
108 OUString _sUpdateTableName, // this can be the alias or the full qualified name
109 const Reference< XSingleSelectQueryAnalyzer >& _xComposer,
110 const ORowSetValueVector& _aParameterValueForCache,
111 sal_Int32 i_nMaxRows,
112 sal_Int32& o_nRowCount)
113 :OCacheSet(i_nMaxRows)
114 ,m_aParameterValueForCache(new ORowSetValueVector(_aParameterValueForCache))
115 ,m_xTable(std::move(_xTable))
116 ,m_xComposer(_xComposer)
117 ,m_sUpdateTableName(std::move(_sUpdateTableName))
118 ,m_rRowCount(o_nRowCount)
119 ,m_bRowCountFinal(false)
120{
121}
122
123OKeySet::~OKeySet()
124{
125 tryDispose(m_xSet);
126 // m_xStatement is necessarily one of those
127 for (auto & statement : m_vStatements)
128 {
129 tryDispose(statement.second);
130 }
131
132 m_xComposer = nullptr;
133
134}
135
136void OKeySet::initColumns()
137{
138 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
139 bool bCase = xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers();
140 m_pKeyColumnNames.reset( new SelectColumnsMetaData(bCase) );
141 m_pColumnNames.reset( new SelectColumnsMetaData(bCase) );
142 m_pParameterNames.reset( new SelectColumnsMetaData(bCase) );
143 m_pForeignColumnNames.reset( new SelectColumnsMetaData(bCase) );
144}
145
146void OKeySet::findTableColumnsMatching_throw( const Any& i_aTable,
147 const OUString& i_rUpdateTableName,
148 const Reference<XDatabaseMetaData>& i_xMeta,
149 const Reference<XNameAccess>& i_xQueryColumns,
150 std::unique_ptr<SelectColumnsMetaData> const & o_pKeyColumnNames)
151{
152 // first ask the database itself for the best columns which can be used
153 Sequence< OUString> aBestColumnNames;
154 Reference<XNameAccess> xKeyColumns = getPrimaryKeyColumns_throw(i_aTable);
155 if ( xKeyColumns.is() )
156 aBestColumnNames = xKeyColumns->getElementNames();
157
158 const Reference<XColumnsSupplier> xTblColSup(i_aTable,UNO_QUERY_THROW);
159 const Reference<XNameAccess> xTblColumns = xTblColSup->getColumns();
160 // locate parameter in select columns
161 Reference<XParametersSupplier> xParaSup(m_xComposer,UNO_QUERY);
162 Reference<XIndexAccess> xQueryParameters = xParaSup->getParameters();
163 const sal_Int32 nParaCount = xQueryParameters->getCount();
164 Sequence< OUString> aParameterColumns(nParaCount);
165 auto aParameterColumnsRange = asNonConstRange(aParameterColumns);
166 for(sal_Int32 i = 0; i< nParaCount;++i)
167 {
168 Reference<XPropertySet> xPara(xQueryParameters->getByIndex(i),UNO_QUERY_THROW);
169 xPara->getPropertyValue(PROPERTY_REALNAME) >>= aParameterColumnsRange[i];
170 }
171
172 OUString sUpdateTableName( i_rUpdateTableName );
173 if ( sUpdateTableName.isEmpty() )
174 {
175 SAL_WARN("dbaccess", "OKeySet::findTableColumnsMatching_throw: This is a fallback only - it won't work when the table has an alias name." );
176 // If i_aTable originates from a query composer, and is a table which appears with an alias in the SELECT statement,
177 // then the below code will not produce correct results.
178 // For instance, imagine a "SELECT alias.col FROM table AS alias". Now i_aTable would be the table named
179 // "table", so our sUpdateTableName would be "table" as well - not the information about the "alias" is
180 // already lost here.
181 // now getColumnPositions would traverse the columns, and check which of them belong to the table denoted
182 // by sUpdateTableName. Since the latter is "table", but the columns only know that they belong to a table
183 // named "alias", there will be no matching - so getColumnPositions wouldn't find anything.
184
185 OUString sCatalog, sSchema, sTable;
186 Reference<XPropertySet> xTableProp( i_aTable, UNO_QUERY_THROW );
187 xTableProp->getPropertyValue( PROPERTY_CATALOGNAME )>>= sCatalog;
188 xTableProp->getPropertyValue( PROPERTY_SCHEMANAME ) >>= sSchema;
189 xTableProp->getPropertyValue( PROPERTY_NAME ) >>= sTable;
190 sUpdateTableName = dbtools::composeTableName( i_xMeta, sCatalog, sSchema, sTable, false, ::dbtools::EComposeRule::InDataManipulation );
191 }
192
193 ::dbaccess::getColumnPositions(i_xQueryColumns,aBestColumnNames,sUpdateTableName,(*o_pKeyColumnNames),true);
194 ::dbaccess::getColumnPositions(i_xQueryColumns,xTblColumns->getElementNames(),sUpdateTableName,(*m_pColumnNames),true);
195 ::dbaccess::getColumnPositions(i_xQueryColumns,aParameterColumns,sUpdateTableName,(*m_pParameterNames),true);
196
197 if ( o_pKeyColumnNames->empty() )
198 {
199 ::dbtools::throwGenericSQLException("Could not find any key column.", *this );
200 }
201
202 for (auto const& keyColumn : *o_pKeyColumnNames)
203 {
204 if ( !xTblColumns->hasByName( keyColumn.second.sRealName ) )
205 continue;
206
207 Reference<XPropertySet> xProp( xTblColumns->getByName( keyColumn.second.sRealName ), UNO_QUERY );
208 bool bAuto = false;
209 if ( ( xProp->getPropertyValue( PROPERTY_ISAUTOINCREMENT ) >>= bAuto ) && bAuto )
210 m_aAutoColumns.push_back( keyColumn.first );
211 }
212}
213
214namespace
215{
216 void appendOneKeyColumnClause( std::u16string_view tblName, const OUString &colName, const connectivity::ORowSetValue &_rValue, OUStringBuffer &o_buf )
217 {
218 OUString fullName;
219 if (tblName.empty())
220 fullName = colName;
221 else
222 fullName = OUString::Concat(tblName) + "." + colName;
223 if ( _rValue.isNull() )
224 {
225 o_buf.append(fullName + " IS NULL ");
226 }
227 else
228 {
229 o_buf.append(fullName + " = ? ");
230 }
231 }
232}
233
234void OKeySet::setOneKeyColumnParameter( sal_Int32 &nPos, const Reference< XParameters > &_xParameter, const connectivity::ORowSetValue &_rValue, sal_Int32 _nType, sal_Int32 _nScale )
235{
236 if ( _rValue.isNull() )
237 {
238 // Nothing to do, appendOneKeyColumnClause took care of it,
239 // the "IS NULL" is hardcoded in the query
240 }
241 else
242 {
243 setParameter( nPos++, _xParameter, _rValue, _nType, _nScale );
244 }
245}
246
247OUStringBuffer OKeySet::createKeyFilter()
248{
249 connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aIter = m_aKeyIter->second.first->begin();
250
251 static const char aAnd[] = " AND ";
252 const OUString aQuote = getIdentifierQuoteString();
253 OUStringBuffer aFilter;
254 // create the where clause
255 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
256 for (auto const& keyColumnName : *m_pKeyColumnNames)
257 {
258 if ( ! aFilter.isEmpty() )
259 aFilter.append(aAnd);
260 appendOneKeyColumnClause(::dbtools::quoteTableName(xMeta, keyColumnName.second.sTableName, ::dbtools::EComposeRule::InDataManipulation),
261 ::dbtools::quoteName(aQuote, keyColumnName.second.sRealName),
262 *aIter++,
263 aFilter);
264 }
265 for (auto const& foreignColumnName : * m_pForeignColumnNames)
266 {
267 if ( ! aFilter.isEmpty() )
268 aFilter.append(aAnd);
269 appendOneKeyColumnClause(::dbtools::quoteTableName(xMeta, foreignColumnName.second.sTableName, ::dbtools::EComposeRule::InDataManipulation),
270 ::dbtools::quoteName(aQuote, foreignColumnName.second.sRealName),
271 *aIter++,
272 aFilter);
273 }
274 return aFilter;
275}
276
277void OKeySet::construct(const Reference< XResultSet>& _xDriverSet, const OUString& i_sRowSetFilter)
278{
279 OCacheSet::construct(_xDriverSet,i_sRowSetFilter);
280
281 initColumns();
282
283 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
284 Reference<XColumnsSupplier> xQueryColSup(m_xComposer, UNO_QUERY);
285 const Reference<XNameAccess> xQueryColumns = xQueryColSup->getColumns();
286 findTableColumnsMatching_throw( Any(m_xTable), m_sUpdateTableName, xMeta, xQueryColumns, m_pKeyColumnNames );
287
288 Reference< XSingleSelectQueryComposer> xSourceComposer(m_xComposer,UNO_QUERY);
289 Reference< XMultiServiceFactory > xFactory(m_xConnection, UNO_QUERY_THROW);
290 Reference<XSingleSelectQueryComposer> xAnalyzer(xFactory->createInstance(SERVICE_NAME_SINGLESELECTQUERYCOMPOSER),UNO_QUERY);
291 xAnalyzer->setElementaryQuery(xSourceComposer->getElementaryQuery());
292 Reference<XTablesSupplier> xTabSup(xAnalyzer,uno::UNO_QUERY);
293 Reference<XNameAccess> xSelectTables = xTabSup->getTables();
294 const Sequence< OUString> aSeq = xSelectTables->getElementNames();
295 if ( aSeq.getLength() > 1 ) // special handling for join
296 {
297 const OUString* pIter = aSeq.getConstArray();
298 const OUString* const pEnd = pIter + aSeq.getLength();
299 for(;pIter != pEnd;++pIter)
300 {
301 if ( *pIter != m_sUpdateTableName )
302 {
303 connectivity::OSQLTable xSelColSup(xSelectTables->getByName(*pIter),uno::UNO_QUERY);
304 Reference<XPropertySet> xProp(xSelColSup,uno::UNO_QUERY);
305 OUString sSelectTableName = ::dbtools::composeTableName( xMeta, xProp, ::dbtools::EComposeRule::InDataManipulation, false );
306
307 ::dbaccess::getColumnPositions(xQueryColumns, xSelColSup->getColumns()->getElementNames(), sSelectTableName, (*m_pForeignColumnNames), true);
308
309 // LEM: there used to be a break here; however, I see no reason to stop
310 // at first non-updateTable, so I removed it. (think of multiple joins...)
311 }
312 }
313 }
314
315 // the first row is empty because it's now easier for us to distinguish when we are beforefirst or first
316 // without extra variable to be set
317 OKeySetValue keySetValue(nullptr,std::pair<sal_Int32,Reference<XRow> >(0,Reference<XRow>()));
318 m_aKeyMap.emplace(0, keySetValue);
319 m_aKeyIter = m_aKeyMap.begin();
320}
321
322void OKeySet::reset(const Reference< XResultSet>& _xDriverSet)
323{
324 OCacheSet::construct(_xDriverSet, m_sRowSetFilter);
325 m_bRowCountFinal = false;
326 m_aKeyMap.clear();
327 OKeySetValue keySetValue(nullptr,std::pair<sal_Int32,Reference<XRow> >(0,Reference<XRow>()));
328 m_aKeyMap.emplace(0,keySetValue);
329 m_aKeyIter = m_aKeyMap.begin();
330}
331
332void OKeySet::ensureStatement( )
333{
334 // do we already have a statement for the current combination of NULLness
335 // of key & foreign columns?
336 std::vector<bool> FilterColumnsNULL;
337 FilterColumnsNULL.reserve(m_aKeyIter->second.first->size());
338 for (auto const& elem : *m_aKeyIter->second.first)
339 FilterColumnsNULL.push_back(elem.isNull());
340 vStatements_t::const_iterator pNewStatement(m_vStatements.find(FilterColumnsNULL));
341 if(pNewStatement == m_vStatements.end())
342 {
343 // no: make a new one
344 makeNewStatement();
345 std::pair< vStatements_t::const_iterator, bool > insert_result
346 (m_vStatements.emplace( FilterColumnsNULL, m_xStatement));
347 (void) insert_result; // WaE: unused variable
348 assert(insert_result.second);
349 }
350 else
351 // yes: use it
352 m_xStatement = pNewStatement->second;
353}
354
355void OKeySet::makeNewStatement()
356{
357 Reference< XSingleSelectQueryComposer> xSourceComposer(m_xComposer,UNO_QUERY);
358 Reference< XMultiServiceFactory > xFactory(m_xConnection, UNO_QUERY_THROW);
359 Reference<XSingleSelectQueryComposer> xAnalyzer(xFactory->createInstance(SERVICE_NAME_SINGLESELECTQUERYCOMPOSER),UNO_QUERY);
360 xAnalyzer->setElementaryQuery(xSourceComposer->getElementaryQuery());
361
362 OUStringBuffer aFilter(createKeyFilter());
363 executeStatement(aFilter, xAnalyzer);
364}
365
366void OKeySet::executeStatement(OUStringBuffer& io_aFilter, Reference<XSingleSelectQueryComposer>& io_xAnalyzer)
367{
368 bool bFilterSet = !m_sRowSetFilter.isEmpty();
369 if ( bFilterSet )
370 {
371 FilterCreator aFilterCreator;
372 aFilterCreator.append( m_sRowSetFilter );
373 aFilterCreator.append( io_aFilter.makeStringAndClear() );
374 io_aFilter = aFilterCreator.getComposedAndClear();
375 }
376 io_xAnalyzer->setFilter(io_aFilter.makeStringAndClear());
377 if ( bFilterSet )
378 {
379 Sequence< Sequence< PropertyValue > > aFilter2 = io_xAnalyzer->getStructuredFilter();
380 const Sequence< PropertyValue >* pOr = aFilter2.getConstArray();
381 const Sequence< PropertyValue >* pOrEnd = pOr + aFilter2.getLength();
382 for(;pOr != pOrEnd;++pOr)
383 {
384 const PropertyValue* pAnd = pOr->getConstArray();
385 const PropertyValue* pAndEnd = pAnd + pOr->getLength();
386 for(;pAnd != pAndEnd;++pAnd)
387 {
388 OUString sValue;
389 if ( !(pAnd->Value >>= sValue) || !( sValue == "?" || sValue.startsWith( ":" ) ) )
390 { // we have a criteria which has to be taken into account for updates
391 m_aFilterColumns.push_back(pAnd->Name);
392 }
393 }
394 }
395 }
396 m_xStatement = m_xConnection->prepareStatement(io_xAnalyzer->getQueryWithSubstitution());
397 ::comphelper::disposeComponent(io_xAnalyzer);
398}
399
400void OKeySet::invalidateRow()
401{
402 m_xRow = nullptr;
403 ::comphelper::disposeComponent(m_xSet);
404}
405
406Any OKeySet::getBookmark()
407{
408 OSL_ENSURE(m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin(),
409 "getBookmark is only possible when we stand on a valid row!");
410 return Any(m_aKeyIter->first);
411}
412
413bool OKeySet::moveToBookmark( const Any& bookmark )
414{
415 m_bInserted = m_bUpdated = m_bDeleted = false;
416 m_aKeyIter = m_aKeyMap.find(::comphelper::getINT32(bookmark));
417 invalidateRow();
418 return m_aKeyIter != m_aKeyMap.end();
419}
420
421sal_Int32 OKeySet::compareBookmarks( const Any& _first, const Any& _second )
422{
423 sal_Int32 nFirst = 0, nSecond = 0;
424 _first >>= nFirst;
425 _second >>= nSecond;
426
427 return (nFirst != nSecond) ? CompareBookmark::NOT_EQUAL : CompareBookmark::EQUAL;
428}
429
430bool OKeySet::hasOrderedBookmarks( )
431{
432 return true;
433}
434
435sal_Int32 OKeySet::hashBookmark( const Any& bookmark )
436{
437 return ::comphelper::getINT32(bookmark);
438}
439
440
441void OKeySet::updateRow(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOriginalRow,const connectivity::OSQLTable& _xTable )
442{
443 Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
444 fillTableName(xSet);
445
446 OUStringBuffer aSql = "UPDATE " + m_aComposedTableName + " SET ";
447 // list all columns that should be set
448 constexpr OUStringLiteral aPara(u" = ?,");
449 OUString aQuote = getIdentifierQuoteString();
450 constexpr OUStringLiteral aAnd(u" AND ");
451 OUString sIsNull(" IS NULL");
452 OUString sParam(" = ?");
453
454 // use keys and indexes for exact positioning
455 Reference<XIndexesSupplier> xIndexSup(_xTable,UNO_QUERY);
456 Reference<XIndexAccess> xIndexes;
457 if ( xIndexSup.is() )
458 xIndexes.set(xIndexSup->getIndexes(),UNO_QUERY);
459
460 std::vector< Reference<XNameAccess> > aAllIndexColumns;
461 lcl_fillIndexColumns(xIndexes,aAllIndexColumns);
462
463 OUStringBuffer sKeyCondition,sIndexCondition;
464 std::vector<sal_Int32> aIndexColumnPositions;
465
466 const sal_Int32 nOldLength = aSql.getLength();
467 // here we build the condition part for the update statement
468 for (auto const& columnName : *m_pColumnNames)
469 {
470 if ( m_pKeyColumnNames->find(columnName.first) != m_pKeyColumnNames->end() )
471 {
472 sKeyCondition.append(::dbtools::quoteName( aQuote,columnName.second.sRealName));
473 if((*_rOriginalRow)[columnName.second.nPosition].isNull())
474 sKeyCondition.append(sIsNull);
475 else
476 sKeyCondition.append(sParam);
477 sKeyCondition.append(aAnd);
478 }
479 else
480 {
481 for (auto const& indexColumn : aAllIndexColumns)
482 {
483 if(indexColumn->hasByName(columnName.first))
484 {
485 sIndexCondition.append(::dbtools::quoteName( aQuote,columnName.second.sRealName));
486 if((*_rOriginalRow)[columnName.second.nPosition].isNull())
487 sIndexCondition.append(sIsNull);
488 else
489 {
490 sIndexCondition.append(sParam);
491 aIndexColumnPositions.push_back(columnName.second.nPosition);
492 }
493 sIndexCondition.append(aAnd);
494 break;
495 }
496 }
497 }
498 if((*_rInsertRow)[columnName.second.nPosition].isModified())
499 {
500 aSql.append(::dbtools::quoteName( aQuote,columnName.second.sRealName) + aPara);
501 }
502 }
503
504 if( aSql.getLength() != nOldLength )
505 {
506 aSql.setLength(aSql.getLength()-1);
507 }
508 else
509 ::dbtools::throwSQLException( DBA_RES( RID_STR_NO_VALUE_CHANGED ), StandardSQLState::GENERAL_ERROR, m_xConnection );
510
511 if(!sKeyCondition.isEmpty() || !sIndexCondition.isEmpty())
512 {
513 aSql.append(" WHERE ");
514 if(!sKeyCondition.isEmpty() && !sIndexCondition.isEmpty())
515 {
516 aSql.append(sKeyCondition + sIndexCondition);
517 }
518 else if(!sKeyCondition.isEmpty())
519 {
520 aSql.append(sKeyCondition);
521 }
522 else if(!sIndexCondition.isEmpty())
523 {
524 aSql.append(sIndexCondition);
525 }
526 aSql.setLength(aSql.getLength()-5); // remove the last AND
527 }
528 else
529 ::dbtools::throwSQLException( DBA_RES( RID_STR_NO_CONDITION_FOR_PK ), StandardSQLState::GENERAL_ERROR, m_xConnection );
530
531 // now create end execute the prepared statement
532 executeUpdate(_rInsertRow ,_rOriginalRow,aSql.makeStringAndClear(),u"",aIndexColumnPositions);
533}
534
535void OKeySet::executeUpdate(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOriginalRow,const OUString& i_sSQL,std::u16string_view i_sTableName,const std::vector<sal_Int32>& _aIndexColumnPositions)
536{
537 // now create end execute the prepared statement
538 Reference< XPreparedStatement > xPrep(m_xConnection->prepareStatement(i_sSQL));
539 Reference< XParameters > xParameter(xPrep,UNO_QUERY);
540
541 bool bRefetch = true;
542 Reference<XRow> xRow;
543 sal_Int32 i = 1;
544 // first the set values
545 for (auto const& columnName : *m_pColumnNames)
546 {
547 if ( i_sTableName.empty() || columnName.second.sTableName == i_sTableName )
548 {
549 sal_Int32 nPos = columnName.second.nPosition;
550 if((*_rInsertRow)[nPos].isModified())
551 {
552 if ( bRefetch )
553 {
554 bRefetch = std::find(m_aFilterColumns.begin(),m_aFilterColumns.end(),columnName.second.sRealName) == m_aFilterColumns.end();
555 }
556 impl_convertValue_throw(_rInsertRow,columnName.second);
557 (*_rInsertRow)[nPos].setSigned((*_rOriginalRow)[nPos].isSigned());
558 setParameter(i++,xParameter,(*_rInsertRow)[nPos],columnName.second.nType,columnName.second.nScale);
559 }
560 }
561 }
562 // and then the values of the where condition
563 for (auto const& keyColumnName : *m_pKeyColumnNames)
564 {
565 if ( i_sTableName.empty() || keyColumnName.second.sTableName == i_sTableName )
566 {
567 setParameter(i++,xParameter,(*_rOriginalRow)[keyColumnName.second.nPosition],keyColumnName.second.nType,keyColumnName.second.nScale);
568 }
569 }
570 if ( !_aIndexColumnPositions.empty() )
571 {
572 // now we have to set the index values
573 auto aIter = m_pColumnNames->begin();
574 for (auto const& indexColumnPosition : _aIndexColumnPositions)
575 {
576 setParameter(i++,xParameter,(*_rOriginalRow)[indexColumnPosition],(*_rOriginalRow)[indexColumnPosition].getTypeKind(),aIter->second.nScale);
577 ++aIter;
578 }
579 }
580 const sal_Int32 nRowsUpdated = xPrep->executeUpdate();
581 m_bUpdated = nRowsUpdated > 0;
582 if(m_bUpdated)
583 {
584 const sal_Int32 nBookmark = ::comphelper::getINT32((*_rInsertRow)[0].getAny());
585 m_aKeyIter = m_aKeyMap.find(nBookmark);
586 m_aKeyIter->second.second.first = 2;
587 m_aKeyIter->second.second.second = xRow;
588 copyRowValue(_rInsertRow,m_aKeyIter->second.first,nBookmark);
589 tryRefetch(_rInsertRow,bRefetch);
590 }
591}
592
593void OKeySet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable )
594{
595 Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
596 fillTableName(xSet);
597
598 OUStringBuffer aSql( "INSERT INTO " + m_aComposedTableName + " ( ");
599
600 // set values and column names
601 OUStringBuffer aValues(" VALUES ( ");
602 OUString aQuote = getIdentifierQuoteString();
603
604 bool bRefetch = true;
605 bool bModified = false;
606 for (auto const& columnName : *m_pColumnNames)
607 {
608 if((*_rInsertRow)[columnName.second.nPosition].isModified())
609 {
610 if ( bRefetch )
611 {
612 bRefetch = std::find(m_aFilterColumns.begin(),m_aFilterColumns.end(),columnName.second.sRealName) == m_aFilterColumns.end();
613 }
614 aSql.append(::dbtools::quoteName( aQuote,columnName.second.sRealName) + ",");
615 aValues.append("?,");
616 bModified = true;
617 }
618 }
619 if ( !bModified )
620 ::dbtools::throwSQLException( DBA_RES( RID_STR_NO_VALUE_CHANGED ), StandardSQLState::GENERAL_ERROR, m_xConnection );
621
622 aSql[aSql.getLength() - 1] = ')';
623 aValues[aValues.getLength() - 1] = ')';
624 aSql.append(aValues);
625 // now create,fill and execute the prepared statement
626 executeInsert(_rInsertRow,aSql.makeStringAndClear(),u"",bRefetch);
627}
628
629void OKeySet::executeInsert( const ORowSetRow& _rInsertRow,const OUString& i_sSQL,std::u16string_view i_sTableName,bool bRefetch )
630{
631 // now create,fill and execute the prepared statement
632 Reference< XPreparedStatement > xPrep(m_xConnection->prepareStatement(i_sSQL));
633 Reference< XParameters > xParameter(xPrep,UNO_QUERY);
634
635 sal_Int32 i = 1;
636 for (auto const& columnName : *m_pColumnNames)
637 {
638 if ( i_sTableName.empty() || columnName.second.sTableName == i_sTableName )
639 {
640 const sal_Int32 nPos = columnName.second.nPosition;
641 if((*_rInsertRow)[nPos].isModified())
642 {
643 if((*_rInsertRow)[nPos].isNull())
644 xParameter->setNull(i++,(*_rInsertRow)[nPos].getTypeKind());
645 else
646 {
647 impl_convertValue_throw(_rInsertRow,columnName.second);
648 (*_rInsertRow)[nPos].setSigned(m_aSignedFlags[nPos-1]);
649 setParameter(i++,xParameter,(*_rInsertRow)[nPos],columnName.second.nType,columnName.second.nScale);
650 }
651 }
652 }
653 }
654
655 m_bInserted = xPrep->executeUpdate() > 0;
656 bool bAutoValuesFetched = false;
657 if ( m_bInserted )
658 {
659 // first insert the default values into the insertrow
660 for (auto const& columnName : *m_pColumnNames)
661 {
662 if ( !(*_rInsertRow)[columnName.second.nPosition].isModified() )
663 {
664 if(columnName.second.bNullable && columnName.second.sDefaultValue.isEmpty())
665 {
666 (*_rInsertRow)[columnName.second.nPosition].setTypeKind(columnName.second.nType);
667 (*_rInsertRow)[columnName.second.nPosition].setNull();
668 }
669 else
670 {
671 (*_rInsertRow)[columnName.second.nPosition] = columnName.second.sDefaultValue;
672 (*_rInsertRow)[columnName.second.nPosition].setTypeKind(columnName.second.nType);
673 }
674 }
675 }
676 try
677 {
678 Reference< XGeneratedResultSet > xGRes(xPrep, UNO_QUERY);
679 if ( xGRes.is() )
680 {
681 Reference< XResultSet > xRes = xGRes->getGeneratedValues();
682 Reference< XRow > xRow(xRes,UNO_QUERY);
683 if ( xRow.is() && xRes->next() )
684 {
685 Reference< XResultSetMetaDataSupplier > xMdSup(xRes,UNO_QUERY);
686 Reference< XResultSetMetaData > xMd = xMdSup->getMetaData();
687 sal_Int32 nColumnCount = xMd->getColumnCount();
688 std::vector< OUString >::const_iterator aAutoIter = m_aAutoColumns.begin();
689 std::vector< OUString >::const_iterator aAutoEnd = m_aAutoColumns.end();
690 for (sal_Int32 j = 1;aAutoIter != aAutoEnd && j <= nColumnCount; ++aAutoIter,++j)
691 {
692 SelectColumnsMetaData::const_iterator aFind = m_pKeyColumnNames->find(*aAutoIter);
693 if ( aFind != m_pKeyColumnNames->end() )
694 (*_rInsertRow)[aFind->second.nPosition].fill(j, aFind->second.nType, xRow);
695 }
696 bAutoValuesFetched = true;
697 }
698 }
699 }
700 catch(const Exception&)
701 {
702 SAL_WARN("dbaccess", "Could not execute GeneratedKeys() stmt");
703 }
704 }
705
706 ::comphelper::disposeComponent(xPrep);
707
708 if ( i_sTableName.empty() && !bAutoValuesFetched && m_bInserted )
709 {
710 // first check if all key column values were set
711 const OUString sQuote = getIdentifierQuoteString();
712 OUStringBuffer sMaxStmt;
713 auto aEnd = m_pKeyColumnNames->end();
714 for (auto const& autoColumn : m_aAutoColumns)
715 {
716 // we will only fetch values which are keycolumns
717 SelectColumnsMetaData::const_iterator aFind = m_pKeyColumnNames->find(autoColumn);
718 if ( aFind != aEnd )
719 {
720 sMaxStmt.append(" MAX(" + ::dbtools::quoteName( sQuote,aFind->second.sRealName) + "),");
721 }
722 }
723
724 if(!sMaxStmt.isEmpty())
725 {
726 sMaxStmt[sMaxStmt.getLength()-1] = ' ';
727 OUString sStmt = "SELECT " + sMaxStmt + "FROM ";
728 OUString sCatalog,sSchema,sTable;
729 ::dbtools::qualifiedNameComponents(m_xConnection->getMetaData(),m_sUpdateTableName,sCatalog,sSchema,sTable,::dbtools::EComposeRule::InDataManipulation);
730 sStmt += ::dbtools::composeTableNameForSelect( m_xConnection, sCatalog, sSchema, sTable );
731 try
732 {
733 // now fetch the autoincrement values
734 Reference<XStatement> xStatement = m_xConnection->createStatement();
735 Reference<XResultSet> xRes = xStatement->executeQuery(sStmt);
736 Reference<XRow> xRow(xRes,UNO_QUERY);
737 if(xRow.is() && xRes->next())
738 {
739 sal_Int32 j=1;
740 for (auto const& autoColumn : m_aAutoColumns)
741 {
742 // we will only fetch values which are keycolumns
743 SelectColumnsMetaData::const_iterator aFind = m_pKeyColumnNames->find(autoColumn);
744 if ( aFind != aEnd )
745 (*_rInsertRow)[aFind->second.nPosition].fill(j++, aFind->second.nType, xRow);
746 }
747 }
748 ::comphelper::disposeComponent(xStatement);
749 }
750 catch(SQLException&)
751 {
752 SAL_WARN("dbaccess", "Could not fetch with MAX() ");
753 }
754 }
755 }
756 if ( m_bInserted )
757 {
758 OKeySetMatrix::const_iterator aKeyIter = m_aKeyMap.end();
759 --aKeyIter;
760 ORowSetRow aKeyRow = new connectivity::ORowVector< ORowSetValue >(m_pKeyColumnNames->size());
761 copyRowValue(_rInsertRow,aKeyRow,aKeyIter->first + 1);
762
763 m_aKeyIter = m_aKeyMap.emplace( aKeyIter->first + 1, OKeySetValue(aKeyRow,std::pair<sal_Int32,Reference<XRow> >(1,Reference<XRow>())) ).first;
764 // now we set the bookmark for this row
765 (*_rInsertRow)[0] = Any(static_cast<sal_Int32>(m_aKeyIter->first));
766 tryRefetch(_rInsertRow,bRefetch);
767 }
768}
769
770void OKeySet::tryRefetch(const ORowSetRow& _rInsertRow,bool bRefetch)
771{
772 if ( bRefetch )
773 {
774 try
775 {
776 bRefetch = doTryRefetch_throw();
777 }
778 catch(const Exception&)
779 {
780 bRefetch = false;
781 }
782 }
783 if ( !bRefetch )
784 {
785 m_aKeyIter->second.second.second = new OPrivateRow(std::vector(*_rInsertRow));
786 }
787}
788
789void OKeySet::copyRowValue(const ORowSetRow& _rInsertRow, ORowSetRow const & _rKeyRow, sal_Int32 i_nBookmark)
790{
792
793 // check the if the parameter values have been changed
794 OSL_ENSURE((m_aParameterValueForCache->size()-1) == m_pParameterNames->size(),"OKeySet::copyRowValue: Parameter values and names differ!");
795 connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aParaValuesIter = m_aParameterValueForCache->begin() +1;
796
797 bool bChanged = false;
798 sal_Int32 i = 1;
799 for (auto const& parameterName : *m_pParameterNames)
800 {
801 ORowSetValue aValue(*aParaValuesIter);
802 aValue.setSigned(m_aSignedFlags[parameterName.second.nPosition-1]);
803 if ( (*_rInsertRow)[parameterName.second.nPosition] != aValue )
804 {
805 rtl::Reference aCopy(
806 new ORowSetValueVector(*m_aParameterValueForCache));
807 (*aCopy)[i] = (*_rInsertRow)[parameterName.second.nPosition];
808 m_aUpdatedParameter[i_nBookmark] = aCopy;
809 bChanged = true;
810 }
811 ++aParaValuesIter;
812 ++i;
813 }
814 if ( !bChanged )
815 {
816 m_aUpdatedParameter.erase(i_nBookmark);
817 }
818
819 // update the key values
820 for (auto const& keyColumnName : *m_pKeyColumnNames)
821 {
822 impl_convertValue_throw(_rInsertRow,keyColumnName.second);
823 *aIter = (*_rInsertRow)[keyColumnName.second.nPosition];
824 aIter->setTypeKind(keyColumnName.second.nType);
825 ++aIter;
826 }
827}
828
829void OKeySet::deleteRow(const ORowSetRow& _rDeleteRow,const connectivity::OSQLTable& _xTable )
830{
831 Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
832 fillTableName(xSet);
833
834 OUStringBuffer aSql("DELETE FROM " + m_aComposedTableName + " WHERE ");
835
836 // list all columns that should be set
837 OUString aQuote = getIdentifierQuoteString();
838 static const char aAnd[] = " AND ";
839
840 // use keys and indexes for exact positioning
841 Reference<XIndexesSupplier> xIndexSup(_xTable,UNO_QUERY);
842 Reference<XIndexAccess> xIndexes;
843 if ( xIndexSup.is() )
844 xIndexes.set(xIndexSup->getIndexes(),UNO_QUERY);
845
846 // Reference<XColumnsSupplier>
847 std::vector< Reference<XNameAccess> > aAllIndexColumns;
848 lcl_fillIndexColumns(xIndexes,aAllIndexColumns);
849
850 OUStringBuffer sIndexCondition;
851 std::vector<sal_Int32> aIndexColumnPositions;
852
853 for (auto const& columnName : *m_pColumnNames)
854 {
855 if ( m_pKeyColumnNames->find(columnName.first) != m_pKeyColumnNames->end() )
856 {
857 aSql.append(::dbtools::quoteName( aQuote,columnName.second.sRealName));
858 if((*_rDeleteRow)[columnName.second.nPosition].isNull())
859 {
860 SAL_WARN("dbaccess", "can a primary key be null");
861 aSql.append(" IS NULL");
862 }
863 else
864 aSql.append(" = ?");
865 aSql.append(aAnd);
866 }
867 else
868 {
869 for (auto const& indexColumn : aAllIndexColumns)
870 {
871 if(indexColumn->hasByName(columnName.first))
872 {
873 sIndexCondition.append(::dbtools::quoteName( aQuote,columnName.second.sRealName));
874 if((*_rDeleteRow)[columnName.second.nPosition].isNull())
875 sIndexCondition.append(" IS NULL");
876 else
877 {
878 sIndexCondition.append(" = ?");
879 aIndexColumnPositions.push_back(columnName.second.nPosition);
880 }
881 sIndexCondition.append(aAnd);
882
883 break;
884 }
885 }
886 }
887 }
888 aSql.append(sIndexCondition);
889 aSql.setLength(aSql.getLength()-5);
890
891 // now create end execute the prepared statement
892 Reference< XPreparedStatement > xPrep(m_xConnection->prepareStatement(aSql.makeStringAndClear()));
893 Reference< XParameters > xParameter(xPrep,UNO_QUERY);
894
895 sal_Int32 i = 1;
896 for (auto const& keyColumnName : *m_pKeyColumnNames)
897 {
898 setParameter(i++,xParameter,(*_rDeleteRow)[keyColumnName.second.nPosition],keyColumnName.second.nType,keyColumnName.second.nScale);
899 }
900
901 // now we have to set the index values
902 auto aIter = m_pColumnNames->begin();
903 for (auto const& indexColumnPosition : aIndexColumnPositions)
904 {
905 setParameter(i++,xParameter,(*_rDeleteRow)[indexColumnPosition],(*_rDeleteRow)[indexColumnPosition].getTypeKind(),aIter->second.nScale);
906 ++aIter;
907 }
908
909 m_bDeleted = xPrep->executeUpdate() > 0;
910
911 if(m_bDeleted)
912 {
913 sal_Int32 nBookmark = ::comphelper::getINT32((*_rDeleteRow)[0].getAny());
914 if(m_aKeyIter == m_aKeyMap.find(nBookmark) && m_aKeyIter != m_aKeyMap.end())
915 ++m_aKeyIter;
916 m_aKeyMap.erase(nBookmark);
917 m_bDeleted = true;
918 }
919}
920
921bool OKeySet::next()
922{
923 m_bInserted = m_bUpdated = m_bDeleted = false;
924
925 if(isAfterLast())
926 return false;
927 ++m_aKeyIter;
928 if(!m_bRowCountFinal && m_aKeyIter == m_aKeyMap.end())
929 {
930 // not yet all records fetched, but we reached the end of those we fetched
931 // try to fetch one more row
932 if (fetchRow())
933 {
934 OSL_ENSURE(!isAfterLast(), "fetchRow succeeded, but isAfterLast()");
935 return true;
936 }
937 else
938 {
939 // nope, we arrived at end of data
940 m_aKeyIter = m_aKeyMap.end();
941 OSL_ENSURE(isAfterLast(), "fetchRow failed, but not end of data");
942 }
943 }
944
945 invalidateRow();
946 return !isAfterLast();
947}
948
949bool OKeySet::isBeforeFirst( )
950{
951 return m_aKeyIter == m_aKeyMap.begin();
952}
953
954bool OKeySet::isAfterLast( )
955{
956 return m_bRowCountFinal && m_aKeyIter == m_aKeyMap.end();
957}
958
959void OKeySet::beforeFirst( )
960{
961 m_bInserted = m_bUpdated = m_bDeleted = false;
962 m_aKeyIter = m_aKeyMap.begin();
963 invalidateRow();
964}
965
966void OKeySet::afterLast( )
967{
968 m_bInserted = m_bUpdated = m_bDeleted = false;
969 fillAllRows();
970 m_aKeyIter = m_aKeyMap.end();
971 invalidateRow();
972}
973
974bool OKeySet::first()
975{
976 m_bInserted = m_bUpdated = m_bDeleted = false;
977 m_aKeyIter = m_aKeyMap.begin();
978 ++m_aKeyIter;
979 if(m_aKeyIter == m_aKeyMap.end())
980 {
981 if (!fetchRow())
982 {
983 m_aKeyIter = m_aKeyMap.end();
984 return false;
985 }
986 }
987 else
988 invalidateRow();
989 return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
990}
991
992bool OKeySet::last( )
993{
994 m_bInserted = m_bUpdated = m_bDeleted = false;
995 bool bFetchedRow = fillAllRows();
996
997 m_aKeyIter = m_aKeyMap.end();
998 --m_aKeyIter;
999 if ( !bFetchedRow )
1000 {
1001 invalidateRow();
1002 }
1003 return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1004}
1005
1006sal_Int32 OKeySet::getRow( )
1007{
1008 OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
1009 OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
1010
1011 return std::distance(m_aKeyMap.begin(),m_aKeyIter);
1012}
1013
1014bool OKeySet::absolute( sal_Int32 row )
1015{
1016 m_bInserted = m_bUpdated = m_bDeleted = false;
1017 OSL_ENSURE(row,"absolute(0) isn't allowed!");
1018 bool bFetchedRow = false;
1019 if(row < 0)
1020 {
1021 if(!m_bRowCountFinal)
1022 bFetchedRow = fillAllRows();
1023
1024 row = std::min(std::abs(row), static_cast<sal_Int32>(std::distance(m_aKeyMap.begin(), m_aKeyIter)));
1025 m_aKeyIter = std::prev(m_aKeyIter, row);
1026 }
1027 else
1028 {
1029 if(row >= static_cast<sal_Int32>(m_aKeyMap.size()))
1030 {
1031 // we don't have this row
1032 if(!m_bRowCountFinal)
1033 {
1034 // but there may still be rows to fetch.
1035 bool bNext = true;
1036 for(sal_Int32 i=m_aKeyMap.size()-1;i < row && bNext;++i)
1037 bNext = fetchRow();
1038 // it is guaranteed that the above loop has executed at least once,
1039 // that is fetchRow called at least once.
1040 if ( bNext )
1041 {
1042 bFetchedRow = true;
1043 }
1044 else
1045 {
1046 // reached end of data before desired row
1047 m_aKeyIter = m_aKeyMap.end();
1048 return false;
1049 }
1050 }
1051 else
1052 {
1053 // no more rows to fetch -> fail
1054 m_aKeyIter = m_aKeyMap.end();
1055 return false;
1056 }
1057 }
1058 else
1059 {
1060 m_aKeyIter = std::next(m_aKeyMap.begin(), row);
1061 }
1062 }
1063 if ( !bFetchedRow )
1064 {
1065 invalidateRow();
1066 }
1067
1068 return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1069}
1070
1071bool OKeySet::previous()
1072{
1073 m_bInserted = m_bUpdated = m_bDeleted = false;
1074 if(m_aKeyIter != m_aKeyMap.begin())
1075 {
1076 --m_aKeyIter;
1077 invalidateRow();
1078 }
1079 return m_aKeyIter != m_aKeyMap.begin();
1080}
1081
1082bool OKeySet::doTryRefetch_throw()
1083{
1084 ensureStatement( );
1085 // we just reassign the base members
1086 Reference< XParameters > xParameter(m_xStatement,UNO_QUERY);
1087 OSL_ENSURE(xParameter.is(),"No Parameter interface!");
1088 xParameter->clearParameters();
1089
1090 sal_Int32 nPos=1;
1093 OUpdatedParameter::const_iterator aUpdateFind = m_aUpdatedParameter.find(m_aKeyIter->first);
1094 if ( aUpdateFind == m_aUpdatedParameter.end() )
1095 {
1096 aParaIter = m_aParameterValueForCache->begin();
1097 aParaEnd = m_aParameterValueForCache->end();
1098 }
1099 else
1100 {
1101 aParaIter = aUpdateFind->second->begin();
1102 aParaEnd = aUpdateFind->second->end();
1103 }
1104
1105 for(++aParaIter;aParaIter != aParaEnd;++aParaIter,++nPos)
1106 {
1107 ::dbtools::setObjectWithInfo( xParameter, nPos, aParaIter->makeAny(), aParaIter->getTypeKind() );
1108 }
1109
1110 // now set the primary key column values
1111 connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aIter = m_aKeyIter->second.first->begin();
1112 for (auto const& keyColumnName : *m_pKeyColumnNames)
1113 setOneKeyColumnParameter(nPos,xParameter,*aIter++,keyColumnName.second.nType,keyColumnName.second.nScale);
1114 for (auto const& foreignColumnName : *m_pForeignColumnNames)
1115 setOneKeyColumnParameter(nPos,xParameter,*aIter++,foreignColumnName.second.nType,foreignColumnName.second.nScale);
1116
1117 m_xSet = m_xStatement->executeQuery();
1118 OSL_ENSURE(m_xSet.is(),"No resultset from statement!");
1119 return m_xSet->next();
1120}
1121
1122void OKeySet::refreshRow()
1123{
1124 invalidateRow();
1125
1126 if(isBeforeFirst() || isAfterLast())
1127 return;
1128
1129 if ( m_aKeyIter->second.second.second.is() )
1130 {
1131 m_xRow = m_aKeyIter->second.second.second;
1132 return;
1133 }
1134
1135 bool bOK = doTryRefetch_throw();
1136 if ( !bOK )
1137 {
1138 // This row has disappeared; remove it.
1139 OKeySetMatrix::const_iterator aTemp = m_aKeyIter;
1140 // use *next* row
1141 ++m_aKeyIter;
1142 m_aKeyMap.erase(aTemp);
1143
1144 // adjust RowCount for the row we have removed
1145 if (m_rRowCount > 0)
1146 --m_rRowCount;
1147 else
1148 SAL_WARN("dbaccess", "m_rRowCount got out of sync: non-empty m_aKeyMap, but m_rRowCount <= 0");
1149
1150 if (m_aKeyIter == m_aKeyMap.end())
1151 {
1152 ::comphelper::disposeComponent(m_xSet);
1153 if (!isAfterLast())
1154 {
1155 // it was the last fetched row,
1156 // but there may be another one to fetch
1157 if (!fetchRow())
1158 {
1159 // nope, that really was the last
1160 m_aKeyIter = m_aKeyMap.end();
1161 OSL_ENSURE(isAfterLast(), "fetchRow() failed but not isAfterLast()!");
1162 }
1163 }
1164 // Now, either fetchRow has set m_xRow or isAfterLast()
1165 }
1166 else
1167 {
1168 refreshRow();
1169 }
1170 }
1171 else
1172 {
1173 m_xRow.set(m_xSet,UNO_QUERY);
1174 OSL_ENSURE(m_xRow.is(),"No row from statement!");
1175 }
1176}
1177
1178bool OKeySet::fetchRow()
1179{
1180 // fetch the next row and append on the keyset
1181 bool bRet = false;
1182 if ( !m_bRowCountFinal && (!m_nMaxRows || sal_Int32(m_aKeyMap.size()) < m_nMaxRows) )
1183 bRet = m_xDriverSet->next();
1184 if ( bRet )
1185 {
1186 ORowSetRow aKeyRow = new connectivity::ORowVector< ORowSetValue >((*m_pKeyColumnNames).size() + m_pForeignColumnNames->size());
1187
1188 ::comphelper::disposeComponent(m_xSet);
1189 m_xRow.set(m_xDriverRow, UNO_SET_THROW);
1190
1192 // copy key columns
1193 for (auto const& keyColumnName : *m_pKeyColumnNames)
1194 {
1195 const SelectColumnDescription& rColDesc = keyColumnName.second;
1196 aIter->fill(rColDesc.nPosition, rColDesc.nType, m_xRow);
1197 ++aIter;
1198 }
1199 // copy missing columns from other tables
1200 for (auto const& foreignColumnName : *m_pForeignColumnNames)
1201 {
1202 const SelectColumnDescription& rColDesc = foreignColumnName.second;
1203 aIter->fill(rColDesc.nPosition, rColDesc.nType, m_xRow);
1204 ++aIter;
1205 }
1206 m_aKeyIter = m_aKeyMap.emplace( m_aKeyMap.rbegin()->first+1,OKeySetValue(aKeyRow,std::pair<sal_Int32,Reference<XRow> >(0,Reference<XRow>())) ).first;
1207 }
1208 else
1209 m_bRowCountFinal = true;
1210 return bRet;
1211}
1212
1213bool OKeySet::fillAllRows()
1214{
1215 if(m_bRowCountFinal)
1216 {
1217 return false;
1218 }
1219 else
1220 {
1221 while(fetchRow())
1222 ;
1223 return true;
1224 }
1225}
1226
1227// XRow
1228sal_Bool SAL_CALL OKeySet::wasNull( )
1229{
1230 if ( ! m_xRow.is() )
1231 throwGenericSQLException("Must call getFOO() for some FOO before wasNull()", *this);
1232
1233 OSL_ENSURE(m_xRow.is(),"m_xRow is null! I've thrown, but function execution continued?");
1234 return m_xRow->wasNull();
1235}
1236
1237inline void OKeySet::ensureRowForData( )
1238{
1239 if (! m_xRow.is() )
1240 refreshRow();
1241 if (! m_xRow.is() )
1242 throwSQLException("Failed to refetch row", "02000", *this, -2);
1243
1244 OSL_ENSURE(m_xRow.is(),"m_xRow is null! I've called throwSQLException but execution continued?");
1245}
1246
1247OUString SAL_CALL OKeySet::getString( sal_Int32 columnIndex )
1248{
1249 ensureRowForData();
1250 return m_xRow->getString(columnIndex);
1251}
1252
1253sal_Bool SAL_CALL OKeySet::getBoolean( sal_Int32 columnIndex )
1254{
1255 ensureRowForData();
1256 return m_xRow->getBoolean(columnIndex);
1257}
1258
1259sal_Int8 SAL_CALL OKeySet::getByte( sal_Int32 columnIndex )
1260{
1261 ensureRowForData();
1262 return m_xRow->getByte(columnIndex);
1263}
1264
1265sal_Int16 SAL_CALL OKeySet::getShort( sal_Int32 columnIndex )
1266{
1267 ensureRowForData();
1268 return m_xRow->getShort(columnIndex);
1269}
1270
1271sal_Int32 SAL_CALL OKeySet::getInt( sal_Int32 columnIndex )
1272{
1273 ensureRowForData();
1274 return m_xRow->getInt(columnIndex);
1275}
1276
1277sal_Int64 SAL_CALL OKeySet::getLong( sal_Int32 columnIndex )
1278{
1279 ensureRowForData();
1280 return m_xRow->getLong(columnIndex);
1281}
1282
1283float SAL_CALL OKeySet::getFloat( sal_Int32 columnIndex )
1284{
1285 ensureRowForData();
1286 return m_xRow->getFloat(columnIndex);
1287}
1288
1289double SAL_CALL OKeySet::getDouble( sal_Int32 columnIndex )
1290{
1291 ensureRowForData();
1292 return m_xRow->getDouble(columnIndex);
1293}
1294
1295Sequence< sal_Int8 > SAL_CALL OKeySet::getBytes( sal_Int32 columnIndex )
1296{
1297 ensureRowForData();
1298 return m_xRow->getBytes(columnIndex);
1299}
1300
1301css::util::Date SAL_CALL OKeySet::getDate( sal_Int32 columnIndex )
1302{
1303 ensureRowForData();
1304 return m_xRow->getDate(columnIndex);
1305}
1306
1307css::util::Time SAL_CALL OKeySet::getTime( sal_Int32 columnIndex )
1308{
1309 ensureRowForData();
1310 return m_xRow->getTime(columnIndex);
1311}
1312
1313css::util::DateTime SAL_CALL OKeySet::getTimestamp( sal_Int32 columnIndex )
1314{
1315 ensureRowForData();
1316 return m_xRow->getTimestamp(columnIndex);
1317}
1318
1319Reference< css::io::XInputStream > SAL_CALL OKeySet::getBinaryStream( sal_Int32 columnIndex )
1320{
1321 ensureRowForData();
1322 return m_xRow->getBinaryStream(columnIndex);
1323}
1324
1325Reference< css::io::XInputStream > SAL_CALL OKeySet::getCharacterStream( sal_Int32 columnIndex )
1326{
1327 ensureRowForData();
1328 return m_xRow->getCharacterStream(columnIndex);
1329}
1330
1331Any SAL_CALL OKeySet::getObject( sal_Int32 columnIndex, const Reference< css::container::XNameAccess >& typeMap )
1332{
1333 ensureRowForData();
1334 return m_xRow->getObject(columnIndex,typeMap);
1335}
1336
1337Reference< XRef > SAL_CALL OKeySet::getRef( sal_Int32 columnIndex )
1338{
1339 ensureRowForData();
1340 return m_xRow->getRef(columnIndex);
1341}
1342
1343Reference< XBlob > SAL_CALL OKeySet::getBlob( sal_Int32 columnIndex )
1344{
1345 ensureRowForData();
1346 return m_xRow->getBlob(columnIndex);
1347}
1348
1349Reference< XClob > SAL_CALL OKeySet::getClob( sal_Int32 columnIndex )
1350{
1351 ensureRowForData();
1352 return m_xRow->getClob(columnIndex);
1353}
1354
1355Reference< XArray > SAL_CALL OKeySet::getArray( sal_Int32 columnIndex )
1356{
1357 ensureRowForData();
1358 return m_xRow->getArray(columnIndex);
1359}
1360
1361bool OKeySet::rowUpdated( )
1362{
1363 return m_aKeyIter != m_aKeyMap.begin() && m_aKeyIter != m_aKeyMap.end() && m_aKeyIter->second.second.first == 2;
1364}
1365
1366bool OKeySet::rowInserted( )
1367{
1368 return m_aKeyIter != m_aKeyMap.begin() && m_aKeyIter != m_aKeyMap.end() && m_aKeyIter->second.second.first == 1;
1369}
1370
1371bool OKeySet::rowDeleted( )
1372{
1373 bool bDeleted = m_bDeleted;
1374 m_bDeleted = false;
1375 return bDeleted;
1376}
1377
1378namespace dbaccess
1379{
1380
1381void getColumnPositions(const Reference<XNameAccess>& _rxQueryColumns,
1382 const css::uno::Sequence< OUString >& _aColumnNames,
1383 std::u16string_view _rsUpdateTableName,
1384 SelectColumnsMetaData& o_rColumnNames,
1385 bool i_bAppendTableName)
1386 {
1387 // get the real name of the columns
1388 Sequence< OUString> aSelNames(_rxQueryColumns->getElementNames());
1389 const OUString* pSelIter = aSelNames.getConstArray();
1390 const OUString* pSelEnd = pSelIter + aSelNames.getLength();
1391
1392 const OUString* pTblColumnIter = _aColumnNames.getConstArray();
1393 const OUString* pTblColumnEnd = pTblColumnIter + _aColumnNames.getLength();
1394
1395 ::comphelper::UStringMixEqual bCase(o_rColumnNames.key_comp().isCaseSensitive());
1396
1397 for(sal_Int32 nPos = 1;pSelIter != pSelEnd;++pSelIter,++nPos)
1398 {
1399 Reference<XPropertySet> xQueryColumnProp(_rxQueryColumns->getByName(*pSelIter),UNO_QUERY_THROW);
1400 OUString sRealName,sTableName;
1401 OSL_ENSURE(xQueryColumnProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
1402 OSL_ENSURE(xQueryColumnProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
1403 xQueryColumnProp->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
1404 xQueryColumnProp->getPropertyValue(PROPERTY_TABLENAME) >>= sTableName;
1405
1406 for(;pTblColumnIter != pTblColumnEnd;++pTblColumnIter)
1407 {
1408 if(bCase(sRealName,*pTblColumnIter) && bCase(_rsUpdateTableName,sTableName) && o_rColumnNames.find(*pTblColumnIter) == o_rColumnNames.end())
1409 {
1410 sal_Int32 nType = 0;
1411 xQueryColumnProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
1412 sal_Int32 nScale = 0;
1413 xQueryColumnProp->getPropertyValue(PROPERTY_SCALE) >>= nScale;
1414 OUString sColumnDefault;
1415 if ( xQueryColumnProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_DEFAULTVALUE) )
1416 xQueryColumnProp->getPropertyValue(PROPERTY_DEFAULTVALUE) >>= sColumnDefault;
1417
1418 sal_Int32 nNullable = ColumnValue::NULLABLE_UNKNOWN;
1419 OSL_VERIFY( xQueryColumnProp->getPropertyValue( PROPERTY_ISNULLABLE ) >>= nNullable );
1420
1421 SelectColumnDescription aColDesc( nPos, nType, nScale, nNullable != sdbc::ColumnValue::NO_NULLS, sColumnDefault );
1422 OUString sName;
1423 if ( i_bAppendTableName )
1424 {
1425 sName = sTableName + "." + sRealName;
1426 aColDesc.sRealName = sRealName;
1427 aColDesc.sTableName = sTableName;
1428 }
1429 else
1430 {
1431 sName = sRealName;
1432 }
1433 o_rColumnNames[sName] = aColDesc;
1434
1435 break;
1436 }
1437 }
1438 pTblColumnIter = _aColumnNames.getConstArray();
1439 }
1440 }
1441}
1442
1443void OKeySet::impl_convertValue_throw(const ORowSetRow& _rInsertRow,const SelectColumnDescription& i_aMetaData)
1444{
1445 ORowSetValue& aValue((*_rInsertRow)[i_aMetaData.nPosition]);
1446 switch(i_aMetaData.nType)
1447 {
1448 case DataType::DECIMAL:
1449 case DataType::NUMERIC:
1450 {
1451 OUString sValue = aValue.getString();
1452 sal_Int32 nIndex = sValue.indexOf('.');
1453 if ( nIndex != -1 )
1454 {
1455 aValue = sValue.copy(0,std::min(sValue.getLength(),nIndex + (i_aMetaData.nScale > 0 ? i_aMetaData.nScale + 1 : 0)));
1456 }
1457 }
1458 break;
1459 default:
1460 break;
1461 }
1462}
1463
1464/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const Reference< XRow > m_xRow
OptionalString sSchema
OptionalString sCatalog
OptionalString sName
OUString getString() const
void setSigned(bool _bSig)
vStatements_t m_vStatements
Definition: KeySet.hxx:97
css::uno::Reference< css::sdb::XSingleSelectQueryAnalyzer > m_xComposer
Definition: KeySet.hxx:101
css::uno::Reference< css::sdbc::XResultSet > m_xSet
Definition: KeySet.hxx:99
#define DBA_RES(id)
int nCount
float u
Reference< XSingleServiceFactory > xFactory
const char sQuote[]
sal_Int32 nNullable
sal_Int32 nIndex
sal_uInt16 nPos
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
@ Exception
bool getBOOL(const Any &_rAny)
css::uno::Reference< css::sdbcx::XColumnsSupplier > OSQLTable
std::map< OUString, SelectColumnDescription, ::comphelper::UStringMixLess > SelectColumnsMetaData
Definition: KeySet.hxx:62
std::pair< ORowSetRow, std::pair< sal_Int32, css::uno::Reference< css::sdbc::XRow > > > OKeySetValue
Definition: KeySet.hxx:71
connectivity::ORowVector< connectivity::ORowSetValue > ORowSetValueVector
Definition: RowSetRow.hxx:29
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
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
void throwSQLException(const OUString &_rMessage, const OUString &_rSQLState, const Reference< XInterface > &_rxContext, const sal_Int32 _nErrorCode)
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
Reference< XNameAccess > getPrimaryKeyColumns_throw(const Any &i_aTable)
int i
Reference< XConnection > m_xConnection
Definition: objectnames.cxx:79
const char * columnName
QPRO_FUNC_TYPE nType
constexpr OUStringLiteral PROPERTY_ISAUTOINCREMENT(u"IsAutoIncrement")
constexpr OUStringLiteral PROPERTY_ISNULLABLE(u"IsNullable")
constexpr OUStringLiteral PROPERTY_SCHEMANAME(u"SchemaName")
constexpr OUStringLiteral PROPERTY_SCALE(u"Scale")
constexpr OUStringLiteral PROPERTY_REALNAME(u"RealName")
constexpr OUStringLiteral SERVICE_NAME_SINGLESELECTQUERYCOMPOSER
Definition: strings.hxx:201
constexpr OUStringLiteral PROPERTY_TABLENAME(u"TableName")
constexpr OUStringLiteral PROPERTY_DEFAULTVALUE(u"DefaultValue")
constexpr OUStringLiteral PROPERTY_CATALOGNAME(u"CatalogName")
constexpr OUStringLiteral PROPERTY_TYPE(u"Type")
constexpr OUStringLiteral PROPERTY_NAME(u"Name")
constexpr OUStringLiteral PROPERTY_ISUNIQUE(u"IsUnique")
constexpr OUStringLiteral PROPERTY_ISPRIMARYKEYINDEX(u"IsPrimaryKeyIndex")
void append(const OUString &lhs)
unsigned char sal_Bool
signed char sal_Int8