Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / file / FPreparedStatement.cxx
blob6fb706bde90ac4e26958295881eba5791f36d025
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <stdio.h>
22 #include "connectivity/sdbcx/VColumn.hxx"
23 #include <osl/diagnose.h>
24 #include "file/FPreparedStatement.hxx"
25 #include <com/sun/star/sdbc/DataType.hpp>
26 #include "file/FResultSetMetaData.hxx"
27 #include <cppuhelper/typeprovider.hxx>
28 #include <comphelper/sequence.hxx>
29 #include <com/sun/star/lang/DisposedException.hpp>
30 #include "connectivity/dbconversion.hxx"
31 #include "connectivity/dbexception.hxx"
32 #include "connectivity/dbtools.hxx"
33 #include "connectivity/PColumn.hxx"
34 #include "diagnose_ex.h"
35 #include <comphelper/types.hxx>
36 #include <com/sun/star/sdbc/ColumnValue.hpp>
37 #include <tools/debug.hxx>
38 #include "resource/file_res.hrc"
40 using namespace connectivity;
41 using namespace comphelper;
42 using namespace ::dbtools;
43 using namespace connectivity::file;
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::lang;
46 using namespace com::sun::star::beans;
47 using namespace com::sun::star::sdbc;
48 using namespace com::sun::star::sdbcx;
49 using namespace com::sun::star::container;
50 using namespace com::sun::star;
52 IMPLEMENT_SERVICE_INFO(OPreparedStatement,"com.sun.star.sdbc.driver.file.PreparedStatement","com.sun.star.sdbc.PreparedStatement");
54 OPreparedStatement::OPreparedStatement( OConnection* _pConnection)
55 : OStatement_BASE2( _pConnection )
60 OPreparedStatement::~OPreparedStatement()
65 void OPreparedStatement::disposing()
67 ::osl::MutexGuard aGuard(m_aMutex);
69 OStatement_BASE2::disposing();
71 m_xParamColumns = NULL;
72 m_xMetaData.clear();
73 if(m_aParameterRow.is())
75 m_aParameterRow->get().clear();
76 m_aParameterRow = NULL;
80 void OPreparedStatement::construct(const OUString& sql) throw(SQLException, RuntimeException)
82 OStatement_Base::construct(sql);
84 m_aParameterRow = new OValueRefVector();
85 m_aParameterRow->get().push_back(new ORowSetValueDecorator(sal_Int32(0)) );
87 Reference<XIndexAccess> xNames(m_xColNames,UNO_QUERY);
89 if ( m_aSQLIterator.getStatementType() == SQL_STATEMENT_SELECT )
90 m_xParamColumns = m_aSQLIterator.getParameters();
91 else
93 m_xParamColumns = new OSQLColumns();
94 // describe all parameters need for the resultset
95 describeParameter();
98 OValueRefRow aTemp;
99 OResultSet::setBoundedColumns(m_aEvaluateRow,aTemp,m_xParamColumns,xNames,false,m_xDBMetaData,m_aColMapping);
102 Reference<XResultSet> OPreparedStatement::makeResultSet()
104 OResultSet *pResultSet = createResultSet();
105 Reference<XResultSet> xRS(pResultSet);
106 initializeResultSet(pResultSet);
107 initResultSet(pResultSet);
108 return xRS;
113 Any SAL_CALL OPreparedStatement::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
115 Any aRet = OStatement_BASE2::queryInterface(rType);
116 return aRet.hasValue() ? aRet : ::cppu::queryInterface( rType,
117 static_cast< XPreparedStatement*>(this),
118 static_cast< XParameters*>(this),
119 static_cast< XResultSetMetaDataSupplier*>(this));
122 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL OPreparedStatement::getTypes( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
124 ::cppu::OTypeCollection aTypes( ::getCppuType( (const ::com::sun::star::uno::Reference< XPreparedStatement > *)0 ),
125 ::getCppuType( (const ::com::sun::star::uno::Reference< XParameters > *)0 ),
126 ::getCppuType( (const ::com::sun::star::uno::Reference< XResultSetMetaDataSupplier > *)0 ));
128 return ::comphelper::concatSequences(aTypes.getTypes(),OStatement_BASE2::getTypes());
132 Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData( ) throw(SQLException, RuntimeException, std::exception)
134 ::osl::MutexGuard aGuard( m_aMutex );
135 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
138 if(!m_xMetaData.is())
139 m_xMetaData = new OResultSetMetaData(m_aSQLIterator.getSelectColumns(),m_aSQLIterator.getTables().begin()->first,m_pTable);
140 return m_xMetaData;
144 void SAL_CALL OPreparedStatement::close( ) throw(SQLException, RuntimeException, std::exception)
146 ::osl::MutexGuard aGuard( m_aMutex );
147 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
151 sal_Bool SAL_CALL OPreparedStatement::execute( ) throw(SQLException, RuntimeException, std::exception)
153 ::osl::MutexGuard aGuard( m_aMutex );
154 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
156 Reference<XResultSet> xRS(makeResultSet());
158 // since we don't support the XMultipleResults interface, nobody will ever get that ResultSet...
159 Reference< XComponent > xComp(xRS, UNO_QUERY);
160 if (xComp.is())
161 xComp->dispose();
163 return m_aSQLIterator.getStatementType() == SQL_STATEMENT_SELECT;
167 sal_Int32 SAL_CALL OPreparedStatement::executeUpdate( ) throw(SQLException, RuntimeException, std::exception)
169 ::osl::MutexGuard aGuard( m_aMutex );
170 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
172 Reference<XResultSet> xRS(makeResultSet());
173 if(xRS.is())
175 assert(dynamic_cast<OResultSet*>(xRS.get()));
176 const sal_Int32 res(static_cast<OResultSet*>(xRS.get())->getRowCountResult());
177 // nobody will ever get that ResultSet...
178 Reference< XComponent > xComp(xRS, UNO_QUERY);
179 assert(xComp.is());
180 if (xComp.is())
181 xComp->dispose();
182 return res;
184 else
185 return 0;
189 void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x ) throw(SQLException, RuntimeException, std::exception)
191 setParameter(parameterIndex,x);
195 Reference< XConnection > SAL_CALL OPreparedStatement::getConnection( ) throw(SQLException, RuntimeException, std::exception)
197 ::osl::MutexGuard aGuard( m_aMutex );
198 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
200 return (Reference< XConnection >)m_pConnection;
204 Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery( ) throw(SQLException, RuntimeException, std::exception)
206 ::osl::MutexGuard aGuard( m_aMutex );
207 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
209 return makeResultSet();
213 void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x ) throw(SQLException, RuntimeException, std::exception)
215 setParameter(parameterIndex,static_cast<bool>(x));
218 void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x ) throw(SQLException, RuntimeException, std::exception)
220 setParameter(parameterIndex,x);
224 void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const util::Date& aData ) throw(SQLException, RuntimeException, std::exception)
226 setParameter(parameterIndex,DBTypeConversion::toDouble(aData));
229 void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const util::Time& aVal ) throw(SQLException, RuntimeException, std::exception)
231 setParameter(parameterIndex,DBTypeConversion::toDouble(aVal));
235 void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const util::DateTime& aVal ) throw(SQLException, RuntimeException, std::exception)
237 setParameter(parameterIndex,DBTypeConversion::toDouble(aVal));
241 void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x ) throw(SQLException, RuntimeException, std::exception)
243 setParameter(parameterIndex,x);
248 void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x ) throw(SQLException, RuntimeException, std::exception)
250 setParameter(parameterIndex,x);
254 void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x ) throw(SQLException, RuntimeException, std::exception)
256 setParameter(parameterIndex,x);
260 void SAL_CALL OPreparedStatement::setLong( sal_Int32 /*parameterIndex*/, sal_Int64 /*aVal*/ ) throw(SQLException, RuntimeException, std::exception)
262 throwFeatureNotImplementedException( "XParameters::setLong", *this );
266 void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 /*sqlType*/ ) throw(SQLException, RuntimeException, std::exception)
268 ::osl::MutexGuard aGuard( m_aMutex );
269 checkAndResizeParameters(parameterIndex);
271 if ( m_aAssignValues.is() )
272 (m_aAssignValues->get())[m_aParameterIndexes[parameterIndex]]->setNull();
273 else
274 (m_aParameterRow->get())[parameterIndex]->setNull();
278 void SAL_CALL OPreparedStatement::setClob( sal_Int32 /*parameterIndex*/, const Reference< XClob >& /*x*/ ) throw(SQLException, RuntimeException, std::exception)
280 throwFeatureNotImplementedException( "XParameters::setClob", *this );
284 void SAL_CALL OPreparedStatement::setBlob( sal_Int32 /*parameterIndex*/, const Reference< XBlob >& /*x*/ ) throw(SQLException, RuntimeException, std::exception)
286 throwFeatureNotImplementedException( "XParameters::setBlob", *this );
290 void SAL_CALL OPreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const Reference< XArray >& /*x*/ ) throw(SQLException, RuntimeException, std::exception)
292 throwFeatureNotImplementedException( "XParameters::setArray", *this );
296 void SAL_CALL OPreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const Reference< XRef >& /*x*/ ) throw(SQLException, RuntimeException, std::exception)
298 throwFeatureNotImplementedException( "XParameters::setRef", *this );
302 void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 sqlType, sal_Int32 scale ) throw(SQLException, RuntimeException, std::exception)
304 switch(sqlType)
306 case DataType::DECIMAL:
307 case DataType::NUMERIC:
308 setString(parameterIndex,::comphelper::getString(x));
309 break;
310 default:
311 ::dbtools::setObjectWithInfo(this,parameterIndex,x,sqlType,scale);
312 break;
317 void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const OUString& /*typeName*/ ) throw(SQLException, RuntimeException, std::exception)
319 setNull(parameterIndex,sqlType);
323 void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x ) throw(SQLException, RuntimeException, std::exception)
325 if(!::dbtools::implSetObject(this,parameterIndex,x))
327 const OUString sError( m_pConnection->getResources().getResourceStringWithSubstitution(
328 STR_UNKNOWN_PARA_TYPE,
329 "$position$", OUString::number(parameterIndex)
330 ) );
331 ::dbtools::throwGenericSQLException(sError,*this);
333 // setObject (parameterIndex, x, sqlType, 0);
337 void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x ) throw(SQLException, RuntimeException, std::exception)
339 setParameter(parameterIndex,x);
343 void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x ) throw(SQLException, RuntimeException, std::exception)
345 setParameter(parameterIndex,x);
350 void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
352 setBinaryStream(parameterIndex,x,length );
356 void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< ::com::sun::star::io::XInputStream >& x, sal_Int32 length ) throw(SQLException, RuntimeException, std::exception)
358 if(!x.is())
359 ::dbtools::throwFunctionSequenceException(*this);
361 Sequence<sal_Int8> aSeq;
362 x->readBytes(aSeq,length);
363 setParameter(parameterIndex,aSeq);
367 void SAL_CALL OPreparedStatement::clearParameters( ) throw(SQLException, RuntimeException, std::exception)
369 ::osl::MutexGuard aGuard( m_aMutex );
370 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
372 m_aParameterRow->get().clear();
373 m_aParameterRow->get().push_back(new ORowSetValueDecorator(sal_Int32(0)) );
376 OResultSet* OPreparedStatement::createResultSet()
378 return new OResultSet(this,m_aSQLIterator);
381 void OPreparedStatement::initResultSet(OResultSet *pResultSet)
383 // check if we got enough parameters
384 if ( (m_aParameterRow.is() && ( m_aParameterRow->get().size() -1 ) < m_xParamColumns->get().size()) ||
385 (m_xParamColumns.is() && !m_aParameterRow.is() && !m_aParameterRow->get().empty()) )
386 m_pConnection->throwGenericSQLException(STR_INVALID_PARA_COUNT,*this);
388 pResultSet->OpenImpl();
389 pResultSet->setMetaData(getMetaData());
392 void SAL_CALL OPreparedStatement::acquire() throw()
394 OStatement_BASE2::acquire();
397 void SAL_CALL OPreparedStatement::release() throw()
399 OStatement_BASE2::release();
402 void OPreparedStatement::checkAndResizeParameters(sal_Int32 parameterIndex)
404 ::connectivity::checkDisposed(OStatement_BASE::rBHelper.bDisposed);
405 if ( m_aAssignValues.is() && (parameterIndex < 1 || parameterIndex >= static_cast<sal_Int32>(m_aParameterIndexes.size())) )
406 throwInvalidIndexException(*this);
407 else if ( static_cast<sal_Int32>((m_aParameterRow->get()).size()) <= parameterIndex )
409 sal_Int32 i = m_aParameterRow->get().size();
410 (m_aParameterRow->get()).resize(parameterIndex+1);
411 for ( ;i <= parameterIndex+1; ++i )
413 if ( !(m_aParameterRow->get())[i].is() )
414 (m_aParameterRow->get())[i] = new ORowSetValueDecorator;
419 void OPreparedStatement::setParameter(sal_Int32 parameterIndex, const ORowSetValue& x)
421 ::osl::MutexGuard aGuard( m_aMutex );
422 checkAndResizeParameters(parameterIndex);
424 if(m_aAssignValues.is())
425 *(m_aAssignValues->get())[m_aParameterIndexes[parameterIndex]] = x;
426 else
427 *((m_aParameterRow->get())[parameterIndex]) = x;
430 sal_uInt32 OPreparedStatement::AddParameter(OSQLParseNode * pParameter, const Reference<XPropertySet>& _xCol)
432 OSL_UNUSED( pParameter );
433 OSL_ENSURE(SQL_ISRULE(pParameter,parameter),"OResultSet::AddParameter: Argument ist kein Parameter");
434 OSL_ENSURE(pParameter->count() > 0,"OResultSet: Fehler im Parse Tree");
435 #if OSL_DEBUG_LEVEL > 0
436 OSQLParseNode * pMark = pParameter->getChild(0);
437 OSL_UNUSED( pMark );
438 #endif
440 OUString sParameterName;
441 // set up Parameter-Column:
442 sal_Int32 eType = DataType::VARCHAR;
443 sal_uInt32 nPrecision = 255;
444 sal_Int32 nScale = 0;
445 sal_Int32 nNullable = ColumnValue::NULLABLE;
447 if (_xCol.is())
449 // Use type, precision, scale ... from the given column,
450 // because this Column will get a value assigned or
451 // with this Column the value will be compared.
452 _xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= eType;
453 _xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)) >>= nPrecision;
454 _xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE)) >>= nScale;
455 _xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE)) >>= nNullable;
456 _xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= sParameterName;
459 Reference<XPropertySet> xParaColumn = new connectivity::parse::OParseColumn(sParameterName
460 ,OUString()
461 ,OUString()
462 ,OUString()
463 ,nNullable
464 ,nPrecision
465 ,nScale
466 ,eType
467 ,false
468 ,false
469 ,m_aSQLIterator.isCaseSensitive()
470 ,OUString()
471 ,OUString()
472 ,OUString());
473 m_xParamColumns->get().push_back(xParaColumn);
474 return m_xParamColumns->get().size();
477 void OPreparedStatement::describeColumn(OSQLParseNode* _pParameter,OSQLParseNode* _pNode,const OSQLTable& _xTable)
479 Reference<XPropertySet> xProp;
480 if(SQL_ISRULE(_pNode,column_ref))
482 OUString sColumnName,sTableRange;
483 m_aSQLIterator.getColumnRange(_pNode,sColumnName,sTableRange);
484 if ( !sColumnName.isEmpty() )
486 Reference<XNameAccess> xNameAccess = _xTable->getColumns();
487 if(xNameAccess->hasByName(sColumnName))
488 xNameAccess->getByName(sColumnName) >>= xProp;
489 AddParameter(_pParameter,xProp);
492 // else
493 // AddParameter(_pParameter,xProp);
496 void OPreparedStatement::describeParameter()
498 ::std::vector< OSQLParseNode*> aParseNodes;
499 scanParameter(m_pParseTree,aParseNodes);
500 if ( !aParseNodes.empty() )
502 // m_xParamColumns = new OSQLColumns();
503 const OSQLTables& xTabs = m_aSQLIterator.getTables();
504 if( !xTabs.empty() )
506 OSQLTable xTable = xTabs.begin()->second;
507 ::std::vector< OSQLParseNode*>::const_iterator aIter = aParseNodes.begin();
508 for (;aIter != aParseNodes.end();++aIter )
510 describeColumn(*aIter,(*aIter)->getParent()->getChild(0),xTable);
516 void OPreparedStatement::initializeResultSet(OResultSet* pRS)
518 OStatement_Base::initializeResultSet(pRS);
520 pRS->setParameterColumns(m_xParamColumns);
521 pRS->setParameterRow(m_aParameterRow);
523 // Substitute parameter (AssignValues and criteria):
524 if (!m_xParamColumns->get().empty())
526 // begin with AssignValues
527 sal_uInt16 nParaCount=0; // gives the current number of previously set Parameters
529 // search for parameters to be substituted:
530 size_t nCount = m_aAssignValues.is() ? m_aAssignValues->get().size() : 1; // 1 is important for the Criteria
531 for (size_t j = 1; j < nCount; j++)
533 sal_uInt32 nParameter = (*m_aAssignValues).getParameterIndex(j);
534 if (nParameter == SQL_NO_PARAMETER)
535 continue; // this AssignValue is no Parameter
537 ++nParaCount; // now the Parameter is valid
540 if (m_aParameterRow.is() && (m_xParamColumns->get().size()+1) != m_aParameterRow->get().size() )
542 sal_Int32 i = m_aParameterRow->get().size();
543 sal_Int32 nParamColumns = m_xParamColumns->get().size()+1;
544 m_aParameterRow->get().resize(nParamColumns);
545 for ( ;i < nParamColumns; ++i )
547 if ( !(m_aParameterRow->get())[i].is() )
548 (m_aParameterRow->get())[i] = new ORowSetValueDecorator;
551 if (m_aParameterRow.is() && nParaCount < m_aParameterRow->get().size() )
552 m_pSQLAnalyzer->bindParameterRow(m_aParameterRow);
556 void OPreparedStatement::parseParamterElem(const OUString& _sColumnName, OSQLParseNode* pRow_Value_Constructor_Elem)
558 Reference<XPropertySet> xCol;
559 m_xColNames->getByName(_sColumnName) >>= xCol;
560 sal_Int32 nParameter = -1;
561 if(m_xParamColumns.is())
563 OSQLColumns::Vector::const_iterator aIter = find(m_xParamColumns->get().begin(),m_xParamColumns->get().end(),_sColumnName,::comphelper::UStringMixEqual(m_pTable->isCaseSensitive()));
564 if(aIter != m_xParamColumns->get().end())
565 nParameter = m_xParamColumns->get().size() - (m_xParamColumns->get().end() - aIter) + 1;// +1 because the rows start at 1
567 if(nParameter == -1)
568 nParameter = AddParameter(pRow_Value_Constructor_Elem,xCol);
569 // Save number of parameter in the variable:
570 SetAssignValue(_sColumnName, OUString(), true, nParameter);
575 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */