1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 #include <stdio.h>
32 #include "connectivity/sdbcx/VColumn.hxx"
33 #include <osl/diagnose.h>
34 #include "file/FPreparedStatement.hxx"
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include "file/FResultSetMetaData.hxx"
37 #include <cppuhelper/typeprovider.hxx>
38 #include <comphelper/sequence.hxx>
39 #include <com/sun/star/lang/DisposedException.hpp>
40 #include "connectivity/dbconversion.hxx"
41 #include "connectivity/dbexception.hxx"
42 #include "connectivity/dbtools.hxx"
43 #include "connectivity/PColumn.hxx"
44 #include "diagnose_ex.h"
45 #include <comphelper/types.hxx>
46 #include <com/sun/star/sdbc/ColumnValue.hpp>
47 #include <tools/debug.hxx>
48 #include "resource/file_res.hrc"
49 #include <rtl/logfile.hxx>
50 
51 using namespace connectivity;
52 using namespace comphelper;
53 using namespace ::dbtools;
54 using namespace connectivity::file;
55 using namespace com::sun::star::uno;
56 using namespace com::sun::star::lang;
57 using namespace com::sun::star::beans;
58 using namespace com::sun::star::sdbc;
59 using namespace com::sun::star::sdbcx;
60 using namespace com::sun::star::container;
61 using namespace com::sun::star::util;
62 
63 IMPLEMENT_SERVICE_INFO(OPreparedStatement,"com.sun.star.sdbc.driver.file.PreparedStatement","com.sun.star.sdbc.PreparedStatement");
64 
65 DBG_NAME( file_OPreparedStatement )
66 // -------------------------------------------------------------------------
67 OPreparedStatement::OPreparedStatement( OConnection* _pConnection)
68 	: OStatement_BASE2( _pConnection )
69 	,m_pResultSet(NULL)
70 {
71     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::OPreparedStatement" );
72 	DBG_CTOR( file_OPreparedStatement, NULL );
73 }
74 
75 // -------------------------------------------------------------------------
76 OPreparedStatement::~OPreparedStatement()
77 {
78     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::~OPreparedStatement" );
79 	DBG_DTOR( file_OPreparedStatement, NULL );
80 }
81 
82 // -------------------------------------------------------------------------
83 void OPreparedStatement::disposing()
84 {
85     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::disposing" );
86 	::osl::MutexGuard aGuard(m_aMutex);
87 
88     clearMyResultSet();
89     OStatement_BASE2::disposing();
90 
91     if(m_pResultSet)
92 	{
93 		m_pResultSet->release();
94 		m_pResultSet = NULL;
95 	}
96 
97     m_xParamColumns = NULL;
98     m_xMetaData.clear();
99 	if(m_aParameterRow.isValid())
100 	{
101 		m_aParameterRow->get().clear();
102 		m_aParameterRow = NULL;
103 	}
104 
105 
106 }
107 // -------------------------------------------------------------------------
108 void OPreparedStatement::construct(const ::rtl::OUString& sql)  throw(SQLException, RuntimeException)
109 {
110     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::construct" );
111 	OStatement_Base::construct(sql);
112 
113 	m_aParameterRow = new OValueRefVector();
114 	m_aParameterRow->get().push_back(new ORowSetValueDecorator(sal_Int32(0)) );
115 
116 	Reference<XIndexAccess> xNames(m_xColNames,UNO_QUERY);
117 
118 	if ( m_aSQLIterator.getStatementType() == SQL_STATEMENT_SELECT )
119 		m_xParamColumns = m_aSQLIterator.getParameters();
120 	else
121 	{
122 		m_xParamColumns = new OSQLColumns();
123 		// describe all parameters need for the resultset
124 		describeParameter();
125 	}
126 
127 	OValueRefRow aTemp;
128 	OResultSet::setBoundedColumns(m_aEvaluateRow,aTemp,m_xParamColumns,xNames,sal_False,m_xDBMetaData,m_aColMapping);
129 
130 	m_pResultSet = createResultSet();
131 	m_pResultSet->acquire();
132 	m_xResultSet = Reference<XResultSet>(m_pResultSet);
133 	initializeResultSet(m_pResultSet);
134 }
135 // -------------------------------------------------------------------------
136 
137 Any SAL_CALL OPreparedStatement::queryInterface( const Type & rType ) throw(RuntimeException)
138 {
139     //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::queryInterface" );
140 	Any aRet = OStatement_BASE2::queryInterface(rType);
141 	return aRet.hasValue() ? aRet : ::cppu::queryInterface(	rType,
142                                         static_cast< XPreparedStatement*>(this),
143                                         static_cast< XParameters*>(this),
144                                         static_cast< XResultSetMetaDataSupplier*>(this));
145 }
146 // -------------------------------------------------------------------------
147 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL OPreparedStatement::getTypes(  ) throw(::com::sun::star::uno::RuntimeException)
148 {
149     //RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::getTypes" );
150         ::cppu::OTypeCollection aTypes( ::getCppuType( (const ::com::sun::star::uno::Reference< XPreparedStatement > *)0 ),
151                                         ::getCppuType( (const ::com::sun::star::uno::Reference< XParameters > *)0 ),
152                                         ::getCppuType( (const ::com::sun::star::uno::Reference< XResultSetMetaDataSupplier > *)0 ));
153 
154 	return ::comphelper::concatSequences(aTypes.getTypes(),OStatement_BASE2::getTypes());
155 }
156 // -------------------------------------------------------------------------
157 
158 Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData(  ) throw(SQLException, RuntimeException)
159 {
160     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::getMetaData" );
161 	::osl::MutexGuard aGuard( m_aMutex );
162 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
163 
164 
165 	if(!m_xMetaData.is())
166 		m_xMetaData = new OResultSetMetaData(m_aSQLIterator.getSelectColumns(),m_aSQLIterator.getTables().begin()->first,m_pTable);
167 	return m_xMetaData;
168 }
169 // -------------------------------------------------------------------------
170 
171 void SAL_CALL OPreparedStatement::close(  ) throw(SQLException, RuntimeException)
172 {
173     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::close" );
174 	::osl::MutexGuard aGuard( m_aMutex );
175 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
176 
177 
178 	clearMyResultSet();
179 }
180 // -------------------------------------------------------------------------
181 
182 sal_Bool SAL_CALL OPreparedStatement::execute(  ) throw(SQLException, RuntimeException)
183 {
184     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::execute" );
185 	::osl::MutexGuard aGuard( m_aMutex );
186 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
187 
188 	initResultSet();
189 
190 	return m_aSQLIterator.getStatementType() == SQL_STATEMENT_SELECT;
191 }
192 // -------------------------------------------------------------------------
193 
194 sal_Int32 SAL_CALL OPreparedStatement::executeUpdate(  ) throw(SQLException, RuntimeException)
195 {
196     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::executeUpdate" );
197 	::osl::MutexGuard aGuard( m_aMutex );
198 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
199 
200 	initResultSet();
201 
202 	return m_pResultSet ? m_pResultSet->getRowCountResult() : sal_Int32(0);
203 }
204 // -------------------------------------------------------------------------
205 
206 void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const ::rtl::OUString& x ) throw(SQLException, RuntimeException)
207 {
208     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setString" );
209 	setParameter(parameterIndex,x);
210 }
211 // -------------------------------------------------------------------------
212 
213 Reference< XConnection > SAL_CALL OPreparedStatement::getConnection(  ) throw(SQLException, RuntimeException)
214 {
215     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::getConnection" );
216 	::osl::MutexGuard aGuard( m_aMutex );
217 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
218 
219 	return (Reference< XConnection >)m_pConnection;
220 }
221 // -------------------------------------------------------------------------
222 
223 Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery(  ) throw(SQLException, RuntimeException)
224 {
225     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::executeQuery" );
226 	::osl::MutexGuard aGuard( m_aMutex );
227 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
228 
229 	return initResultSet();
230 }
231 // -------------------------------------------------------------------------
232 
233 void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x ) throw(SQLException, RuntimeException)
234 {
235     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setBoolean" );
236 	setParameter(parameterIndex,x);
237 }
238 // -------------------------------------------------------------------------
239 void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x ) throw(SQLException, RuntimeException)
240 {
241     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setByte" );
242 	setParameter(parameterIndex,x);
243 }
244 // -------------------------------------------------------------------------
245 
246 void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const Date& aData ) throw(SQLException, RuntimeException)
247 {
248     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setDate" );
249 	setParameter(parameterIndex,DBTypeConversion::toDouble(aData));
250 }
251 // -------------------------------------------------------------------------
252 void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const Time& aVal ) throw(SQLException, RuntimeException)
253 {
254     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setTime" );
255 	setParameter(parameterIndex,DBTypeConversion::toDouble(aVal));
256 }
257 // -------------------------------------------------------------------------
258 
259 void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const DateTime& aVal ) throw(SQLException, RuntimeException)
260 {
261     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setTimestamp" );
262 	setParameter(parameterIndex,DBTypeConversion::toDouble(aVal));
263 }
264 // -------------------------------------------------------------------------
265 
266 void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x ) throw(SQLException, RuntimeException)
267 {
268     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setDouble" );
269 	setParameter(parameterIndex,x);
270 }
271 
272 // -------------------------------------------------------------------------
273 
274 void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x ) throw(SQLException, RuntimeException)
275 {
276     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setFloat" );
277 	setParameter(parameterIndex,x);
278 }
279 // -------------------------------------------------------------------------
280 
281 void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x ) throw(SQLException, RuntimeException)
282 {
283     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setInt" );
284 	setParameter(parameterIndex,x);
285 }
286 // -------------------------------------------------------------------------
287 
288 void SAL_CALL OPreparedStatement::setLong( sal_Int32 /*parameterIndex*/, sal_Int64 /*aVal*/ ) throw(SQLException, RuntimeException)
289 {
290     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setLong" );
291     throwFeatureNotImplementedException( "XParameters::setLong", *this );
292 }
293 // -------------------------------------------------------------------------
294 
295 void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 /*sqlType*/ ) throw(SQLException, RuntimeException)
296 {
297     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setNull" );
298 	::osl::MutexGuard aGuard( m_aMutex );
299 	checkAndResizeParameters(parameterIndex);
300 
301 	if ( m_aAssignValues.isValid() )
302 		(m_aAssignValues->get())[m_aParameterIndexes[parameterIndex]]->setNull();
303 	else
304 		(m_aParameterRow->get())[parameterIndex]->setNull();
305 }
306 // -------------------------------------------------------------------------
307 
308 void SAL_CALL OPreparedStatement::setClob( sal_Int32 /*parameterIndex*/, const Reference< XClob >& /*x*/ ) throw(SQLException, RuntimeException)
309 {
310     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setClob" );
311     throwFeatureNotImplementedException( "XParameters::setClob", *this );
312 }
313 // -------------------------------------------------------------------------
314 
315 void SAL_CALL OPreparedStatement::setBlob( sal_Int32 /*parameterIndex*/, const Reference< XBlob >& /*x*/ ) throw(SQLException, RuntimeException)
316 {
317     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setBlob" );
318     throwFeatureNotImplementedException( "XParameters::setBlob", *this );
319 }
320 // -------------------------------------------------------------------------
321 
322 void SAL_CALL OPreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const Reference< XArray >& /*x*/ ) throw(SQLException, RuntimeException)
323 {
324     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setArray" );
325     throwFeatureNotImplementedException( "XParameters::setArray", *this );
326 }
327 // -------------------------------------------------------------------------
328 
329 void SAL_CALL OPreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const Reference< XRef >& /*x*/ ) throw(SQLException, RuntimeException)
330 {
331     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setRef" );
332     throwFeatureNotImplementedException( "XParameters::setRef", *this );
333 }
334 // -------------------------------------------------------------------------
335 
336 void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 sqlType, sal_Int32 scale ) throw(SQLException, RuntimeException)
337 {
338     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setObjectWithInfo" );
339     switch(sqlType)
340 	{
341         case DataType::DECIMAL:
342         case DataType::NUMERIC:
343             setString(parameterIndex,::comphelper::getString(x));
344             break;
345         default:
346             ::dbtools::setObjectWithInfo(this,parameterIndex,x,sqlType,scale);
347             break;
348     }
349 }
350 // -------------------------------------------------------------------------
351 
352 void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const ::rtl::OUString& /*typeName*/ ) throw(SQLException, RuntimeException)
353 {
354     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setObjectNull" );
355 	setNull(parameterIndex,sqlType);
356 }
357 // -------------------------------------------------------------------------
358 
359 void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x ) throw(SQLException, RuntimeException)
360 {
361     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setObject" );
362 	if(!::dbtools::implSetObject(this,parameterIndex,x))
363 	{
364         const ::rtl::OUString sError( m_pConnection->getResources().getResourceStringWithSubstitution(
365                 STR_UNKNOWN_PARA_TYPE,
366                 "$position$", ::rtl::OUString::valueOf(parameterIndex)
367              ) );
368 		::dbtools::throwGenericSQLException(sError,*this);
369 	}
370 	//	setObject (parameterIndex, x, sqlType, 0);
371 }
372 // -------------------------------------------------------------------------
373 
374 void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x ) throw(SQLException, RuntimeException)
375 {
376     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setShort" );
377 	setParameter(parameterIndex,x);
378 }
379 // -------------------------------------------------------------------------
380 
381 void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x ) throw(SQLException, RuntimeException)
382 {
383     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setBytes" );
384 	setParameter(parameterIndex,x);
385 }
386 // -------------------------------------------------------------------------
387 
388 
389 void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException)
390 {
391     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setCharacterStream" );
392 	setBinaryStream(parameterIndex,x,length );
393 }
394 // -------------------------------------------------------------------------
395 
396 void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException)
397 {
398     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setBinaryStream" );
399 	if(!x.is())
400 		::dbtools::throwFunctionSequenceException(*this);
401 
402 	Sequence<sal_Int8> aSeq;
403 	x->readBytes(aSeq,length);
404 	setParameter(parameterIndex,aSeq);
405 }
406 // -------------------------------------------------------------------------
407 
408 void SAL_CALL OPreparedStatement::clearParameters(  ) throw(SQLException, RuntimeException)
409 {
410     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::clearParameters" );
411 	::osl::MutexGuard aGuard( m_aMutex );
412 	checkDisposed(OStatement_BASE::rBHelper.bDisposed);
413 
414 	m_aParameterRow->get().clear();
415 	m_aParameterRow->get().push_back(new ORowSetValueDecorator(sal_Int32(0)) );
416 }
417 // -------------------------------------------------------------------------
418 OResultSet* OPreparedStatement::createResultSet()
419 {
420     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::createResultSet" );
421 	return new OResultSet(this,m_aSQLIterator);
422 }
423 // -----------------------------------------------------------------------------
424 Reference<XResultSet> OPreparedStatement::initResultSet()
425 {
426     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::initResultSet" );
427 	m_pResultSet->clear();
428 	Reference<XResultSet> xRs(m_pResultSet);
429 
430 	// check if we got enough paramters
431 	if ( (m_aParameterRow.isValid() && ( m_aParameterRow->get().size() -1 ) < m_xParamColumns->get().size()) ||
432 		 (m_xParamColumns.isValid() && !m_aParameterRow.isValid() && !m_aParameterRow->get().empty()) )
433          m_pConnection->throwGenericSQLException(STR_INVALID_PARA_COUNT,*this);
434 
435     m_pResultSet->OpenImpl();
436     m_pResultSet->setMetaData(getMetaData());
437 
438 	return xRs;
439 }
440 // -----------------------------------------------------------------------------
441 void SAL_CALL OPreparedStatement::acquire() throw()
442 {
443 	OStatement_BASE2::acquire();
444 }
445 // -----------------------------------------------------------------------------
446 void SAL_CALL OPreparedStatement::release() throw()
447 {
448 	OStatement_BASE2::release();
449 }
450 // -----------------------------------------------------------------------------
451 void OPreparedStatement::checkAndResizeParameters(sal_Int32 parameterIndex)
452 {
453     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::checkAndResizeParameters" );
454 	::connectivity::checkDisposed(OStatement_BASE::rBHelper.bDisposed);
455 	if ( m_aAssignValues.isValid() && (parameterIndex < 1 || parameterIndex >= static_cast<sal_Int32>(m_aParameterIndexes.size())) )
456 		throwInvalidIndexException(*this);
457 	else if ( static_cast<sal_Int32>((m_aParameterRow->get()).size()) <= parameterIndex )
458 	{
459 		sal_Int32 i = m_aParameterRow->get().size();
460 		(m_aParameterRow->get()).resize(parameterIndex+1);
461 		for ( ;i <= parameterIndex+1; ++i )
462 		{
463 			if ( !(m_aParameterRow->get())[i].isValid() )
464 				(m_aParameterRow->get())[i] = new ORowSetValueDecorator;
465 		}
466 	}
467 }
468 // -----------------------------------------------------------------------------
469 void OPreparedStatement::setParameter(sal_Int32 parameterIndex, const ORowSetValue& x)
470 {
471     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::setParameter" );
472 	::osl::MutexGuard aGuard( m_aMutex );
473 	checkAndResizeParameters(parameterIndex);
474 
475 	if(m_aAssignValues.isValid())
476 		*(m_aAssignValues->get())[m_aParameterIndexes[parameterIndex]] = x;
477 	else
478 		*((m_aParameterRow->get())[parameterIndex]) = x;
479 }
480 // -----------------------------------------------------------------------------
481 sal_uInt32 OPreparedStatement::AddParameter(OSQLParseNode * pParameter, const Reference<XPropertySet>& _xCol)
482 {
483     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::AddParameter" );
484     OSL_UNUSED( pParameter );
485 	OSL_ENSURE(SQL_ISRULE(pParameter,parameter),"OResultSet::AddParameter: Argument ist kein Parameter");
486 	OSL_ENSURE(pParameter->count() > 0,"OResultSet: Fehler im Parse Tree");
487 #if OSL_DEBUG_LEVEL > 0
488 	OSQLParseNode * pMark = pParameter->getChild(0);
489     OSL_UNUSED( pMark );
490 #endif
491 
492 	::rtl::OUString sParameterName;
493 	// Parameter-Column aufsetzen:
494 	sal_Int32 eType = DataType::VARCHAR;
495 	sal_uInt32 nPrecision = 255;
496 	sal_Int32 nScale = 0;
497 	sal_Int32 nNullable = ColumnValue::NULLABLE;
498 
499 	if (_xCol.is())
500 	{
501 		// Typ, Precision, Scale ... der angegebenen Column verwenden,
502 		// denn dieser Column wird der Wert zugewiesen bzw. mit dieser
503 		// Column wird der Wert verglichen.
504 		_xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))			>>= eType;
505 		_xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))	>>= nPrecision;
506 		_xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))		>>= nScale;
507 		_xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))	>>= nNullable;
508 		_xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))			>>= sParameterName;
509 	}
510 
511 	Reference<XPropertySet> xParaColumn = new connectivity::parse::OParseColumn(sParameterName
512 													,::rtl::OUString()
513 													,::rtl::OUString()
514                                                     ,::rtl::OUString()
515 													,nNullable
516 													,nPrecision
517 													,nScale
518 													,eType
519 													,sal_False
520 													,sal_False
521 													,m_aSQLIterator.isCaseSensitive());
522 	m_xParamColumns->get().push_back(xParaColumn);
523 	return m_xParamColumns->get().size();
524 }
525 // -----------------------------------------------------------------------------
526 void OPreparedStatement::describeColumn(OSQLParseNode* _pParameter,OSQLParseNode* _pNode,const OSQLTable& _xTable)
527 {
528     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::describeColumn" );
529 	Reference<XPropertySet> xProp;
530 	if(SQL_ISRULE(_pNode,column_ref))
531 	{
532 		::rtl::OUString sColumnName,sTableRange;
533 		m_aSQLIterator.getColumnRange(_pNode,sColumnName,sTableRange);
534 		if ( sColumnName.getLength() )
535 		{
536 			Reference<XNameAccess> xNameAccess = _xTable->getColumns();
537 			if(xNameAccess->hasByName(sColumnName))
538 				xNameAccess->getByName(sColumnName) >>= xProp;
539 			AddParameter(_pParameter,xProp);
540 		}
541 	}
542 	//	else
543 		//	AddParameter(_pParameter,xProp);
544 }
545 // -------------------------------------------------------------------------
546 void OPreparedStatement::describeParameter()
547 {
548     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::describeParameter" );
549 	::std::vector< OSQLParseNode*> aParseNodes;
550 	scanParameter(m_pParseTree,aParseNodes);
551 	if ( !aParseNodes.empty() )
552 	{
553 		//	m_xParamColumns = new OSQLColumns();
554 		const OSQLTables& xTabs = m_aSQLIterator.getTables();
555 		if( !xTabs.empty() )
556 		{
557 			OSQLTable xTable = xTabs.begin()->second;
558 			::std::vector< OSQLParseNode*>::const_iterator aIter = aParseNodes.begin();
559 			for (;aIter != aParseNodes.end();++aIter )
560 			{
561 				describeColumn(*aIter,(*aIter)->getParent()->getChild(0),xTable);
562 			}
563 		}
564 	}
565 }
566 // -----------------------------------------------------------------------------
567 void OPreparedStatement::initializeResultSet(OResultSet* _pResult)
568 {
569     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::initializeResultSet" );
570 	OStatement_Base::initializeResultSet(_pResult);
571 
572 	m_pResultSet->setParameterColumns(m_xParamColumns);
573 	m_pResultSet->setParameterRow(m_aParameterRow);
574 
575 	// Parameter substituieren (AssignValues und Kriterien):
576 	if (!m_xParamColumns->get().empty())
577 	{
578         // Zunaechst AssignValues
579 		sal_uInt16 nParaCount=0; // gibt die aktuelle Anzahl der bisher gesetzen Parameter an
580 
581 		// Nach zu substituierenden Parametern suchen:
582         size_t nCount = m_aAssignValues.isValid() ? m_aAssignValues->get().size() : 1; // 1 ist wichtig fuer die Kriterien
583         for (size_t j = 1; j < nCount; j++)
584 		{
585 			sal_uInt32 nParameter = (*m_aAssignValues).getParameterIndex(j);
586 			if (nParameter == SQL_NO_PARAMETER)
587 				continue;	// dieser AssignValue ist kein Parameter
588 
589 			++nParaCount; // ab hier ist der Parameter gueltig
590 			// Parameter ersetzen. Wenn Parameter nicht verfuegbar,
591 			//	Value auf NULL setzen.
592 			//	(*m_aAssignValues)[j] = (*m_aParameterRow)[(sal_uInt16)nParameter];
593 		}
594 
595 		if (m_aParameterRow.isValid() &&  (m_xParamColumns->get().size()+1) != m_aParameterRow->get().size() )
596         {
597             sal_Int32 i = m_aParameterRow->get().size();
598             sal_Int32 nParamColumns = m_xParamColumns->get().size()+1;
599 		    m_aParameterRow->get().resize(nParamColumns);
600 		    for ( ;i < nParamColumns; ++i )
601 		    {
602 			    if ( !(m_aParameterRow->get())[i].isValid() )
603 				    (m_aParameterRow->get())[i] = new ORowSetValueDecorator;
604 		    }
605 			//m_aParameterRow->resize(m_xParamColumns->size()+1);
606         }
607 		if (m_aParameterRow.isValid() && nParaCount < m_aParameterRow->get().size() )
608 		{
609 
610 			m_pSQLAnalyzer->bindParameterRow(m_aParameterRow);
611 		}
612 	}
613 }
614 // -----------------------------------------------------------------------------
615 void OPreparedStatement::parseParamterElem(const String& _sColumnName,OSQLParseNode* pRow_Value_Constructor_Elem)
616 {
617     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "file", "Ocke.Janssen@sun.com", "OPreparedStatement::parseParamterElem" );
618 	Reference<XPropertySet> xCol;
619 	m_xColNames->getByName(_sColumnName) >>= xCol;
620 	sal_Int32 nParameter = -1;
621 	if(m_xParamColumns.isValid())
622 	{
623 		OSQLColumns::Vector::const_iterator aIter = find(m_xParamColumns->get().begin(),m_xParamColumns->get().end(),_sColumnName,::comphelper::UStringMixEqual(m_pTable->isCaseSensitive()));
624 		if(aIter != m_xParamColumns->get().end())
625 			nParameter = m_xParamColumns->get().size() - (m_xParamColumns->get().end() - aIter) + 1;// +1 because the rows start at 1
626 	}
627 	if(nParameter == -1)
628 		nParameter = AddParameter(pRow_Value_Constructor_Elem,xCol);
629 	// Nr. des Parameters in der Variablen merken:
630 	SetAssignValue(_sColumnName, String(), sal_True, nParameter);
631 }
632 // -----------------------------------------------------------------------------
633 
634 
635