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