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