1*c82f2877SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*c82f2877SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*c82f2877SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*c82f2877SAndrew Rist * distributed with this work for additional information
6*c82f2877SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*c82f2877SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*c82f2877SAndrew Rist * "License"); you may not use this file except in compliance
9*c82f2877SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*c82f2877SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*c82f2877SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*c82f2877SAndrew Rist * software distributed under the License is distributed on an
15*c82f2877SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c82f2877SAndrew Rist * KIND, either express or implied. See the License for the
17*c82f2877SAndrew Rist * specific language governing permissions and limitations
18*c82f2877SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*c82f2877SAndrew Rist *************************************************************/
21*c82f2877SAndrew Rist
22*c82f2877SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #ifndef DBACCESS_CORE_API_KEYSET_HXX
28cdf0e10cSrcweir #include "KeySet.hxx"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #ifndef _DBA_CORE_RESOURCE_HXX_
31cdf0e10cSrcweir #include "core_resource.hxx"
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #ifndef _DBA_CORE_RESOURCE_HRC_
34cdf0e10cSrcweir #include "core_resource.hrc"
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
37cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
40cdf0e10cSrcweir #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
43cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XPREPAREDSTATEMENT_HPP_
44cdf0e10cSrcweir #include <com/sun/star/sdbc/XPreparedStatement.hpp>
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCxParameterS_HPP_
47cdf0e10cSrcweir #include <com/sun/star/sdbc/XParameters.hpp>
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XGENERATEDRESULTSET_HPP_
50cdf0e10cSrcweir #include <com/sun/star/sdbc/XGeneratedResultSet.hpp>
51cdf0e10cSrcweir #endif
52cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_XCOLUMNLOCATE_HPP_
53cdf0e10cSrcweir #include <com/sun/star/sdbc/XColumnLocate.hpp>
54cdf0e10cSrcweir #endif
55cdf0e10cSrcweir #ifndef _COM_SUN_STAR_CONTAINER_XINDEXACCESS_HPP_
56cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
57cdf0e10cSrcweir #endif
58cdf0e10cSrcweir #ifndef DBACCESS_SHARED_DBASTRINGS_HRC
59cdf0e10cSrcweir #include "dbastrings.hrc"
60cdf0e10cSrcweir #endif
61cdf0e10cSrcweir #ifndef _DBASHARED_APITOOLS_HXX_
62cdf0e10cSrcweir #include "apitools.hxx"
63cdf0e10cSrcweir #endif
64cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XKEYSSUPPLIER_HPP_
65cdf0e10cSrcweir #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
66cdf0e10cSrcweir #endif
67cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSER_HPP_
68cdf0e10cSrcweir #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
69cdf0e10cSrcweir #endif
70cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XINDEXESSUPPLIER_HPP_
71cdf0e10cSrcweir #include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
72cdf0e10cSrcweir #endif
73cdf0e10cSrcweir #ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
74cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
75cdf0e10cSrcweir #endif
76cdf0e10cSrcweir #ifndef _COMPHELPER_TYPES_HXX_
77cdf0e10cSrcweir #include <comphelper/types.hxx>
78cdf0e10cSrcweir #endif
79cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_KEYTYPE_HPP_
80cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp>
81cdf0e10cSrcweir #endif
82cdf0e10cSrcweir #ifndef _CONNECTIVITY_DBTOOLS_HXX_
83cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
84cdf0e10cSrcweir #endif
85cdf0e10cSrcweir #ifndef _DBHELPER_DBEXCEPTION_HXX_
86cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
87cdf0e10cSrcweir #endif
88cdf0e10cSrcweir #include <list>
89cdf0e10cSrcweir #include <algorithm>
90cdf0e10cSrcweir #include <string.h>
91cdf0e10cSrcweir #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HPP_
92cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
93cdf0e10cSrcweir #endif
94cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
95cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
96cdf0e10cSrcweir #endif
97cdf0e10cSrcweir #ifndef DBACCESS_CORE_API_QUERYCOMPOSER_HXX
98cdf0e10cSrcweir #include "querycomposer.hxx"
99cdf0e10cSrcweir #endif
100cdf0e10cSrcweir #ifndef DBACCESS_SOURCE_CORE_INC_COMPOSERTOOLS_HXX
101cdf0e10cSrcweir #include "composertools.hxx"
102cdf0e10cSrcweir #endif
103cdf0e10cSrcweir #ifndef _TOOLS_DEBUG_HXX
104cdf0e10cSrcweir #include <tools/debug.hxx>
105cdf0e10cSrcweir #endif
106cdf0e10cSrcweir #include <string.h>
107cdf0e10cSrcweir #include <rtl/logfile.hxx>
108cdf0e10cSrcweir #include "PrivateRow.hxx"
109cdf0e10cSrcweir
110cdf0e10cSrcweir using namespace dbaccess;
111cdf0e10cSrcweir using namespace ::connectivity;
112cdf0e10cSrcweir using namespace ::dbtools;
113cdf0e10cSrcweir using namespace ::com::sun::star::uno;
114cdf0e10cSrcweir using namespace ::com::sun::star::beans;
115cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
116cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
117cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
118cdf0e10cSrcweir using namespace ::com::sun::star::container;
119cdf0e10cSrcweir using namespace ::com::sun::star::lang;
120cdf0e10cSrcweir using namespace ::com::sun::star::util;
121cdf0e10cSrcweir using namespace ::com::sun::star::io;
122cdf0e10cSrcweir using namespace ::com::sun::star;
123cdf0e10cSrcweir using namespace ::cppu;
124cdf0e10cSrcweir using namespace ::osl;
125cdf0e10cSrcweir
126cdf0e10cSrcweir namespace
127cdf0e10cSrcweir {
lcl_fillIndexColumns(const Reference<XIndexAccess> & _xIndexes,::std::vector<Reference<XNameAccess>> & _rAllIndexColumns)128cdf0e10cSrcweir void lcl_fillIndexColumns(const Reference<XIndexAccess>& _xIndexes, ::std::vector< Reference<XNameAccess> >& _rAllIndexColumns)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir if ( _xIndexes.is() )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir Reference<XPropertySet> xIndexColsSup;
133cdf0e10cSrcweir sal_Int32 nCount = _xIndexes->getCount();
134cdf0e10cSrcweir for(sal_Int32 j = 0 ; j < nCount ; ++j)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir xIndexColsSup.set(_xIndexes->getByIndex(j),UNO_QUERY);
137cdf0e10cSrcweir if( xIndexColsSup.is()
138cdf0e10cSrcweir && comphelper::getBOOL(xIndexColsSup->getPropertyValue(PROPERTY_ISUNIQUE))
139cdf0e10cSrcweir && !comphelper::getBOOL(xIndexColsSup->getPropertyValue(PROPERTY_ISPRIMARYKEYINDEX))
140cdf0e10cSrcweir )
141cdf0e10cSrcweir _rAllIndexColumns.push_back(Reference<XColumnsSupplier>(xIndexColsSup,UNO_QUERY)->getColumns());
142cdf0e10cSrcweir }
143cdf0e10cSrcweir }
144cdf0e10cSrcweir }
145cdf0e10cSrcweir }
DBG_NAME(OKeySet)146cdf0e10cSrcweir DBG_NAME(OKeySet)
147cdf0e10cSrcweir // -------------------------------------------------------------------------
148cdf0e10cSrcweir OKeySet::OKeySet(const connectivity::OSQLTable& _xTable,
149cdf0e10cSrcweir const Reference< XIndexAccess>& _xTableKeys,
150cdf0e10cSrcweir const ::rtl::OUString& _rUpdateTableName, // this can be the alias or the full qualified name
151cdf0e10cSrcweir const Reference< XSingleSelectQueryAnalyzer >& _xComposer,
152cdf0e10cSrcweir const ORowSetValueVector& _aParameterValueForCache,
153cdf0e10cSrcweir sal_Int32 i_nMaxRows,
154cdf0e10cSrcweir sal_Int32& o_nRowCount)
155cdf0e10cSrcweir :OCacheSet(i_nMaxRows)
156cdf0e10cSrcweir ,m_aParameterValueForCache(_aParameterValueForCache)
157cdf0e10cSrcweir ,m_pKeyColumnNames(NULL)
158cdf0e10cSrcweir ,m_pColumnNames(NULL)
159cdf0e10cSrcweir ,m_pParameterNames(NULL)
160cdf0e10cSrcweir ,m_pForeignColumnNames(NULL)
161cdf0e10cSrcweir ,m_xTable(_xTable)
162cdf0e10cSrcweir ,m_xTableKeys(_xTableKeys)
163cdf0e10cSrcweir ,m_xComposer(_xComposer)
164cdf0e10cSrcweir ,m_sUpdateTableName(_rUpdateTableName)
165cdf0e10cSrcweir ,m_rRowCount(o_nRowCount)
166cdf0e10cSrcweir ,m_bRowCountFinal(sal_False)
167cdf0e10cSrcweir {
168cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::OKeySet" );
169cdf0e10cSrcweir DBG_CTOR(OKeySet,NULL);
170cdf0e10cSrcweir
171cdf0e10cSrcweir }
172cdf0e10cSrcweir // -----------------------------------------------------------------------------
~OKeySet()173cdf0e10cSrcweir OKeySet::~OKeySet()
174cdf0e10cSrcweir {
175cdf0e10cSrcweir try
176cdf0e10cSrcweir {
177cdf0e10cSrcweir ::comphelper::disposeComponent(m_xStatement);
178cdf0e10cSrcweir }
179cdf0e10cSrcweir catch(Exception&)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir m_xStatement = NULL;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir catch(...)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir OSL_ENSURE(0,"Unknown Exception occured");
186cdf0e10cSrcweir }
187cdf0e10cSrcweir m_xComposer = NULL;
188cdf0e10cSrcweir
189cdf0e10cSrcweir DBG_DTOR(OKeySet,NULL);
190cdf0e10cSrcweir }
initColumns()191cdf0e10cSrcweir void OKeySet::initColumns()
192cdf0e10cSrcweir {
193cdf0e10cSrcweir Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
194cdf0e10cSrcweir bool bCase = (xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers()) ? true : false;
195cdf0e10cSrcweir m_pKeyColumnNames.reset( new SelectColumnsMetaData(bCase) );
196cdf0e10cSrcweir m_pColumnNames.reset( new SelectColumnsMetaData(bCase) );
197cdf0e10cSrcweir m_pParameterNames.reset( new SelectColumnsMetaData(bCase) );
198cdf0e10cSrcweir m_pForeignColumnNames.reset( new SelectColumnsMetaData(bCase) );
199cdf0e10cSrcweir }
findTableColumnsMatching_throw(const Any & i_aTable,const::rtl::OUString & i_rUpdateTableName,const Reference<XDatabaseMetaData> & i_xMeta,const Reference<XNameAccess> & i_xQueryColumns,::std::auto_ptr<SelectColumnsMetaData> & o_pKeyColumnNames)200cdf0e10cSrcweir void OKeySet::findTableColumnsMatching_throw( const Any& i_aTable,
201cdf0e10cSrcweir const ::rtl::OUString& i_rUpdateTableName,
202cdf0e10cSrcweir const Reference<XDatabaseMetaData>& i_xMeta,
203cdf0e10cSrcweir const Reference<XNameAccess>& i_xQueryColumns,
204cdf0e10cSrcweir ::std::auto_ptr<SelectColumnsMetaData>& o_pKeyColumnNames)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir // first ask the database itself for the best columns which can be used
207cdf0e10cSrcweir Sequence< ::rtl::OUString> aBestColumnNames;
208cdf0e10cSrcweir Reference<XNameAccess> xKeyColumns = getPrimaryKeyColumns_throw(i_aTable);
209cdf0e10cSrcweir if ( xKeyColumns.is() )
210cdf0e10cSrcweir aBestColumnNames = xKeyColumns->getElementNames();
211cdf0e10cSrcweir
212cdf0e10cSrcweir const Reference<XColumnsSupplier> xTblColSup(i_aTable,UNO_QUERY_THROW);
213cdf0e10cSrcweir const Reference<XNameAccess> xTblColumns = xTblColSup->getColumns();
214cdf0e10cSrcweir // locate parameter in select columns
215cdf0e10cSrcweir Reference<XParametersSupplier> xParaSup(m_xComposer,UNO_QUERY);
216cdf0e10cSrcweir Reference<XIndexAccess> xQueryParameters = xParaSup->getParameters();
217cdf0e10cSrcweir const sal_Int32 nParaCount = xQueryParameters->getCount();
218cdf0e10cSrcweir Sequence< ::rtl::OUString> aParameterColumns(nParaCount);
219cdf0e10cSrcweir for(sal_Int32 i = 0; i< nParaCount;++i)
220cdf0e10cSrcweir {
221cdf0e10cSrcweir Reference<XPropertySet> xPara(xQueryParameters->getByIndex(i),UNO_QUERY_THROW);
222cdf0e10cSrcweir xPara->getPropertyValue(PROPERTY_REALNAME) >>= aParameterColumns[i];
223cdf0e10cSrcweir }
224cdf0e10cSrcweir
225cdf0e10cSrcweir ::rtl::OUString sUpdateTableName( i_rUpdateTableName );
226cdf0e10cSrcweir if ( sUpdateTableName.getLength() == 0 )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir OSL_ENSURE( false, "OKeySet::findTableColumnsMatching_throw: This is a fallback only - it won't work when the table has an alias name." );
229cdf0e10cSrcweir // If i_aTable originates from a query composer, and is a table which appears with an alias in the SELECT statement,
230cdf0e10cSrcweir // then the below code will not produce correct results.
231cdf0e10cSrcweir // For instance, imagine a "SELECT alias.col FROM table AS alias". Now i_aTable would be the table named
232cdf0e10cSrcweir // "table", so our sUpdateTableName would be "table" as well - not the information about the "alias" is
233cdf0e10cSrcweir // already lost here.
234cdf0e10cSrcweir // now getColumnPositions would travers the columns, and check which of them belong to the table denoted
235cdf0e10cSrcweir // by sUpdateTableName. Since the latter is "table", but the columns only know that they belong to a table
236cdf0e10cSrcweir // named "alias", there will be no matching - so getColumnPositions wouldn't find anything.
237cdf0e10cSrcweir
238cdf0e10cSrcweir ::rtl::OUString sCatalog, sSchema, sTable;
239cdf0e10cSrcweir Reference<XPropertySet> xTableProp( i_aTable, UNO_QUERY_THROW );
240cdf0e10cSrcweir xTableProp->getPropertyValue( PROPERTY_CATALOGNAME )>>= sCatalog;
241cdf0e10cSrcweir xTableProp->getPropertyValue( PROPERTY_SCHEMANAME ) >>= sSchema;
242cdf0e10cSrcweir xTableProp->getPropertyValue( PROPERTY_NAME ) >>= sTable;
243cdf0e10cSrcweir sUpdateTableName = dbtools::composeTableName( i_xMeta, sCatalog, sSchema, sTable, sal_False, ::dbtools::eInDataManipulation );
244cdf0e10cSrcweir }
245cdf0e10cSrcweir
246cdf0e10cSrcweir ::dbaccess::getColumnPositions(i_xQueryColumns,aBestColumnNames,sUpdateTableName,(*o_pKeyColumnNames),true);
247cdf0e10cSrcweir ::dbaccess::getColumnPositions(i_xQueryColumns,xTblColumns->getElementNames(),sUpdateTableName,(*m_pColumnNames),true);
248cdf0e10cSrcweir ::dbaccess::getColumnPositions(i_xQueryColumns,aParameterColumns,sUpdateTableName,(*m_pParameterNames),true);
249cdf0e10cSrcweir
250cdf0e10cSrcweir if ( o_pKeyColumnNames->empty() )
251cdf0e10cSrcweir {
252cdf0e10cSrcweir ::dbtools::throwGenericSQLException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Could not find any key column." ) ), *this );
253cdf0e10cSrcweir }
254cdf0e10cSrcweir
255cdf0e10cSrcweir for ( SelectColumnsMetaData::const_iterator keyColumn = o_pKeyColumnNames->begin();
256cdf0e10cSrcweir keyColumn != o_pKeyColumnNames->end();
257cdf0e10cSrcweir ++keyColumn
258cdf0e10cSrcweir )
259cdf0e10cSrcweir {
260cdf0e10cSrcweir if ( !xTblColumns->hasByName( keyColumn->second.sRealName ) )
261cdf0e10cSrcweir continue;
262cdf0e10cSrcweir
263cdf0e10cSrcweir Reference<XPropertySet> xProp( xTblColumns->getByName( keyColumn->second.sRealName ), UNO_QUERY );
264cdf0e10cSrcweir sal_Bool bAuto = sal_False;
265cdf0e10cSrcweir if ( ( xProp->getPropertyValue( PROPERTY_ISAUTOINCREMENT ) >>= bAuto ) && bAuto )
266cdf0e10cSrcweir m_aAutoColumns.push_back( keyColumn->first );
267cdf0e10cSrcweir }
268cdf0e10cSrcweir }
createKeyFilter()269cdf0e10cSrcweir ::rtl::OUStringBuffer OKeySet::createKeyFilter()
270cdf0e10cSrcweir {
271cdf0e10cSrcweir static ::rtl::OUString aAnd = ::rtl::OUString::createFromAscii(" AND ");
272cdf0e10cSrcweir const ::rtl::OUString aQuote = getIdentifierQuoteString();
273cdf0e10cSrcweir ::rtl::OUStringBuffer aFilter;
274cdf0e10cSrcweir static ::rtl::OUString s_sDot(RTL_CONSTASCII_USTRINGPARAM("."));
275cdf0e10cSrcweir static ::rtl::OUString s_sParam(RTL_CONSTASCII_USTRINGPARAM(" = ?"));
276cdf0e10cSrcweir // create the where clause
277cdf0e10cSrcweir Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
278cdf0e10cSrcweir SelectColumnsMetaData::iterator aPosEnd = m_pKeyColumnNames->end();
279cdf0e10cSrcweir for(SelectColumnsMetaData::iterator aPosIter = m_pKeyColumnNames->begin();aPosIter != aPosEnd;)
280cdf0e10cSrcweir {
281cdf0e10cSrcweir aFilter.append(::dbtools::quoteTableName( xMeta,aPosIter->second.sTableName,::dbtools::eInDataManipulation));
282cdf0e10cSrcweir aFilter.append(s_sDot);
283cdf0e10cSrcweir aFilter.append(::dbtools::quoteName( aQuote,aPosIter->second.sRealName));
284cdf0e10cSrcweir aFilter.append(s_sParam);
285cdf0e10cSrcweir ++aPosIter;
286cdf0e10cSrcweir if(aPosIter != aPosEnd)
287cdf0e10cSrcweir aFilter.append(aAnd);
288cdf0e10cSrcweir }
289cdf0e10cSrcweir return aFilter;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir // -----------------------------------------------------------------------------
construct(const Reference<XResultSet> & _xDriverSet,const::rtl::OUString & i_sRowSetFilter)292cdf0e10cSrcweir void OKeySet::construct(const Reference< XResultSet>& _xDriverSet,const ::rtl::OUString& i_sRowSetFilter)
293cdf0e10cSrcweir {
294cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::construct" );
295cdf0e10cSrcweir OCacheSet::construct(_xDriverSet,i_sRowSetFilter);
296cdf0e10cSrcweir initColumns();
297cdf0e10cSrcweir
298cdf0e10cSrcweir Reference<XNameAccess> xKeyColumns = getKeyColumns();
299cdf0e10cSrcweir Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
300cdf0e10cSrcweir Reference<XColumnsSupplier> xQueryColSup(m_xComposer,UNO_QUERY);
301cdf0e10cSrcweir const Reference<XNameAccess> xQueryColumns = xQueryColSup->getColumns();
302cdf0e10cSrcweir findTableColumnsMatching_throw(makeAny(m_xTable),m_sUpdateTableName,xMeta,xQueryColumns,m_pKeyColumnNames);
303cdf0e10cSrcweir
304cdf0e10cSrcweir // the first row is empty because it's now easier for us to distinguish when we are beforefirst or first
305cdf0e10cSrcweir // without extra varaible to be set
306cdf0e10cSrcweir m_aKeyMap.insert(OKeySetMatrix::value_type(0,OKeySetValue(NULL,::std::pair<sal_Int32,Reference<XRow> >(0,NULL))));
307cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.begin();
308cdf0e10cSrcweir
309cdf0e10cSrcweir ::rtl::OUStringBuffer aFilter = createKeyFilter();
310cdf0e10cSrcweir
311cdf0e10cSrcweir Reference< XSingleSelectQueryComposer> xSourceComposer(m_xComposer,UNO_QUERY);
312cdf0e10cSrcweir Reference< XMultiServiceFactory > xFactory(m_xConnection, UNO_QUERY_THROW);
313cdf0e10cSrcweir Reference<XSingleSelectQueryComposer> xAnalyzer(xFactory->createInstance(SERVICE_NAME_SINGLESELECTQUERYCOMPOSER),UNO_QUERY);
314cdf0e10cSrcweir xAnalyzer->setElementaryQuery(xSourceComposer->getElementaryQuery());
315cdf0e10cSrcweir Reference<XTablesSupplier> xTabSup(xAnalyzer,uno::UNO_QUERY);
316cdf0e10cSrcweir Reference<XNameAccess> xSelectTables(xTabSup->getTables(),uno::UNO_QUERY);
317cdf0e10cSrcweir const Sequence< ::rtl::OUString> aSeq = xSelectTables->getElementNames();
318cdf0e10cSrcweir if ( aSeq.getLength() > 1 ) // special handling for join
319cdf0e10cSrcweir {
320cdf0e10cSrcweir static ::rtl::OUString aAnd = ::rtl::OUString::createFromAscii(" AND ");
321cdf0e10cSrcweir const ::rtl::OUString aQuote = getIdentifierQuoteString();
322cdf0e10cSrcweir static ::rtl::OUString s_sDot(RTL_CONSTASCII_USTRINGPARAM("."));
323cdf0e10cSrcweir static ::rtl::OUString s_sParam(RTL_CONSTASCII_USTRINGPARAM(" = ?"));
324cdf0e10cSrcweir const ::rtl::OUString* pIter = aSeq.getConstArray();
325cdf0e10cSrcweir const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
326cdf0e10cSrcweir for(;pIter != pEnd;++pIter)
327cdf0e10cSrcweir {
328cdf0e10cSrcweir if ( *pIter != m_sUpdateTableName )
329cdf0e10cSrcweir {
330cdf0e10cSrcweir connectivity::OSQLTable xSelColSup(xSelectTables->getByName(*pIter),uno::UNO_QUERY);
331cdf0e10cSrcweir Reference<XPropertySet> xProp(xSelColSup,uno::UNO_QUERY);
332cdf0e10cSrcweir ::rtl::OUString sSelectTableName = ::dbtools::composeTableName( xMeta, xProp, ::dbtools::eInDataManipulation, false, false, false );
333cdf0e10cSrcweir
334cdf0e10cSrcweir ::dbaccess::getColumnPositions(xQueryColumns,xSelColSup->getColumns()->getElementNames(),sSelectTableName,(*m_pForeignColumnNames));
335cdf0e10cSrcweir
336cdf0e10cSrcweir SelectColumnsMetaData::iterator aPosEnd = (*m_pForeignColumnNames).end();
337cdf0e10cSrcweir for(SelectColumnsMetaData::iterator aPosIter = (*m_pForeignColumnNames).begin();aPosIter != aPosEnd;++aPosIter)
338cdf0e10cSrcweir {
339cdf0e10cSrcweir // look for columns not in the source columns to use them as filter as well
340cdf0e10cSrcweir // if ( !xSourceColumns->hasByName(aPosIter->first) )
341cdf0e10cSrcweir {
342cdf0e10cSrcweir if ( aFilter.getLength() )
343cdf0e10cSrcweir aFilter.append(aAnd);
344cdf0e10cSrcweir aFilter.append(::dbtools::quoteName( aQuote,sSelectTableName));
345cdf0e10cSrcweir aFilter.append(s_sDot);
346cdf0e10cSrcweir aFilter.append(::dbtools::quoteName( aQuote,aPosIter->second.sRealName));
347cdf0e10cSrcweir aFilter.append(s_sParam);
348cdf0e10cSrcweir }
349cdf0e10cSrcweir }
350cdf0e10cSrcweir break;
351cdf0e10cSrcweir }
352cdf0e10cSrcweir }
353cdf0e10cSrcweir } // if ( aSeq.getLength() > 1 ) // special handling for join
354cdf0e10cSrcweir executeStatement(aFilter,i_sRowSetFilter,xAnalyzer);
355cdf0e10cSrcweir }
executeStatement(::rtl::OUStringBuffer & io_aFilter,const::rtl::OUString & i_sRowSetFilter,Reference<XSingleSelectQueryComposer> & io_xAnalyzer)356cdf0e10cSrcweir void OKeySet::executeStatement(::rtl::OUStringBuffer& io_aFilter,const ::rtl::OUString& i_sRowSetFilter,Reference<XSingleSelectQueryComposer>& io_xAnalyzer)
357cdf0e10cSrcweir {
358cdf0e10cSrcweir bool bFilterSet = i_sRowSetFilter.getLength() != 0;
359cdf0e10cSrcweir if ( bFilterSet )
360cdf0e10cSrcweir {
361cdf0e10cSrcweir FilterCreator aFilterCreator;
362cdf0e10cSrcweir aFilterCreator.append( i_sRowSetFilter );
363cdf0e10cSrcweir aFilterCreator.append( io_aFilter.makeStringAndClear() );
364cdf0e10cSrcweir io_aFilter = aFilterCreator.getComposedAndClear();
365cdf0e10cSrcweir }
366cdf0e10cSrcweir io_xAnalyzer->setFilter(io_aFilter.makeStringAndClear());
367cdf0e10cSrcweir if ( bFilterSet )
368cdf0e10cSrcweir {
369cdf0e10cSrcweir Sequence< Sequence< PropertyValue > > aFilter2 = io_xAnalyzer->getStructuredFilter();
370cdf0e10cSrcweir const Sequence< PropertyValue >* pOr = aFilter2.getConstArray();
371cdf0e10cSrcweir const Sequence< PropertyValue >* pOrEnd = pOr + aFilter2.getLength();
372cdf0e10cSrcweir for(;pOr != pOrEnd;++pOr)
373cdf0e10cSrcweir {
374cdf0e10cSrcweir const PropertyValue* pAnd = pOr->getConstArray();
375cdf0e10cSrcweir const PropertyValue* pAndEnd = pAnd + pOr->getLength();
376cdf0e10cSrcweir for(;pAnd != pAndEnd;++pAnd)
377cdf0e10cSrcweir {
378cdf0e10cSrcweir ::rtl::OUString sValue;
379cdf0e10cSrcweir if ( !(pAnd->Value >>= sValue) || !(sValue.equalsAscii("?") || sValue.matchAsciiL(":",1,0)) )
380cdf0e10cSrcweir { // we have a criteria which has to be taken into account for updates
381cdf0e10cSrcweir m_aFilterColumns.push_back(pAnd->Name);
382cdf0e10cSrcweir }
383cdf0e10cSrcweir }
384cdf0e10cSrcweir }
385cdf0e10cSrcweir }
386cdf0e10cSrcweir m_xStatement = m_xConnection->prepareStatement(io_xAnalyzer->getQueryWithSubstitution());
387cdf0e10cSrcweir ::comphelper::disposeComponent(io_xAnalyzer);
388cdf0e10cSrcweir }
389cdf0e10cSrcweir // -------------------------------------------------------------------------
getBookmark()390cdf0e10cSrcweir Any SAL_CALL OKeySet::getBookmark() throw(SQLException, RuntimeException)
391cdf0e10cSrcweir {
392cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getBookmark" );
393cdf0e10cSrcweir OSL_ENSURE(m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin(),
394cdf0e10cSrcweir "getBookmark is only possible when we stand on a valid row!");
395cdf0e10cSrcweir return makeAny(m_aKeyIter->first);
396cdf0e10cSrcweir }
397cdf0e10cSrcweir
398cdf0e10cSrcweir // -------------------------------------------------------------------------
moveToBookmark(const Any & bookmark)399cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::moveToBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
400cdf0e10cSrcweir {
401cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::moveToBookmark" );
402cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
403cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.find(::comphelper::getINT32(bookmark));
404cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.end();
405cdf0e10cSrcweir }
406cdf0e10cSrcweir // -------------------------------------------------------------------------
moveRelativeToBookmark(const Any & bookmark,sal_Int32 rows)407cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::moveRelativeToBookmark( const Any& bookmark, sal_Int32 rows ) throw(SQLException, RuntimeException)
408cdf0e10cSrcweir {
409cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::moveRelativeToBookmark" );
410cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
411cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.find(::comphelper::getINT32(bookmark));
412cdf0e10cSrcweir if(m_aKeyIter != m_aKeyMap.end())
413cdf0e10cSrcweir {
414cdf0e10cSrcweir relative(rows);
415cdf0e10cSrcweir }
416cdf0e10cSrcweir
417cdf0e10cSrcweir return !isBeforeFirst() && !isAfterLast();
418cdf0e10cSrcweir }
419cdf0e10cSrcweir // -------------------------------------------------------------------------
compareBookmarks(const Any & _first,const Any & _second)420cdf0e10cSrcweir sal_Int32 SAL_CALL OKeySet::compareBookmarks( const Any& _first, const Any& _second ) throw(SQLException, RuntimeException)
421cdf0e10cSrcweir {
422cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::compareBookmarks" );
423cdf0e10cSrcweir sal_Int32 nFirst = 0, nSecond = 0;
424cdf0e10cSrcweir _first >>= nFirst;
425cdf0e10cSrcweir _second >>= nSecond;
426cdf0e10cSrcweir
427cdf0e10cSrcweir return (nFirst != nSecond) ? CompareBookmark::NOT_EQUAL : CompareBookmark::EQUAL;
428cdf0e10cSrcweir }
429cdf0e10cSrcweir // -------------------------------------------------------------------------
hasOrderedBookmarks()430cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::hasOrderedBookmarks( ) throw(SQLException, RuntimeException)
431cdf0e10cSrcweir {
432cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::hasOrderedBookmarks" );
433cdf0e10cSrcweir return sal_True;
434cdf0e10cSrcweir }
435cdf0e10cSrcweir // -------------------------------------------------------------------------
hashBookmark(const Any & bookmark)436cdf0e10cSrcweir sal_Int32 SAL_CALL OKeySet::hashBookmark( const Any& bookmark ) throw(SQLException, RuntimeException)
437cdf0e10cSrcweir {
438cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::hashBookmark" );
439cdf0e10cSrcweir return ::comphelper::getINT32(bookmark);
440cdf0e10cSrcweir }
441cdf0e10cSrcweir // -------------------------------------------------------------------------
442cdf0e10cSrcweir // ::com::sun::star::sdbcx::XDeleteRows
deleteRows(const Sequence<Any> & rows,const connectivity::OSQLTable & _xTable)443cdf0e10cSrcweir Sequence< sal_Int32 > SAL_CALL OKeySet::deleteRows( const Sequence< Any >& rows ,const connectivity::OSQLTable& _xTable) throw(SQLException, RuntimeException)
444cdf0e10cSrcweir {
445cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::deleteRows" );
446cdf0e10cSrcweir Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
447cdf0e10cSrcweir fillTableName(xSet);
448cdf0e10cSrcweir
449cdf0e10cSrcweir ::rtl::OUStringBuffer aSql = ::rtl::OUString::createFromAscii("DELETE FROM ");
450cdf0e10cSrcweir aSql.append(m_aComposedTableName);
451cdf0e10cSrcweir aSql.append(::rtl::OUString::createFromAscii(" WHERE "));
452cdf0e10cSrcweir
453cdf0e10cSrcweir // list all cloumns that should be set
454cdf0e10cSrcweir const ::rtl::OUString aQuote = getIdentifierQuoteString();
455cdf0e10cSrcweir static ::rtl::OUString aAnd = ::rtl::OUString::createFromAscii(" AND ");
456cdf0e10cSrcweir static ::rtl::OUString aOr = ::rtl::OUString::createFromAscii(" OR ");
457cdf0e10cSrcweir static ::rtl::OUString aEqual = ::rtl::OUString::createFromAscii(" = ?");
458cdf0e10cSrcweir
459cdf0e10cSrcweir
460cdf0e10cSrcweir // use keys and indexes for excat postioning
461cdf0e10cSrcweir // first the keys
462cdf0e10cSrcweir Reference<XNameAccess> xKeyColumns = getKeyColumns();
463cdf0e10cSrcweir
464cdf0e10cSrcweir ::rtl::OUStringBuffer aCondition = ::rtl::OUString::createFromAscii("( ");
465cdf0e10cSrcweir
466cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aIter = (*m_pKeyColumnNames).begin();
467cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosEnd = (*m_pKeyColumnNames).end();
468cdf0e10cSrcweir for(;aIter != aPosEnd;++aIter)
469cdf0e10cSrcweir {
470cdf0e10cSrcweir aCondition.append(::dbtools::quoteName( aQuote,aIter->second.sRealName));
471cdf0e10cSrcweir aCondition.append(aEqual);
472cdf0e10cSrcweir aCondition.append(aAnd);
473cdf0e10cSrcweir }
474cdf0e10cSrcweir aCondition.setLength(aCondition.getLength()-5);
475cdf0e10cSrcweir const ::rtl::OUString sCon( aCondition.makeStringAndClear() );
476cdf0e10cSrcweir
477cdf0e10cSrcweir const Any* pBegin = rows.getConstArray();
478cdf0e10cSrcweir const Any* pEnd = pBegin + rows.getLength();
479cdf0e10cSrcweir
480cdf0e10cSrcweir Sequence< Any > aKeys;
481cdf0e10cSrcweir for(;pBegin != pEnd;++pBegin)
482cdf0e10cSrcweir {
483cdf0e10cSrcweir aSql.append(sCon);
484cdf0e10cSrcweir aSql.append(aOr);
485cdf0e10cSrcweir }
486cdf0e10cSrcweir aSql.setLength(aSql.getLength()-3);
487cdf0e10cSrcweir
488cdf0e10cSrcweir // now create end execute the prepared statement
489cdf0e10cSrcweir
490cdf0e10cSrcweir Reference< XPreparedStatement > xPrep(m_xConnection->prepareStatement(aSql.makeStringAndClear()));
491cdf0e10cSrcweir Reference< XParameters > xParameter(xPrep,UNO_QUERY);
492cdf0e10cSrcweir
493cdf0e10cSrcweir pBegin = rows.getConstArray();
494cdf0e10cSrcweir sal_Int32 i=1;
495cdf0e10cSrcweir for(;pBegin != pEnd;++pBegin)
496cdf0e10cSrcweir {
497cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.find(::comphelper::getINT32(*pBegin));
498cdf0e10cSrcweir if(m_aKeyIter != m_aKeyMap.end())
499cdf0e10cSrcweir {
500cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::iterator aKeyIter = m_aKeyIter->second.first->get().begin();
501cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::iterator aKeyEnd = m_aKeyIter->second.first->get().end();
502cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosIter = (*m_pKeyColumnNames).begin();
503cdf0e10cSrcweir for(sal_uInt16 j = 0;aKeyIter != aKeyEnd;++aKeyIter,++j,++aPosIter)
504cdf0e10cSrcweir {
505cdf0e10cSrcweir setParameter(i++,xParameter,*aKeyIter,aPosIter->second.nType,aPosIter->second.nScale);
506cdf0e10cSrcweir }
507cdf0e10cSrcweir }
508cdf0e10cSrcweir }
509cdf0e10cSrcweir
510cdf0e10cSrcweir sal_Bool bOk = xPrep->executeUpdate() > 0;
511cdf0e10cSrcweir Sequence< sal_Int32 > aRet(rows.getLength());
512cdf0e10cSrcweir memset(aRet.getArray(),bOk,sizeof(sal_Int32)*aRet.getLength());
513cdf0e10cSrcweir if(bOk)
514cdf0e10cSrcweir {
515cdf0e10cSrcweir pBegin = rows.getConstArray();
516cdf0e10cSrcweir pEnd = pBegin + rows.getLength();
517cdf0e10cSrcweir
518cdf0e10cSrcweir for(;pBegin != pEnd;++pBegin)
519cdf0e10cSrcweir {
520cdf0e10cSrcweir sal_Int32 nPos = 0;
521cdf0e10cSrcweir *pBegin >>= nPos;
522cdf0e10cSrcweir if(m_aKeyIter == m_aKeyMap.find(nPos) && m_aKeyIter != m_aKeyMap.end())
523cdf0e10cSrcweir ++m_aKeyIter;
524cdf0e10cSrcweir m_aKeyMap.erase(nPos);
525cdf0e10cSrcweir m_bDeleted = sal_True;
526cdf0e10cSrcweir }
527cdf0e10cSrcweir }
528cdf0e10cSrcweir return aRet;
529cdf0e10cSrcweir }
530cdf0e10cSrcweir // -------------------------------------------------------------------------
updateRow(const ORowSetRow & _rInsertRow,const ORowSetRow & _rOrginalRow,const connectivity::OSQLTable & _xTable)531cdf0e10cSrcweir void SAL_CALL OKeySet::updateRow(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOrginalRow,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
532cdf0e10cSrcweir {
533cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::updateRow" );
534cdf0e10cSrcweir Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
535cdf0e10cSrcweir fillTableName(xSet);
536cdf0e10cSrcweir
537cdf0e10cSrcweir ::rtl::OUStringBuffer aSql = ::rtl::OUString::createFromAscii("UPDATE ");
538cdf0e10cSrcweir aSql.append(m_aComposedTableName);
539cdf0e10cSrcweir aSql.append(::rtl::OUString::createFromAscii(" SET "));
540cdf0e10cSrcweir // list all cloumns that should be set
541cdf0e10cSrcweir static ::rtl::OUString aPara = ::rtl::OUString::createFromAscii(" = ?,");
542cdf0e10cSrcweir ::rtl::OUString aQuote = getIdentifierQuoteString();
543cdf0e10cSrcweir static ::rtl::OUString aAnd = ::rtl::OUString::createFromAscii(" AND ");
544cdf0e10cSrcweir ::rtl::OUString sIsNull(RTL_CONSTASCII_USTRINGPARAM(" IS NULL"));
545cdf0e10cSrcweir ::rtl::OUString sParam(RTL_CONSTASCII_USTRINGPARAM(" = ?"));
546cdf0e10cSrcweir
547cdf0e10cSrcweir // use keys and indexes for excat postioning
548cdf0e10cSrcweir // first the keys
549cdf0e10cSrcweir Reference<XNameAccess> xKeyColumns = getKeyColumns();
550cdf0e10cSrcweir
551cdf0e10cSrcweir // second the indexes
552cdf0e10cSrcweir Reference<XIndexesSupplier> xIndexSup(_xTable,UNO_QUERY);
553cdf0e10cSrcweir Reference<XIndexAccess> xIndexes;
554cdf0e10cSrcweir if ( xIndexSup.is() )
555cdf0e10cSrcweir xIndexes.set(xIndexSup->getIndexes(),UNO_QUERY);
556cdf0e10cSrcweir
557cdf0e10cSrcweir
558cdf0e10cSrcweir ::std::vector< Reference<XNameAccess> > aAllIndexColumns;
559cdf0e10cSrcweir lcl_fillIndexColumns(xIndexes,aAllIndexColumns);
560cdf0e10cSrcweir
561cdf0e10cSrcweir ::rtl::OUString aColumnName;
562cdf0e10cSrcweir ::rtl::OUStringBuffer sKeyCondition,sIndexCondition;
563cdf0e10cSrcweir ::std::vector<sal_Int32> aIndexColumnPositions;
564cdf0e10cSrcweir
565cdf0e10cSrcweir const sal_Int32 nOldLength = aSql.getLength();
566cdf0e10cSrcweir sal_Int32 i = 1;
567cdf0e10cSrcweir // here we build the condition part for the update statement
568cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aIter = m_pColumnNames->begin();
569cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aEnd = m_pColumnNames->end();
570cdf0e10cSrcweir for(;aIter != aEnd;++aIter,++i)
571cdf0e10cSrcweir {
572cdf0e10cSrcweir //if(xKeyColumns.is() && xKeyColumns->hasByName(aIter->first))
573cdf0e10cSrcweir if ( m_pKeyColumnNames->find(aIter->first) != m_pKeyColumnNames->end() )
574cdf0e10cSrcweir {
575cdf0e10cSrcweir sKeyCondition.append(::dbtools::quoteName( aQuote,aIter->second.sRealName));
576cdf0e10cSrcweir if((_rOrginalRow->get())[aIter->second.nPosition].isNull())
577cdf0e10cSrcweir sKeyCondition.append(sIsNull);
578cdf0e10cSrcweir else
579cdf0e10cSrcweir sKeyCondition.append(sParam);
580cdf0e10cSrcweir sKeyCondition.append(aAnd);
581cdf0e10cSrcweir }
582cdf0e10cSrcweir else
583cdf0e10cSrcweir {
584cdf0e10cSrcweir ::std::vector< Reference<XNameAccess> >::const_iterator aIndexEnd = aAllIndexColumns.end();
585cdf0e10cSrcweir for( ::std::vector< Reference<XNameAccess> >::const_iterator aIndexIter = aAllIndexColumns.begin();
586cdf0e10cSrcweir aIndexIter != aIndexEnd;++aIndexIter)
587cdf0e10cSrcweir {
588cdf0e10cSrcweir if((*aIndexIter)->hasByName(aIter->first))
589cdf0e10cSrcweir {
590cdf0e10cSrcweir sIndexCondition.append(::dbtools::quoteName( aQuote,aIter->second.sRealName));
591cdf0e10cSrcweir if((_rOrginalRow->get())[aIter->second.nPosition].isNull())
592cdf0e10cSrcweir sIndexCondition.append(sIsNull);
593cdf0e10cSrcweir else
594cdf0e10cSrcweir {
595cdf0e10cSrcweir sIndexCondition.append(sParam);
596cdf0e10cSrcweir aIndexColumnPositions.push_back(aIter->second.nPosition);
597cdf0e10cSrcweir }
598cdf0e10cSrcweir sIndexCondition.append(aAnd);
599cdf0e10cSrcweir break;
600cdf0e10cSrcweir }
601cdf0e10cSrcweir }
602cdf0e10cSrcweir }
603cdf0e10cSrcweir if((_rInsertRow->get())[aIter->second.nPosition].isModified())
604cdf0e10cSrcweir {
605cdf0e10cSrcweir aSql.append(::dbtools::quoteName( aQuote,aIter->second.sRealName));
606cdf0e10cSrcweir aSql.append(aPara);
607cdf0e10cSrcweir }
608cdf0e10cSrcweir }
609cdf0e10cSrcweir
610cdf0e10cSrcweir if( aSql.getLength() != nOldLength )
611cdf0e10cSrcweir {
612cdf0e10cSrcweir aSql.setLength(aSql.getLength()-1);
613cdf0e10cSrcweir }
614cdf0e10cSrcweir else
615cdf0e10cSrcweir ::dbtools::throwSQLException( DBACORE_RESSTRING( RID_STR_NO_VALUE_CHANGED ), SQL_GENERAL_ERROR, m_xConnection );
616cdf0e10cSrcweir
617cdf0e10cSrcweir if(sKeyCondition.getLength() || sIndexCondition.getLength())
618cdf0e10cSrcweir {
619cdf0e10cSrcweir aSql.append(::rtl::OUString::createFromAscii(" WHERE "));
620cdf0e10cSrcweir if(sKeyCondition.getLength() && sIndexCondition.getLength())
621cdf0e10cSrcweir {
622cdf0e10cSrcweir aSql.append(sKeyCondition.makeStringAndClear());
623cdf0e10cSrcweir aSql.append(sIndexCondition.makeStringAndClear());
624cdf0e10cSrcweir }
625cdf0e10cSrcweir else if(sKeyCondition.getLength())
626cdf0e10cSrcweir {
627cdf0e10cSrcweir aSql.append(sKeyCondition.makeStringAndClear());
628cdf0e10cSrcweir }
629cdf0e10cSrcweir else if(sIndexCondition.getLength())
630cdf0e10cSrcweir {
631cdf0e10cSrcweir aSql.append(sIndexCondition.makeStringAndClear());
632cdf0e10cSrcweir }
633cdf0e10cSrcweir aSql.setLength(aSql.getLength()-5); // remove the last AND
634cdf0e10cSrcweir }
635cdf0e10cSrcweir else
636cdf0e10cSrcweir ::dbtools::throwSQLException( DBACORE_RESSTRING( RID_STR_NO_CONDITION_FOR_PK ), SQL_GENERAL_ERROR, m_xConnection );
637cdf0e10cSrcweir
638cdf0e10cSrcweir // now create end execute the prepared statement
639cdf0e10cSrcweir
640cdf0e10cSrcweir ::rtl::OUString sEmpty;
641cdf0e10cSrcweir executeUpdate(_rInsertRow ,_rOrginalRow,aSql.makeStringAndClear(),sEmpty,aIndexColumnPositions);
642cdf0e10cSrcweir }
643cdf0e10cSrcweir // -----------------------------------------------------------------------------
executeUpdate(const ORowSetRow & _rInsertRow,const ORowSetRow & _rOrginalRow,const::rtl::OUString & i_sSQL,const::rtl::OUString & i_sTableName,const::std::vector<sal_Int32> & _aIndexColumnPositions)644cdf0e10cSrcweir void OKeySet::executeUpdate(const ORowSetRow& _rInsertRow ,const ORowSetRow& _rOrginalRow,const ::rtl::OUString& i_sSQL,const ::rtl::OUString& i_sTableName,const ::std::vector<sal_Int32>& _aIndexColumnPositions)
645cdf0e10cSrcweir {
646cdf0e10cSrcweir // now create end execute the prepared statement
647cdf0e10cSrcweir Reference< XPreparedStatement > xPrep(m_xConnection->prepareStatement(i_sSQL));
648cdf0e10cSrcweir Reference< XParameters > xParameter(xPrep,UNO_QUERY);
649cdf0e10cSrcweir
650cdf0e10cSrcweir bool bRefetch = true;
651cdf0e10cSrcweir Reference<XRow> xRow;
652cdf0e10cSrcweir sal_Int32 i = 1;
653cdf0e10cSrcweir // first the set values
654cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aIter = m_pColumnNames->begin();
655cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aEnd = m_pColumnNames->end();
656cdf0e10cSrcweir sal_uInt16 j = 0;
657cdf0e10cSrcweir for(;aIter != aEnd;++aIter,++j)
658cdf0e10cSrcweir {
659cdf0e10cSrcweir if ( !i_sTableName.getLength() || aIter->second.sTableName == i_sTableName )
660cdf0e10cSrcweir {
661cdf0e10cSrcweir sal_Int32 nPos = aIter->second.nPosition;
662cdf0e10cSrcweir if((_rInsertRow->get())[nPos].isModified())
663cdf0e10cSrcweir {
664cdf0e10cSrcweir if ( bRefetch )
665cdf0e10cSrcweir {
666cdf0e10cSrcweir bRefetch = ::std::find(m_aFilterColumns.begin(),m_aFilterColumns.end(),aIter->second.sRealName) == m_aFilterColumns.end();
667cdf0e10cSrcweir }
668cdf0e10cSrcweir impl_convertValue_throw(_rInsertRow,aIter->second);
669cdf0e10cSrcweir (_rInsertRow->get())[nPos].setSigned((_rOrginalRow->get())[nPos].isSigned());
670cdf0e10cSrcweir setParameter(i++,xParameter,(_rInsertRow->get())[nPos],aIter->second.nType,aIter->second.nScale);
671cdf0e10cSrcweir }
672cdf0e10cSrcweir }
673cdf0e10cSrcweir }
674cdf0e10cSrcweir // and then the values of the where condition
675cdf0e10cSrcweir aIter = m_pKeyColumnNames->begin();
676cdf0e10cSrcweir aEnd = m_pKeyColumnNames->end();
677cdf0e10cSrcweir j = 0;
678cdf0e10cSrcweir for(;aIter != aEnd;++aIter,++j)
679cdf0e10cSrcweir {
680cdf0e10cSrcweir if ( !i_sTableName.getLength() || aIter->second.sTableName == i_sTableName )
681cdf0e10cSrcweir {
682cdf0e10cSrcweir setParameter(i++,xParameter,(_rOrginalRow->get())[aIter->second.nPosition],aIter->second.nType,aIter->second.nScale);
683cdf0e10cSrcweir }
684cdf0e10cSrcweir }
685cdf0e10cSrcweir if ( !_aIndexColumnPositions.empty() )
686cdf0e10cSrcweir {
687cdf0e10cSrcweir // now we have to set the index values
688cdf0e10cSrcweir ::std::vector<sal_Int32>::const_iterator aIdxColIter = _aIndexColumnPositions.begin();
689cdf0e10cSrcweir ::std::vector<sal_Int32>::const_iterator aIdxColEnd = _aIndexColumnPositions.end();
690cdf0e10cSrcweir j = 0;
691cdf0e10cSrcweir aIter = m_pColumnNames->begin();
692cdf0e10cSrcweir for(;aIdxColIter != aIdxColEnd;++aIdxColIter,++i,++j,++aIter)
693cdf0e10cSrcweir {
694cdf0e10cSrcweir setParameter(i,xParameter,(_rOrginalRow->get())[*aIdxColIter],(_rOrginalRow->get())[*aIdxColIter].getTypeKind(),aIter->second.nScale);
695cdf0e10cSrcweir }
696cdf0e10cSrcweir }
697cdf0e10cSrcweir const sal_Int32 nRowsUpdated = xPrep->executeUpdate();
698cdf0e10cSrcweir m_bUpdated = nRowsUpdated > 0;
699cdf0e10cSrcweir if(m_bUpdated)
700cdf0e10cSrcweir {
701cdf0e10cSrcweir const sal_Int32 nBookmark = ::comphelper::getINT32((_rInsertRow->get())[0].getAny());
702cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.find(nBookmark);
703cdf0e10cSrcweir m_aKeyIter->second.second.first = 2;
704cdf0e10cSrcweir m_aKeyIter->second.second.second = xRow;
705cdf0e10cSrcweir copyRowValue(_rInsertRow,m_aKeyIter->second.first,nBookmark);
706cdf0e10cSrcweir tryRefetch(_rInsertRow,bRefetch);
707cdf0e10cSrcweir }
708cdf0e10cSrcweir }
709cdf0e10cSrcweir // -------------------------------------------------------------------------
insertRow(const ORowSetRow & _rInsertRow,const connectivity::OSQLTable & _xTable)710cdf0e10cSrcweir void SAL_CALL OKeySet::insertRow( const ORowSetRow& _rInsertRow,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
711cdf0e10cSrcweir {
712cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::insertRow" );
713cdf0e10cSrcweir ::rtl::OUStringBuffer aSql(::rtl::OUString::createFromAscii("INSERT INTO "));
714cdf0e10cSrcweir Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
715cdf0e10cSrcweir fillTableName(xSet);
716cdf0e10cSrcweir
717cdf0e10cSrcweir aSql.append(m_aComposedTableName);
718cdf0e10cSrcweir aSql.append(::rtl::OUString::createFromAscii(" ( "));
719cdf0e10cSrcweir // set values and column names
720cdf0e10cSrcweir ::rtl::OUStringBuffer aValues(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" VALUES ( ")));
721cdf0e10cSrcweir static ::rtl::OUString aPara(RTL_CONSTASCII_USTRINGPARAM("?,"));
722cdf0e10cSrcweir ::rtl::OUString aQuote = getIdentifierQuoteString();
723cdf0e10cSrcweir static ::rtl::OUString aComma(RTL_CONSTASCII_USTRINGPARAM(","));
724cdf0e10cSrcweir
725cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aIter = m_pColumnNames->begin();
726cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aEnd = m_pColumnNames->end();
727cdf0e10cSrcweir sal_Int32 j = 1;
728cdf0e10cSrcweir bool bRefetch = true;
729cdf0e10cSrcweir sal_Bool bModified = sal_False;
730cdf0e10cSrcweir for(;aIter != aEnd;++aIter,++j)
731cdf0e10cSrcweir {
732cdf0e10cSrcweir if((_rInsertRow->get())[aIter->second.nPosition].isModified())
733cdf0e10cSrcweir {
734cdf0e10cSrcweir if ( bRefetch )
735cdf0e10cSrcweir {
736cdf0e10cSrcweir bRefetch = ::std::find(m_aFilterColumns.begin(),m_aFilterColumns.end(),aIter->second.sRealName) == m_aFilterColumns.end();
737cdf0e10cSrcweir }
738cdf0e10cSrcweir aSql.append(::dbtools::quoteName( aQuote,aIter->second.sRealName));
739cdf0e10cSrcweir aSql.append(aComma);
740cdf0e10cSrcweir aValues.append(aPara);
741cdf0e10cSrcweir bModified = sal_True;
742cdf0e10cSrcweir }
743cdf0e10cSrcweir }
744cdf0e10cSrcweir if ( !bModified )
745cdf0e10cSrcweir ::dbtools::throwSQLException( DBACORE_RESSTRING( RID_STR_NO_VALUE_CHANGED ), SQL_GENERAL_ERROR, m_xConnection );
746cdf0e10cSrcweir
747cdf0e10cSrcweir aSql.setCharAt(aSql.getLength()-1,')');
748cdf0e10cSrcweir aValues.setCharAt(aValues.getLength()-1,')');
749cdf0e10cSrcweir aSql.append(aValues.makeStringAndClear());
750cdf0e10cSrcweir // now create,fill and execute the prepared statement
751cdf0e10cSrcweir ::rtl::OUString sEmpty;
752cdf0e10cSrcweir executeInsert(_rInsertRow,aSql.makeStringAndClear(),sEmpty,bRefetch);
753cdf0e10cSrcweir }
754cdf0e10cSrcweir // -------------------------------------------------------------------------
executeInsert(const ORowSetRow & _rInsertRow,const::rtl::OUString & i_sSQL,const::rtl::OUString & i_sTableName,bool bRefetch)755cdf0e10cSrcweir void OKeySet::executeInsert( const ORowSetRow& _rInsertRow,const ::rtl::OUString& i_sSQL,const ::rtl::OUString& i_sTableName,bool bRefetch )
756cdf0e10cSrcweir {
757cdf0e10cSrcweir // now create,fill and execute the prepared statement
758cdf0e10cSrcweir Reference< XPreparedStatement > xPrep(m_xConnection->prepareStatement(i_sSQL));
759cdf0e10cSrcweir Reference< XParameters > xParameter(xPrep,UNO_QUERY);
760cdf0e10cSrcweir
761cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aIter = m_pColumnNames->begin();
762cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aEnd = m_pColumnNames->end();
763cdf0e10cSrcweir for(sal_Int32 i = 1;aIter != aEnd;++aIter)
764cdf0e10cSrcweir {
765cdf0e10cSrcweir if ( !i_sTableName.getLength() || aIter->second.sTableName == i_sTableName )
766cdf0e10cSrcweir {
767cdf0e10cSrcweir const sal_Int32 nPos = aIter->second.nPosition;
768cdf0e10cSrcweir if((_rInsertRow->get())[nPos].isModified())
769cdf0e10cSrcweir {
770cdf0e10cSrcweir if((_rInsertRow->get())[nPos].isNull())
771cdf0e10cSrcweir xParameter->setNull(i++,(_rInsertRow->get())[nPos].getTypeKind());
772cdf0e10cSrcweir else
773cdf0e10cSrcweir {
774cdf0e10cSrcweir impl_convertValue_throw(_rInsertRow,aIter->second);
775cdf0e10cSrcweir (_rInsertRow->get())[nPos].setSigned(m_aSignedFlags[nPos-1]);
776cdf0e10cSrcweir setParameter(i++,xParameter,(_rInsertRow->get())[nPos],aIter->second.nType,aIter->second.nScale);
777cdf0e10cSrcweir }
778cdf0e10cSrcweir }
779cdf0e10cSrcweir }
780cdf0e10cSrcweir }
781cdf0e10cSrcweir
782cdf0e10cSrcweir m_bInserted = xPrep->executeUpdate() > 0;
783cdf0e10cSrcweir sal_Bool bAutoValuesFetched = sal_False;
784cdf0e10cSrcweir if ( m_bInserted )
785cdf0e10cSrcweir {
786cdf0e10cSrcweir // first insert the default values into the insertrow
787cdf0e10cSrcweir aIter = m_pColumnNames->begin();
788cdf0e10cSrcweir for(;aIter != aEnd;++aIter)
789cdf0e10cSrcweir {
790cdf0e10cSrcweir if ( !(_rInsertRow->get())[aIter->second.nPosition].isModified() )
791cdf0e10cSrcweir (_rInsertRow->get())[aIter->second.nPosition] = aIter->second.sDefaultValue;
792cdf0e10cSrcweir }
793cdf0e10cSrcweir try
794cdf0e10cSrcweir {
795cdf0e10cSrcweir Reference< XGeneratedResultSet > xGRes(xPrep, UNO_QUERY);
796cdf0e10cSrcweir if ( xGRes.is() )
797cdf0e10cSrcweir {
798cdf0e10cSrcweir Reference< XResultSet > xRes = xGRes->getGeneratedValues();
799cdf0e10cSrcweir Reference< XRow > xRow(xRes,UNO_QUERY);
800cdf0e10cSrcweir if ( xRow.is() && xRes->next() )
801cdf0e10cSrcweir {
802cdf0e10cSrcweir Reference< XResultSetMetaDataSupplier > xMdSup(xRes,UNO_QUERY);
803cdf0e10cSrcweir Reference< XResultSetMetaData > xMd = xMdSup->getMetaData();
804cdf0e10cSrcweir sal_Int32 nColumnCount = xMd->getColumnCount();
805cdf0e10cSrcweir ::std::vector< ::rtl::OUString >::iterator aAutoIter = m_aAutoColumns.begin();
806cdf0e10cSrcweir ::std::vector< ::rtl::OUString >::iterator aAutoEnd = m_aAutoColumns.end();
807cdf0e10cSrcweir for (sal_Int32 i = 1;aAutoIter != aAutoEnd && i <= nColumnCount; ++aAutoIter,++i)
808cdf0e10cSrcweir {
809cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
810cdf0e10cSrcweir ::rtl::OUString sColumnName( xMd->getColumnName(i) );
811cdf0e10cSrcweir #endif
812cdf0e10cSrcweir SelectColumnsMetaData::iterator aFind = m_pKeyColumnNames->find(*aAutoIter);
813cdf0e10cSrcweir if ( aFind != m_pKeyColumnNames->end() )
814cdf0e10cSrcweir (_rInsertRow->get())[aFind->second.nPosition].fill(i,aFind->second.nType,aFind->second.bNullable,xRow);
815cdf0e10cSrcweir }
816cdf0e10cSrcweir bAutoValuesFetched = sal_True;
817cdf0e10cSrcweir }
818cdf0e10cSrcweir }
819cdf0e10cSrcweir }
820cdf0e10cSrcweir catch(Exception&)
821cdf0e10cSrcweir {
822cdf0e10cSrcweir OSL_ENSURE(0,"Could not execute GeneratedKeys() stmt");
823cdf0e10cSrcweir }
824cdf0e10cSrcweir }
825cdf0e10cSrcweir
826cdf0e10cSrcweir ::comphelper::disposeComponent(xPrep);
827cdf0e10cSrcweir
828cdf0e10cSrcweir if ( !i_sTableName.getLength() && !bAutoValuesFetched && m_bInserted )
829cdf0e10cSrcweir {
830cdf0e10cSrcweir // first check if all key column values were set
831cdf0e10cSrcweir const ::rtl::OUString sMax(RTL_CONSTASCII_USTRINGPARAM(" MAX("));
832cdf0e10cSrcweir const ::rtl::OUString sMaxEnd(RTL_CONSTASCII_USTRINGPARAM("),"));
833cdf0e10cSrcweir const ::rtl::OUString sQuote = getIdentifierQuoteString();
834cdf0e10cSrcweir ::rtl::OUString sMaxStmt;
835cdf0e10cSrcweir aEnd = m_pKeyColumnNames->end();
836cdf0e10cSrcweir ::std::vector< ::rtl::OUString >::iterator aAutoIter = m_aAutoColumns.begin();
837cdf0e10cSrcweir ::std::vector< ::rtl::OUString >::iterator aAutoEnd = m_aAutoColumns.end();
838cdf0e10cSrcweir for (;aAutoIter != aAutoEnd; ++aAutoIter)
839cdf0e10cSrcweir {
840cdf0e10cSrcweir // we will only fetch values which are keycolumns
841cdf0e10cSrcweir SelectColumnsMetaData::iterator aFind = m_pKeyColumnNames->find(*aAutoIter);
842cdf0e10cSrcweir if ( aFind != aEnd )
843cdf0e10cSrcweir {
844cdf0e10cSrcweir sMaxStmt += sMax;
845cdf0e10cSrcweir sMaxStmt += ::dbtools::quoteName( sQuote,aFind->second.sRealName
846cdf0e10cSrcweir );
847cdf0e10cSrcweir sMaxStmt += sMaxEnd;
848cdf0e10cSrcweir }
849cdf0e10cSrcweir }
850cdf0e10cSrcweir
851cdf0e10cSrcweir if(sMaxStmt.getLength())
852cdf0e10cSrcweir {
853cdf0e10cSrcweir sMaxStmt = sMaxStmt.replaceAt(sMaxStmt.getLength()-1,1,::rtl::OUString::createFromAscii(" "));
854cdf0e10cSrcweir ::rtl::OUString sStmt = ::rtl::OUString::createFromAscii("SELECT ");
855cdf0e10cSrcweir sStmt += sMaxStmt;
856cdf0e10cSrcweir sStmt += ::rtl::OUString::createFromAscii("FROM ");
857cdf0e10cSrcweir ::rtl::OUString sCatalog,sSchema,sTable;
858cdf0e10cSrcweir ::dbtools::qualifiedNameComponents(m_xConnection->getMetaData(),m_sUpdateTableName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
859cdf0e10cSrcweir sStmt += ::dbtools::composeTableNameForSelect( m_xConnection, sCatalog, sSchema, sTable );
860cdf0e10cSrcweir try
861cdf0e10cSrcweir {
862cdf0e10cSrcweir // now fetch the autoincrement values
863cdf0e10cSrcweir Reference<XStatement> xStatement = m_xConnection->createStatement();
864cdf0e10cSrcweir Reference<XResultSet> xRes = xStatement->executeQuery(sStmt);
865cdf0e10cSrcweir Reference<XRow> xRow(xRes,UNO_QUERY);
866cdf0e10cSrcweir if(xRow.is() && xRes->next())
867cdf0e10cSrcweir {
868cdf0e10cSrcweir aAutoIter = m_aAutoColumns.begin();
869cdf0e10cSrcweir for (sal_Int32 i=1;aAutoIter != aAutoEnd; ++aAutoIter,++i)
870cdf0e10cSrcweir {
871cdf0e10cSrcweir // we will only fetch values which are keycolumns
872cdf0e10cSrcweir SelectColumnsMetaData::iterator aFind = m_pKeyColumnNames->find(*aAutoIter);
873cdf0e10cSrcweir if ( aFind != aEnd )
874cdf0e10cSrcweir (_rInsertRow->get())[aFind->second.nPosition].fill(i,aFind->second.nType,aFind->second.bNullable,xRow);
875cdf0e10cSrcweir }
876cdf0e10cSrcweir }
877cdf0e10cSrcweir ::comphelper::disposeComponent(xStatement);
878cdf0e10cSrcweir }
879cdf0e10cSrcweir catch(SQLException&)
880cdf0e10cSrcweir {
881cdf0e10cSrcweir OSL_ENSURE(0,"Could not fetch with MAX() ");
882cdf0e10cSrcweir }
883cdf0e10cSrcweir }
884cdf0e10cSrcweir }
885cdf0e10cSrcweir if ( m_bInserted )
886cdf0e10cSrcweir {
887cdf0e10cSrcweir OKeySetMatrix::iterator aKeyIter = m_aKeyMap.end();
888cdf0e10cSrcweir --aKeyIter;
889cdf0e10cSrcweir ORowSetRow aKeyRow = new connectivity::ORowVector< ORowSetValue >(m_pKeyColumnNames->size());
890cdf0e10cSrcweir copyRowValue(_rInsertRow,aKeyRow,aKeyIter->first + 1);
891cdf0e10cSrcweir
892cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.insert(OKeySetMatrix::value_type(aKeyIter->first + 1,OKeySetValue(aKeyRow,::std::pair<sal_Int32,Reference<XRow> >(1,NULL)))).first;
893cdf0e10cSrcweir // now we set the bookmark for this row
894cdf0e10cSrcweir (_rInsertRow->get())[0] = makeAny((sal_Int32)m_aKeyIter->first);
895cdf0e10cSrcweir tryRefetch(_rInsertRow,bRefetch);
896cdf0e10cSrcweir }
897cdf0e10cSrcweir }
tryRefetch(const ORowSetRow & _rInsertRow,bool bRefetch)898cdf0e10cSrcweir void OKeySet::tryRefetch(const ORowSetRow& _rInsertRow,bool bRefetch)
899cdf0e10cSrcweir {
900cdf0e10cSrcweir if ( bRefetch )
901cdf0e10cSrcweir {
902cdf0e10cSrcweir // we just areassign the base members
903cdf0e10cSrcweir try
904cdf0e10cSrcweir {
905cdf0e10cSrcweir Reference< XParameters > xParameter(m_xStatement,UNO_QUERY);
906cdf0e10cSrcweir OSL_ENSURE(xParameter.is(),"No Parameter interface!");
907cdf0e10cSrcweir xParameter->clearParameters();
908cdf0e10cSrcweir
909cdf0e10cSrcweir sal_Int32 nPos=1;
910cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aParaIter;
911cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aParaEnd;
912cdf0e10cSrcweir OUpdatedParameter::iterator aUpdateFind = m_aUpdatedParameter.find(m_aKeyIter->first);
913cdf0e10cSrcweir if ( aUpdateFind == m_aUpdatedParameter.end() )
914cdf0e10cSrcweir {
915cdf0e10cSrcweir aParaIter = m_aParameterValueForCache.get().begin();
916cdf0e10cSrcweir aParaEnd = m_aParameterValueForCache.get().end();
917cdf0e10cSrcweir }
918cdf0e10cSrcweir else
919cdf0e10cSrcweir {
920cdf0e10cSrcweir aParaIter = aUpdateFind->second.get().begin();
921cdf0e10cSrcweir aParaEnd = aUpdateFind->second.get().end();
922cdf0e10cSrcweir }
923cdf0e10cSrcweir
924cdf0e10cSrcweir for(++aParaIter;aParaIter != aParaEnd;++aParaIter,++nPos)
925cdf0e10cSrcweir {
926cdf0e10cSrcweir ::dbtools::setObjectWithInfo( xParameter, nPos, aParaIter->makeAny(), aParaIter->getTypeKind() );
927cdf0e10cSrcweir }
928cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aIter2 = m_aKeyIter->second.first->get().begin();
929cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosIter = (*m_pKeyColumnNames).begin();
930cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosEnd = (*m_pKeyColumnNames).end();
931cdf0e10cSrcweir for(;aPosIter != aPosEnd;++aPosIter,++aIter2,++nPos)
932cdf0e10cSrcweir setParameter(nPos,xParameter,*aIter2,aPosIter->second.nType,aPosIter->second.nScale);
933cdf0e10cSrcweir aPosIter = (*m_pForeignColumnNames).begin();
934cdf0e10cSrcweir aPosEnd = (*m_pForeignColumnNames).end();
935cdf0e10cSrcweir for(;aPosIter != aPosEnd;++aPosIter,++aIter2,++nPos)
936cdf0e10cSrcweir setParameter(nPos,xParameter,*aIter2,aPosIter->second.nType,aPosIter->second.nScale);
937cdf0e10cSrcweir
938cdf0e10cSrcweir m_xSet = m_xStatement->executeQuery();
939cdf0e10cSrcweir OSL_ENSURE(m_xSet.is(),"No resultset form statement!");
940cdf0e10cSrcweir bRefetch = m_xSet->next();
941cdf0e10cSrcweir }
942cdf0e10cSrcweir catch(Exception)
943cdf0e10cSrcweir {
944cdf0e10cSrcweir bRefetch = false;
945cdf0e10cSrcweir }
946cdf0e10cSrcweir }
947cdf0e10cSrcweir if ( !bRefetch )
948cdf0e10cSrcweir {
949cdf0e10cSrcweir m_aKeyIter->second.second.second = new OPrivateRow(_rInsertRow->get());
950cdf0e10cSrcweir }
951cdf0e10cSrcweir }
952cdf0e10cSrcweir // -----------------------------------------------------------------------------
copyRowValue(const ORowSetRow & _rInsertRow,ORowSetRow & _rKeyRow,sal_Int32 i_nBookmark)953cdf0e10cSrcweir void OKeySet::copyRowValue(const ORowSetRow& _rInsertRow,ORowSetRow& _rKeyRow,sal_Int32 i_nBookmark)
954cdf0e10cSrcweir {
955cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::copyRowValue" );
956cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::iterator aIter = _rKeyRow->get().begin();
957cdf0e10cSrcweir
958cdf0e10cSrcweir // check the if the parameter values have been changed
959cdf0e10cSrcweir OSL_ENSURE((m_aParameterValueForCache.get().size()-1) == m_pParameterNames->size(),"OKeySet::copyRowValue: Parameter values and names differ!");
960cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aParaValuesIter = m_aParameterValueForCache.get().begin() +1;
961cdf0e10cSrcweir
962cdf0e10cSrcweir bool bChanged = false;
963cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aParaIter = (*m_pParameterNames).begin();
964cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aParaEnd = (*m_pParameterNames).end();
965cdf0e10cSrcweir for(sal_Int32 i = 1;aParaIter != aParaEnd;++aParaIter,++aParaValuesIter,++i)
966cdf0e10cSrcweir {
967cdf0e10cSrcweir ORowSetValue aValue(*aParaValuesIter);
968cdf0e10cSrcweir aValue.setSigned(m_aSignedFlags[aParaIter->second.nPosition]);
969cdf0e10cSrcweir if ( (_rInsertRow->get())[aParaIter->second.nPosition] != aValue )
970cdf0e10cSrcweir {
971cdf0e10cSrcweir ORowSetValueVector aCopy(m_aParameterValueForCache);
972cdf0e10cSrcweir (aCopy.get())[i] = (_rInsertRow->get())[aParaIter->second.nPosition];
973cdf0e10cSrcweir m_aUpdatedParameter[i_nBookmark] = aCopy;
974cdf0e10cSrcweir bChanged = true;
975cdf0e10cSrcweir }
976cdf0e10cSrcweir }
977cdf0e10cSrcweir if ( !bChanged )
978cdf0e10cSrcweir {
979cdf0e10cSrcweir m_aUpdatedParameter.erase(i_nBookmark);
980cdf0e10cSrcweir }
981cdf0e10cSrcweir
982cdf0e10cSrcweir // update the key values
983cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosIter = (*m_pKeyColumnNames).begin();
984cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosEnd = (*m_pKeyColumnNames).end();
985cdf0e10cSrcweir for(;aPosIter != aPosEnd;++aPosIter,++aIter)
986cdf0e10cSrcweir {
987cdf0e10cSrcweir impl_convertValue_throw(_rInsertRow,aPosIter->second);
988cdf0e10cSrcweir *aIter = (_rInsertRow->get())[aPosIter->second.nPosition];
989cdf0e10cSrcweir aIter->setTypeKind(aPosIter->second.nType);
990cdf0e10cSrcweir }
991cdf0e10cSrcweir }
992cdf0e10cSrcweir // -------------------------------------------------------------------------
deleteRow(const ORowSetRow & _rDeleteRow,const connectivity::OSQLTable & _xTable)993cdf0e10cSrcweir void SAL_CALL OKeySet::deleteRow(const ORowSetRow& _rDeleteRow,const connectivity::OSQLTable& _xTable ) throw(SQLException, RuntimeException)
994cdf0e10cSrcweir {
995cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::deleteRow" );
996cdf0e10cSrcweir Reference<XPropertySet> xSet(_xTable,UNO_QUERY);
997cdf0e10cSrcweir fillTableName(xSet);
998cdf0e10cSrcweir
999cdf0e10cSrcweir ::rtl::OUStringBuffer aSql = ::rtl::OUString::createFromAscii("DELETE FROM ");
1000cdf0e10cSrcweir aSql.append(m_aComposedTableName);
1001cdf0e10cSrcweir aSql.append(::rtl::OUString::createFromAscii(" WHERE "));
1002cdf0e10cSrcweir
1003cdf0e10cSrcweir // list all cloumns that should be set
1004cdf0e10cSrcweir ::rtl::OUString aQuote = getIdentifierQuoteString();
1005cdf0e10cSrcweir static ::rtl::OUString aAnd = ::rtl::OUString::createFromAscii(" AND ");
1006cdf0e10cSrcweir
1007cdf0e10cSrcweir // use keys and indexes for excat postioning
1008cdf0e10cSrcweir Reference<XNameAccess> xKeyColumns = getKeyColumns();
1009cdf0e10cSrcweir // second the indexes
1010cdf0e10cSrcweir Reference<XIndexesSupplier> xIndexSup(_xTable,UNO_QUERY);
1011cdf0e10cSrcweir Reference<XIndexAccess> xIndexes;
1012cdf0e10cSrcweir if ( xIndexSup.is() )
1013cdf0e10cSrcweir xIndexes.set(xIndexSup->getIndexes(),UNO_QUERY);
1014cdf0e10cSrcweir
1015cdf0e10cSrcweir // Reference<XColumnsSupplier>
1016cdf0e10cSrcweir ::std::vector< Reference<XNameAccess> > aAllIndexColumns;
1017cdf0e10cSrcweir lcl_fillIndexColumns(xIndexes,aAllIndexColumns);
1018cdf0e10cSrcweir
1019cdf0e10cSrcweir ::rtl::OUString aColumnName;
1020cdf0e10cSrcweir ::rtl::OUStringBuffer sIndexCondition;
1021cdf0e10cSrcweir ::std::vector<sal_Int32> aIndexColumnPositions;
1022cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aIter = m_pColumnNames->begin();
1023cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aEnd = m_pColumnNames->end();
1024cdf0e10cSrcweir
1025cdf0e10cSrcweir sal_Int32 i = 1;
1026cdf0e10cSrcweir for(i = 1;aIter != aEnd;++aIter,++i)
1027cdf0e10cSrcweir {
1028cdf0e10cSrcweir if ( m_pKeyColumnNames->find(aIter->first) != m_pKeyColumnNames->end() )
1029cdf0e10cSrcweir {
1030cdf0e10cSrcweir aSql.append(::dbtools::quoteName( aQuote,aIter->second.sRealName));
1031cdf0e10cSrcweir if((_rDeleteRow->get())[aIter->second.nPosition].isNull())
1032cdf0e10cSrcweir {
1033cdf0e10cSrcweir OSL_ENSURE(0,"can a primary key be null");
1034cdf0e10cSrcweir aSql.append(::rtl::OUString::createFromAscii(" IS NULL"));
1035cdf0e10cSrcweir }
1036cdf0e10cSrcweir else
1037cdf0e10cSrcweir aSql.append(::rtl::OUString::createFromAscii(" = ?"));
1038cdf0e10cSrcweir aSql.append(aAnd);
1039cdf0e10cSrcweir }
1040cdf0e10cSrcweir else
1041cdf0e10cSrcweir {
1042cdf0e10cSrcweir ::std::vector< Reference<XNameAccess> >::const_iterator aIndexEnd = aAllIndexColumns.end();
1043cdf0e10cSrcweir for( ::std::vector< Reference<XNameAccess> >::const_iterator aIndexIter = aAllIndexColumns.begin();
1044cdf0e10cSrcweir aIndexIter != aIndexEnd;++aIndexIter)
1045cdf0e10cSrcweir {
1046cdf0e10cSrcweir if((*aIndexIter)->hasByName(aIter->first))
1047cdf0e10cSrcweir {
1048cdf0e10cSrcweir sIndexCondition.append(::dbtools::quoteName( aQuote,aIter->second.sRealName));
1049cdf0e10cSrcweir if((_rDeleteRow->get())[aIter->second.nPosition].isNull())
1050cdf0e10cSrcweir sIndexCondition.append(::rtl::OUString::createFromAscii(" IS NULL"));
1051cdf0e10cSrcweir else
1052cdf0e10cSrcweir {
1053cdf0e10cSrcweir sIndexCondition.append(::rtl::OUString::createFromAscii(" = ?"));
1054cdf0e10cSrcweir aIndexColumnPositions.push_back(aIter->second.nPosition);
1055cdf0e10cSrcweir }
1056cdf0e10cSrcweir sIndexCondition.append(aAnd);
1057cdf0e10cSrcweir
1058cdf0e10cSrcweir break;
1059cdf0e10cSrcweir }
1060cdf0e10cSrcweir }
1061cdf0e10cSrcweir }
1062cdf0e10cSrcweir }
1063cdf0e10cSrcweir aSql.append(sIndexCondition.makeStringAndClear());
1064cdf0e10cSrcweir aSql.setLength(aSql.getLength()-5);
1065cdf0e10cSrcweir
1066cdf0e10cSrcweir // now create end execute the prepared statement
1067cdf0e10cSrcweir Reference< XPreparedStatement > xPrep(m_xConnection->prepareStatement(aSql.makeStringAndClear()));
1068cdf0e10cSrcweir Reference< XParameters > xParameter(xPrep,UNO_QUERY);
1069cdf0e10cSrcweir
1070cdf0e10cSrcweir aIter = (*m_pKeyColumnNames).begin();
1071cdf0e10cSrcweir aEnd = (*m_pKeyColumnNames).end();
1072cdf0e10cSrcweir i = 1;
1073cdf0e10cSrcweir for(;aIter != aEnd;++aIter,++i)
1074cdf0e10cSrcweir {
1075cdf0e10cSrcweir setParameter(i,xParameter,(_rDeleteRow->get())[aIter->second.nPosition],aIter->second.nType,aIter->second.nScale);
1076cdf0e10cSrcweir }
1077cdf0e10cSrcweir
1078cdf0e10cSrcweir // now we have to set the index values
1079cdf0e10cSrcweir ::std::vector<sal_Int32>::iterator aIdxColIter = aIndexColumnPositions.begin();
1080cdf0e10cSrcweir ::std::vector<sal_Int32>::iterator aIdxColEnd = aIndexColumnPositions.end();
1081cdf0e10cSrcweir aIter = m_pColumnNames->begin();
1082cdf0e10cSrcweir for(;aIdxColIter != aIdxColEnd;++aIdxColIter,++i,++aIter)
1083cdf0e10cSrcweir {
1084cdf0e10cSrcweir setParameter(i,xParameter,(_rDeleteRow->get())[*aIdxColIter],(_rDeleteRow->get())[*aIdxColIter].getTypeKind(),aIter->second.nScale);
1085cdf0e10cSrcweir }
1086cdf0e10cSrcweir
1087cdf0e10cSrcweir m_bDeleted = xPrep->executeUpdate() > 0;
1088cdf0e10cSrcweir
1089cdf0e10cSrcweir if(m_bDeleted)
1090cdf0e10cSrcweir {
1091cdf0e10cSrcweir sal_Int32 nBookmark = ::comphelper::getINT32((_rDeleteRow->get())[0].getAny());
1092cdf0e10cSrcweir if(m_aKeyIter == m_aKeyMap.find(nBookmark) && m_aKeyIter != m_aKeyMap.end())
1093cdf0e10cSrcweir ++m_aKeyIter;
1094cdf0e10cSrcweir m_aKeyMap.erase(nBookmark);
1095cdf0e10cSrcweir m_bDeleted = sal_True;
1096cdf0e10cSrcweir }
1097cdf0e10cSrcweir }
1098cdf0e10cSrcweir // -------------------------------------------------------------------------
cancelRowUpdates()1099cdf0e10cSrcweir void SAL_CALL OKeySet::cancelRowUpdates( ) throw(SQLException, RuntimeException)
1100cdf0e10cSrcweir {
1101cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::cancelRowUpdates" );
1102cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1103cdf0e10cSrcweir }
1104cdf0e10cSrcweir // -------------------------------------------------------------------------
moveToInsertRow()1105cdf0e10cSrcweir void SAL_CALL OKeySet::moveToInsertRow( ) throw(SQLException, RuntimeException)
1106cdf0e10cSrcweir {
1107cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::moveToInsertRow" );
1108cdf0e10cSrcweir }
1109cdf0e10cSrcweir // -------------------------------------------------------------------------
moveToCurrentRow()1110cdf0e10cSrcweir void SAL_CALL OKeySet::moveToCurrentRow( ) throw(SQLException, RuntimeException)
1111cdf0e10cSrcweir {
1112cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::moveToCurrentRow" );
1113cdf0e10cSrcweir }
1114cdf0e10cSrcweir // -------------------------------------------------------------------------
getKeyColumns() const1115cdf0e10cSrcweir Reference<XNameAccess> OKeySet::getKeyColumns() const
1116cdf0e10cSrcweir {
1117cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getKeyColumns" );
1118cdf0e10cSrcweir // use keys and indexes for excat postioning
1119cdf0e10cSrcweir // first the keys
1120cdf0e10cSrcweir
1121cdf0e10cSrcweir Reference<XIndexAccess> xKeys = m_xTableKeys;
1122cdf0e10cSrcweir if ( !xKeys.is() )
1123cdf0e10cSrcweir {
1124cdf0e10cSrcweir Reference<XPropertySet> xSet(m_xTable,UNO_QUERY);
1125cdf0e10cSrcweir const Reference<XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(xSet);
1126cdf0e10cSrcweir return xPrimaryKeyColumns;
1127cdf0e10cSrcweir }
1128cdf0e10cSrcweir
1129cdf0e10cSrcweir Reference<XColumnsSupplier> xKeyColsSup;
1130cdf0e10cSrcweir Reference<XNameAccess> xKeyColumns;
1131cdf0e10cSrcweir if(xKeys.is())
1132cdf0e10cSrcweir {
1133cdf0e10cSrcweir Reference<XPropertySet> xProp;
1134cdf0e10cSrcweir sal_Int32 nCount = xKeys->getCount();
1135cdf0e10cSrcweir for(sal_Int32 i = 0;i< nCount;++i)
1136cdf0e10cSrcweir {
1137cdf0e10cSrcweir xProp.set(xKeys->getByIndex(i),UNO_QUERY);
1138cdf0e10cSrcweir if ( xProp.is() )
1139cdf0e10cSrcweir {
1140cdf0e10cSrcweir sal_Int32 nKeyType = 0;
1141cdf0e10cSrcweir xProp->getPropertyValue(PROPERTY_TYPE) >>= nKeyType;
1142cdf0e10cSrcweir if(KeyType::PRIMARY == nKeyType)
1143cdf0e10cSrcweir {
1144cdf0e10cSrcweir xKeyColsSup.set(xProp,UNO_QUERY);
1145cdf0e10cSrcweir OSL_ENSURE(xKeyColsSup.is(),"Columnsupplier is null!");
1146cdf0e10cSrcweir xKeyColumns = xKeyColsSup->getColumns();
1147cdf0e10cSrcweir break;
1148cdf0e10cSrcweir }
1149cdf0e10cSrcweir }
1150cdf0e10cSrcweir }
1151cdf0e10cSrcweir }
1152cdf0e10cSrcweir
1153cdf0e10cSrcweir return xKeyColumns;
1154cdf0e10cSrcweir }
1155cdf0e10cSrcweir // -----------------------------------------------------------------------------
next()1156cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::next( ) throw(SQLException, RuntimeException)
1157cdf0e10cSrcweir {
1158cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::next" );
1159cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1160cdf0e10cSrcweir
1161cdf0e10cSrcweir if(isAfterLast())
1162cdf0e10cSrcweir return sal_False;
1163cdf0e10cSrcweir if(!m_bRowCountFinal) // not yet all records fetched
1164cdf0e10cSrcweir {
1165cdf0e10cSrcweir ++m_aKeyIter; // this is possible because we stand on begin() and this is the "beforefirst" row
1166cdf0e10cSrcweir if(m_aKeyIter == m_aKeyMap.end() && !fetchRow())
1167cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.end();
1168cdf0e10cSrcweir else
1169cdf0e10cSrcweir {
1170cdf0e10cSrcweir //m_aKeyIter->second.second.second = new OPrivateRow(_rInsertRow->get());
1171cdf0e10cSrcweir m_xRow.set(m_xDriverRow,UNO_QUERY_THROW);
1172cdf0e10cSrcweir return !isAfterLast();
1173cdf0e10cSrcweir }
1174cdf0e10cSrcweir }
1175cdf0e10cSrcweir else if(!isAfterLast())
1176cdf0e10cSrcweir ++m_aKeyIter;
1177cdf0e10cSrcweir
1178cdf0e10cSrcweir refreshRow();
1179cdf0e10cSrcweir return !isAfterLast();
1180cdf0e10cSrcweir }
1181cdf0e10cSrcweir // -----------------------------------------------------------------------------
isBeforeFirst()1182cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::isBeforeFirst( ) throw(SQLException, RuntimeException)
1183cdf0e10cSrcweir {
1184cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::isBeforeFirst" );
1185cdf0e10cSrcweir return m_aKeyIter == m_aKeyMap.begin();
1186cdf0e10cSrcweir }
1187cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAfterLast()1188cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::isAfterLast( ) throw(SQLException, RuntimeException)
1189cdf0e10cSrcweir {
1190cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::isAfterLast" );
1191cdf0e10cSrcweir return m_bRowCountFinal && m_aKeyIter == m_aKeyMap.end();
1192cdf0e10cSrcweir }
1193cdf0e10cSrcweir // -----------------------------------------------------------------------------
isFirst()1194cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::isFirst( ) throw(SQLException, RuntimeException)
1195cdf0e10cSrcweir {
1196cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::isFirst" );
1197cdf0e10cSrcweir OKeySetMatrix::iterator aTemp = m_aKeyMap.begin();
1198cdf0e10cSrcweir ++aTemp;
1199cdf0e10cSrcweir return m_aKeyIter == aTemp && m_aKeyIter != m_aKeyMap.end();
1200cdf0e10cSrcweir }
1201cdf0e10cSrcweir // -----------------------------------------------------------------------------
isLast()1202cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::isLast( ) throw(SQLException, RuntimeException)
1203cdf0e10cSrcweir {
1204cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::isLast" );
1205cdf0e10cSrcweir if(!m_bRowCountFinal)
1206cdf0e10cSrcweir return sal_False;
1207cdf0e10cSrcweir
1208cdf0e10cSrcweir OKeySetMatrix::iterator aTemp = m_aKeyMap.end();
1209cdf0e10cSrcweir --aTemp;
1210cdf0e10cSrcweir return m_aKeyIter == aTemp;
1211cdf0e10cSrcweir }
1212cdf0e10cSrcweir // -----------------------------------------------------------------------------
beforeFirst()1213cdf0e10cSrcweir void SAL_CALL OKeySet::beforeFirst( ) throw(SQLException, RuntimeException)
1214cdf0e10cSrcweir {
1215cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::beforeFirst" );
1216cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1217cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.begin();
1218cdf0e10cSrcweir m_xRow = NULL;
1219cdf0e10cSrcweir ::comphelper::disposeComponent(m_xSet);
1220cdf0e10cSrcweir }
1221cdf0e10cSrcweir // -----------------------------------------------------------------------------
afterLast()1222cdf0e10cSrcweir void SAL_CALL OKeySet::afterLast( ) throw(SQLException, RuntimeException)
1223cdf0e10cSrcweir {
1224cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::afterLast" );
1225cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1226cdf0e10cSrcweir fillAllRows();
1227cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.end();
1228cdf0e10cSrcweir m_xRow = NULL;
1229cdf0e10cSrcweir ::comphelper::disposeComponent(m_xSet);
1230cdf0e10cSrcweir }
1231cdf0e10cSrcweir // -----------------------------------------------------------------------------
first()1232cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::first( ) throw(SQLException, RuntimeException)
1233cdf0e10cSrcweir {
1234cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::first" );
1235cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1236cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.begin();
1237cdf0e10cSrcweir ++m_aKeyIter;
1238cdf0e10cSrcweir if(m_aKeyIter == m_aKeyMap.end() && !fetchRow())
1239cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.end();
1240cdf0e10cSrcweir else
1241cdf0e10cSrcweir refreshRow();
1242cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1243cdf0e10cSrcweir }
1244cdf0e10cSrcweir // -----------------------------------------------------------------------------
last()1245cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::last( ) throw(SQLException, RuntimeException)
1246cdf0e10cSrcweir {
1247cdf0e10cSrcweir return last_checked(sal_True);
1248cdf0e10cSrcweir }
1249cdf0e10cSrcweir
last_checked(sal_Bool i_bFetchRow)1250cdf0e10cSrcweir sal_Bool OKeySet::last_checked( sal_Bool i_bFetchRow)
1251cdf0e10cSrcweir {
1252cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::last_checked" );
1253cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1254cdf0e10cSrcweir fillAllRows();
1255cdf0e10cSrcweir
1256cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.end();
1257cdf0e10cSrcweir --m_aKeyIter;
1258cdf0e10cSrcweir if ( i_bFetchRow )
1259cdf0e10cSrcweir refreshRow();
1260cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1261cdf0e10cSrcweir }
1262cdf0e10cSrcweir // -----------------------------------------------------------------------------
getRow()1263cdf0e10cSrcweir sal_Int32 SAL_CALL OKeySet::getRow( ) throw(SQLException, RuntimeException)
1264cdf0e10cSrcweir {
1265cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getRow" );
1266cdf0e10cSrcweir OSL_ENSURE(!isAfterLast(),"getRow is not allowed when afterlast record!");
1267cdf0e10cSrcweir OSL_ENSURE(!isBeforeFirst(),"getRow is not allowed when beforefirst record!");
1268cdf0e10cSrcweir
1269cdf0e10cSrcweir return ::std::distance(m_aKeyMap.begin(),m_aKeyIter);
1270cdf0e10cSrcweir }
1271cdf0e10cSrcweir // -----------------------------------------------------------------------------
absolute(sal_Int32 row)1272cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::absolute( sal_Int32 row ) throw(SQLException, RuntimeException)
1273cdf0e10cSrcweir {
1274cdf0e10cSrcweir return absolute_checked(row,sal_True);
1275cdf0e10cSrcweir }
absolute_checked(sal_Int32 row,sal_Bool i_bFetchRow)1276cdf0e10cSrcweir sal_Bool OKeySet::absolute_checked( sal_Int32 row,sal_Bool i_bFetchRow )
1277cdf0e10cSrcweir {
1278cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::absolute" );
1279cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1280cdf0e10cSrcweir OSL_ENSURE(row,"absolute(0) isn't allowed!");
1281cdf0e10cSrcweir if(row < 0)
1282cdf0e10cSrcweir {
1283cdf0e10cSrcweir if(!m_bRowCountFinal)
1284cdf0e10cSrcweir fillAllRows();
1285cdf0e10cSrcweir
1286cdf0e10cSrcweir for(;row < 0 && m_aKeyIter != m_aKeyMap.begin();++row)
1287cdf0e10cSrcweir m_aKeyIter--;
1288cdf0e10cSrcweir }
1289cdf0e10cSrcweir else
1290cdf0e10cSrcweir {
1291cdf0e10cSrcweir if(row >= (sal_Int32)m_aKeyMap.size())
1292cdf0e10cSrcweir {
1293cdf0e10cSrcweir if(!m_bRowCountFinal)
1294cdf0e10cSrcweir {
1295cdf0e10cSrcweir sal_Bool bNext = sal_True;
1296cdf0e10cSrcweir for(sal_Int32 i=m_aKeyMap.size()-1;i < row && bNext;++i)
1297cdf0e10cSrcweir bNext = fetchRow();
1298cdf0e10cSrcweir if ( bNext )
1299cdf0e10cSrcweir {
1300cdf0e10cSrcweir m_xRow.set(m_xDriverRow,UNO_QUERY_THROW);
1301cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1302cdf0e10cSrcweir }
1303cdf0e10cSrcweir }
1304cdf0e10cSrcweir else
1305cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.end();
1306cdf0e10cSrcweir }
1307cdf0e10cSrcweir else
1308cdf0e10cSrcweir {
1309cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.begin();
1310cdf0e10cSrcweir for(;row > 0 && m_aKeyIter != m_aKeyMap.end();--row)
1311cdf0e10cSrcweir ++m_aKeyIter;
1312cdf0e10cSrcweir }
1313cdf0e10cSrcweir }
1314cdf0e10cSrcweir if ( i_bFetchRow )
1315cdf0e10cSrcweir refreshRow();
1316cdf0e10cSrcweir
1317cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.end() && m_aKeyIter != m_aKeyMap.begin();
1318cdf0e10cSrcweir }
1319cdf0e10cSrcweir // -----------------------------------------------------------------------------
relative(sal_Int32 rows)1320cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::relative( sal_Int32 rows ) throw(SQLException, RuntimeException)
1321cdf0e10cSrcweir {
1322cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::relative" );
1323cdf0e10cSrcweir if(!rows)
1324cdf0e10cSrcweir {
1325cdf0e10cSrcweir refreshRow();
1326cdf0e10cSrcweir return sal_True;
1327cdf0e10cSrcweir }
1328cdf0e10cSrcweir return absolute(getRow()+rows);
1329cdf0e10cSrcweir }
1330cdf0e10cSrcweir // -----------------------------------------------------------------------------
previous_checked(sal_Bool i_bFetchRow)1331cdf0e10cSrcweir sal_Bool OKeySet::previous_checked( sal_Bool i_bFetchRow )
1332cdf0e10cSrcweir {
1333cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::previous" );
1334cdf0e10cSrcweir m_bInserted = m_bUpdated = m_bDeleted = sal_False;
1335cdf0e10cSrcweir if(m_aKeyIter != m_aKeyMap.begin())
1336cdf0e10cSrcweir {
1337cdf0e10cSrcweir --m_aKeyIter;
1338cdf0e10cSrcweir if ( i_bFetchRow )
1339cdf0e10cSrcweir refreshRow();
1340cdf0e10cSrcweir }
1341cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.begin();
1342cdf0e10cSrcweir }
1343cdf0e10cSrcweir // -----------------------------------------------------------------------------
previous()1344cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::previous( ) throw(SQLException, RuntimeException)
1345cdf0e10cSrcweir {
1346cdf0e10cSrcweir return previous_checked(sal_True);
1347cdf0e10cSrcweir }
1348cdf0e10cSrcweir
1349cdf0e10cSrcweir // -----------------------------------------------------------------------------
refreshRow()1350cdf0e10cSrcweir void SAL_CALL OKeySet::refreshRow() throw(SQLException, RuntimeException)
1351cdf0e10cSrcweir {
1352cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::refreshRow" );
1353cdf0e10cSrcweir if(isBeforeFirst() || isAfterLast() || !m_xStatement.is())
1354cdf0e10cSrcweir return;
1355cdf0e10cSrcweir
1356cdf0e10cSrcweir m_xRow = NULL;
1357cdf0e10cSrcweir ::comphelper::disposeComponent(m_xSet);
1358cdf0e10cSrcweir
1359cdf0e10cSrcweir if ( m_aKeyIter->second.second.second.is() )
1360cdf0e10cSrcweir {
1361cdf0e10cSrcweir m_xRow = m_aKeyIter->second.second.second;
1362cdf0e10cSrcweir return;
1363cdf0e10cSrcweir }
1364cdf0e10cSrcweir // we just areassign the base members
1365cdf0e10cSrcweir Reference< XParameters > xParameter(m_xStatement,UNO_QUERY);
1366cdf0e10cSrcweir OSL_ENSURE(xParameter.is(),"No Parameter interface!");
1367cdf0e10cSrcweir xParameter->clearParameters();
1368cdf0e10cSrcweir
1369cdf0e10cSrcweir sal_Int32 nPos=1;
1370cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aParaIter;
1371cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aParaEnd;
1372cdf0e10cSrcweir OUpdatedParameter::iterator aUpdateFind = m_aUpdatedParameter.find(m_aKeyIter->first);
1373cdf0e10cSrcweir if ( aUpdateFind == m_aUpdatedParameter.end() )
1374cdf0e10cSrcweir {
1375cdf0e10cSrcweir aParaIter = m_aParameterValueForCache.get().begin();
1376cdf0e10cSrcweir aParaEnd = m_aParameterValueForCache.get().end();
1377cdf0e10cSrcweir }
1378cdf0e10cSrcweir else
1379cdf0e10cSrcweir {
1380cdf0e10cSrcweir aParaIter = aUpdateFind->second.get().begin();
1381cdf0e10cSrcweir aParaEnd = aUpdateFind->second.get().end();
1382cdf0e10cSrcweir }
1383cdf0e10cSrcweir
1384cdf0e10cSrcweir for(++aParaIter;aParaIter != aParaEnd;++aParaIter,++nPos)
1385cdf0e10cSrcweir {
1386cdf0e10cSrcweir ::dbtools::setObjectWithInfo( xParameter, nPos, aParaIter->makeAny(), aParaIter->getTypeKind() );
1387cdf0e10cSrcweir }
1388cdf0e10cSrcweir
1389cdf0e10cSrcweir // now set the primary key column values
1390cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::const_iterator aIter = m_aKeyIter->second.first->get().begin();
1391cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosIter = (*m_pKeyColumnNames).begin();
1392cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosEnd = (*m_pKeyColumnNames).end();
1393cdf0e10cSrcweir for(;aPosIter != aPosEnd;++aPosIter,++aIter,++nPos)
1394cdf0e10cSrcweir setParameter(nPos,xParameter,*aIter,aPosIter->second.nType,aPosIter->second.nScale);
1395cdf0e10cSrcweir aPosIter = (*m_pForeignColumnNames).begin();
1396cdf0e10cSrcweir aPosEnd = (*m_pForeignColumnNames).end();
1397cdf0e10cSrcweir for(;aPosIter != aPosEnd;++aPosIter,++aIter,++nPos)
1398cdf0e10cSrcweir setParameter(nPos,xParameter,*aIter,aPosIter->second.nType,aPosIter->second.nScale);
1399cdf0e10cSrcweir
1400cdf0e10cSrcweir m_xSet = m_xStatement->executeQuery();
1401cdf0e10cSrcweir OSL_ENSURE(m_xSet.is(),"No resultset form statement!");
1402cdf0e10cSrcweir sal_Bool bOK = m_xSet->next();
1403cdf0e10cSrcweir if ( !bOK )
1404cdf0e10cSrcweir {
1405cdf0e10cSrcweir OKeySetMatrix::iterator aTemp = m_aKeyIter;
1406cdf0e10cSrcweir ++m_aKeyIter;
1407cdf0e10cSrcweir m_aKeyMap.erase(aTemp);
1408cdf0e10cSrcweir --m_rRowCount;
1409cdf0e10cSrcweir refreshRow();
1410cdf0e10cSrcweir }
1411cdf0e10cSrcweir else
1412cdf0e10cSrcweir {
1413cdf0e10cSrcweir m_xRow.set(m_xSet,UNO_QUERY);
1414cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"No row form statement!");
1415cdf0e10cSrcweir }
1416cdf0e10cSrcweir }
1417cdf0e10cSrcweir // -----------------------------------------------------------------------------
fetchRow()1418cdf0e10cSrcweir sal_Bool OKeySet::fetchRow()
1419cdf0e10cSrcweir {
1420cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::fetchRow" );
1421cdf0e10cSrcweir // fetch the next row and append on the keyset
1422cdf0e10cSrcweir sal_Bool bRet = sal_False;
1423cdf0e10cSrcweir if ( !m_bRowCountFinal && (!m_nMaxRows || sal_Int32(m_aKeyMap.size()) < m_nMaxRows) )
1424cdf0e10cSrcweir bRet = m_xDriverSet->next();
1425cdf0e10cSrcweir if ( bRet )
1426cdf0e10cSrcweir {
1427cdf0e10cSrcweir ORowSetRow aKeyRow = new connectivity::ORowVector< ORowSetValue >((*m_pKeyColumnNames).size() + m_pForeignColumnNames->size());
1428cdf0e10cSrcweir connectivity::ORowVector< ORowSetValue >::Vector::iterator aIter = aKeyRow->get().begin();
1429cdf0e10cSrcweir // first fetch the values needed for the key column
1430cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosIter = (*m_pKeyColumnNames).begin();
1431cdf0e10cSrcweir SelectColumnsMetaData::const_iterator aPosEnd = (*m_pKeyColumnNames).end();
1432cdf0e10cSrcweir for(;aPosIter != aPosEnd;++aPosIter,++aIter)
1433cdf0e10cSrcweir {
1434cdf0e10cSrcweir const SelectColumnDescription& rColDesc = aPosIter->second;
1435cdf0e10cSrcweir aIter->fill(rColDesc.nPosition,rColDesc.nType,rColDesc.bNullable,m_xDriverRow);
1436cdf0e10cSrcweir }
1437cdf0e10cSrcweir // now fetch the values from the missing columns from other tables
1438cdf0e10cSrcweir aPosIter = (*m_pForeignColumnNames).begin();
1439cdf0e10cSrcweir aPosEnd = (*m_pForeignColumnNames).end();
1440cdf0e10cSrcweir for(;aPosIter != aPosEnd;++aPosIter,++aIter)
1441cdf0e10cSrcweir {
1442cdf0e10cSrcweir const SelectColumnDescription& rColDesc = aPosIter->second;
1443cdf0e10cSrcweir aIter->fill(rColDesc.nPosition,rColDesc.nType,rColDesc.bNullable,m_xDriverRow);
1444cdf0e10cSrcweir }
1445cdf0e10cSrcweir m_aKeyIter = m_aKeyMap.insert(OKeySetMatrix::value_type(m_aKeyMap.rbegin()->first+1,OKeySetValue(aKeyRow,::std::pair<sal_Int32,Reference<XRow> >(0,NULL)))).first;
1446cdf0e10cSrcweir }
1447cdf0e10cSrcweir else
1448cdf0e10cSrcweir m_bRowCountFinal = sal_True;
1449cdf0e10cSrcweir return bRet;
1450cdf0e10cSrcweir }
1451cdf0e10cSrcweir // -------------------------------------------------------------------------
fillAllRows()1452cdf0e10cSrcweir void OKeySet::fillAllRows()
1453cdf0e10cSrcweir {
1454cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::fillAllRows" );
1455cdf0e10cSrcweir if(!m_bRowCountFinal)
1456cdf0e10cSrcweir {
1457cdf0e10cSrcweir while(fetchRow())
1458cdf0e10cSrcweir ;
1459cdf0e10cSrcweir }
1460cdf0e10cSrcweir }
1461cdf0e10cSrcweir // XRow
wasNull()1462cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::wasNull( ) throw(SQLException, RuntimeException)
1463cdf0e10cSrcweir {
1464cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::wasNull" );
1465cdf0e10cSrcweir return m_xRow->wasNull();
1466cdf0e10cSrcweir }
1467cdf0e10cSrcweir // -------------------------------------------------------------------------
getString(sal_Int32 columnIndex)1468cdf0e10cSrcweir ::rtl::OUString SAL_CALL OKeySet::getString( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1469cdf0e10cSrcweir {
1470cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getString" );
1471cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1472cdf0e10cSrcweir return m_xRow->getString(columnIndex);
1473cdf0e10cSrcweir }
1474cdf0e10cSrcweir // -------------------------------------------------------------------------
getBoolean(sal_Int32 columnIndex)1475cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::getBoolean( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1476cdf0e10cSrcweir {
1477cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getBoolean" );
1478cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1479cdf0e10cSrcweir return m_xRow->getBoolean(columnIndex);
1480cdf0e10cSrcweir }
1481cdf0e10cSrcweir // -------------------------------------------------------------------------
getByte(sal_Int32 columnIndex)1482cdf0e10cSrcweir sal_Int8 SAL_CALL OKeySet::getByte( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1483cdf0e10cSrcweir {
1484cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getByte" );
1485cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1486cdf0e10cSrcweir return m_xRow->getByte(columnIndex);
1487cdf0e10cSrcweir }
1488cdf0e10cSrcweir // -------------------------------------------------------------------------
getShort(sal_Int32 columnIndex)1489cdf0e10cSrcweir sal_Int16 SAL_CALL OKeySet::getShort( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1490cdf0e10cSrcweir {
1491cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getShort" );
1492cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1493cdf0e10cSrcweir return m_xRow->getShort(columnIndex);
1494cdf0e10cSrcweir }
1495cdf0e10cSrcweir // -------------------------------------------------------------------------
getInt(sal_Int32 columnIndex)1496cdf0e10cSrcweir sal_Int32 SAL_CALL OKeySet::getInt( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1497cdf0e10cSrcweir {
1498cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getInt" );
1499cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1500cdf0e10cSrcweir return m_xRow->getInt(columnIndex);
1501cdf0e10cSrcweir }
1502cdf0e10cSrcweir // -------------------------------------------------------------------------
getLong(sal_Int32 columnIndex)1503cdf0e10cSrcweir sal_Int64 SAL_CALL OKeySet::getLong( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1504cdf0e10cSrcweir {
1505cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getLong" );
1506cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1507cdf0e10cSrcweir return m_xRow->getLong(columnIndex);
1508cdf0e10cSrcweir }
1509cdf0e10cSrcweir // -------------------------------------------------------------------------
getFloat(sal_Int32 columnIndex)1510cdf0e10cSrcweir float SAL_CALL OKeySet::getFloat( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1511cdf0e10cSrcweir {
1512cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getFloat" );
1513cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1514cdf0e10cSrcweir return m_xRow->getFloat(columnIndex);
1515cdf0e10cSrcweir }
1516cdf0e10cSrcweir // -------------------------------------------------------------------------
getDouble(sal_Int32 columnIndex)1517cdf0e10cSrcweir double SAL_CALL OKeySet::getDouble( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1518cdf0e10cSrcweir {
1519cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getDouble" );
1520cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1521cdf0e10cSrcweir return m_xRow->getDouble(columnIndex);
1522cdf0e10cSrcweir }
1523cdf0e10cSrcweir // -------------------------------------------------------------------------
getBytes(sal_Int32 columnIndex)1524cdf0e10cSrcweir Sequence< sal_Int8 > SAL_CALL OKeySet::getBytes( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1525cdf0e10cSrcweir {
1526cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getBytes" );
1527cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1528cdf0e10cSrcweir return m_xRow->getBytes(columnIndex);
1529cdf0e10cSrcweir }
1530cdf0e10cSrcweir // -------------------------------------------------------------------------
getDate(sal_Int32 columnIndex)1531cdf0e10cSrcweir ::com::sun::star::util::Date SAL_CALL OKeySet::getDate( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1532cdf0e10cSrcweir {
1533cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getDate" );
1534cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1535cdf0e10cSrcweir return m_xRow->getDate(columnIndex);
1536cdf0e10cSrcweir }
1537cdf0e10cSrcweir // -------------------------------------------------------------------------
getTime(sal_Int32 columnIndex)1538cdf0e10cSrcweir ::com::sun::star::util::Time SAL_CALL OKeySet::getTime( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1539cdf0e10cSrcweir {
1540cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getTime" );
1541cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1542cdf0e10cSrcweir return m_xRow->getTime(columnIndex);
1543cdf0e10cSrcweir }
1544cdf0e10cSrcweir // -------------------------------------------------------------------------
getTimestamp(sal_Int32 columnIndex)1545cdf0e10cSrcweir ::com::sun::star::util::DateTime SAL_CALL OKeySet::getTimestamp( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1546cdf0e10cSrcweir {
1547cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getTimestamp" );
1548cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1549cdf0e10cSrcweir return m_xRow->getTimestamp(columnIndex);
1550cdf0e10cSrcweir }
1551cdf0e10cSrcweir // -------------------------------------------------------------------------
getBinaryStream(sal_Int32 columnIndex)1552cdf0e10cSrcweir Reference< ::com::sun::star::io::XInputStream > SAL_CALL OKeySet::getBinaryStream( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1553cdf0e10cSrcweir {
1554cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getBinaryStream" );
1555cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1556cdf0e10cSrcweir return m_xRow->getBinaryStream(columnIndex);
1557cdf0e10cSrcweir }
1558cdf0e10cSrcweir // -------------------------------------------------------------------------
getCharacterStream(sal_Int32 columnIndex)1559cdf0e10cSrcweir Reference< ::com::sun::star::io::XInputStream > SAL_CALL OKeySet::getCharacterStream( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1560cdf0e10cSrcweir {
1561cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getCharacterStream" );
1562cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1563cdf0e10cSrcweir return m_xRow->getCharacterStream(columnIndex);
1564cdf0e10cSrcweir }
1565cdf0e10cSrcweir // -------------------------------------------------------------------------
getObject(sal_Int32 columnIndex,const Reference<::com::sun::star::container::XNameAccess> & typeMap)1566cdf0e10cSrcweir Any SAL_CALL OKeySet::getObject( sal_Int32 columnIndex, const Reference< ::com::sun::star::container::XNameAccess >& typeMap ) throw(SQLException, RuntimeException)
1567cdf0e10cSrcweir {
1568cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getObject" );
1569cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1570cdf0e10cSrcweir return m_xRow->getObject(columnIndex,typeMap);
1571cdf0e10cSrcweir }
1572cdf0e10cSrcweir // -------------------------------------------------------------------------
getRef(sal_Int32 columnIndex)1573cdf0e10cSrcweir Reference< XRef > SAL_CALL OKeySet::getRef( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1574cdf0e10cSrcweir {
1575cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getRef" );
1576cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1577cdf0e10cSrcweir return m_xRow->getRef(columnIndex);
1578cdf0e10cSrcweir }
1579cdf0e10cSrcweir // -------------------------------------------------------------------------
getBlob(sal_Int32 columnIndex)1580cdf0e10cSrcweir Reference< XBlob > SAL_CALL OKeySet::getBlob( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1581cdf0e10cSrcweir {
1582cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getBlob" );
1583cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1584cdf0e10cSrcweir return m_xRow->getBlob(columnIndex);
1585cdf0e10cSrcweir }
1586cdf0e10cSrcweir // -------------------------------------------------------------------------
getClob(sal_Int32 columnIndex)1587cdf0e10cSrcweir Reference< XClob > SAL_CALL OKeySet::getClob( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1588cdf0e10cSrcweir {
1589cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getClob" );
1590cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1591cdf0e10cSrcweir return m_xRow->getClob(columnIndex);
1592cdf0e10cSrcweir }
1593cdf0e10cSrcweir // -------------------------------------------------------------------------
getArray(sal_Int32 columnIndex)1594cdf0e10cSrcweir Reference< XArray > SAL_CALL OKeySet::getArray( sal_Int32 columnIndex ) throw(SQLException, RuntimeException)
1595cdf0e10cSrcweir {
1596cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getArray" );
1597cdf0e10cSrcweir OSL_ENSURE(m_xRow.is(),"m_xRow is null!");
1598cdf0e10cSrcweir return m_xRow->getArray(columnIndex);
1599cdf0e10cSrcweir }
1600cdf0e10cSrcweir // -------------------------------------------------------------------------
rowUpdated()1601cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::rowUpdated( ) throw(SQLException, RuntimeException)
1602cdf0e10cSrcweir {
1603cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::rowUpdated" );
1604cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.begin() && m_aKeyIter != m_aKeyMap.end() && m_aKeyIter->second.second.first == 2;
1605cdf0e10cSrcweir }
1606cdf0e10cSrcweir // -------------------------------------------------------------------------
rowInserted()1607cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::rowInserted( ) throw(SQLException, RuntimeException)
1608cdf0e10cSrcweir {
1609cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::rowInserted" );
1610cdf0e10cSrcweir return m_aKeyIter != m_aKeyMap.begin() && m_aKeyIter != m_aKeyMap.end() && m_aKeyIter->second.second.first == 1;
1611cdf0e10cSrcweir }
1612cdf0e10cSrcweir // -------------------------------------------------------------------------
rowDeleted()1613cdf0e10cSrcweir sal_Bool SAL_CALL OKeySet::rowDeleted( ) throw(SQLException, RuntimeException)
1614cdf0e10cSrcweir {
1615cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::rowDeleted" );
1616cdf0e10cSrcweir sal_Bool bDeleted = m_bDeleted;
1617cdf0e10cSrcweir m_bDeleted = sal_False;
1618cdf0e10cSrcweir return bDeleted;
1619cdf0e10cSrcweir }
1620cdf0e10cSrcweir // -----------------------------------------------------------------------------
getComposedTableName(const::rtl::OUString & _sCatalog,const::rtl::OUString & _sSchema,const::rtl::OUString & _sTable)1621cdf0e10cSrcweir ::rtl::OUString OKeySet::getComposedTableName(const ::rtl::OUString& _sCatalog,
1622cdf0e10cSrcweir const ::rtl::OUString& _sSchema,
1623cdf0e10cSrcweir const ::rtl::OUString& _sTable)
1624cdf0e10cSrcweir {
1625cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "OKeySet::getComposedTableName" );
1626cdf0e10cSrcweir ::rtl::OUString aComposedName;
1627cdf0e10cSrcweir Reference<XDatabaseMetaData> xMetaData = m_xConnection->getMetaData();
1628cdf0e10cSrcweir
1629cdf0e10cSrcweir if( xMetaData.is() && xMetaData->supportsTableCorrelationNames() )
1630cdf0e10cSrcweir {
1631cdf0e10cSrcweir aComposedName = ::dbtools::composeTableName( xMetaData, _sCatalog, _sSchema, _sTable, sal_False, ::dbtools::eInDataManipulation );
1632cdf0e10cSrcweir // first we have to check if the composed tablename is in the select clause or if an alias is used
1633cdf0e10cSrcweir Reference<XTablesSupplier> xTabSup(m_xComposer,UNO_QUERY);
1634cdf0e10cSrcweir Reference<XNameAccess> xSelectTables = xTabSup->getTables();
1635cdf0e10cSrcweir OSL_ENSURE(xSelectTables.is(),"No Select tables!");
1636cdf0e10cSrcweir if(xSelectTables.is())
1637cdf0e10cSrcweir {
1638cdf0e10cSrcweir if(!xSelectTables->hasByName(aComposedName))
1639cdf0e10cSrcweir { // the composed name isn't used in the select clause so we have to find out which name is used instead
1640cdf0e10cSrcweir ::rtl::OUString sCatalog,sSchema,sTable;
1641cdf0e10cSrcweir ::dbtools::qualifiedNameComponents(xMetaData,m_sUpdateTableName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
1642cdf0e10cSrcweir aComposedName = ::dbtools::composeTableNameForSelect( m_xConnection, sCatalog, sSchema, sTable );
1643cdf0e10cSrcweir }
1644cdf0e10cSrcweir else
1645cdf0e10cSrcweir aComposedName = ::dbtools::composeTableNameForSelect( m_xConnection, _sCatalog, _sSchema, _sTable );
1646cdf0e10cSrcweir }
1647cdf0e10cSrcweir }
1648cdf0e10cSrcweir else
1649cdf0e10cSrcweir aComposedName = ::dbtools::composeTableNameForSelect( m_xConnection, _sCatalog, _sSchema, _sTable );
1650cdf0e10cSrcweir
1651cdf0e10cSrcweir return aComposedName;
1652cdf0e10cSrcweir }
1653cdf0e10cSrcweir // -----------------------------------------------------------------------------
1654cdf0e10cSrcweir namespace dbaccess
1655cdf0e10cSrcweir {
1656cdf0e10cSrcweir
getColumnPositions(const Reference<XNameAccess> & _rxQueryColumns,const::com::sun::star::uno::Sequence<::rtl::OUString> & _aColumnNames,const::rtl::OUString & _rsUpdateTableName,SelectColumnsMetaData & o_rColumnNames,bool i_bAppendTableName)1657cdf0e10cSrcweir void getColumnPositions(const Reference<XNameAccess>& _rxQueryColumns,
1658cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _aColumnNames,
1659cdf0e10cSrcweir const ::rtl::OUString& _rsUpdateTableName,
1660cdf0e10cSrcweir SelectColumnsMetaData& o_rColumnNames,
1661cdf0e10cSrcweir bool i_bAppendTableName)
1662cdf0e10cSrcweir {
1663cdf0e10cSrcweir // get the real name of the columns
1664cdf0e10cSrcweir Sequence< ::rtl::OUString> aSelNames(_rxQueryColumns->getElementNames());
1665cdf0e10cSrcweir const ::rtl::OUString* pSelIter = aSelNames.getConstArray();
1666cdf0e10cSrcweir const ::rtl::OUString* pSelEnd = pSelIter + aSelNames.getLength();
1667cdf0e10cSrcweir
1668cdf0e10cSrcweir const ::rtl::OUString* pTblColumnIter = _aColumnNames.getConstArray();
1669cdf0e10cSrcweir const ::rtl::OUString* pTblColumnEnd = pTblColumnIter + _aColumnNames.getLength();
1670cdf0e10cSrcweir
1671cdf0e10cSrcweir
1672cdf0e10cSrcweir ::comphelper::UStringMixLess aTmp(o_rColumnNames.key_comp());
1673cdf0e10cSrcweir ::comphelper::UStringMixEqual bCase(static_cast< ::comphelper::UStringMixLess*>(&aTmp)->isCaseSensitive());
1674cdf0e10cSrcweir
1675cdf0e10cSrcweir for(sal_Int32 nPos = 1;pSelIter != pSelEnd;++pSelIter,++nPos)
1676cdf0e10cSrcweir {
1677cdf0e10cSrcweir Reference<XPropertySet> xQueryColumnProp(_rxQueryColumns->getByName(*pSelIter),UNO_QUERY_THROW);
1678cdf0e10cSrcweir ::rtl::OUString sRealName,sTableName;
1679cdf0e10cSrcweir OSL_ENSURE(xQueryColumnProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
1680cdf0e10cSrcweir OSL_ENSURE(xQueryColumnProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
1681cdf0e10cSrcweir xQueryColumnProp->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
1682cdf0e10cSrcweir xQueryColumnProp->getPropertyValue(PROPERTY_TABLENAME) >>= sTableName;
1683cdf0e10cSrcweir
1684cdf0e10cSrcweir for(;pTblColumnIter != pTblColumnEnd;++pTblColumnIter)
1685cdf0e10cSrcweir {
1686cdf0e10cSrcweir if(bCase(sRealName,*pTblColumnIter) && bCase(_rsUpdateTableName,sTableName) && o_rColumnNames.find(*pTblColumnIter) == o_rColumnNames.end())
1687cdf0e10cSrcweir {
1688cdf0e10cSrcweir sal_Int32 nType = 0;
1689cdf0e10cSrcweir xQueryColumnProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
1690cdf0e10cSrcweir sal_Int32 nScale = 0;
1691cdf0e10cSrcweir xQueryColumnProp->getPropertyValue(PROPERTY_SCALE) >>= nScale;
1692cdf0e10cSrcweir ::rtl::OUString sColumnDefault;
1693cdf0e10cSrcweir if ( xQueryColumnProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_DEFAULTVALUE) )
1694cdf0e10cSrcweir xQueryColumnProp->getPropertyValue(PROPERTY_DEFAULTVALUE) >>= sColumnDefault;
1695cdf0e10cSrcweir
1696cdf0e10cSrcweir sal_Int32 nNullable = ColumnValue::NULLABLE_UNKNOWN;
1697cdf0e10cSrcweir OSL_VERIFY( xQueryColumnProp->getPropertyValue( PROPERTY_ISNULLABLE ) >>= nNullable );
1698cdf0e10cSrcweir
1699cdf0e10cSrcweir if ( i_bAppendTableName )
1700cdf0e10cSrcweir {
1701cdf0e10cSrcweir ::rtl::OUStringBuffer sName;
1702cdf0e10cSrcweir sName.append(sTableName);
1703cdf0e10cSrcweir sName.appendAscii(".");
1704cdf0e10cSrcweir sName.append(sRealName);
1705cdf0e10cSrcweir SelectColumnDescription aColDesc( nPos, nType,nScale,nNullable != sdbc::ColumnValue::NO_NULLS, sColumnDefault );
1706cdf0e10cSrcweir aColDesc.sRealName = sRealName;
1707cdf0e10cSrcweir aColDesc.sTableName = sTableName;
1708cdf0e10cSrcweir o_rColumnNames[sName.makeStringAndClear()] = aColDesc;
1709cdf0e10cSrcweir }
1710cdf0e10cSrcweir else
1711cdf0e10cSrcweir o_rColumnNames[sRealName] = SelectColumnDescription( nPos, nType,nScale,nNullable != sdbc::ColumnValue::NO_NULLS, sColumnDefault );
1712cdf0e10cSrcweir
1713cdf0e10cSrcweir break;
1714cdf0e10cSrcweir }
1715cdf0e10cSrcweir }
1716cdf0e10cSrcweir pTblColumnIter = _aColumnNames.getConstArray();
1717cdf0e10cSrcweir }
1718cdf0e10cSrcweir }
1719cdf0e10cSrcweir }
1720cdf0e10cSrcweir // -----------------------------------------------------------------------------
impl_convertValue_throw(const ORowSetRow & _rInsertRow,const SelectColumnDescription & i_aMetaData)1721cdf0e10cSrcweir void OKeySet::impl_convertValue_throw(const ORowSetRow& _rInsertRow,const SelectColumnDescription& i_aMetaData)
1722cdf0e10cSrcweir {
1723cdf0e10cSrcweir ORowSetValue& aValue((_rInsertRow->get())[i_aMetaData.nPosition]);
1724cdf0e10cSrcweir switch(i_aMetaData.nType)
1725cdf0e10cSrcweir {
1726cdf0e10cSrcweir case DataType::DECIMAL:
1727cdf0e10cSrcweir case DataType::NUMERIC:
1728cdf0e10cSrcweir {
1729cdf0e10cSrcweir ::rtl::OUString sValue = aValue.getString();
1730cdf0e10cSrcweir sal_Int32 nIndex = sValue.indexOf('.');
1731cdf0e10cSrcweir if ( nIndex != -1 )
1732cdf0e10cSrcweir {
1733cdf0e10cSrcweir aValue = sValue.copy(0,::std::min(sValue.getLength(),nIndex + (i_aMetaData.nScale > 0 ? i_aMetaData.nScale + 1 : 0)));
1734cdf0e10cSrcweir }
1735cdf0e10cSrcweir }
1736cdf0e10cSrcweir break;
1737cdf0e10cSrcweir default:
1738cdf0e10cSrcweir break;
1739cdf0e10cSrcweir }
1740cdf0e10cSrcweir }
1741cdf0e10cSrcweir // -----------------------------------------------------------------------------
1742cdf0e10cSrcweir
1743