25#include <core_resource.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>
46#include <com/sun/star/io/XInputStream.hpp>
47#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
65using namespace ::
cppu;
71 void lcl_fillIndexColumns(
const Reference<XIndexAccess>& _xIndexes, std::vector< Reference<XNameAccess> >& _rAllIndexColumns)
73 if ( !_xIndexes.is() )
76 Reference<XPropertySet> xIndexColsSup;
77 sal_Int32
nCount = _xIndexes->getCount();
78 for(sal_Int32 j = 0 ; j <
nCount ; ++j)
80 xIndexColsSup.set(_xIndexes->getByIndex(j),UNO_QUERY);
81 if( xIndexColsSup.is()
85 _rAllIndexColumns.push_back(Reference<XColumnsSupplier>(xIndexColsSup,UNO_QUERY_THROW)->getColumns());
89 template <
typename T >
void tryDispose( Reference<T> &r )
93 ::comphelper::disposeComponent(r);
95 catch(
const Exception&)
101 SAL_WARN(
"dbaccess",
"Unknown Exception occurred");
108 OUString _sUpdateTableName,
109 const Reference< XSingleSelectQueryAnalyzer >& _xComposer,
111 sal_Int32 i_nMaxRows,
112 sal_Int32& o_nRowCount)
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)
129 tryDispose(statement.second);
136void OKeySet::initColumns()
138 Reference<XDatabaseMetaData> xMeta =
m_xConnection->getMetaData();
139 bool bCase = xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers();
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)
153 Sequence< OUString> aBestColumnNames;
155 if ( xKeyColumns.is() )
156 aBestColumnNames = xKeyColumns->getElementNames();
158 const Reference<XColumnsSupplier> xTblColSup(i_aTable,UNO_QUERY_THROW);
159 const Reference<XNameAccess> xTblColumns = xTblColSup->getColumns();
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)
168 Reference<XPropertySet> xPara(xQueryParameters->getByIndex(
i),UNO_QUERY_THROW);
172 OUString sUpdateTableName( i_rUpdateTableName );
173 if ( sUpdateTableName.isEmpty() )
175 SAL_WARN(
"dbaccess",
"OKeySet::findTableColumnsMatching_throw: This is a fallback only - it won't work when the table has an alias name." );
186 Reference<XPropertySet> xTableProp( i_aTable, UNO_QUERY_THROW );
197 if ( o_pKeyColumnNames->empty() )
199 ::dbtools::throwGenericSQLException(
"Could not find any key column.", *
this );
202 for (
auto const& keyColumn : *o_pKeyColumnNames)
204 if ( !xTblColumns->hasByName( keyColumn.second.sRealName ) )
207 Reference<XPropertySet> xProp( xTblColumns->getByName( keyColumn.second.sRealName ), UNO_QUERY );
210 m_aAutoColumns.push_back( keyColumn.first );
216 void appendOneKeyColumnClause( std::u16string_view tblName,
const OUString &colName,
const connectivity::ORowSetValue &_rValue, OUStringBuffer &o_buf )
222 fullName = OUString::Concat(tblName) +
"." + colName;
225 o_buf.append(fullName +
" IS NULL ");
229 o_buf.append(fullName +
" = ? ");
234void OKeySet::setOneKeyColumnParameter( sal_Int32 &nPos,
const Reference< XParameters > &_xParameter,
const connectivity::ORowSetValue &_rValue, sal_Int32 _nType, sal_Int32 _nScale )
243 setParameter(
nPos++, _xParameter, _rValue, _nType, _nScale );
247OUStringBuffer OKeySet::createKeyFilter()
251 static const char aAnd[] =
" AND ";
252 const OUString aQuote = getIdentifierQuoteString();
253 OUStringBuffer aFilter;
255 Reference<XDatabaseMetaData> xMeta =
m_xConnection->getMetaData();
256 for (
auto const& keyColumnName : *m_pKeyColumnNames)
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),
265 for (
auto const& foreignColumnName : * m_pForeignColumnNames)
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),
277void OKeySet::construct(
const Reference< XResultSet>& _xDriverSet,
const OUString& i_sRowSetFilter)
279 OCacheSet::construct(_xDriverSet,i_sRowSetFilter);
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 );
288 Reference< XSingleSelectQueryComposer> xSourceComposer(m_xComposer,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 )
297 const OUString* pIter =
aSeq.getConstArray();
298 const OUString*
const pEnd = pIter +
aSeq.getLength();
299 for(;pIter != pEnd;++pIter)
301 if ( *pIter != m_sUpdateTableName )
304 Reference<XPropertySet> xProp(xSelColSup,uno::UNO_QUERY);
305 OUString sSelectTableName = ::dbtools::composeTableName( xMeta, xProp, ::dbtools::EComposeRule::InDataManipulation,
false );
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();
322void OKeySet::reset(
const Reference< XResultSet>& _xDriverSet)
324 OCacheSet::construct(_xDriverSet, m_sRowSetFilter);
325 m_bRowCountFinal =
false;
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();
332void OKeySet::ensureStatement( )
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())
345 std::pair< vStatements_t::const_iterator, bool > insert_result
346 (m_vStatements.emplace( FilterColumnsNULL, m_xStatement));
347 (void) insert_result;
348 assert(insert_result.second);
352 m_xStatement = pNewStatement->second;
355void OKeySet::makeNewStatement()
357 Reference< XSingleSelectQueryComposer> xSourceComposer(m_xComposer,UNO_QUERY);
360 xAnalyzer->setElementaryQuery(xSourceComposer->getElementaryQuery());
362 OUStringBuffer aFilter(createKeyFilter());
363 executeStatement(aFilter, xAnalyzer);
366void OKeySet::executeStatement(OUStringBuffer& io_aFilter, Reference<XSingleSelectQueryComposer>& io_xAnalyzer)
368 bool bFilterSet = !m_sRowSetFilter.isEmpty();
372 aFilterCreator.
append( m_sRowSetFilter );
373 aFilterCreator.
append( io_aFilter.makeStringAndClear() );
376 io_xAnalyzer->setFilter(io_aFilter.makeStringAndClear());
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)
384 const PropertyValue* pAnd = pOr->getConstArray();
385 const PropertyValue* pAndEnd = pAnd + pOr->getLength();
386 for(;pAnd != pAndEnd;++pAnd)
389 if ( !(pAnd->Value >>= sValue) || !( sValue ==
"?" || sValue.startsWith(
":" ) ) )
391 m_aFilterColumns.push_back(pAnd->Name);
396 m_xStatement =
m_xConnection->prepareStatement(io_xAnalyzer->getQueryWithSubstitution());
397 ::comphelper::disposeComponent(io_xAnalyzer);
400void OKeySet::invalidateRow()
403 ::comphelper::disposeComponent(m_xSet);
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);
413bool OKeySet::moveToBookmark(
const Any& bookmark )
415 m_bInserted = m_bUpdated = m_bDeleted =
false;
416 m_aKeyIter = m_aKeyMap.find(::comphelper::getINT32(bookmark));
418 return m_aKeyIter != m_aKeyMap.end();
421sal_Int32 OKeySet::compareBookmarks(
const Any& _first,
const Any& _second )
423 sal_Int32 nFirst = 0, nSecond = 0;
427 return (nFirst != nSecond) ? CompareBookmark::NOT_EQUAL : CompareBookmark::EQUAL;
430bool OKeySet::hasOrderedBookmarks( )
435sal_Int32 OKeySet::hashBookmark(
const Any& bookmark )
437 return ::comphelper::getINT32(bookmark);
443 Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
446 OUStringBuffer aSql =
"UPDATE " + m_aComposedTableName +
" SET ";
448 constexpr OUStringLiteral aPara(
u" = ?,");
449 OUString aQuote = getIdentifierQuoteString();
450 constexpr OUStringLiteral aAnd(
u" AND ");
451 OUString sIsNull(
" IS NULL");
452 OUString sParam(
" = ?");
455 Reference<XIndexesSupplier> xIndexSup(_xTable,UNO_QUERY);
456 Reference<XIndexAccess> xIndexes;
457 if ( xIndexSup.is() )
458 xIndexes.set(xIndexSup->getIndexes(),UNO_QUERY);
460 std::vector< Reference<XNameAccess> > aAllIndexColumns;
461 lcl_fillIndexColumns(xIndexes,aAllIndexColumns);
463 OUStringBuffer sKeyCondition,sIndexCondition;
464 std::vector<sal_Int32> aIndexColumnPositions;
466 const sal_Int32 nOldLength = aSql.getLength();
468 for (
auto const&
columnName : *m_pColumnNames)
470 if ( m_pKeyColumnNames->find(
columnName.first) != m_pKeyColumnNames->end() )
472 sKeyCondition.append(::dbtools::quoteName( aQuote,
columnName.second.sRealName));
473 if((*_rOriginalRow)[
columnName.second.nPosition].isNull())
474 sKeyCondition.append(sIsNull);
476 sKeyCondition.append(sParam);
477 sKeyCondition.append(aAnd);
481 for (
auto const& indexColumn : aAllIndexColumns)
485 sIndexCondition.append(::dbtools::quoteName( aQuote,
columnName.second.sRealName));
486 if((*_rOriginalRow)[
columnName.second.nPosition].isNull())
487 sIndexCondition.append(sIsNull);
490 sIndexCondition.append(sParam);
491 aIndexColumnPositions.push_back(
columnName.second.nPosition);
493 sIndexCondition.append(aAnd);
498 if((*_rInsertRow)[
columnName.second.nPosition].isModified())
500 aSql.append(::dbtools::quoteName( aQuote,
columnName.second.sRealName) + aPara);
504 if( aSql.getLength() != nOldLength )
506 aSql.setLength(aSql.getLength()-1);
509 ::dbtools::throwSQLException(
DBA_RES( RID_STR_NO_VALUE_CHANGED ), StandardSQLState::GENERAL_ERROR,
m_xConnection );
511 if(!sKeyCondition.isEmpty() || !sIndexCondition.isEmpty())
513 aSql.append(
" WHERE ");
514 if(!sKeyCondition.isEmpty() && !sIndexCondition.isEmpty())
516 aSql.append(sKeyCondition + sIndexCondition);
518 else if(!sKeyCondition.isEmpty())
520 aSql.append(sKeyCondition);
522 else if(!sIndexCondition.isEmpty())
524 aSql.append(sIndexCondition);
526 aSql.setLength(aSql.getLength()-5);
529 ::dbtools::throwSQLException(
DBA_RES( RID_STR_NO_CONDITION_FOR_PK ), StandardSQLState::GENERAL_ERROR,
m_xConnection );
532 executeUpdate(_rInsertRow ,_rOriginalRow,aSql.makeStringAndClear(),
u"",aIndexColumnPositions);
535void OKeySet::executeUpdate(
const ORowSetRow& _rInsertRow ,
const ORowSetRow& _rOriginalRow,
const OUString& i_sSQL,std::u16string_view i_sTableName,
const std::vector<sal_Int32>& _aIndexColumnPositions)
538 Reference< XPreparedStatement > xPrep(
m_xConnection->prepareStatement(i_sSQL));
539 Reference< XParameters > xParameter(xPrep,UNO_QUERY);
541 bool bRefetch =
true;
542 Reference<XRow> xRow;
545 for (
auto const&
columnName : *m_pColumnNames)
547 if ( i_sTableName.empty() ||
columnName.second.sTableName == i_sTableName )
550 if((*_rInsertRow)[
nPos].isModified())
554 bRefetch = std::find(m_aFilterColumns.begin(),m_aFilterColumns.end(),
columnName.second.sRealName) == m_aFilterColumns.end();
556 impl_convertValue_throw(_rInsertRow,
columnName.second);
557 (*_rInsertRow)[
nPos].setSigned((*_rOriginalRow)[
nPos].isSigned());
563 for (
auto const& keyColumnName : *m_pKeyColumnNames)
565 if ( i_sTableName.empty() || keyColumnName.second.sTableName == i_sTableName )
567 setParameter(
i++,xParameter,(*_rOriginalRow)[keyColumnName.second.nPosition],keyColumnName.second.nType,keyColumnName.second.nScale);
570 if ( !_aIndexColumnPositions.empty() )
573 auto aIter = m_pColumnNames->begin();
574 for (
auto const& indexColumnPosition : _aIndexColumnPositions)
576 setParameter(
i++,xParameter,(*_rOriginalRow)[indexColumnPosition],(*_rOriginalRow)[indexColumnPosition].getTypeKind(),aIter->second.nScale);
580 const sal_Int32 nRowsUpdated = xPrep->executeUpdate();
581 m_bUpdated = nRowsUpdated > 0;
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);
595 Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
598 OUStringBuffer aSql(
"INSERT INTO " + m_aComposedTableName +
" ( ");
601 OUStringBuffer aValues(
" VALUES ( ");
602 OUString aQuote = getIdentifierQuoteString();
604 bool bRefetch =
true;
605 bool bModified =
false;
606 for (
auto const&
columnName : *m_pColumnNames)
608 if((*_rInsertRow)[
columnName.second.nPosition].isModified())
612 bRefetch = std::find(m_aFilterColumns.begin(),m_aFilterColumns.end(),
columnName.second.sRealName) == m_aFilterColumns.end();
614 aSql.append(::dbtools::quoteName( aQuote,
columnName.second.sRealName) +
",");
615 aValues.append(
"?,");
620 ::dbtools::throwSQLException(
DBA_RES( RID_STR_NO_VALUE_CHANGED ), StandardSQLState::GENERAL_ERROR,
m_xConnection );
622 aSql[aSql.getLength() - 1] =
')';
623 aValues[aValues.getLength() - 1] =
')';
624 aSql.append(aValues);
626 executeInsert(_rInsertRow,aSql.makeStringAndClear(),
u"",bRefetch);
629void OKeySet::executeInsert(
const ORowSetRow& _rInsertRow,
const OUString& i_sSQL,std::u16string_view i_sTableName,
bool bRefetch )
632 Reference< XPreparedStatement > xPrep(
m_xConnection->prepareStatement(i_sSQL));
633 Reference< XParameters > xParameter(xPrep,UNO_QUERY);
636 for (
auto const&
columnName : *m_pColumnNames)
638 if ( i_sTableName.empty() ||
columnName.second.sTableName == i_sTableName )
641 if((*_rInsertRow)[
nPos].isModified())
643 if((*_rInsertRow)[
nPos].isNull())
644 xParameter->setNull(
i++,(*_rInsertRow)[
nPos].getTypeKind());
647 impl_convertValue_throw(_rInsertRow,
columnName.second);
648 (*_rInsertRow)[
nPos].setSigned(m_aSignedFlags[
nPos-1]);
655 m_bInserted = xPrep->executeUpdate() > 0;
656 bool bAutoValuesFetched =
false;
660 for (
auto const&
columnName : *m_pColumnNames)
662 if ( !(*_rInsertRow)[
columnName.second.nPosition].isModified() )
667 (*_rInsertRow)[
columnName.second.nPosition].setNull();
678 Reference< XGeneratedResultSet > xGRes(xPrep, UNO_QUERY);
681 Reference< XResultSet > xRes = xGRes->getGeneratedValues();
682 Reference< XRow > xRow(xRes,UNO_QUERY);
683 if ( xRow.is() && xRes->next() )
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)
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);
696 bAutoValuesFetched =
true;
702 SAL_WARN(
"dbaccess",
"Could not execute GeneratedKeys() stmt");
706 ::comphelper::disposeComponent(xPrep);
708 if ( i_sTableName.empty() && !bAutoValuesFetched && m_bInserted )
711 const OUString
sQuote = getIdentifierQuoteString();
712 OUStringBuffer sMaxStmt;
713 auto aEnd = m_pKeyColumnNames->end();
714 for (
auto const& autoColumn : m_aAutoColumns)
717 SelectColumnsMetaData::const_iterator aFind = m_pKeyColumnNames->find(autoColumn);
720 sMaxStmt.append(
" MAX(" + ::dbtools::quoteName(
sQuote,aFind->second.sRealName) +
"),");
724 if(!sMaxStmt.isEmpty())
726 sMaxStmt[sMaxStmt.getLength()-1] =
' ';
727 OUString sStmt =
"SELECT " + sMaxStmt +
"FROM ";
729 ::dbtools::qualifiedNameComponents(
m_xConnection->getMetaData(),m_sUpdateTableName,
sCatalog,
sSchema,sTable,::dbtools::EComposeRule::InDataManipulation);
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())
740 for (
auto const& autoColumn : m_aAutoColumns)
743 SelectColumnsMetaData::const_iterator aFind = m_pKeyColumnNames->find(autoColumn);
745 (*_rInsertRow)[aFind->second.nPosition].fill(j++, aFind->second.nType, xRow);
748 ::comphelper::disposeComponent(xStatement);
752 SAL_WARN(
"dbaccess",
"Could not fetch with MAX() ");
758 OKeySetMatrix::const_iterator aKeyIter = m_aKeyMap.end();
761 copyRowValue(_rInsertRow,aKeyRow,aKeyIter->first + 1);
763 m_aKeyIter = m_aKeyMap.emplace( aKeyIter->first + 1,
OKeySetValue(aKeyRow,std::pair<sal_Int32,Reference<XRow> >(1,Reference<XRow>())) ).first;
765 (*_rInsertRow)[0] =
Any(
static_cast<sal_Int32
>(m_aKeyIter->first));
766 tryRefetch(_rInsertRow,bRefetch);
770void OKeySet::tryRefetch(
const ORowSetRow& _rInsertRow,
bool bRefetch)
776 bRefetch = doTryRefetch_throw();
785 m_aKeyIter->second.second.second =
new OPrivateRow(std::vector(*_rInsertRow));
794 OSL_ENSURE((m_aParameterValueForCache->size()-1) == m_pParameterNames->size(),
"OKeySet::copyRowValue: Parameter values and names differ!");
797 bool bChanged =
false;
799 for (
auto const& parameterName : *m_pParameterNames)
802 aValue.
setSigned(m_aSignedFlags[parameterName.second.nPosition-1]);
803 if ( (*_rInsertRow)[parameterName.second.nPosition] != aValue )
807 (*aCopy)[
i] = (*_rInsertRow)[parameterName.second.nPosition];
808 m_aUpdatedParameter[i_nBookmark] = aCopy;
816 m_aUpdatedParameter.erase(i_nBookmark);
820 for (
auto const& keyColumnName : *m_pKeyColumnNames)
822 impl_convertValue_throw(_rInsertRow,keyColumnName.second);
823 *aIter = (*_rInsertRow)[keyColumnName.second.nPosition];
824 aIter->setTypeKind(keyColumnName.second.nType);
831 Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
834 OUStringBuffer aSql(
"DELETE FROM " + m_aComposedTableName +
" WHERE ");
837 OUString aQuote = getIdentifierQuoteString();
838 static const char aAnd[] =
" AND ";
841 Reference<XIndexesSupplier> xIndexSup(_xTable,UNO_QUERY);
842 Reference<XIndexAccess> xIndexes;
843 if ( xIndexSup.is() )
844 xIndexes.set(xIndexSup->getIndexes(),UNO_QUERY);
847 std::vector< Reference<XNameAccess> > aAllIndexColumns;
848 lcl_fillIndexColumns(xIndexes,aAllIndexColumns);
850 OUStringBuffer sIndexCondition;
851 std::vector<sal_Int32> aIndexColumnPositions;
853 for (
auto const&
columnName : *m_pColumnNames)
855 if ( m_pKeyColumnNames->find(
columnName.first) != m_pKeyColumnNames->end() )
857 aSql.append(::dbtools::quoteName( aQuote,
columnName.second.sRealName));
858 if((*_rDeleteRow)[
columnName.second.nPosition].isNull())
860 SAL_WARN(
"dbaccess",
"can a primary key be null");
861 aSql.append(
" IS NULL");
869 for (
auto const& indexColumn : aAllIndexColumns)
873 sIndexCondition.append(::dbtools::quoteName( aQuote,
columnName.second.sRealName));
874 if((*_rDeleteRow)[
columnName.second.nPosition].isNull())
875 sIndexCondition.append(
" IS NULL");
878 sIndexCondition.append(
" = ?");
879 aIndexColumnPositions.push_back(
columnName.second.nPosition);
881 sIndexCondition.append(aAnd);
888 aSql.append(sIndexCondition);
889 aSql.setLength(aSql.getLength()-5);
892 Reference< XPreparedStatement > xPrep(
m_xConnection->prepareStatement(aSql.makeStringAndClear()));
893 Reference< XParameters > xParameter(xPrep,UNO_QUERY);
896 for (
auto const& keyColumnName : *m_pKeyColumnNames)
898 setParameter(
i++,xParameter,(*_rDeleteRow)[keyColumnName.second.nPosition],keyColumnName.second.nType,keyColumnName.second.nScale);
902 auto aIter = m_pColumnNames->begin();
903 for (
auto const& indexColumnPosition : aIndexColumnPositions)
905 setParameter(
i++,xParameter,(*_rDeleteRow)[indexColumnPosition],(*_rDeleteRow)[indexColumnPosition].getTypeKind(),aIter->second.nScale);
909 m_bDeleted = xPrep->executeUpdate() > 0;
913 sal_Int32 nBookmark = ::comphelper::getINT32((*_rDeleteRow)[0].getAny());
914 if(m_aKeyIter == m_aKeyMap.find(nBookmark) && m_aKeyIter != m_aKeyMap.end())
916 m_aKeyMap.erase(nBookmark);
923 m_bInserted = m_bUpdated = m_bDeleted =
false;
928 if(!m_bRowCountFinal && m_aKeyIter == m_aKeyMap.end())
934 OSL_ENSURE(!isAfterLast(),
"fetchRow succeeded, but isAfterLast()");
940 m_aKeyIter = m_aKeyMap.end();
941 OSL_ENSURE(isAfterLast(),
"fetchRow failed, but not end of data");
946 return !isAfterLast();
949bool OKeySet::isBeforeFirst( )
951 return m_aKeyIter == m_aKeyMap.begin();
954bool OKeySet::isAfterLast( )
956 return m_bRowCountFinal && m_aKeyIter == m_aKeyMap.end();
959void OKeySet::beforeFirst( )
961 m_bInserted = m_bUpdated = m_bDeleted =
false;
962 m_aKeyIter = m_aKeyMap.begin();
966void OKeySet::afterLast( )
968 m_bInserted = m_bUpdated = m_bDeleted =
false;
970 m_aKeyIter = m_aKeyMap.end();
976 m_bInserted = m_bUpdated = m_bDeleted =
false;
977 m_aKeyIter = m_aKeyMap.begin();
979 if(m_aKeyIter == m_aKeyMap.end())
983 m_aKeyIter = m_aKeyMap.end();
989 return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
994 m_bInserted = m_bUpdated = m_bDeleted =
false;
995 bool bFetchedRow = fillAllRows();
997 m_aKeyIter = m_aKeyMap.end();
1003 return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1006sal_Int32 OKeySet::getRow( )
1008 OSL_ENSURE(!isAfterLast(),
"getRow is not allowed when afterlast record!");
1009 OSL_ENSURE(!isBeforeFirst(),
"getRow is not allowed when beforefirst record!");
1011 return std::distance(m_aKeyMap.begin(),m_aKeyIter);
1014bool OKeySet::absolute( sal_Int32 row )
1016 m_bInserted = m_bUpdated = m_bDeleted =
false;
1017 OSL_ENSURE(row,
"absolute(0) isn't allowed!");
1018 bool bFetchedRow =
false;
1021 if(!m_bRowCountFinal)
1022 bFetchedRow = fillAllRows();
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);
1029 if(row >=
static_cast<sal_Int32
>(m_aKeyMap.size()))
1032 if(!m_bRowCountFinal)
1036 for(sal_Int32
i=m_aKeyMap.size()-1;
i < row && bNext;++
i)
1047 m_aKeyIter = m_aKeyMap.end();
1054 m_aKeyIter = m_aKeyMap.end();
1060 m_aKeyIter = std::next(m_aKeyMap.begin(), row);
1068 return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1071bool OKeySet::previous()
1073 m_bInserted = m_bUpdated = m_bDeleted =
false;
1074 if(m_aKeyIter != m_aKeyMap.begin())
1079 return m_aKeyIter != m_aKeyMap.begin();
1082bool OKeySet::doTryRefetch_throw()
1086 Reference< XParameters > xParameter(m_xStatement,UNO_QUERY);
1087 OSL_ENSURE(xParameter.is(),
"No Parameter interface!");
1088 xParameter->clearParameters();
1093 OUpdatedParameter::const_iterator aUpdateFind = m_aUpdatedParameter.find(m_aKeyIter->first);
1094 if ( aUpdateFind == m_aUpdatedParameter.end() )
1096 aParaIter = m_aParameterValueForCache->begin();
1097 aParaEnd = m_aParameterValueForCache->end();
1101 aParaIter = aUpdateFind->second->begin();
1102 aParaEnd = aUpdateFind->second->end();
1105 for(++aParaIter;aParaIter != aParaEnd;++aParaIter,++
nPos)
1107 ::dbtools::setObjectWithInfo( xParameter,
nPos, aParaIter->makeAny(), aParaIter->getTypeKind() );
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);
1117 m_xSet = m_xStatement->executeQuery();
1118 OSL_ENSURE(m_xSet.is(),
"No resultset from statement!");
1119 return m_xSet->next();
1122void OKeySet::refreshRow()
1126 if(isBeforeFirst() || isAfterLast())
1129 if ( m_aKeyIter->second.second.second.is() )
1131 m_xRow = m_aKeyIter->second.second.second;
1135 bool bOK = doTryRefetch_throw();
1139 OKeySetMatrix::const_iterator aTemp = m_aKeyIter;
1142 m_aKeyMap.erase(aTemp);
1145 if (m_rRowCount > 0)
1148 SAL_WARN(
"dbaccess",
"m_rRowCount got out of sync: non-empty m_aKeyMap, but m_rRowCount <= 0");
1150 if (m_aKeyIter == m_aKeyMap.end())
1152 ::comphelper::disposeComponent(m_xSet);
1160 m_aKeyIter = m_aKeyMap.end();
1161 OSL_ENSURE(isAfterLast(),
"fetchRow() failed but not isAfterLast()!");
1173 m_xRow.set(m_xSet,UNO_QUERY);
1174 OSL_ENSURE(
m_xRow.is(),
"No row from statement!");
1178bool OKeySet::fetchRow()
1182 if ( !m_bRowCountFinal && (!m_nMaxRows || sal_Int32(m_aKeyMap.size()) < m_nMaxRows) )
1183 bRet = m_xDriverSet->next();
1188 ::comphelper::disposeComponent(m_xSet);
1189 m_xRow.set(m_xDriverRow, UNO_SET_THROW);
1193 for (
auto const& keyColumnName : *m_pKeyColumnNames)
1200 for (
auto const& foreignColumnName : *m_pForeignColumnNames)
1206 m_aKeyIter = m_aKeyMap.emplace( m_aKeyMap.rbegin()->first+1,
OKeySetValue(aKeyRow,std::pair<sal_Int32,Reference<XRow> >(0,Reference<XRow>())) ).first;
1209 m_bRowCountFinal =
true;
1213bool OKeySet::fillAllRows()
1215 if(m_bRowCountFinal)
1233 OSL_ENSURE(
m_xRow.is(),
"m_xRow is null! I've thrown, but function execution continued?");
1234 return m_xRow->wasNull();
1237inline void OKeySet::ensureRowForData( )
1244 OSL_ENSURE(
m_xRow.is(),
"m_xRow is null! I've called throwSQLException but execution continued?");
1247OUString SAL_CALL OKeySet::getString( sal_Int32 columnIndex )
1250 return m_xRow->getString(columnIndex);
1253sal_Bool SAL_CALL OKeySet::getBoolean( sal_Int32 columnIndex )
1256 return m_xRow->getBoolean(columnIndex);
1259sal_Int8 SAL_CALL OKeySet::getByte( sal_Int32 columnIndex )
1262 return m_xRow->getByte(columnIndex);
1265sal_Int16 SAL_CALL OKeySet::getShort( sal_Int32 columnIndex )
1268 return m_xRow->getShort(columnIndex);
1271sal_Int32 SAL_CALL OKeySet::getInt( sal_Int32 columnIndex )
1274 return m_xRow->getInt(columnIndex);
1277sal_Int64 SAL_CALL OKeySet::getLong( sal_Int32 columnIndex )
1280 return m_xRow->getLong(columnIndex);
1283float SAL_CALL OKeySet::getFloat( sal_Int32 columnIndex )
1286 return m_xRow->getFloat(columnIndex);
1289double SAL_CALL OKeySet::getDouble( sal_Int32 columnIndex )
1292 return m_xRow->getDouble(columnIndex);
1295Sequence< sal_Int8 > SAL_CALL OKeySet::getBytes( sal_Int32 columnIndex )
1298 return m_xRow->getBytes(columnIndex);
1301css::util::Date SAL_CALL OKeySet::getDate( sal_Int32 columnIndex )
1304 return m_xRow->getDate(columnIndex);
1307css::util::Time SAL_CALL OKeySet::getTime( sal_Int32 columnIndex )
1310 return m_xRow->getTime(columnIndex);
1313css::util::DateTime SAL_CALL OKeySet::getTimestamp( sal_Int32 columnIndex )
1316 return m_xRow->getTimestamp(columnIndex);
1319Reference< css::io::XInputStream > SAL_CALL OKeySet::getBinaryStream( sal_Int32 columnIndex )
1322 return m_xRow->getBinaryStream(columnIndex);
1325Reference< css::io::XInputStream > SAL_CALL OKeySet::getCharacterStream( sal_Int32 columnIndex )
1328 return m_xRow->getCharacterStream(columnIndex);
1331Any SAL_CALL OKeySet::getObject( sal_Int32 columnIndex,
const Reference< css::container::XNameAccess >& typeMap )
1334 return m_xRow->getObject(columnIndex,typeMap);
1337Reference< XRef > SAL_CALL OKeySet::getRef( sal_Int32 columnIndex )
1340 return m_xRow->getRef(columnIndex);
1343Reference< XBlob > SAL_CALL OKeySet::getBlob( sal_Int32 columnIndex )
1346 return m_xRow->getBlob(columnIndex);
1349Reference< XClob > SAL_CALL OKeySet::getClob( sal_Int32 columnIndex )
1352 return m_xRow->getClob(columnIndex);
1355Reference< XArray > SAL_CALL OKeySet::getArray( sal_Int32 columnIndex )
1358 return m_xRow->getArray(columnIndex);
1361bool OKeySet::rowUpdated( )
1363 return m_aKeyIter != m_aKeyMap.begin() && m_aKeyIter != m_aKeyMap.end() && m_aKeyIter->second.second.first == 2;
1366bool OKeySet::rowInserted( )
1368 return m_aKeyIter != m_aKeyMap.begin() && m_aKeyIter != m_aKeyMap.end() && m_aKeyIter->second.second.first == 1;
1371bool OKeySet::rowDeleted( )
1373 bool bDeleted = m_bDeleted;
1382 const css::uno::Sequence< OUString >& _aColumnNames,
1383 std::u16string_view _rsUpdateTableName,
1385 bool i_bAppendTableName)
1388 Sequence< OUString> aSelNames(_rxQueryColumns->getElementNames());
1389 const OUString* pSelIter = aSelNames.getConstArray();
1390 const OUString* pSelEnd = pSelIter + aSelNames.getLength();
1392 const OUString* pTblColumnIter = _aColumnNames.getConstArray();
1393 const OUString* pTblColumnEnd = pTblColumnIter + _aColumnNames.getLength();
1397 for(sal_Int32
nPos = 1;pSelIter != pSelEnd;++pSelIter,++
nPos)
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!");
1406 for(;pTblColumnIter != pTblColumnEnd;++pTblColumnIter)
1408 if(bCase(sRealName,*pTblColumnIter) && bCase(_rsUpdateTableName,sTableName) && o_rColumnNames.find(*pTblColumnIter) == o_rColumnNames.end())
1410 sal_Int32
nType = 0;
1412 sal_Int32 nScale = 0;
1414 OUString sColumnDefault;
1418 sal_Int32
nNullable = ColumnValue::NULLABLE_UNKNOWN;
1423 if ( i_bAppendTableName )
1425 sName = sTableName +
"." + sRealName;
1433 o_rColumnNames[
sName] = aColDesc;
1438 pTblColumnIter = _aColumnNames.getConstArray();
1446 switch(i_aMetaData.
nType)
1448 case DataType::DECIMAL:
1449 case DataType::NUMERIC:
1452 sal_Int32
nIndex = sValue.indexOf(
'.');
1455 aValue = sValue.copy(0,std::min(sValue.getLength(),
nIndex + (i_aMetaData.
nScale > 0 ? i_aMetaData.
nScale + 1 : 0)));
const Reference< XRow > m_xRow
OUString getString() const
void setSigned(bool _bSig)
vStatements_t m_vStatements
css::uno::Reference< css::sdb::XSingleSelectQueryAnalyzer > m_xComposer
css::uno::Reference< css::sdbc::XResultSet > m_xSet
Reference< XSingleServiceFactory > xFactory
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
bool getBOOL(const Any &_rAny)
css::uno::Reference< css::sdbcx::XColumnsSupplier > OSQLTable
std::map< OUString, SelectColumnDescription, ::comphelper::UStringMixLess > SelectColumnsMetaData
std::pair< ORowSetRow, std::pair< sal_Int32, css::uno::Reference< css::sdbc::XRow > > > OKeySetValue
connectivity::ORowVector< connectivity::ORowSetValue > ORowSetValueVector
void getColumnPositions(const Reference< XNameAccess > &_rxQueryColumns, const css::uno::Sequence< OUString > &_aColumnNames, std::u16string_view _rsUpdateTableName, SelectColumnsMetaData &o_rColumnNames, bool i_bAppendTableName)
Reference< XConnection > m_xConnection
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
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")
OUString getComposedAndClear()
void append(const OUString &lhs)