/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
sal_Bool SAL_CALL OResultSet::last( )
{ // here I know definitely that I stand on the last record
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OResultSet_BASE::rBHelper.bDisposed); return m_pTable.is() && m_aSkipDeletedSet.skipDeleted(IResultSetHelper::LAST,1,true);
}
// we know that we append new rows at the end // so we have to know where the end is
(void)m_aSkipDeletedSet.skipDeleted(IResultSetHelper::LAST,1,false);
m_bRowInserted = m_pTable->InsertRow(*m_aInsertRow, m_xColsIdx); if(m_bRowInserted && m_pFileSet.is())
{
sal_Int32 nPos = (*m_aInsertRow)[0]->getValue().getInt32();
m_pFileSet->push_back(nPos);
*(*m_aInsertRow)[0] = sal_Int32(m_pFileSet->size());
clearInsertRow();
// For further Fetch-Operations this information may possibly be changed ...
IResultSetHelper::Movement eCursorPosition = eFirstCursorPosition;
sal_Int32 nOffset = nFirstOffset;
// protect from reading over the end when somebody is inserting while we are reading // this method works only for dBase at the moment!!! if (eCursorPosition == IResultSetHelper::NEXT && m_nFilePos == m_nLastVisitedPos)
{ returnfalse;
}
if (!m_pTable.is() || !m_pTable->seekRow(eCursorPosition, nOffset, m_nFilePos))
{ returnfalse;
}
if (!bEvaluate) // If no evaluation runs, then just fill the results-row
{
m_pTable->fetchRow(m_aRow,rTableCols, bRetrieveData);
} else
{
m_pTable->fetchRow(m_aEvaluateRow, rTableCols, bRetrieveData || bHasRestriction);
// Evaluate may only be set, // if the Keyset will be constructed further if ( ( m_aSQLIterator.getStatementType() == OSQLStatementType::Select )
&& !isCount()
&& bEvaluate
)
{ if (m_pSortIndex)
{
std::unique_ptr<OKeyValue> pKeyValue = GetOrderbyKeyValue( m_aSelectRow );
m_pSortIndex->AddKeyValue(std::move(pKeyValue));
} elseif (m_pFileSet.is())
{
sal_uInt32 nBookmarkValue = std::abs((*m_aEvaluateRow)[0]->getValue().getInt32());
m_pFileSet->push_back(nBookmarkValue);
}
} elseif (m_aSQLIterator.getStatementType() == OSQLStatementType::Update)
{ bool bOK = true; if (bEvaluate)
{ // read the actual result-row
bOK = m_pTable->fetchRow(m_aEvaluateRow, *(m_pTable->getTableColumns()), true);
}
if (bOK)
{ // just give the values to be changed: if(!m_pTable->UpdateRow(*m_aAssignValues,m_aEvaluateRow, m_xColsIdx)) returnfalse;
}
} elseif (m_aSQLIterator.getStatementType() == OSQLStatementType::Delete)
{ bool bOK = true; if (bEvaluate)
{
bOK = m_pTable->fetchRow(m_aEvaluateRow, *(m_pTable->getTableColumns()), true);
} if (bOK)
{ if(!m_pTable->DeleteRow(*m_xColumns)) returnfalse;
}
} returntrue;
}
if (m_aSQLIterator.getStatementType() == OSQLStatementType::Select &&
!isCount())
{ if (!m_pFileSet.is()) //no Index available
{ // Normal FETCH
ExecuteRow(eCursorPosition,nOffset,false,bRetrieveData);
// now set the bookmark for outside this is the logical pos and not the file pos
*(*m_aRow->begin()) = sal_Int32(m_nRowPos + 1);
} else
{ switch(eCursorPosition)
{ case IResultSetHelper::NEXT:
++m_nRowPos; break; case IResultSetHelper::PRIOR: if (m_nRowPos >= 0)
--m_nRowPos; break; case IResultSetHelper::FIRST:
m_nRowPos = 0; break; case IResultSetHelper::LAST:
m_nRowPos = m_pFileSet->size() - 1; break; case IResultSetHelper::RELATIVE1:
m_nRowPos += nOffset; break; case IResultSetHelper::ABSOLUTE1: case IResultSetHelper::BOOKMARK: if ( m_nRowPos == (nOffset -1) ) returntrue;
m_nRowPos = nOffset -1; break;
}
// OffRange? // The FileCursor is outside of the valid range, if: // a.) m_nRowPos < 1 // b.) a KeySet exists and m_nRowPos > m_pFileSet->size() if (m_nRowPos < 0 || (m_pFileSet->isFrozen() && eCursorPosition != IResultSetHelper::BOOKMARK && o3tl::make_unsigned(m_nRowPos) >= m_pFileSet->size() )) // && m_pFileSet->IsFrozen()
{ goto Error;
} else
{ if (m_nRowPos < static_cast<sal_Int32>(m_pFileSet->size()))
{ // Fetch via Index bool bOK = ExecuteRow(IResultSetHelper::BOOKMARK,(*m_pFileSet)[m_nRowPos],false,bRetrieveData); if (!bOK) goto Error;
// now set the bookmark for outside
*(*m_aRow->begin()) = sal_Int32(m_nRowPos + 1); if ( (bRetrieveData || m_pSQLAnalyzer->hasRestriction()) && m_pSQLAnalyzer->hasFunctions() )
{
m_pSQLAnalyzer->setSelectionEvaluationResult(m_aSelectRow,m_aColMapping);
}
} else// Index must be further constructed
{ // set first on the last known row if (m_pFileSet->empty())
{
m_pTable->seekRow(IResultSetHelper::ABSOLUTE1, 0, m_nFilePos);
} else
{
m_aFileSetIter = m_pFileSet->end()-1;
m_pTable->seekRow(IResultSetHelper::BOOKMARK, *m_aFileSetIter, m_nFilePos);
} bool bOK = true; // Determine the number of further Fetches while (bOK && m_nRowPos >= static_cast<sal_Int32>(m_pFileSet->size()))
{
bOK = ExecuteRow(IResultSetHelper::NEXT,1,true, false);//bRetrieveData);
}
if (bOK)
{ // read the results again
m_pTable->fetchRow(m_aRow, *(m_pTable->getTableColumns()), bRetrieveData);
// now set the bookmark for outside
*(*m_aRow->begin()) = sal_Int32(m_nRowPos + 1);
if ( (bRetrieveData || m_pSQLAnalyzer->hasRestriction()) && m_pSQLAnalyzer->hasFunctions() )
{
m_pSQLAnalyzer->setSelectionEvaluationResult(m_aSelectRow,m_aColMapping);
}
} elseif (!m_pFileSet->isFrozen()) // no valid record found
{
m_pFileSet->setFrozen(); goto Error;
}
}
}
}
} elseif (m_aSQLIterator.getStatementType() == OSQLStatementType::Select && isCount())
{ // Fetch the COUNT(*) switch (eCursorPosition)
{ case IResultSetHelper::NEXT:
++m_nRowPos; break; case IResultSetHelper::PRIOR:
--m_nRowPos; break; case IResultSetHelper::FIRST: case IResultSetHelper::LAST:
m_nRowPos = 0; break; case IResultSetHelper::RELATIVE1:
m_nRowPos += nOffset; break; case IResultSetHelper::ABSOLUTE1: case IResultSetHelper::BOOKMARK:
m_nRowPos = nOffset - 1; break;
}
if ( m_nRowPos < 0 ) goto Error; elseif (m_nRowPos == 0)
{ // put COUNT(*) in result-row // (must be the first and only variable in the row) if (m_aRow->size() >= 2)
{
*(*m_aRow)[1] = m_nRowCountResult;
*(*m_aRow)[0] = sal_Int32(1);
(*m_aRow)[1]->setBound(true);
(*m_aSelectRow)[1] = (*m_aRow)[1];
}
} else
{
m_nRowPos = 1; returnfalse;
}
} else // Fetch only possible at SELECT! returnfalse;
returntrue;
Error: // is the Cursor positioned before the first row // then the position will be maintained if (nTempPos == -1)
m_nRowPos = nTempPos; else
{ switch(eCursorPosition)
{ case IResultSetHelper::PRIOR: case IResultSetHelper::FIRST:
m_nRowPos = -1; break; case IResultSetHelper::LAST: case IResultSetHelper::NEXT: case IResultSetHelper::ABSOLUTE1: case IResultSetHelper::RELATIVE1: if (nOffset > 0)
m_nRowPos = m_pFileSet.is() ? static_cast<sal_Int32>(m_pFileSet->size()) : -1; elseif (nOffset < 0)
m_nRowPos = -1; break; case IResultSetHelper::BOOKMARK:
m_nRowPos = nTempPos; // last Position
}
} returnfalse;
}
void OResultSet::sortRows()
{ if (!m_pSQLAnalyzer->hasRestriction() && m_aOrderbyColumnNumber.size() == 1)
{ // is just one field given for sorting // and this field is indexed, then the Index will be used
Reference<XIndexesSupplier> xIndexSup;
m_pTable->queryInterface(cppu::UnoType<XIndexesSupplier>::get()) >>= xIndexSup;
Reference<XIndexAccess> xIndexes; if(xIndexSup.is())
{
xIndexes.set(xIndexSup->getIndexes(),UNO_QUERY);
Reference<XPropertySet> xColProp; if(m_aOrderbyColumnNumber[0] < xIndexes->getCount())
{
xColProp.set(xIndexes->getByIndex(m_aOrderbyColumnNumber[0]),UNO_QUERY); // iterate through the indexes to find the matching column const sal_Int32 nCount = xIndexes->getCount(); for(sal_Int32 i=0; i < nCount;++i)
{
Reference<XColumnsSupplier> xIndex(xIndexes->getByIndex(i),UNO_QUERY);
Reference<XNameAccess> xIndexCols = xIndex->getColumns(); if(xIndexCols->hasByName(comphelper::getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)))))
{
m_pFileSet = new OKeySet();
if(fillIndexValues(xIndex)) return;
}
}
}
}
}
OSortIndex::TKeyTypeVector eKeyType(m_aOrderbyColumnNumber.size());
size_t i = 0; for (autoconst& elem : m_aOrderbyColumnNumber)
{
OSL_ENSURE(static_cast<sal_Int32>(m_aSelectRow->size()) > elem,"Invalid Index"); switch ((*(m_aSelectRow->begin()+elem))->getValue().getTypeKind())
{ case DataType::CHAR: case DataType::VARCHAR: case DataType::LONGVARCHAR:
eKeyType[i] = OKeyType::String; break;
case DataType::OTHER: case DataType::TINYINT: case DataType::SMALLINT: case DataType::INTEGER: case DataType::DECIMAL: case DataType::NUMERIC: case DataType::REAL: case DataType::DOUBLE: case DataType::DATE: case DataType::TIME: case DataType::TIMESTAMP: case DataType::BIT:
eKeyType[i] = OKeyType::Double; break;
// Other types aren't implemented (so they are always FALSE) default:
eKeyType[i] = OKeyType::NONE;
SAL_WARN( "connectivity.drivers","OFILECursor::Execute: Data type not implemented"); break;
}
(*m_aSelectRow)[elem]->setBound(true);
++i;
}
// create sorted Keyset
m_pFileSet = nullptr;
m_pFileSet = m_pSortIndex->CreateKeySet();
m_pSortIndex.reset(); // now access to a sorted set is possible via Index
}
void OResultSet::OpenImpl()
{
OSL_ENSURE(m_pSQLAnalyzer,"No analyzer set with setSqlAnalyzer!"); if(!m_pTable.is())
{ const OSQLTables& rTabs = m_aSQLIterator.getTables(); if (rTabs.empty() || !rTabs.begin()->second.is())
lcl_throwError(STR_QUERY_TOO_COMPLEX,*this);
if ( rTabs.size() > 1 || m_aSQLIterator.hasErrors() )
lcl_throwError(STR_QUERY_MORE_TABLES,*this);
m_nRowCountResult = 0; // for now simply iterate over all rows and // do all actions (or just count)
{ bool bOK = true; while (bOK)
{
bOK = ExecuteRow(IResultSetHelper::NEXT);
if (bOK)
{
m_nRowCountResult++;
}
}
// save result of COUNT(*) in m_nRowCountResult. // nRowCount (number of Rows in the result) = 1 for this request!
}
} else
{ bool bDistinct = false;
assert(m_pParseTree != nullptr);
OSQLParseNode *pDistinct = m_pParseTree->getChild(1);
assert(m_aOrderbyColumnNumber.size() ==
m_aOrderbyAscending.size()); if (pDistinct && pDistinct->getTokenID() == SQL_TOKEN_DISTINCT )
{ // To eliminate duplicates we need to sort on all columns. // This is not a problem because the SQL spec says that the // order of columns that are not specified in ORDER BY // clause is undefined, so it doesn't hurt to sort on // these; pad the vectors to include them. for (size_t i = 1; // 0: bookmark (see setBoundedColumns)
i < m_aColMapping.size(); ++i)
{ if (std::find(m_aOrderbyColumnNumber.begin(),
m_aOrderbyColumnNumber.end(),
sal::static_int_cast<sal_Int32>(i))
== m_aOrderbyColumnNumber.end())
{
m_aOrderbyColumnNumber.push_back(i); // ASC or DESC doesn't matter
m_aOrderbyAscending.push_back(TAscendingOrder::ASC);
}
}
bDistinct = true;
}
if (IsSorted())
sortRows();
if (!m_pFileSet.is())
{
m_pFileSet = new OKeySet();
if (!m_pSQLAnalyzer->hasRestriction()) // now the Keyset can be filled! // But be careful: It is assumed, that the FilePositions will be stored as sequence 1..n
{ if ( m_nLastVisitedPos > 0)
m_pFileSet->reserve( m_nLastVisitedPos ); for (sal_Int32 i = 0; i < m_nLastVisitedPos; i++)
m_pFileSet->push_back(i + 1);
}
}
OSL_ENSURE(m_pFileSet.is(),"No KeySet existing! :-(");
if(bDistinct && m_pFileSet.is())
{
OValueRow aSearchRow = new OValueVector(m_aRow->size());
OValueRefVector::iterator aRowIter = m_aRow->begin();
OValueVector::iterator aSearchIter = aSearchRow->begin(); for ( ++aRowIter,++aSearchIter; // the first column is the bookmark column
aRowIter != m_aRow->end();
++aRowIter,++aSearchIter)
aSearchIter->setBound((*aRowIter)->isBound());
for( size_t j = nMaxRow-1; j > 0; --j)
{
nPos = (*m_pFileSet)[j];
ExecuteRow(IResultSetHelper::BOOKMARK,nPos,false);
m_pSQLAnalyzer->setSelectionEvaluationResult(m_aSelectRow,m_aColMapping);
{ // cop*y row values
OValueRefVector::iterator copyFrom = m_aSelectRow->begin();
OValueVector::iterator copyTo = aSearchRow->begin(); for ( ++copyFrom,++copyTo; // the first column is the bookmark column
copyFrom != m_aSelectRow->end();
++copyFrom,++copyTo)
*copyTo = *(*copyFrom);
}
// compare with next row
nKey = (*m_pFileSet)[j-1];
ExecuteRow(IResultSetHelper::BOOKMARK,nKey,false);
m_pSQLAnalyzer->setSelectionEvaluationResult(m_aSelectRow,m_aColMapping); auto rowsMismatchIters = std::mismatch(std::next(m_aSelectRow->begin()), m_aSelectRow->end(),
std::next(aSearchRow->begin()), // the first column is the bookmark column
[](const OValueRefVector::value_type& a, const OValueVector::value_type& b) { return !a->isBound() || (*a == b); });
if(rowsMismatchIters.first == m_aSelectRow->end())
(*m_pFileSet)[j] = 0; // Rows match -- Mark for deletion by setting key to 0 #if OSL_DEBUG_LEVEL > 1 else
nFound++; #endif
}
std::erase(*m_pFileSet, 0);
}
}
}
} break;
case OSQLStatementType::Update: case OSQLStatementType::Delete: // during processing count the number of processed Rows
m_nRowCountResult = 0; // for now simply iterate over all rows and // run the actions (or simply count):
{
bool bOK = true; while (bOK)
{
bOK = ExecuteRow(IResultSetHelper::NEXT);
if (bOK)
{
m_nRowCountResult++;
}
}
// save result of COUNT(*) in nRowCountResult. // nRowCount (number of rows in the result-set) = 1 for this request!
} break; case OSQLStatementType::Insert:
m_nRowCountResult = 0;
std::map<OSQLColumns::iterator,bool> aSelectIters;
OValueRefVector::const_iterator aRowIter = _rRow->begin()+1; for (sal_Int32 i=0; // the first column is the bookmark column
aRowIter != _rRow->end();
++i, ++aRowIter
)
{
(*aRowIter)->setBound(false); try
{ // get the table column and its name
_xNames->getByIndex(i) >>= xTableColumn;
OSL_ENSURE(xTableColumn.is(), "OResultSet::setBoundedColumns: invalid table column!"); if (xTableColumn.is())
xTableColumn->getPropertyValue(sName) >>= sTableColumnName; else
sTableColumnName.clear();
// look if we have such a select column // TODO: would like to have a O(log n) search here ... for ( OSQLColumns::iterator aIter = _rxColumns->begin();
aIter != _rxColumns->end();
++aIter
)
{ if((*aIter)->getPropertySetInfo()->hasPropertyByName(sRealName))
(*aIter)->getPropertyValue(sRealName) >>= sSelectColumnRealName; else
(*aIter)->getPropertyValue(sName) >>= sSelectColumnRealName;
if ( aCase(sTableColumnName, sSelectColumnRealName) && !(*aRowIter)->isBound() && aSelectIters.end() == aSelectIters.find(aIter) )
{
aSelectIters.emplace(aIter,true); if(_bSetColumnMapping)
{
sal_Int32 nSelectColumnPos = aIter - _rxColumns->begin() + 1; // the getXXX methods are 1-based ...
sal_Int32 nTableColumnPos = i + 1; // get first table column is the bookmark column ...
_rColMapping[nSelectColumnPos] = nTableColumnPos;
(*_rSelectRow)[nSelectColumnPos] = *aRowIter;
}
break;
}
}
} catch (Exception&)
{
TOOLS_WARN_EXCEPTION( "connectivity.drivers","");
}
} // in this case we got more select columns as columns exist in the table if ( !(_bSetColumnMapping && aSelectIters.size() != _rColMapping.size()) ) return;
void OResultSet::clearInsertRow()
{
m_aRow->setDeleted(false); // set to false here because this is the new row
sal_Int32 nPos = 0; for(ORowSetValueDecoratorRef& rValue : *m_aInsertRow)
{ if ( rValue->isBound() )
{
(*m_aRow)[nPos]->setValue( rValue->getValue() );
}
rValue->setBound(nPos == 0);
rValue->setModified(false);
rValue->setNull();
++nPos;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.