1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include "file/FStatement.hxx"
28cdf0e10cSrcweir #include "file/FConnection.hxx"
29cdf0e10cSrcweir #include "file/FDriver.hxx"
30cdf0e10cSrcweir #include "file/FResultSet.hxx"
31cdf0e10cSrcweir #include <comphelper/property.hxx>
32cdf0e10cSrcweir #include <comphelper/uno3.hxx>
33cdf0e10cSrcweir #include <osl/thread.h>
34cdf0e10cSrcweir #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
35cdf0e10cSrcweir #include <com/sun/star/sdbc/ResultSetType.hpp>
36cdf0e10cSrcweir #include <com/sun/star/sdbc/FetchDirection.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
38cdf0e10cSrcweir #include <comphelper/sequence.hxx>
39cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
40cdf0e10cSrcweir #include "connectivity/dbexception.hxx"
41cdf0e10cSrcweir #include "resource/file_res.hrc"
42cdf0e10cSrcweir #include <algorithm>
43cdf0e10cSrcweir #include <tools/debug.hxx>
44cdf0e10cSrcweir #include <rtl/logfile.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #define THROW_SQL(x) \
47cdf0e10cSrcweir 	OTools::ThrowException(x,m_aStatementHandle,SQL_HANDLE_STMT,*this)
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace connectivity
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	namespace file
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //------------------------------------------------------------------------------
55cdf0e10cSrcweir using namespace dbtools;
56cdf0e10cSrcweir using namespace com::sun::star::uno;
57cdf0e10cSrcweir using namespace com::sun::star::lang;
58cdf0e10cSrcweir using namespace com::sun::star::beans;
59cdf0e10cSrcweir using namespace com::sun::star::sdbc;
60cdf0e10cSrcweir using namespace com::sun::star::sdbcx;
61cdf0e10cSrcweir using namespace com::sun::star::container;
DBG_NAME(file_OStatement_Base)62cdf0e10cSrcweir DBG_NAME( file_OStatement_Base )
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //------------------------------------------------------------------------------
65cdf0e10cSrcweir OStatement_Base::OStatement_Base(OConnection* _pConnection )
66cdf0e10cSrcweir     :OStatement_BASE(m_aMutex)
67cdf0e10cSrcweir 	,::comphelper::OPropertyContainer(OStatement_BASE::rBHelper)
68cdf0e10cSrcweir     ,m_xDBMetaData(_pConnection->getMetaData())
69cdf0e10cSrcweir 	,m_aParser(_pConnection->getDriver()->getFactory())
70cdf0e10cSrcweir 	,m_aSQLIterator( _pConnection, _pConnection->createCatalog()->getTables(), m_aParser, NULL )
71cdf0e10cSrcweir     ,m_pConnection(_pConnection)
72cdf0e10cSrcweir     ,m_pParseTree(NULL)
73cdf0e10cSrcweir     ,m_pSQLAnalyzer(NULL)
74cdf0e10cSrcweir     ,m_pEvaluationKeySet(NULL)
75cdf0e10cSrcweir     ,m_pTable(NULL)
76cdf0e10cSrcweir 	,m_nMaxFieldSize(0)
77cdf0e10cSrcweir 	,m_nMaxRows(0)
78cdf0e10cSrcweir 	,m_nQueryTimeOut(0)
79cdf0e10cSrcweir 	,m_nFetchSize(0)
80cdf0e10cSrcweir 	,m_nResultSetType(ResultSetType::FORWARD_ONLY)
81cdf0e10cSrcweir 	,m_nFetchDirection(FetchDirection::FORWARD)
82cdf0e10cSrcweir 	,m_nResultSetConcurrency(ResultSetConcurrency::UPDATABLE)
83cdf0e10cSrcweir     ,m_bEscapeProcessing(sal_True)
84cdf0e10cSrcweir     ,rBHelper(OStatement_BASE::rBHelper)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::OStatement_Base" );
87cdf0e10cSrcweir 	DBG_CTOR( file_OStatement_Base, NULL );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	m_pConnection->acquire();
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	sal_Int32 nAttrib = 0;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_CURSORNAME),		PROPERTY_ID_CURSORNAME,			nAttrib,&m_aCursorName,		::getCppuType(reinterpret_cast< ::rtl::OUString*>(NULL)));
94cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXFIELDSIZE),	PROPERTY_ID_MAXFIELDSIZE,		nAttrib,&m_nMaxFieldSize,		::getCppuType(reinterpret_cast<sal_Int32*>(NULL)));
95cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXROWS),			PROPERTY_ID_MAXROWS,			nAttrib,&m_nMaxRows,		::getCppuType(reinterpret_cast<sal_Int32*>(NULL)));
96cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_QUERYTIMEOUT),	PROPERTY_ID_QUERYTIMEOUT,		nAttrib,&m_nQueryTimeOut,	::getCppuType(reinterpret_cast<sal_Int32*>(NULL)));
97cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE),		PROPERTY_ID_FETCHSIZE,			nAttrib,&m_nFetchSize,		::getCppuType(reinterpret_cast<sal_Int32*>(NULL)));
98cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE),	PROPERTY_ID_RESULTSETTYPE,		nAttrib,&m_nResultSetType,	::getCppuType(reinterpret_cast<sal_Int32*>(NULL)));
99cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION),	PROPERTY_ID_FETCHDIRECTION,		nAttrib,&m_nFetchDirection,	::getCppuType(reinterpret_cast<sal_Int32*>(NULL)));
100cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ESCAPEPROCESSING),PROPERTY_ID_ESCAPEPROCESSING,	nAttrib,&m_bEscapeProcessing,::getCppuBooleanType());
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY),		PROPERTY_ID_RESULTSETCONCURRENCY,	nAttrib,&m_nResultSetConcurrency,		::getCppuType(reinterpret_cast<sal_Int32*>(NULL)));
103cdf0e10cSrcweir }
104cdf0e10cSrcweir // -----------------------------------------------------------------------------
~OStatement_Base()105cdf0e10cSrcweir OStatement_Base::~OStatement_Base()
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	osl_incrementInterlockedCount( &m_refCount );
108cdf0e10cSrcweir 	disposing();
109cdf0e10cSrcweir 	delete m_pSQLAnalyzer;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	DBG_DTOR( file_OStatement_Base, NULL );
112cdf0e10cSrcweir }
113cdf0e10cSrcweir //------------------------------------------------------------------------------
disposeResultSet()114cdf0e10cSrcweir void OStatement_Base::disposeResultSet()
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::disposeResultSet" );
117cdf0e10cSrcweir 	// free the cursor if alive
118cdf0e10cSrcweir     Reference< XComponent > xComp(m_xResultSet.get(), UNO_QUERY);
119cdf0e10cSrcweir 	if (xComp.is())
120cdf0e10cSrcweir 		xComp->dispose();
121cdf0e10cSrcweir     m_xResultSet = Reference< XResultSet>();
122cdf0e10cSrcweir }
123cdf0e10cSrcweir //------------------------------------------------------------------------------
disposing()124cdf0e10cSrcweir void OStatement_BASE2::disposing()
125cdf0e10cSrcweir {
126cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	disposeResultSet();
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 	if(m_pSQLAnalyzer)
131cdf0e10cSrcweir 		m_pSQLAnalyzer->dispose();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	if(m_aRow.isValid())
134cdf0e10cSrcweir 	{
135cdf0e10cSrcweir 		m_aRow->get().clear();
136cdf0e10cSrcweir 		m_aRow = NULL;
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	m_aSQLIterator.dispose();
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	if(m_pTable)
142cdf0e10cSrcweir 	{
143cdf0e10cSrcweir 		m_pTable->release();
144cdf0e10cSrcweir 		m_pTable = NULL;
145cdf0e10cSrcweir 	}
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	if (m_pConnection)
148cdf0e10cSrcweir 	{
149cdf0e10cSrcweir 		m_pConnection->release();
150cdf0e10cSrcweir 		m_pConnection = NULL;
151cdf0e10cSrcweir 	}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	dispose_ChildImpl();
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	if ( m_pParseTree )
156cdf0e10cSrcweir 	{
157cdf0e10cSrcweir 		delete m_pParseTree;
158cdf0e10cSrcweir 		m_pParseTree = NULL;
159cdf0e10cSrcweir 	}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	OStatement_Base::disposing();
162cdf0e10cSrcweir }
163cdf0e10cSrcweir // -----------------------------------------------------------------------------
acquire()164cdf0e10cSrcweir void SAL_CALL OStatement_Base::acquire() throw()
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	OStatement_BASE::acquire();
167cdf0e10cSrcweir }
168cdf0e10cSrcweir //-----------------------------------------------------------------------------
release()169cdf0e10cSrcweir void SAL_CALL OStatement_BASE2::release() throw()
170cdf0e10cSrcweir {
171cdf0e10cSrcweir 	relase_ChildImpl();
172cdf0e10cSrcweir }
173cdf0e10cSrcweir //-----------------------------------------------------------------------------
queryInterface(const Type & rType)174cdf0e10cSrcweir Any SAL_CALL OStatement_Base::queryInterface( const Type & rType ) throw(RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::queryInterface" );
177cdf0e10cSrcweir     const Any aRet = OStatement_BASE::queryInterface(rType);
178cdf0e10cSrcweir 	return aRet.hasValue() ? aRet : OPropertySetHelper::queryInterface(rType);
179cdf0e10cSrcweir }
180cdf0e10cSrcweir // -------------------------------------------------------------------------
getTypes()181cdf0e10cSrcweir Sequence< Type > SAL_CALL OStatement_Base::getTypes(  ) throw(RuntimeException)
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getTypes" );
184cdf0e10cSrcweir     ::cppu::OTypeCollection aTypes( ::getCppuType( (const Reference< ::com::sun::star::beans::XMultiPropertySet > *)0 ),
185cdf0e10cSrcweir                                                                     ::getCppuType( (const Reference< ::com::sun::star::beans::XFastPropertySet > *)0 ),
186cdf0e10cSrcweir                                                                     ::getCppuType( (const Reference< ::com::sun::star::beans::XPropertySet > *)0 ));
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	return ::comphelper::concatSequences(aTypes.getTypes(),OStatement_BASE::getTypes());
189cdf0e10cSrcweir }
190cdf0e10cSrcweir // -------------------------------------------------------------------------
191cdf0e10cSrcweir 
cancel()192cdf0e10cSrcweir void SAL_CALL OStatement_Base::cancel(  ) throw(RuntimeException)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::cancel" );
195cdf0e10cSrcweir }
196cdf0e10cSrcweir // -------------------------------------------------------------------------
197cdf0e10cSrcweir 
close()198cdf0e10cSrcweir void SAL_CALL OStatement_Base::close(  ) throw(SQLException, RuntimeException)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::close" );
201cdf0e10cSrcweir 	{
202cdf0e10cSrcweir 		::osl::MutexGuard aGuard( m_aMutex );
203cdf0e10cSrcweir 		checkDisposed(OStatement_BASE::rBHelper.bDisposed);
204cdf0e10cSrcweir 	}
205cdf0e10cSrcweir 	dispose();
206cdf0e10cSrcweir }
207cdf0e10cSrcweir // -------------------------------------------------------------------------
208cdf0e10cSrcweir 
reset()209cdf0e10cSrcweir void OStatement_Base::reset() throw (SQLException)
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::reset" );
212cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
213cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	clearWarnings ();
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	if (m_xResultSet.get().is())
219cdf0e10cSrcweir 		clearMyResultSet();
220cdf0e10cSrcweir }
221cdf0e10cSrcweir //--------------------------------------------------------------------
222cdf0e10cSrcweir // clearMyResultSet
223cdf0e10cSrcweir // If a ResultSet was created for this Statement, close it
224cdf0e10cSrcweir //--------------------------------------------------------------------
225cdf0e10cSrcweir 
clearMyResultSet()226cdf0e10cSrcweir void OStatement_Base::clearMyResultSet () throw (SQLException)
227cdf0e10cSrcweir {
228cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::clearMyResultSet " );
229cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
230cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     try
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         Reference<XCloseable> xCloseable;
235cdf0e10cSrcweir         if ( ::comphelper::query_interface( m_xResultSet.get(), xCloseable ) )
236cdf0e10cSrcweir             xCloseable->close();
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir     catch( const DisposedException& ) { }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 	m_xResultSet = Reference< XResultSet>();
241cdf0e10cSrcweir }
242cdf0e10cSrcweir //--------------------------------------------------------------------
243cdf0e10cSrcweir // setWarning
244cdf0e10cSrcweir // Sets the warning
245cdf0e10cSrcweir //--------------------------------------------------------------------
246cdf0e10cSrcweir 
setWarning(const SQLWarning & ex)247cdf0e10cSrcweir void OStatement_Base::setWarning (const SQLWarning &ex) throw( SQLException)
248cdf0e10cSrcweir {
249cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::setWarning " );
250cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
251cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 	m_aLastWarning = ex;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir // -------------------------------------------------------------------------
getWarnings()258cdf0e10cSrcweir Any SAL_CALL OStatement_Base::getWarnings(  ) throw(SQLException, RuntimeException)
259cdf0e10cSrcweir {
260cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getWarnings" );
261cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
262cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 	return makeAny(m_aLastWarning);
265cdf0e10cSrcweir }
266cdf0e10cSrcweir // -------------------------------------------------------------------------
clearWarnings()267cdf0e10cSrcweir void SAL_CALL OStatement_Base::clearWarnings(  ) throw(SQLException, RuntimeException)
268cdf0e10cSrcweir {
269cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::clearWarnings" );
270cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
271cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 	m_aLastWarning = SQLWarning();
274cdf0e10cSrcweir }
275cdf0e10cSrcweir // -------------------------------------------------------------------------
createArrayHelper() const276cdf0e10cSrcweir ::cppu::IPropertyArrayHelper* OStatement_Base::createArrayHelper( ) const
277cdf0e10cSrcweir {
278cdf0e10cSrcweir     //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createArrayHelper" );
279cdf0e10cSrcweir 	Sequence< Property > aProps;
280cdf0e10cSrcweir 	describeProperties(aProps);
281cdf0e10cSrcweir 	return new ::cppu::OPropertyArrayHelper(aProps);
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir // -------------------------------------------------------------------------
getInfoHelper()285cdf0e10cSrcweir ::cppu::IPropertyArrayHelper & OStatement_Base::getInfoHelper()
286cdf0e10cSrcweir {
287cdf0e10cSrcweir     //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getInfoHelper" );
288cdf0e10cSrcweir 	return *const_cast<OStatement_Base*>(this)->getArrayHelper();
289cdf0e10cSrcweir }
290cdf0e10cSrcweir // -------------------------------------------------------------------------
createResultSet()291cdf0e10cSrcweir OResultSet* OStatement::createResultSet()
292cdf0e10cSrcweir {
293cdf0e10cSrcweir 	return new OResultSet(this,m_aSQLIterator);
294cdf0e10cSrcweir }
295cdf0e10cSrcweir // -------------------------------------------------------------------------
296cdf0e10cSrcweir IMPLEMENT_SERVICE_INFO(OStatement,"com.sun.star.sdbc.driver.file.Statement","com.sun.star.sdbc.Statement");
297cdf0e10cSrcweir // -----------------------------------------------------------------------------
acquire()298cdf0e10cSrcweir void SAL_CALL OStatement::acquire() throw()
299cdf0e10cSrcweir {
300cdf0e10cSrcweir 	OStatement_BASE2::acquire();
301cdf0e10cSrcweir }
302cdf0e10cSrcweir // -----------------------------------------------------------------------------
release()303cdf0e10cSrcweir void SAL_CALL OStatement::release() throw()
304cdf0e10cSrcweir {
305cdf0e10cSrcweir 	OStatement_BASE2::release();
306cdf0e10cSrcweir }
307cdf0e10cSrcweir // -----------------------------------------------------------------------------
308cdf0e10cSrcweir // -------------------------------------------------------------------------
execute(const::rtl::OUString & sql)309cdf0e10cSrcweir sal_Bool SAL_CALL OStatement::execute( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
310cdf0e10cSrcweir {
311cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     executeQuery(sql);
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	return m_aSQLIterator.getStatementType() == SQL_STATEMENT_SELECT;
316cdf0e10cSrcweir }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir // -------------------------------------------------------------------------
319cdf0e10cSrcweir 
executeQuery(const::rtl::OUString & sql)320cdf0e10cSrcweir Reference< XResultSet > SAL_CALL OStatement::executeQuery( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
321cdf0e10cSrcweir {
322cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
323cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
324cdf0e10cSrcweir 
325cdf0e10cSrcweir     construct(sql);
326cdf0e10cSrcweir     Reference< XResultSet > xRS;
327cdf0e10cSrcweir 	OResultSet* pResult = createResultSet();
328cdf0e10cSrcweir 	xRS = pResult;
329cdf0e10cSrcweir 	initializeResultSet(pResult);
330cdf0e10cSrcweir     m_xResultSet = Reference<XResultSet>(pResult);
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 	pResult->OpenImpl();
333cdf0e10cSrcweir 
334cdf0e10cSrcweir 	return xRS;
335cdf0e10cSrcweir }
336cdf0e10cSrcweir // -------------------------------------------------------------------------
getConnection()337cdf0e10cSrcweir Reference< XConnection > SAL_CALL OStatement::getConnection(  ) throw(SQLException, RuntimeException)
338cdf0e10cSrcweir {
339cdf0e10cSrcweir 	return (Reference< XConnection >)m_pConnection;
340cdf0e10cSrcweir }
341cdf0e10cSrcweir // -------------------------------------------------------------------------
executeUpdate(const::rtl::OUString & sql)342cdf0e10cSrcweir sal_Int32 SAL_CALL OStatement::executeUpdate( const ::rtl::OUString& sql ) throw(SQLException, RuntimeException)
343cdf0e10cSrcweir {
344cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
345cdf0e10cSrcweir 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 	construct(sql);
349cdf0e10cSrcweir 	OResultSet* pResult = createResultSet();
350cdf0e10cSrcweir 	Reference< XResultSet > xRS = pResult;
351cdf0e10cSrcweir 	initializeResultSet(pResult);
352cdf0e10cSrcweir 	pResult->OpenImpl();
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 	return pResult->getRowCountResult();
355cdf0e10cSrcweir }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir // -----------------------------------------------------------------------------
disposing(void)358cdf0e10cSrcweir void SAL_CALL OStatement_Base::disposing(void)
359cdf0e10cSrcweir {
360cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::disposing" );
361cdf0e10cSrcweir 	if(m_aEvaluateRow.isValid())
362cdf0e10cSrcweir 	{
363cdf0e10cSrcweir 		m_aEvaluateRow->get().clear();
364cdf0e10cSrcweir 		m_aEvaluateRow = NULL;
365cdf0e10cSrcweir 	}
366cdf0e10cSrcweir 	delete m_pEvaluationKeySet;
367cdf0e10cSrcweir 	OStatement_BASE::disposing();
368cdf0e10cSrcweir }
369cdf0e10cSrcweir // -----------------------------------------------------------------------------
getPropertySetInfo()370cdf0e10cSrcweir Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL OStatement_Base::getPropertySetInfo(  ) throw(RuntimeException)
371cdf0e10cSrcweir {
372cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::getPropertySetInfo" );
373cdf0e10cSrcweir 	return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
374cdf0e10cSrcweir }
375cdf0e10cSrcweir // -----------------------------------------------------------------------------
queryInterface(const Type & rType)376cdf0e10cSrcweir Any SAL_CALL OStatement::queryInterface( const Type & rType ) throw(RuntimeException)
377cdf0e10cSrcweir {
378cdf0e10cSrcweir 	Any aRet = OStatement_XStatement::queryInterface( rType);
379cdf0e10cSrcweir 	return aRet.hasValue() ? aRet : OStatement_BASE2::queryInterface( rType);
380cdf0e10cSrcweir }
381cdf0e10cSrcweir // -----------------------------------------------------------------------------
createAnalyzer()382cdf0e10cSrcweir OSQLAnalyzer* OStatement_Base::createAnalyzer()
383cdf0e10cSrcweir {
384cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createAnalyzer" );
385cdf0e10cSrcweir 	return new OSQLAnalyzer(m_pConnection);
386cdf0e10cSrcweir }
387cdf0e10cSrcweir // -----------------------------------------------------------------------------
anylizeSQL()388cdf0e10cSrcweir void OStatement_Base::anylizeSQL()
389cdf0e10cSrcweir {
390cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::anylizeSQL" );
391cdf0e10cSrcweir 	OSL_ENSURE(m_pSQLAnalyzer,"OResultSet::anylizeSQL: Analyzer isn't set!");
392cdf0e10cSrcweir 	// start analysing the statement
393cdf0e10cSrcweir 	m_pSQLAnalyzer->setOrigColumns(m_xColNames);
394cdf0e10cSrcweir 	m_pSQLAnalyzer->start(m_pParseTree);
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 	const OSQLParseNode* pOrderbyClause = m_aSQLIterator.getOrderTree();
397cdf0e10cSrcweir 	if(pOrderbyClause)
398cdf0e10cSrcweir 	{
399cdf0e10cSrcweir 		OSQLParseNode * pOrderingSpecCommalist = pOrderbyClause->getChild(2);
400cdf0e10cSrcweir 		OSL_ENSURE(SQL_ISRULE(pOrderingSpecCommalist,ordering_spec_commalist),"OResultSet: Fehler im Parse Tree");
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 		for (sal_uInt32 m = 0; m < pOrderingSpecCommalist->count(); m++)
403cdf0e10cSrcweir 		{
404cdf0e10cSrcweir 			OSQLParseNode * pOrderingSpec = pOrderingSpecCommalist->getChild(m);
405cdf0e10cSrcweir 			OSL_ENSURE(SQL_ISRULE(pOrderingSpec,ordering_spec),"OResultSet: Fehler im Parse Tree");
406cdf0e10cSrcweir 			OSL_ENSURE(pOrderingSpec->count() == 2,"OResultSet: Fehler im Parse Tree");
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 			OSQLParseNode * pColumnRef = pOrderingSpec->getChild(0);
409cdf0e10cSrcweir 			if(!SQL_ISRULE(pColumnRef,column_ref))
410cdf0e10cSrcweir 			{
411cdf0e10cSrcweir 				throw SQLException();
412cdf0e10cSrcweir 			}
413cdf0e10cSrcweir 			OSQLParseNode * pAscendingDescending = pOrderingSpec->getChild(1);
414cdf0e10cSrcweir 			setOrderbyColumn(pColumnRef,pAscendingDescending);
415cdf0e10cSrcweir 		}
416cdf0e10cSrcweir 	}
417cdf0e10cSrcweir }
418cdf0e10cSrcweir //------------------------------------------------------------------
setOrderbyColumn(OSQLParseNode * pColumnRef,OSQLParseNode * pAscendingDescending)419cdf0e10cSrcweir void OStatement_Base::setOrderbyColumn(	OSQLParseNode* pColumnRef,
420cdf0e10cSrcweir 										OSQLParseNode* pAscendingDescending)
421cdf0e10cSrcweir {
422cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::setOrderbyColumn" );
423cdf0e10cSrcweir 	::rtl::OUString aColumnName;
424cdf0e10cSrcweir 	if (pColumnRef->count() == 1)
425cdf0e10cSrcweir 		aColumnName = pColumnRef->getChild(0)->getTokenValue();
426cdf0e10cSrcweir 	else if (pColumnRef->count() == 3)
427cdf0e10cSrcweir 	{
428cdf0e10cSrcweir 		// Nur die Table Range-Variable darf hier vorkommen:
429cdf0e10cSrcweir //		if (!(pColumnRef->getChild(0)->getTokenValue() == aTableRange))
430cdf0e10cSrcweir //		{
431cdf0e10cSrcweir //			aStatus.Set(SQL_STAT_ERROR,
432cdf0e10cSrcweir //						String::CreateFromAscii("S1000"),
433cdf0e10cSrcweir //						aStatus.CreateErrorMessage(String(SdbResId(STR_STAT_INVALID_RANGE_VAR))),
434cdf0e10cSrcweir //						0, String() );
435cdf0e10cSrcweir 			//	return;
436cdf0e10cSrcweir 		//	}
437cdf0e10cSrcweir 		pColumnRef->getChild(2)->parseNodeToStr( aColumnName, getOwnConnection(), NULL, sal_False, sal_False );
438cdf0e10cSrcweir 	}
439cdf0e10cSrcweir 	else
440cdf0e10cSrcweir 	{
441cdf0e10cSrcweir 		//	aStatus.SetStatementTooComplex();
442cdf0e10cSrcweir 		throw SQLException();
443cdf0e10cSrcweir 	}
444cdf0e10cSrcweir 
445cdf0e10cSrcweir 	Reference<XColumnLocate> xColLocate(m_xColNames,UNO_QUERY);
446cdf0e10cSrcweir 	if(!xColLocate.is())
447cdf0e10cSrcweir 		return;
448cdf0e10cSrcweir 	// Alles geprueft und wir haben den Namen der Column.
449cdf0e10cSrcweir 	// Die wievielte Column ist das?
450cdf0e10cSrcweir 	::vos::ORef<OSQLColumns> aSelectColumns = m_aSQLIterator.getSelectColumns();
451cdf0e10cSrcweir 	::comphelper::UStringMixEqual aCase;
452cdf0e10cSrcweir 	OSQLColumns::Vector::const_iterator aFind = ::connectivity::find(aSelectColumns->get().begin(),aSelectColumns->get().end(),aColumnName,aCase);
453cdf0e10cSrcweir 	if ( aFind == aSelectColumns->get().end() )
454cdf0e10cSrcweir 		throw SQLException();
455cdf0e10cSrcweir 	m_aOrderbyColumnNumber.push_back((aFind - aSelectColumns->get().begin()) + 1);
456cdf0e10cSrcweir 
457cdf0e10cSrcweir 	// Ascending or Descending?
458cdf0e10cSrcweir 	m_aOrderbyAscending.push_back((SQL_ISTOKEN(pAscendingDescending,DESC)) ? SQL_DESC : SQL_ASC);
459cdf0e10cSrcweir }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir // -----------------------------------------------------------------------------
construct(const::rtl::OUString & sql)462cdf0e10cSrcweir void OStatement_Base::construct(const ::rtl::OUString& sql)  throw(SQLException, RuntimeException)
463cdf0e10cSrcweir {
464cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::construct" );
465cdf0e10cSrcweir 	::rtl::OUString aErr;
466cdf0e10cSrcweir 	m_pParseTree = m_aParser.parseTree(aErr,sql);
467cdf0e10cSrcweir 	if(m_pParseTree)
468cdf0e10cSrcweir 	{
469cdf0e10cSrcweir 		m_aSQLIterator.setParseTree(m_pParseTree);
470cdf0e10cSrcweir 		m_aSQLIterator.traverseAll();
471cdf0e10cSrcweir 		const OSQLTables& xTabs = m_aSQLIterator.getTables();
472cdf0e10cSrcweir 
473cdf0e10cSrcweir         // sanity checks
474cdf0e10cSrcweir 		if ( xTabs.empty() )
475cdf0e10cSrcweir             // no tables -> nothing to operate on -> error
476cdf0e10cSrcweir             m_pConnection->throwGenericSQLException(STR_QUERY_NO_TABLE,*this);
477cdf0e10cSrcweir 
478cdf0e10cSrcweir         if ( xTabs.size() > 1 || m_aSQLIterator.hasErrors() )
479cdf0e10cSrcweir             // more than one table -> can't operate on them -> error
480cdf0e10cSrcweir             m_pConnection->throwGenericSQLException(STR_QUERY_MORE_TABLES,*this);
481cdf0e10cSrcweir 
482cdf0e10cSrcweir 		if ( (m_aSQLIterator.getStatementType() == SQL_STATEMENT_SELECT) && m_aSQLIterator.getSelectColumns()->get().empty() )
483cdf0e10cSrcweir             // SELECT statement without columns -> error
484cdf0e10cSrcweir             m_pConnection->throwGenericSQLException(STR_QUERY_NO_COLUMN,*this);
485cdf0e10cSrcweir 
486cdf0e10cSrcweir 		switch(m_aSQLIterator.getStatementType())
487cdf0e10cSrcweir 		{
488cdf0e10cSrcweir 			case SQL_STATEMENT_CREATE_TABLE:
489cdf0e10cSrcweir 			case SQL_STATEMENT_ODBC_CALL:
490cdf0e10cSrcweir 			case SQL_STATEMENT_UNKNOWN:
491cdf0e10cSrcweir 				m_pConnection->throwGenericSQLException(STR_QUERY_TOO_COMPLEX,*this);
492cdf0e10cSrcweir 				break;
493cdf0e10cSrcweir 			default:
494cdf0e10cSrcweir 				break;
495cdf0e10cSrcweir 		}
496cdf0e10cSrcweir 
497cdf0e10cSrcweir 		// at this moment we support only one table per select statement
498cdf0e10cSrcweir 		Reference< ::com::sun::star::lang::XUnoTunnel> xTunnel(xTabs.begin()->second,UNO_QUERY);
499cdf0e10cSrcweir 		if(xTunnel.is())
500cdf0e10cSrcweir 		{
501cdf0e10cSrcweir 			if(m_pTable)
502cdf0e10cSrcweir 				m_pTable->release();
503cdf0e10cSrcweir 			m_pTable = reinterpret_cast<OFileTable*>(xTunnel->getSomething(OFileTable::getUnoTunnelImplementationId()));
504cdf0e10cSrcweir 			if(m_pTable)
505cdf0e10cSrcweir 				m_pTable->acquire();
506cdf0e10cSrcweir 		}
507cdf0e10cSrcweir 		OSL_ENSURE(m_pTable,"No table!");
508cdf0e10cSrcweir         if ( m_pTable )
509cdf0e10cSrcweir 		    m_xColNames		= m_pTable->getColumns();
510cdf0e10cSrcweir 		Reference<XIndexAccess> xNames(m_xColNames,UNO_QUERY);
511cdf0e10cSrcweir 		// set the binding of the resultrow
512cdf0e10cSrcweir 		m_aRow			= new OValueRefVector(xNames->getCount());
513cdf0e10cSrcweir 		(m_aRow->get())[0]->setBound(sal_True);
514cdf0e10cSrcweir 		::std::for_each(m_aRow->get().begin()+1,m_aRow->get().end(),TSetRefBound(sal_False));
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 		// set the binding of the resultrow
517cdf0e10cSrcweir 		m_aEvaluateRow	= new OValueRefVector(xNames->getCount());
518cdf0e10cSrcweir 
519cdf0e10cSrcweir 		(m_aEvaluateRow->get())[0]->setBound(sal_True);
520cdf0e10cSrcweir 		::std::for_each(m_aEvaluateRow->get().begin()+1,m_aEvaluateRow->get().end(),TSetRefBound(sal_False));
521cdf0e10cSrcweir 
522cdf0e10cSrcweir 		// set the select row
523cdf0e10cSrcweir 		m_aSelectRow = new OValueRefVector(m_aSQLIterator.getSelectColumns()->get().size());
524cdf0e10cSrcweir 		::std::for_each(m_aSelectRow->get().begin(),m_aSelectRow->get().end(),TSetRefBound(sal_True));
525cdf0e10cSrcweir 
526cdf0e10cSrcweir 		// create the column mapping
527cdf0e10cSrcweir 		createColumnMapping();
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 		m_pSQLAnalyzer	= createAnalyzer();
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 		Reference<XIndexesSupplier> xIndexSup(xTunnel,UNO_QUERY);
532cdf0e10cSrcweir 		if(xIndexSup.is())
533cdf0e10cSrcweir 			m_pSQLAnalyzer->setIndexes(xIndexSup->getIndexes());
534cdf0e10cSrcweir 
535cdf0e10cSrcweir 		anylizeSQL();
536cdf0e10cSrcweir 	}
537cdf0e10cSrcweir 	else
538cdf0e10cSrcweir 		throw SQLException(aErr,*this,::rtl::OUString(),0,Any());
539cdf0e10cSrcweir }
540cdf0e10cSrcweir // -----------------------------------------------------------------------------
createColumnMapping()541cdf0e10cSrcweir void OStatement_Base::createColumnMapping()
542cdf0e10cSrcweir {
543cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::createColumnMapping" );
544cdf0e10cSrcweir 	// initialize the column index map (mapping select columns to table columns)
545cdf0e10cSrcweir 	::vos::ORef<connectivity::OSQLColumns>	xColumns = m_aSQLIterator.getSelectColumns();
546cdf0e10cSrcweir 	m_aColMapping.resize(xColumns->get().size() + 1);
547cdf0e10cSrcweir 	for (sal_Int32 i=0; i<(sal_Int32)m_aColMapping.size(); ++i)
548cdf0e10cSrcweir 		m_aColMapping[i] = i;
549cdf0e10cSrcweir 
550cdf0e10cSrcweir 	Reference<XIndexAccess> xNames(m_xColNames,UNO_QUERY);
551cdf0e10cSrcweir 	// now check which columns are bound
552cdf0e10cSrcweir 	OResultSet::setBoundedColumns(m_aRow,m_aSelectRow,xColumns,xNames,sal_True,m_xDBMetaData,m_aColMapping);
553cdf0e10cSrcweir }
554cdf0e10cSrcweir // -----------------------------------------------------------------------------
initializeResultSet(OResultSet * _pResult)555cdf0e10cSrcweir void OStatement_Base::initializeResultSet(OResultSet* _pResult)
556cdf0e10cSrcweir {
557cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::initializeResultSet" );
558cdf0e10cSrcweir 	GetAssignValues();
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 	_pResult->setSqlAnalyzer(m_pSQLAnalyzer);
561cdf0e10cSrcweir 	_pResult->setOrderByColumns(m_aOrderbyColumnNumber);
562cdf0e10cSrcweir 	_pResult->setOrderByAscending(m_aOrderbyAscending);
563cdf0e10cSrcweir 	_pResult->setBindingRow(m_aRow);
564cdf0e10cSrcweir 	_pResult->setColumnMapping(m_aColMapping);
565cdf0e10cSrcweir 	_pResult->setEvaluationRow(m_aEvaluateRow);
566cdf0e10cSrcweir 	_pResult->setAssignValues(m_aAssignValues);
567cdf0e10cSrcweir 	_pResult->setSelectRow(m_aSelectRow);
568cdf0e10cSrcweir 
569cdf0e10cSrcweir 	m_pSQLAnalyzer->bindSelectRow(m_aRow);
570cdf0e10cSrcweir 	m_pEvaluationKeySet = m_pSQLAnalyzer->bindEvaluationRow(m_aEvaluateRow);	// Werte im Code des Compilers setzen
571cdf0e10cSrcweir 	_pResult->setEvaluationKeySet(m_pEvaluationKeySet);
572cdf0e10cSrcweir }
573cdf0e10cSrcweir // -----------------------------------------------------------------------------
GetAssignValues()574cdf0e10cSrcweir void OStatement_Base::GetAssignValues()
575cdf0e10cSrcweir {
576cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::GetAssignValues" );
577cdf0e10cSrcweir 	if (m_pParseTree == NULL)
578cdf0e10cSrcweir 	{
579cdf0e10cSrcweir 		::dbtools::throwFunctionSequenceException(*this);
580cdf0e10cSrcweir 		return;
581cdf0e10cSrcweir 	}
582cdf0e10cSrcweir 
583cdf0e10cSrcweir 	if (SQL_ISRULE(m_pParseTree,select_statement))
584cdf0e10cSrcweir 		// Keine zu setzenden Werte bei SELECT
585cdf0e10cSrcweir 		return;
586cdf0e10cSrcweir 	else if (SQL_ISRULE(m_pParseTree,insert_statement))
587cdf0e10cSrcweir 	{
588cdf0e10cSrcweir 		// Row fuer die zu setzenden Werte anlegen (Referenz durch new)
589cdf0e10cSrcweir 		if(m_aAssignValues.isValid())
590cdf0e10cSrcweir 			m_aAssignValues->get().clear();
591cdf0e10cSrcweir 		sal_Int32 nCount = Reference<XIndexAccess>(m_xColNames,UNO_QUERY)->getCount();
592cdf0e10cSrcweir 		m_aAssignValues = new OAssignValues(nCount);
593cdf0e10cSrcweir 		// unbound all
594cdf0e10cSrcweir 		::std::for_each(m_aAssignValues->get().begin()+1,m_aAssignValues->get().end(),TSetRefBound(sal_False));
595cdf0e10cSrcweir 
596cdf0e10cSrcweir 		m_aParameterIndexes.resize(nCount+1,SQL_NO_PARAMETER);
597cdf0e10cSrcweir 
598cdf0e10cSrcweir 		// Liste der Columns-Namen, die in der column_commalist vorkommen (mit ; getrennt):
599cdf0e10cSrcweir 		::std::vector<String> aColumnNameList;
600cdf0e10cSrcweir 
601cdf0e10cSrcweir 		OSL_ENSURE(m_pParseTree->count() >= 4,"OResultSet: Fehler im Parse Tree");
602cdf0e10cSrcweir 
603cdf0e10cSrcweir 		OSQLParseNode * pOptColumnCommalist = m_pParseTree->getChild(3);
604cdf0e10cSrcweir 		OSL_ENSURE(pOptColumnCommalist != NULL,"OResultSet: Fehler im Parse Tree");
605cdf0e10cSrcweir 		OSL_ENSURE(SQL_ISRULE(pOptColumnCommalist,opt_column_commalist),"OResultSet: Fehler im Parse Tree");
606cdf0e10cSrcweir 		if (pOptColumnCommalist->count() == 0)
607cdf0e10cSrcweir 		{
608cdf0e10cSrcweir 			const Sequence< ::rtl::OUString>& aNames = m_xColNames->getElementNames();
609cdf0e10cSrcweir 			const ::rtl::OUString* pBegin = aNames.getConstArray();
610cdf0e10cSrcweir 			const ::rtl::OUString* pEnd = pBegin + aNames.getLength();
611cdf0e10cSrcweir             for (; pBegin != pEnd; ++pBegin)
612cdf0e10cSrcweir                 aColumnNameList.push_back(*pBegin);
613cdf0e10cSrcweir 		}
614cdf0e10cSrcweir 		else
615cdf0e10cSrcweir 		{
616cdf0e10cSrcweir 			OSL_ENSURE(pOptColumnCommalist->count() == 3,"OResultSet: Fehler im Parse Tree");
617cdf0e10cSrcweir 
618cdf0e10cSrcweir 			OSQLParseNode * pColumnCommalist = pOptColumnCommalist->getChild(1);
619cdf0e10cSrcweir 			OSL_ENSURE(pColumnCommalist != NULL,"OResultSet: Fehler im Parse Tree");
620cdf0e10cSrcweir 			OSL_ENSURE(SQL_ISRULE(pColumnCommalist,column_commalist),"OResultSet: Fehler im Parse Tree");
621cdf0e10cSrcweir 			OSL_ENSURE(pColumnCommalist->count() > 0,"OResultSet: Fehler im Parse Tree");
622cdf0e10cSrcweir 
623cdf0e10cSrcweir 			// Alle Columns in der column_commalist ...
624cdf0e10cSrcweir 			for (sal_uInt32 i = 0; i < pColumnCommalist->count(); i++)
625cdf0e10cSrcweir 			{
626cdf0e10cSrcweir 				OSQLParseNode * pCol = pColumnCommalist->getChild(i);
627cdf0e10cSrcweir 				OSL_ENSURE(pCol != NULL,"OResultSet: Fehler im Parse Tree");
628cdf0e10cSrcweir 				aColumnNameList.push_back(pCol->getTokenValue());
629cdf0e10cSrcweir 			}
630cdf0e10cSrcweir 		}
631cdf0e10cSrcweir 		if ( aColumnNameList.empty() )
632cdf0e10cSrcweir 			throwFunctionSequenceException(*this);
633cdf0e10cSrcweir 
634cdf0e10cSrcweir 		// Werte ...
635cdf0e10cSrcweir 		OSQLParseNode * pValuesOrQuerySpec = m_pParseTree->getChild(4);
636cdf0e10cSrcweir 		OSL_ENSURE(pValuesOrQuerySpec != NULL,"OResultSet: pValuesOrQuerySpec darf nicht NULL sein!");
637cdf0e10cSrcweir 		OSL_ENSURE(SQL_ISRULE(pValuesOrQuerySpec,values_or_query_spec),"OResultSet: ! SQL_ISRULE(pValuesOrQuerySpec,values_or_query_spec)");
638cdf0e10cSrcweir 		OSL_ENSURE(pValuesOrQuerySpec->count() > 0,"OResultSet: pValuesOrQuerySpec->count() <= 0");
639cdf0e10cSrcweir 
640cdf0e10cSrcweir 		// nur "VALUES" ist erlaubt ...
641cdf0e10cSrcweir 		if (! SQL_ISTOKEN(pValuesOrQuerySpec->getChild(0),VALUES))
642cdf0e10cSrcweir 			throwFunctionSequenceException(*this);
643cdf0e10cSrcweir 
644cdf0e10cSrcweir 		OSL_ENSURE(pValuesOrQuerySpec->count() == 4,"OResultSet: pValuesOrQuerySpec->count() != 4");
645cdf0e10cSrcweir 
646cdf0e10cSrcweir 		// Liste von Werten
647cdf0e10cSrcweir 		OSQLParseNode * pInsertAtomCommalist = pValuesOrQuerySpec->getChild(2);
648cdf0e10cSrcweir 		OSL_ENSURE(pInsertAtomCommalist != NULL,"OResultSet: pInsertAtomCommalist darf nicht NULL sein!");
649cdf0e10cSrcweir 		OSL_ENSURE(pInsertAtomCommalist->count() > 0,"OResultSet: pInsertAtomCommalist <= 0");
650cdf0e10cSrcweir 
651cdf0e10cSrcweir 		String aColumnName;
652cdf0e10cSrcweir 		OSQLParseNode * pRow_Value_Const;
653cdf0e10cSrcweir 		xub_StrLen nIndex=0;
654cdf0e10cSrcweir 		for (sal_uInt32 i = 0; i < pInsertAtomCommalist->count(); i++)
655cdf0e10cSrcweir 		{
656cdf0e10cSrcweir 			pRow_Value_Const = pInsertAtomCommalist->getChild(i); // row_value_constructor
657cdf0e10cSrcweir 			OSL_ENSURE(pRow_Value_Const != NULL,"OResultSet: pRow_Value_Const darf nicht NULL sein!");
658cdf0e10cSrcweir 			if(SQL_ISRULE(pRow_Value_Const,parameter))
659cdf0e10cSrcweir 			{
660cdf0e10cSrcweir 				ParseAssignValues(aColumnNameList,pRow_Value_Const,nIndex++); // kann nur ein Columnname vorhanden sein pro Schleife
661cdf0e10cSrcweir 			}
662cdf0e10cSrcweir 			else if(pRow_Value_Const->isToken())
663cdf0e10cSrcweir 				ParseAssignValues(aColumnNameList,pRow_Value_Const,static_cast<xub_StrLen>(i));
664cdf0e10cSrcweir 			else
665cdf0e10cSrcweir 			{
666cdf0e10cSrcweir 				if(pRow_Value_Const->count() == aColumnNameList.size())
667cdf0e10cSrcweir 				{
668cdf0e10cSrcweir 					for (sal_uInt32 j = 0; j < pRow_Value_Const->count(); ++j)
669cdf0e10cSrcweir 						ParseAssignValues(aColumnNameList,pRow_Value_Const->getChild(j),nIndex++);
670cdf0e10cSrcweir 				}
671cdf0e10cSrcweir 				else
672cdf0e10cSrcweir 					throwFunctionSequenceException(*this);
673cdf0e10cSrcweir 			}
674cdf0e10cSrcweir 		}
675cdf0e10cSrcweir 	}
676cdf0e10cSrcweir 	else if (SQL_ISRULE(m_pParseTree,update_statement_searched))
677cdf0e10cSrcweir 	{
678cdf0e10cSrcweir 		if(m_aAssignValues.isValid())
679cdf0e10cSrcweir 			m_aAssignValues->get().clear();
680cdf0e10cSrcweir 		sal_Int32 nCount = Reference<XIndexAccess>(m_xColNames,UNO_QUERY)->getCount();
681cdf0e10cSrcweir 		m_aAssignValues = new OAssignValues(nCount);
682cdf0e10cSrcweir 		// unbound all
683cdf0e10cSrcweir 		::std::for_each(m_aAssignValues->get().begin()+1,m_aAssignValues->get().end(),TSetRefBound(sal_False));
684cdf0e10cSrcweir 
685cdf0e10cSrcweir 		m_aParameterIndexes.resize(nCount+1,SQL_NO_PARAMETER);
686cdf0e10cSrcweir 
687cdf0e10cSrcweir 		OSL_ENSURE(m_pParseTree->count() >= 4,"OResultSet: Fehler im Parse Tree");
688cdf0e10cSrcweir 
689cdf0e10cSrcweir 		OSQLParseNode * pAssignmentCommalist = m_pParseTree->getChild(3);
690cdf0e10cSrcweir 		OSL_ENSURE(pAssignmentCommalist != NULL,"OResultSet: pAssignmentCommalist == NULL");
691cdf0e10cSrcweir 		OSL_ENSURE(SQL_ISRULE(pAssignmentCommalist,assignment_commalist),"OResultSet: Fehler im Parse Tree");
692cdf0e10cSrcweir 		OSL_ENSURE(pAssignmentCommalist->count() > 0,"OResultSet: pAssignmentCommalist->count() <= 0");
693cdf0e10cSrcweir 
694cdf0e10cSrcweir 		// Alle Zuweisungen (Kommaliste) bearbeiten ...
695cdf0e10cSrcweir 		::std::vector< String> aList(1);
696cdf0e10cSrcweir 		for (sal_uInt32 i = 0; i < pAssignmentCommalist->count(); i++)
697cdf0e10cSrcweir 		{
698cdf0e10cSrcweir 			OSQLParseNode * pAssignment = pAssignmentCommalist->getChild(i);
699cdf0e10cSrcweir 			OSL_ENSURE(pAssignment != NULL,"OResultSet: pAssignment == NULL");
700cdf0e10cSrcweir 			OSL_ENSURE(SQL_ISRULE(pAssignment,assignment),"OResultSet: Fehler im Parse Tree");
701cdf0e10cSrcweir 			OSL_ENSURE(pAssignment->count() == 3,"OResultSet: pAssignment->count() != 3");
702cdf0e10cSrcweir 
703cdf0e10cSrcweir 			OSQLParseNode * pCol = pAssignment->getChild(0);
704cdf0e10cSrcweir 			OSL_ENSURE(pCol != NULL,"OResultSet: pCol == NULL");
705cdf0e10cSrcweir 
706cdf0e10cSrcweir 			OSQLParseNode * pComp = pAssignment->getChild(1);
707cdf0e10cSrcweir 			OSL_ENSURE(pComp != NULL,"OResultSet: pComp == NULL");
708cdf0e10cSrcweir 			OSL_ENSURE(pComp->getNodeType() == SQL_NODE_EQUAL,"OResultSet: pComp->getNodeType() != SQL_NODE_COMPARISON");
709cdf0e10cSrcweir 			if (pComp->getTokenValue().toChar() != '=')
710cdf0e10cSrcweir 			{
711cdf0e10cSrcweir 				//	aStatus.SetInvalidStatement();
712cdf0e10cSrcweir 				throwFunctionSequenceException(*this);
713cdf0e10cSrcweir 			}
714cdf0e10cSrcweir 
715cdf0e10cSrcweir 			OSQLParseNode * pVal = pAssignment->getChild(2);
716cdf0e10cSrcweir 			OSL_ENSURE(pVal != NULL,"OResultSet: pVal == NULL");
717cdf0e10cSrcweir 			aList[0] = pCol->getTokenValue();
718cdf0e10cSrcweir 			ParseAssignValues(aList,pVal,0);
719cdf0e10cSrcweir 		}
720cdf0e10cSrcweir 
721cdf0e10cSrcweir 	}
722cdf0e10cSrcweir }
723cdf0e10cSrcweir // -------------------------------------------------------------------------
ParseAssignValues(const::std::vector<String> & aColumnNameList,OSQLParseNode * pRow_Value_Constructor_Elem,xub_StrLen nIndex)724cdf0e10cSrcweir void OStatement_Base::ParseAssignValues(const ::std::vector< String>& aColumnNameList,OSQLParseNode* pRow_Value_Constructor_Elem,xub_StrLen nIndex)
725cdf0e10cSrcweir {
726cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::ParseAssignValues" );
727cdf0e10cSrcweir 	OSL_ENSURE(nIndex <= aColumnNameList.size(),"SdbFileCursor::ParseAssignValues: nIndex > aColumnNameList.GetTokenCount()");
728cdf0e10cSrcweir 	String aColumnName(aColumnNameList[nIndex]);
729cdf0e10cSrcweir 	OSL_ENSURE(aColumnName.Len() > 0,"OResultSet: Column-Name nicht gefunden");
730cdf0e10cSrcweir 	OSL_ENSURE(pRow_Value_Constructor_Elem != NULL,"OResultSet: pRow_Value_Constructor_Elem darf nicht NULL sein!");
731cdf0e10cSrcweir 
732cdf0e10cSrcweir 	if (pRow_Value_Constructor_Elem->getNodeType() == SQL_NODE_STRING ||
733cdf0e10cSrcweir 		pRow_Value_Constructor_Elem->getNodeType() == SQL_NODE_INTNUM ||
734cdf0e10cSrcweir 		pRow_Value_Constructor_Elem->getNodeType() == SQL_NODE_APPROXNUM)
735cdf0e10cSrcweir 	{
736cdf0e10cSrcweir 		// Wert setzen:
737cdf0e10cSrcweir 		SetAssignValue(aColumnName, pRow_Value_Constructor_Elem->getTokenValue());
738cdf0e10cSrcweir 	}
739cdf0e10cSrcweir 	else if (SQL_ISTOKEN(pRow_Value_Constructor_Elem,NULL))
740cdf0e10cSrcweir 	{
741cdf0e10cSrcweir 		// NULL setzen
742cdf0e10cSrcweir 		SetAssignValue(aColumnName, String(), sal_True);
743cdf0e10cSrcweir 	}
744cdf0e10cSrcweir 	else if (SQL_ISRULE(pRow_Value_Constructor_Elem,parameter))
745cdf0e10cSrcweir 		parseParamterElem(aColumnName,pRow_Value_Constructor_Elem);
746cdf0e10cSrcweir 	else
747cdf0e10cSrcweir 	{
748cdf0e10cSrcweir 		//	aStatus.SetStatementTooComplex();
749cdf0e10cSrcweir 		throwFunctionSequenceException(*this);
750cdf0e10cSrcweir 	}
751cdf0e10cSrcweir }
752cdf0e10cSrcweir //------------------------------------------------------------------
SetAssignValue(const String & aColumnName,const String & aValue,sal_Bool bSetNull,sal_uInt32 nParameter)753cdf0e10cSrcweir void OStatement_Base::SetAssignValue(const String& aColumnName,
754cdf0e10cSrcweir 								   const String& aValue,
755cdf0e10cSrcweir 								   sal_Bool bSetNull,
756cdf0e10cSrcweir 								   sal_uInt32 nParameter)
757cdf0e10cSrcweir {
758cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::SetAssignValue" );
759cdf0e10cSrcweir 	Reference<XPropertySet> xCol;
760cdf0e10cSrcweir 	m_xColNames->getByName(aColumnName) >>= xCol;
761cdf0e10cSrcweir 	sal_Int32 nId = Reference<XColumnLocate>(m_xColNames,UNO_QUERY)->findColumn(aColumnName);
762cdf0e10cSrcweir 	// Kommt diese Column ueberhaupt in der Datei vor?
763cdf0e10cSrcweir 
764cdf0e10cSrcweir 	if (!xCol.is())
765cdf0e10cSrcweir 	{
766cdf0e10cSrcweir 		// Diese Column gibt es nicht!
767cdf0e10cSrcweir //		aStatus.Set(SQL_STAT_ERROR,
768cdf0e10cSrcweir //					String::CreateFromAscii("S0022"),
769cdf0e10cSrcweir //					aStatus.CreateErrorMessage(String(SdbResId(STR_STAT_COLUMN_NOT_FOUND))),
770cdf0e10cSrcweir //					0, String() );
771cdf0e10cSrcweir 		throwFunctionSequenceException(*this);
772cdf0e10cSrcweir 	}
773cdf0e10cSrcweir 
774cdf0e10cSrcweir 	// Value an die Row mit den zuzuweisenden Werten binden:
775cdf0e10cSrcweir 	//	const ODbVariantRef& xValue = (*aAssignValues)[pFileColumn->GetId()];
776cdf0e10cSrcweir 
777cdf0e10cSrcweir 	// Alles geprueft und wir haben den Namen der Column.
778cdf0e10cSrcweir 	// Jetzt eine Value allozieren, den Wert setzen und die Value an die Row binden.
779cdf0e10cSrcweir 	if (bSetNull)
780cdf0e10cSrcweir 		(m_aAssignValues->get())[nId]->setNull();
781cdf0e10cSrcweir 	else
782cdf0e10cSrcweir 	{
783cdf0e10cSrcweir 		switch (::comphelper::getINT32(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))))
784cdf0e10cSrcweir 		{
785cdf0e10cSrcweir 			// Kriterium je nach Typ als String oder double in die Variable packen ...
786cdf0e10cSrcweir 			case DataType::CHAR:
787cdf0e10cSrcweir 			case DataType::VARCHAR:
788cdf0e10cSrcweir             case DataType::LONGVARCHAR:
789cdf0e10cSrcweir 				*(m_aAssignValues->get())[nId] = ORowSetValue(aValue);
790cdf0e10cSrcweir 				// Zeichensatz ist bereits konvertiert, da ja das gesamte Statement konvertiert wurde
791cdf0e10cSrcweir 				break;
792cdf0e10cSrcweir 
793cdf0e10cSrcweir 			case DataType::BIT:
794cdf0e10cSrcweir 				{
795cdf0e10cSrcweir 					if (aValue.EqualsIgnoreCaseAscii("TRUE")  || aValue.GetChar(0) == '1')
796cdf0e10cSrcweir 						*(m_aAssignValues->get())[nId] = sal_True;
797cdf0e10cSrcweir 					else if (aValue.EqualsIgnoreCaseAscii("FALSE") || aValue.GetChar(0) == '0')
798cdf0e10cSrcweir 						*(m_aAssignValues->get())[nId] = sal_False;
799cdf0e10cSrcweir 					else
800cdf0e10cSrcweir 					{
801cdf0e10cSrcweir 						//	aStatus.Set(SQL_STAT_ERROR);	// nyi: genauer!
802cdf0e10cSrcweir 						throwFunctionSequenceException(*this);
803cdf0e10cSrcweir 					}
804cdf0e10cSrcweir 				}
805cdf0e10cSrcweir 				break;
806cdf0e10cSrcweir 			case DataType::TINYINT:
807cdf0e10cSrcweir 			case DataType::SMALLINT:
808cdf0e10cSrcweir 			case DataType::INTEGER:
809cdf0e10cSrcweir 			case DataType::DECIMAL:
810cdf0e10cSrcweir 			case DataType::NUMERIC:
811cdf0e10cSrcweir 			case DataType::REAL:
812cdf0e10cSrcweir 			case DataType::DOUBLE:
813cdf0e10cSrcweir 			case DataType::DATE:
814cdf0e10cSrcweir 			case DataType::TIME:
815cdf0e10cSrcweir 			case DataType::TIMESTAMP:
816cdf0e10cSrcweir 			{
817cdf0e10cSrcweir 				*(m_aAssignValues->get())[nId] = ORowSetValue(aValue); // .ToDouble
818cdf0e10cSrcweir //				try
819cdf0e10cSrcweir //				{
820cdf0e10cSrcweir //					double n = xValue->toDouble();
821cdf0e10cSrcweir //					xValue->setDouble(n);
822cdf0e10cSrcweir //				}
823cdf0e10cSrcweir //				catch ( ... )
824cdf0e10cSrcweir //				{
825cdf0e10cSrcweir //					aStatus.SetDriverNotCapableError();
826cdf0e10cSrcweir //				}
827cdf0e10cSrcweir 			}	break;
828cdf0e10cSrcweir 			default:
829cdf0e10cSrcweir 				throwFunctionSequenceException(*this);
830cdf0e10cSrcweir 		}
831cdf0e10cSrcweir 	}
832cdf0e10cSrcweir 
833cdf0e10cSrcweir 	// Parameter-Nr. merken (als User Data)
834cdf0e10cSrcweir 	// SQL_NO_PARAMETER = kein Parameter.
835cdf0e10cSrcweir 	m_aAssignValues->setParameterIndex(nId,nParameter);
836cdf0e10cSrcweir 	if(nParameter != SQL_NO_PARAMETER)
837cdf0e10cSrcweir 		m_aParameterIndexes[nParameter] = nId;
838cdf0e10cSrcweir }
839cdf0e10cSrcweir // -----------------------------------------------------------------------------
parseParamterElem(const String &,OSQLParseNode *)840cdf0e10cSrcweir void OStatement_Base::parseParamterElem(const String& /*_sColumnName*/,OSQLParseNode* /*pRow_Value_Constructor_Elem*/)
841cdf0e10cSrcweir {
842cdf0e10cSrcweir     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OStatement_Base::parseParamterElem" );
843cdf0e10cSrcweir 	// do nothing here
844cdf0e10cSrcweir }
845cdf0e10cSrcweir // =============================================================================
846cdf0e10cSrcweir 	} // namespace file
847cdf0e10cSrcweir // =============================================================================
848cdf0e10cSrcweir }// namespace connectivity
849cdf0e10cSrcweir // -----------------------------------------------------------------------------
850