1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <connectivity/sdbcx/VColumn.hxx>
22 #include <osl/diagnose.h>
23 #include "file/FPreparedStatement.hxx"
24 #include <com/sun/star/sdbc/DataType.hpp>
25 #include "file/FResultSetMetaData.hxx"
26 #include <cppuhelper/queryinterface.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 "resource/file_res.hrc"
39 using namespace connectivity
;
40 using namespace comphelper
;
41 using namespace ::dbtools
;
42 using namespace connectivity::file
;
43 using namespace com::sun::star::uno
;
44 using namespace com::sun::star::lang
;
45 using namespace com::sun::star::beans
;
46 using namespace com::sun::star::sdbc
;
47 using namespace com::sun::star::sdbcx
;
48 using namespace com::sun::star::container
;
49 using namespace com::sun::star
;
51 IMPLEMENT_SERVICE_INFO(OPreparedStatement
,"com.sun.star.sdbc.driver.file.PreparedStatement","com.sun.star.sdbc.PreparedStatement");
53 OPreparedStatement::OPreparedStatement( OConnection
* _pConnection
)
54 : OStatement_BASE2( _pConnection
)
59 OPreparedStatement::~OPreparedStatement()
64 void OPreparedStatement::disposing()
66 ::osl::MutexGuard
aGuard(m_aMutex
);
68 OStatement_BASE2::disposing();
70 m_xParamColumns
= NULL
;
72 if(m_aParameterRow
.is())
74 m_aParameterRow
->get().clear();
75 m_aParameterRow
= NULL
;
79 void OPreparedStatement::construct(const OUString
& sql
) throw(SQLException
, RuntimeException
)
81 OStatement_Base::construct(sql
);
83 m_aParameterRow
= new OValueRefVector();
84 m_aParameterRow
->get().push_back(new ORowSetValueDecorator(sal_Int32(0)) );
86 Reference
<XIndexAccess
> xNames(m_xColNames
,UNO_QUERY
);
88 if ( m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
)
89 m_xParamColumns
= m_aSQLIterator
.getParameters();
92 m_xParamColumns
= new OSQLColumns();
93 // describe all parameters need for the resultset
98 OResultSet::setBoundedColumns(m_aEvaluateRow
,aTemp
,m_xParamColumns
,xNames
,false,m_xDBMetaData
,m_aColMapping
);
101 rtl::Reference
<OResultSet
> OPreparedStatement::makeResultSet()
105 rtl::Reference
<OResultSet
> xResultSet(createResultSet());
106 m_xResultSet
= xResultSet
.get();
107 initializeResultSet(xResultSet
.get());
108 initResultSet(xResultSet
.get());
112 Any SAL_CALL
OPreparedStatement::queryInterface( const Type
& rType
) throw(RuntimeException
, std::exception
)
114 Any aRet
= OStatement_BASE2::queryInterface(rType
);
115 return aRet
.hasValue() ? aRet
: ::cppu::queryInterface( rType
,
116 static_cast< XPreparedStatement
*>(this),
117 static_cast< XParameters
*>(this),
118 static_cast< XResultSetMetaDataSupplier
*>(this));
121 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> SAL_CALL
OPreparedStatement::getTypes( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
123 ::cppu::OTypeCollection
aTypes( cppu::UnoType
<XPreparedStatement
>::get(),
124 cppu::UnoType
<XParameters
>::get(),
125 cppu::UnoType
<XResultSetMetaDataSupplier
>::get());
127 return ::comphelper::concatSequences(aTypes
.getTypes(),OStatement_BASE2::getTypes());
131 Reference
< XResultSetMetaData
> SAL_CALL
OPreparedStatement::getMetaData( ) throw(SQLException
, RuntimeException
, std::exception
)
133 ::osl::MutexGuard
aGuard( m_aMutex
);
134 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
137 if(!m_xMetaData
.is())
138 m_xMetaData
= new OResultSetMetaData(m_aSQLIterator
.getSelectColumns(),m_aSQLIterator
.getTables().begin()->first
,m_pTable
);
143 void SAL_CALL
OPreparedStatement::close( ) throw(SQLException
, RuntimeException
, std::exception
)
145 ::osl::MutexGuard
aGuard( m_aMutex
);
146 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
152 sal_Bool SAL_CALL
OPreparedStatement::execute( ) throw(SQLException
, RuntimeException
, std::exception
)
154 ::osl::MutexGuard
aGuard( m_aMutex
);
155 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
157 rtl::Reference
<OResultSet
> xRS(makeResultSet());
158 // since we don't support the XMultipleResults interface, nobody will ever get that ResultSet...
162 return m_aSQLIterator
.getStatementType() == SQL_STATEMENT_SELECT
;
166 sal_Int32 SAL_CALL
OPreparedStatement::executeUpdate( ) throw(SQLException
, RuntimeException
, std::exception
)
168 ::osl::MutexGuard
aGuard( m_aMutex
);
169 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
171 rtl::Reference
<OResultSet
> xRS(makeResultSet());
174 const sal_Int32
res(xRS
->getRowCountResult());
175 // nobody will ever get that ResultSet...
184 void SAL_CALL
OPreparedStatement::setString( sal_Int32 parameterIndex
, const OUString
& x
) throw(SQLException
, RuntimeException
, std::exception
)
186 setParameter(parameterIndex
,x
);
190 Reference
< XConnection
> SAL_CALL
OPreparedStatement::getConnection( ) throw(SQLException
, RuntimeException
, std::exception
)
192 ::osl::MutexGuard
aGuard( m_aMutex
);
193 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
195 return Reference
< XConnection
>(m_pConnection
);
199 Reference
< XResultSet
> SAL_CALL
OPreparedStatement::executeQuery( ) throw(SQLException
, RuntimeException
, std::exception
)
201 ::osl::MutexGuard
aGuard( m_aMutex
);
202 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
204 return makeResultSet().get();
208 void SAL_CALL
OPreparedStatement::setBoolean( sal_Int32 parameterIndex
, sal_Bool x
) throw(SQLException
, RuntimeException
, std::exception
)
210 setParameter(parameterIndex
,static_cast<bool>(x
));
213 void SAL_CALL
OPreparedStatement::setByte( sal_Int32 parameterIndex
, sal_Int8 x
) throw(SQLException
, RuntimeException
, std::exception
)
215 setParameter(parameterIndex
,x
);
219 void SAL_CALL
OPreparedStatement::setDate( sal_Int32 parameterIndex
, const util::Date
& aData
) throw(SQLException
, RuntimeException
, std::exception
)
221 setParameter(parameterIndex
,DBTypeConversion::toDouble(aData
));
224 void SAL_CALL
OPreparedStatement::setTime( sal_Int32 parameterIndex
, const util::Time
& aVal
) throw(SQLException
, RuntimeException
, std::exception
)
226 setParameter(parameterIndex
,DBTypeConversion::toDouble(aVal
));
230 void SAL_CALL
OPreparedStatement::setTimestamp( sal_Int32 parameterIndex
, const util::DateTime
& aVal
) throw(SQLException
, RuntimeException
, std::exception
)
232 setParameter(parameterIndex
,DBTypeConversion::toDouble(aVal
));
236 void SAL_CALL
OPreparedStatement::setDouble( sal_Int32 parameterIndex
, double x
) throw(SQLException
, RuntimeException
, std::exception
)
238 setParameter(parameterIndex
,x
);
243 void SAL_CALL
OPreparedStatement::setFloat( sal_Int32 parameterIndex
, float x
) throw(SQLException
, RuntimeException
, std::exception
)
245 setParameter(parameterIndex
,x
);
249 void SAL_CALL
OPreparedStatement::setInt( sal_Int32 parameterIndex
, sal_Int32 x
) throw(SQLException
, RuntimeException
, std::exception
)
251 setParameter(parameterIndex
,x
);
255 void SAL_CALL
OPreparedStatement::setLong( sal_Int32
/*parameterIndex*/, sal_Int64
/*aVal*/ ) throw(SQLException
, RuntimeException
, std::exception
)
257 throwFeatureNotImplementedSQLException( "XParameters::setLong", *this );
261 void SAL_CALL
OPreparedStatement::setNull( sal_Int32 parameterIndex
, sal_Int32
/*sqlType*/ ) throw(SQLException
, RuntimeException
, std::exception
)
263 ::osl::MutexGuard
aGuard( m_aMutex
);
264 checkAndResizeParameters(parameterIndex
);
266 if ( m_aAssignValues
.is() )
267 (m_aAssignValues
->get())[m_aParameterIndexes
[parameterIndex
]]->setNull();
269 (m_aParameterRow
->get())[parameterIndex
]->setNull();
273 void SAL_CALL
OPreparedStatement::setClob( sal_Int32
/*parameterIndex*/, const Reference
< XClob
>& /*x*/ ) throw(SQLException
, RuntimeException
, std::exception
)
275 throwFeatureNotImplementedSQLException( "XParameters::setClob", *this );
279 void SAL_CALL
OPreparedStatement::setBlob( sal_Int32
/*parameterIndex*/, const Reference
< XBlob
>& /*x*/ ) throw(SQLException
, RuntimeException
, std::exception
)
281 throwFeatureNotImplementedSQLException( "XParameters::setBlob", *this );
285 void SAL_CALL
OPreparedStatement::setArray( sal_Int32
/*parameterIndex*/, const Reference
< XArray
>& /*x*/ ) throw(SQLException
, RuntimeException
, std::exception
)
287 throwFeatureNotImplementedSQLException( "XParameters::setArray", *this );
291 void SAL_CALL
OPreparedStatement::setRef( sal_Int32
/*parameterIndex*/, const Reference
< XRef
>& /*x*/ ) throw(SQLException
, RuntimeException
, std::exception
)
293 throwFeatureNotImplementedSQLException( "XParameters::setRef", *this );
297 void SAL_CALL
OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex
, const Any
& x
, sal_Int32 sqlType
, sal_Int32 scale
) throw(SQLException
, RuntimeException
, std::exception
)
301 case DataType::DECIMAL
:
302 case DataType::NUMERIC
:
303 setString(parameterIndex
,::comphelper::getString(x
));
306 ::dbtools::setObjectWithInfo(this,parameterIndex
,x
,sqlType
,scale
);
312 void SAL_CALL
OPreparedStatement::setObjectNull( sal_Int32 parameterIndex
, sal_Int32 sqlType
, const OUString
& /*typeName*/ ) throw(SQLException
, RuntimeException
, std::exception
)
314 setNull(parameterIndex
,sqlType
);
318 void SAL_CALL
OPreparedStatement::setObject( sal_Int32 parameterIndex
, const Any
& x
) throw(SQLException
, RuntimeException
, std::exception
)
320 if(!::dbtools::implSetObject(this,parameterIndex
,x
))
322 const OUString
sError( m_pConnection
->getResources().getResourceStringWithSubstitution(
323 STR_UNKNOWN_PARA_TYPE
,
324 "$position$", OUString::number(parameterIndex
)
326 ::dbtools::throwGenericSQLException(sError
,*this);
328 // setObject (parameterIndex, x, sqlType, 0);
332 void SAL_CALL
OPreparedStatement::setShort( sal_Int32 parameterIndex
, sal_Int16 x
) throw(SQLException
, RuntimeException
, std::exception
)
334 setParameter(parameterIndex
,x
);
338 void SAL_CALL
OPreparedStatement::setBytes( sal_Int32 parameterIndex
, const Sequence
< sal_Int8
>& x
) throw(SQLException
, RuntimeException
, std::exception
)
340 setParameter(parameterIndex
,x
);
345 void SAL_CALL
OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex
, const Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
) throw(SQLException
, RuntimeException
, std::exception
)
347 setBinaryStream(parameterIndex
,x
,length
);
351 void SAL_CALL
OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex
, const Reference
< ::com::sun::star::io::XInputStream
>& x
, sal_Int32 length
) throw(SQLException
, RuntimeException
, std::exception
)
354 ::dbtools::throwFunctionSequenceException(*this);
356 Sequence
<sal_Int8
> aSeq
;
357 x
->readBytes(aSeq
,length
);
358 setParameter(parameterIndex
,aSeq
);
362 void SAL_CALL
OPreparedStatement::clearParameters( ) throw(SQLException
, RuntimeException
, std::exception
)
364 ::osl::MutexGuard
aGuard( m_aMutex
);
365 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
367 m_aParameterRow
->get().clear();
368 m_aParameterRow
->get().push_back(new ORowSetValueDecorator(sal_Int32(0)) );
371 OResultSet
* OPreparedStatement::createResultSet()
373 return new OResultSet(this,m_aSQLIterator
);
376 void OPreparedStatement::initResultSet(OResultSet
*pResultSet
)
378 // check if we got enough parameters
379 if ( (m_aParameterRow
.is() && ( m_aParameterRow
->get().size() -1 ) < m_xParamColumns
->get().size()) ||
380 (m_xParamColumns
.is() && !m_aParameterRow
.is() && !m_aParameterRow
->get().empty()) )
381 m_pConnection
->throwGenericSQLException(STR_INVALID_PARA_COUNT
,*this);
383 pResultSet
->OpenImpl();
384 pResultSet
->setMetaData(getMetaData());
387 void SAL_CALL
OPreparedStatement::acquire() throw()
389 OStatement_BASE2::acquire();
392 void SAL_CALL
OPreparedStatement::release() throw()
394 OStatement_BASE2::release();
397 void OPreparedStatement::checkAndResizeParameters(sal_Int32 parameterIndex
)
399 ::connectivity::checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
400 if ( m_aAssignValues
.is() && (parameterIndex
< 1 || parameterIndex
>= static_cast<sal_Int32
>(m_aParameterIndexes
.size())) )
401 throwInvalidIndexException(*this);
402 else if ( static_cast<sal_Int32
>((m_aParameterRow
->get()).size()) <= parameterIndex
)
404 sal_Int32 i
= m_aParameterRow
->get().size();
405 (m_aParameterRow
->get()).resize(parameterIndex
+1);
406 for ( ; i
<= parameterIndex
; ++i
)
408 if ( !(m_aParameterRow
->get())[i
].is() )
409 (m_aParameterRow
->get())[i
] = new ORowSetValueDecorator
;
414 void OPreparedStatement::setParameter(sal_Int32 parameterIndex
, const ORowSetValue
& x
)
416 ::osl::MutexGuard
aGuard( m_aMutex
);
417 checkAndResizeParameters(parameterIndex
);
419 if(m_aAssignValues
.is())
420 *(m_aAssignValues
->get())[m_aParameterIndexes
[parameterIndex
]] = x
;
422 *((m_aParameterRow
->get())[parameterIndex
]) = x
;
425 sal_uInt32
OPreparedStatement::AddParameter(OSQLParseNode
* pParameter
, const Reference
<XPropertySet
>& _xCol
)
427 OSL_UNUSED( pParameter
);
428 OSL_ENSURE(SQL_ISRULE(pParameter
,parameter
),"OResultSet::AddParameter: Argument ist kein Parameter");
429 OSL_ENSURE(pParameter
->count() > 0,"OResultSet: Fehler im Parse Tree");
430 #if OSL_DEBUG_LEVEL > 0
431 OSQLParseNode
* pMark
= pParameter
->getChild(0);
435 OUString sParameterName
;
436 // set up Parameter-Column:
437 sal_Int32 eType
= DataType::VARCHAR
;
438 sal_uInt32 nPrecision
= 255;
439 sal_Int32 nScale
= 0;
440 sal_Int32 nNullable
= ColumnValue::NULLABLE
;
444 // Use type, precision, scale ... from the given column,
445 // because this Column will get a value assigned or
446 // with this Column the value will be compared.
447 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
)) >>= eType
;
448 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION
)) >>= nPrecision
;
449 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE
)) >>= nScale
;
450 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
)) >>= nNullable
;
451 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
)) >>= sParameterName
;
454 Reference
<XPropertySet
> xParaColumn
= new connectivity::parse::OParseColumn(sParameterName
464 ,m_aSQLIterator
.isCaseSensitive()
468 m_xParamColumns
->get().push_back(xParaColumn
);
469 return m_xParamColumns
->get().size();
472 void OPreparedStatement::describeColumn(OSQLParseNode
* _pParameter
,OSQLParseNode
* _pNode
,const OSQLTable
& _xTable
)
474 Reference
<XPropertySet
> xProp
;
475 if(SQL_ISRULE(_pNode
,column_ref
))
477 OUString sColumnName
,sTableRange
;
478 m_aSQLIterator
.getColumnRange(_pNode
,sColumnName
,sTableRange
);
479 if ( !sColumnName
.isEmpty() )
481 Reference
<XNameAccess
> xNameAccess
= _xTable
->getColumns();
482 if(xNameAccess
->hasByName(sColumnName
))
483 xNameAccess
->getByName(sColumnName
) >>= xProp
;
484 AddParameter(_pParameter
,xProp
);
488 // AddParameter(_pParameter,xProp);
491 void OPreparedStatement::describeParameter()
493 ::std::vector
< OSQLParseNode
*> aParseNodes
;
494 scanParameter(m_pParseTree
,aParseNodes
);
495 if ( !aParseNodes
.empty() )
497 // m_xParamColumns = new OSQLColumns();
498 const OSQLTables
& xTabs
= m_aSQLIterator
.getTables();
501 OSQLTable xTable
= xTabs
.begin()->second
;
502 ::std::vector
< OSQLParseNode
*>::const_iterator aIter
= aParseNodes
.begin();
503 for (;aIter
!= aParseNodes
.end();++aIter
)
505 describeColumn(*aIter
,(*aIter
)->getParent()->getChild(0),xTable
);
510 void OPreparedStatement::initializeResultSet(OResultSet
* pRS
)
512 OStatement_Base::initializeResultSet(pRS
);
514 pRS
->setParameterColumns(m_xParamColumns
);
515 pRS
->setParameterRow(m_aParameterRow
);
517 // Substitute parameter (AssignValues and criteria):
518 if (!m_xParamColumns
->get().empty())
520 // begin with AssignValues
521 sal_uInt16 nParaCount
=0; // gives the current number of previously set Parameters
523 // search for parameters to be substituted:
524 size_t nCount
= m_aAssignValues
.is() ? m_aAssignValues
->get().size() : 1; // 1 is important for the Criteria
525 for (size_t j
= 1; j
< nCount
; j
++)
527 sal_uInt32 nParameter
= (*m_aAssignValues
).getParameterIndex(j
);
528 if (nParameter
== SQL_NO_PARAMETER
)
529 continue; // this AssignValue is no Parameter
531 ++nParaCount
; // now the Parameter is valid
534 if (m_aParameterRow
.is() && (m_xParamColumns
->get().size()+1) != m_aParameterRow
->get().size() )
536 sal_Int32 i
= m_aParameterRow
->get().size();
537 sal_Int32 nParamColumns
= m_xParamColumns
->get().size()+1;
538 m_aParameterRow
->get().resize(nParamColumns
);
539 for ( ;i
< nParamColumns
; ++i
)
541 if ( !(m_aParameterRow
->get())[i
].is() )
542 (m_aParameterRow
->get())[i
] = new ORowSetValueDecorator
;
545 if (m_aParameterRow
.is() && nParaCount
< m_aParameterRow
->get().size() )
546 m_pSQLAnalyzer
->bindParameterRow(m_aParameterRow
);
550 void OPreparedStatement::parseParamterElem(const OUString
& _sColumnName
, OSQLParseNode
* pRow_Value_Constructor_Elem
)
552 Reference
<XPropertySet
> xCol
;
553 m_xColNames
->getByName(_sColumnName
) >>= xCol
;
554 sal_Int32 nParameter
= -1;
555 if(m_xParamColumns
.is())
557 OSQLColumns::Vector::const_iterator aIter
= find(m_xParamColumns
->get().begin(),m_xParamColumns
->get().end(),_sColumnName
,::comphelper::UStringMixEqual(m_pTable
->isCaseSensitive()));
558 if(aIter
!= m_xParamColumns
->get().end())
559 nParameter
= m_xParamColumns
->get().size() - (m_xParamColumns
->get().end() - aIter
) + 1;// +1 because the rows start at 1
562 nParameter
= AddParameter(pRow_Value_Constructor_Elem
,xCol
);
563 // Save number of parameter in the variable:
564 SetAssignValue(_sColumnName
, OUString(), true, nParameter
);
569 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */