We do a "SELECT * FROM table" just to fetch the primary key columns; so reuse the same XResultSet to fetch all columns. Else, we immediately issue a "SELECT * FROM table WHERE primary_key=current_value" to read the other columns, which is wasteful and particularly silly. Commit1ae17f5b03
already tried to do that, but was essentially reverted piecewise because it caused fdo#47520, fdo#48345, fdo#50372. Commit c08067d6da94743d53217cbc26cffae00a22dc3a thought it did that, but actually reverted commit1ae17f5b03
. This implementation fetches the whole current row and caches it in memory; only one row is cached: when the current row changes, the cache contains the new current row. This could be problematic (wrt to memory consumption) if the current row is big (e.g. with BLOBs) and nobody is interested in the data anyway (as would often be the case with BLOBs). Note that because of our "SELECT *", the driver most probably has it in memory already anyway, so we don't make the situation that much worse. This could be incrementally improved with a heuristic of not preemptively caching binary data (and also not LONGVARCHAR / TEXT / MEMO / ...); a getFOO on these columns would issue a specific "SELECT column FROM table WHERE primary_key=current_value" each time. The *real* complete fix to all these issues would be to not do "SELECT *" at all. Use "SELECT pkey_col1, pkey_col2, ..." when we are only interested in the key columns. As to data, somehow figure out which columns were ar interested in and "SELECT" only these (and maybe only those with "small datatype"?). Interesting columns could be determined by our caller (creator) as an argument to our constructor, or some heuristic (no binary data, no "big" unbound data). Also be extra smart and use *(m_aKeyIter) when getFOO is called on a column included in it (and don't include it in any subsequent SELECT). However, there are several pitfalls. One is buggy drivers that give use column names of columns that we cannot fetch :-| Using "SELECT *" works around that because the driver there *obviously* gives us only fetchable columns in the result. Another one is the very restrictive nature of some database access technologies. Take for example ODBC: - Data can be fetched only *once* (with the SQLGetData interface; bound columns offer a way around that, but that's viable only for constant-length data, not variable-length data). This could be addressed by an intelligent & lazy cache. - Data must be fetched in increasing order of column number (again, this is about SQLGetData). This is a harder issue. The current solution has the nice advantage of completely isolating the rest of LibO from these restrictions. I don't currently see how to cleanly avoid (potentially unnecessarily) caching column 4 if we are asked for column 3 then column 5, just in case we are asked for column 4 later on, unless we issue a specific "SELECT column4" later. But the latter would be quite expensive in terms of app-to-database roudtripe times :-( and thus creates another performance issue. Change-Id: I999b3f8f0b8a215acb390ffefc839235346e8353
246 lines
18 KiB
C++
246 lines
18 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
/*************************************************************************
|
|
*
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
*
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
|
*
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
|
*
|
|
* This file is part of OpenOffice.org.
|
|
*
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
* only, as published by the Free Software Foundation.
|
|
*
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License version 3 for more details
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
* <http://www.openoffice.org/license.html>
|
|
* for a copy of the LGPLv3 License.
|
|
*
|
|
************************************************************************/
|
|
|
|
#ifndef DBACCESS_CORE_API_KEYSET_HXX
|
|
#define DBACCESS_CORE_API_KEYSET_HXX
|
|
|
|
#include "CacheSet.hxx"
|
|
|
|
#include <cppuhelper/implbase1.hxx>
|
|
#include <memory>
|
|
#include <map>
|
|
|
|
#include <com/sun/star/lang/XUnoTunnel.hpp>
|
|
#include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
|
|
#include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
|
|
#include <comphelper/stl_types.hxx>
|
|
|
|
namespace dbaccess
|
|
{
|
|
struct SelectColumnDescription
|
|
{
|
|
::rtl::OUString sRealName; // may be empty
|
|
::rtl::OUString sTableName; // may be empty
|
|
::rtl::OUString sDefaultValue;
|
|
sal_Int32 nPosition;
|
|
sal_Int32 nType;
|
|
sal_Int32 nScale;
|
|
sal_Bool bNullable;
|
|
|
|
SelectColumnDescription()
|
|
:nPosition( 0 )
|
|
,nType( 0 )
|
|
,nScale( 0 )
|
|
,bNullable(sal_False)
|
|
{
|
|
}
|
|
|
|
SelectColumnDescription( sal_Int32 _nPosition, sal_Int32 _nType, sal_Int32 _nScale,sal_Bool _bNullable, const ::rtl::OUString& _rDefaultValue )
|
|
:sDefaultValue( _rDefaultValue )
|
|
,nPosition( _nPosition )
|
|
,nType( _nType )
|
|
,nScale( _nScale )
|
|
,bNullable(_bNullable)
|
|
{
|
|
}
|
|
};
|
|
typedef ::std::map< ::rtl::OUString, SelectColumnDescription, ::comphelper::UStringMixLess > SelectColumnsMetaData;
|
|
|
|
// the elements of _rxQueryColumns must have the properties PROPERTY_REALNAME and PROPERTY_TABLENAME
|
|
void getColumnPositions(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& _rxQueryColumns,
|
|
const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rColumnNames,
|
|
const ::rtl::OUString& _rsUpdateTableName,
|
|
SelectColumnsMetaData& o_rColumnNames /* out */,
|
|
bool i_bAppendTableName = false);
|
|
|
|
typedef ::std::pair<ORowSetRow,::std::pair<sal_Int32,::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow> > > OKeySetValue;
|
|
typedef ::std::map<sal_Int32,OKeySetValue > OKeySetMatrix;
|
|
typedef ::std::map<sal_Int32,ORowSetValueVector > OUpdatedParameter;
|
|
// is used when the source supports keys
|
|
class OKeySet : public OCacheSet
|
|
{
|
|
protected:
|
|
OKeySetMatrix m_aKeyMap;
|
|
OKeySetMatrix::iterator m_aKeyIter;
|
|
|
|
::std::vector< ::rtl::OUString > m_aAutoColumns; // contains all columns which are autoincrement ones
|
|
|
|
OUpdatedParameter m_aUpdatedParameter; // contains all parameter which have been updated and are needed for refetching
|
|
ORowSetValueVector m_aParameterValueForCache;
|
|
SAL_WNODEPRECATED_DECLARATIONS_PUSH
|
|
::std::auto_ptr<SelectColumnsMetaData> m_pKeyColumnNames; // contains all key column names
|
|
::std::auto_ptr<SelectColumnsMetaData> m_pColumnNames; // contains all column names
|
|
::std::auto_ptr<SelectColumnsMetaData> m_pParameterNames; // contains all parameter names
|
|
::std::auto_ptr<SelectColumnsMetaData> m_pForeignColumnNames; // contains all column names of the rest
|
|
SAL_WNODEPRECATED_DECLARATIONS_POP
|
|
connectivity::OSQLTable m_xTable; // reference to our table
|
|
::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess> m_xTableKeys;
|
|
::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement> m_xStatement;
|
|
::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet> m_xSet;
|
|
::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow> m_xRow;
|
|
::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer > m_xComposer;
|
|
::rtl::OUString m_sUpdateTableName;
|
|
::std::vector< ::rtl::OUString > m_aFilterColumns;
|
|
sal_Int32& m_rRowCount;
|
|
|
|
sal_Bool m_bRowCountFinal;
|
|
|
|
/**
|
|
getComposedTableName return the composed table name for the query
|
|
@param _sCatalog the catalogname may be empty
|
|
@param _sSchema the schemaname may be empty
|
|
@param _sTable the tablename
|
|
|
|
@return the composed name
|
|
*/
|
|
::rtl::OUString getComposedTableName( const ::rtl::OUString& _sCatalog,
|
|
const ::rtl::OUString& _sSchema,
|
|
const ::rtl::OUString& _sTable);
|
|
|
|
/** copies the values from the insert row into the key row
|
|
*
|
|
* \param _rInsertRow the row which was inserted
|
|
* \param _rKeyRow The current key row of the row set.
|
|
+ \param i_nBookmark The bookmark is used to update the parameter
|
|
*/
|
|
void copyRowValue(const ORowSetRow& _rInsertRow,ORowSetRow& _rKeyRow,sal_Int32 i_nBookmark);
|
|
|
|
::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > getKeyColumns() const;
|
|
// returns true if it did any work
|
|
bool fillAllRows();
|
|
sal_Bool fetchRow();
|
|
void invalidateRow();
|
|
|
|
void impl_convertValue_throw(const ORowSetRow& _rInsertRow,const SelectColumnDescription& i_aMetaData);
|
|
void initColumns();
|
|
SAL_WNODEPRECATED_DECLARATIONS_PUSH
|
|
void findTableColumnsMatching_throw( const ::com::sun::star::uno::Any& i_aTable,
|
|
const ::rtl::OUString& i_rUpdateTableName,
|
|
const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData>& i_xMeta,
|
|
const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>& i_xQueryColumns,
|
|
::std::auto_ptr<SelectColumnsMetaData>& o_pKeyColumnNames);
|
|
SAL_WNODEPRECATED_DECLARATIONS_POP
|
|
void setOneKeyColumnParameter( sal_Int32 &nPos,
|
|
const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XParameters > &_xParameter,
|
|
const connectivity::ORowSetValue &_rValue,
|
|
sal_Int32 _nType,
|
|
sal_Int32 _nScale ) const;
|
|
::rtl::OUStringBuffer createKeyFilter();
|
|
bool doTryRefetch_throw() throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);;
|
|
void tryRefetch(const ORowSetRow& _rInsertRow,bool bRefetch);
|
|
void executeUpdate(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOrginalRow,const ::rtl::OUString& i_sSQL,const ::rtl::OUString& i_sTableName,const ::std::vector<sal_Int32>& _aIndexColumnPositions = ::std::vector<sal_Int32>());
|
|
void executeInsert( const ORowSetRow& _rInsertRow,const ::rtl::OUString& i_sSQL,const ::rtl::OUString& i_sTableName = ::rtl::OUString(),bool bRefetch = false);
|
|
void executeStatement(::rtl::OUStringBuffer& io_aFilter,const ::rtl::OUString& i_sRowSetFilter,::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryComposer>& io_xAnalyzer);
|
|
|
|
virtual ~OKeySet();
|
|
public:
|
|
OKeySet(const connectivity::OSQLTable& _xTable,
|
|
const ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>& _xTableKeys,
|
|
const ::rtl::OUString& _rUpdateTableName,
|
|
const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XSingleSelectQueryAnalyzer >& _xComposer,
|
|
const ORowSetValueVector& _aParameterValueForCache,
|
|
sal_Int32 i_nMaxRows,
|
|
sal_Int32& o_nRowCount);
|
|
|
|
// late ctor which can throw exceptions
|
|
virtual void construct(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet>& _xDriverSet,const ::rtl::OUString& i_sRowSetFilter);
|
|
|
|
// ::com::sun::star::sdbc::XRow
|
|
virtual sal_Bool SAL_CALL wasNull( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::rtl::OUString SAL_CALL getString( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL getBoolean( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Int8 SAL_CALL getByte( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Int16 SAL_CALL getShort( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Int32 SAL_CALL getInt( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Int64 SAL_CALL getLong( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual float SAL_CALL getFloat( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual double SAL_CALL getDouble( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getBytes( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::util::Date SAL_CALL getDate( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::util::Time SAL_CALL getTime( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::util::DateTime SAL_CALL getTimestamp( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getBinaryStream( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getCharacterStream( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Any SAL_CALL getObject( sal_Int32 columnIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& typeMap ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRef > SAL_CALL getRef( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XBlob > SAL_CALL getBlob( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XClob > SAL_CALL getClob( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XArray > SAL_CALL getArray( sal_Int32 columnIndex ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
|
|
virtual sal_Bool SAL_CALL rowUpdated( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL rowInserted( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL rowDeleted( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
// ::com::sun::star::sdbc::XResultSet
|
|
virtual sal_Bool SAL_CALL next( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL isBeforeFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL isAfterLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL isFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL isLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL beforeFirst( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL afterLast( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL first( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL last( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Int32 SAL_CALL getRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL absolute( sal_Int32 row ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL relative( sal_Int32 rows ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual sal_Bool SAL_CALL previous( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL refreshRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
// ::com::sun::star::sdbcx::XRowLocate
|
|
virtual ::com::sun::star::uno::Any SAL_CALL getBookmark() throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
virtual sal_Bool SAL_CALL moveToBookmark( const ::com::sun::star::uno::Any& bookmark ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
virtual sal_Bool SAL_CALL moveRelativeToBookmark( const ::com::sun::star::uno::Any& bookmark, sal_Int32 rows ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
virtual sal_Int32 SAL_CALL compareBookmarks( const ::com::sun::star::uno::Any& first, const ::com::sun::star::uno::Any& second ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
virtual sal_Bool SAL_CALL hasOrderedBookmarks( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
virtual sal_Int32 SAL_CALL hashBookmark( const ::com::sun::star::uno::Any& bookmark ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
// ::com::sun::star::sdbcx::XDeleteRows
|
|
virtual ::com::sun::star::uno::Sequence< sal_Int32 > SAL_CALL deleteRows( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& rows ,const connectivity::OSQLTable& _xTable) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
// ::com::sun::star::sdbc::XResultSetUpdate
|
|
virtual void SAL_CALL updateRow(const ORowSetRow& _rInsertRow,const ORowSetRow& _rOrginalRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL deleteRow(const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL cancelRowUpdates( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL moveToInsertRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
virtual void SAL_CALL moveToCurrentRow( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
|
|
|
|
|
|
virtual sal_Bool previous_checked( sal_Bool i_bFetchRow );
|
|
virtual sal_Bool absolute_checked( sal_Int32 row,sal_Bool i_bFetchRow );
|
|
virtual sal_Bool last_checked( sal_Bool i_bFetchRow);
|
|
};
|
|
}
|
|
#endif // DBACCESS_CORE_API_KEYSET_HXX
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|