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 <o3tl/safeint.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 <connectivity/dbconversion.hxx>
30 #include <connectivity/dbexception.hxx>
31 #include <connectivity/dbtools.hxx>
32 #include <connectivity/PColumn.hxx>
33 #include <comphelper/types.hxx>
34 #include <com/sun/star/sdbc/ColumnValue.hpp>
35 #include <strings.hrc>
37 using namespace connectivity
;
38 using namespace comphelper
;
39 using namespace ::dbtools
;
40 using namespace connectivity::file
;
41 using namespace com::sun::star::uno
;
42 using namespace com::sun::star::lang
;
43 using namespace com::sun::star::beans
;
44 using namespace com::sun::star::sdbc
;
45 using namespace com::sun::star::sdbcx
;
46 using namespace com::sun::star::container
;
47 using namespace com::sun::star
;
49 IMPLEMENT_SERVICE_INFO(OPreparedStatement
,u
"com.sun.star.sdbc.driver.file.PreparedStatement"_ustr
,u
"com.sun.star.sdbc.PreparedStatement"_ustr
);
51 OPreparedStatement::OPreparedStatement( OConnection
* _pConnection
)
52 : OStatement_BASE2( _pConnection
)
57 OPreparedStatement::~OPreparedStatement()
62 void OPreparedStatement::disposing()
64 ::osl::MutexGuard
aGuard(m_aMutex
);
66 OStatement_BASE2::disposing();
68 m_xParamColumns
= nullptr;
70 if(m_aParameterRow
.is())
72 m_aParameterRow
->clear();
73 m_aParameterRow
= nullptr;
77 void OPreparedStatement::construct(const OUString
& sql
)
79 OStatement_Base::construct(sql
);
81 m_aParameterRow
= new OValueRefVector();
82 m_aParameterRow
->push_back(new ORowSetValueDecorator(sal_Int32(0)) );
84 Reference
<XIndexAccess
> xNames(m_xColNames
,UNO_QUERY
);
86 if ( m_aSQLIterator
.getStatementType() == OSQLStatementType::Select
)
87 m_xParamColumns
= m_aSQLIterator
.getParameters();
90 m_xParamColumns
= new OSQLColumns();
91 // describe all parameters need for the resultset
96 OResultSet::setBoundedColumns(m_aEvaluateRow
,aTemp
,m_xParamColumns
,xNames
,false,m_xDBMetaData
,m_aColMapping
);
99 rtl::Reference
<OResultSet
> OPreparedStatement::makeResultSet()
103 rtl::Reference
<OResultSet
> xResultSet(createResultSet());
104 m_xResultSet
= xResultSet
.get();
105 initializeResultSet(xResultSet
.get());
106 initResultSet(xResultSet
.get());
110 Any SAL_CALL
OPreparedStatement::queryInterface( const Type
& rType
)
112 Any aRet
= OStatement_BASE2::queryInterface(rType
);
113 return aRet
.hasValue() ? aRet
: ::cppu::queryInterface( rType
,
114 static_cast< XPreparedStatement
*>(this),
115 static_cast< XParameters
*>(this),
116 static_cast< XResultSetMetaDataSupplier
*>(this));
119 css::uno::Sequence
< css::uno::Type
> SAL_CALL
OPreparedStatement::getTypes( )
121 ::cppu::OTypeCollection
aTypes( cppu::UnoType
<XPreparedStatement
>::get(),
122 cppu::UnoType
<XParameters
>::get(),
123 cppu::UnoType
<XResultSetMetaDataSupplier
>::get());
125 return ::comphelper::concatSequences(aTypes
.getTypes(),OStatement_BASE2::getTypes());
129 Reference
< XResultSetMetaData
> SAL_CALL
OPreparedStatement::getMetaData( )
131 return getMetaDataImpl();
134 const rtl::Reference
< OResultSetMetaData
> & OPreparedStatement::getMetaDataImpl( )
136 ::osl::MutexGuard
aGuard( m_aMutex
);
137 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
140 if(!m_xMetaData
.is())
141 m_xMetaData
= new OResultSetMetaData(m_aSQLIterator
.getSelectColumns(),m_aSQLIterator
.getTables().begin()->first
,m_pTable
.get());
146 void SAL_CALL
OPreparedStatement::close( )
148 ::osl::MutexGuard
aGuard( m_aMutex
);
149 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
155 sal_Bool SAL_CALL
OPreparedStatement::execute( )
157 ::osl::MutexGuard
aGuard( m_aMutex
);
158 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
160 rtl::Reference
<OResultSet
> xRS(makeResultSet());
161 // since we don't support the XMultipleResults interface, nobody will ever get that ResultSet...
165 return m_aSQLIterator
.getStatementType() == OSQLStatementType::Select
;
169 sal_Int32 SAL_CALL
OPreparedStatement::executeUpdate( )
171 ::osl::MutexGuard
aGuard( m_aMutex
);
172 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
174 rtl::Reference
<OResultSet
> xRS(makeResultSet());
177 const sal_Int32
res(xRS
->getRowCountResult());
178 // nobody will ever get that ResultSet...
187 void SAL_CALL
OPreparedStatement::setString( sal_Int32 parameterIndex
, const OUString
& x
)
189 setParameter(parameterIndex
,x
);
193 Reference
< XConnection
> SAL_CALL
OPreparedStatement::getConnection( )
195 ::osl::MutexGuard
aGuard( m_aMutex
);
196 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
198 return m_pConnection
;
202 Reference
< XResultSet
> SAL_CALL
OPreparedStatement::executeQuery( )
204 ::osl::MutexGuard
aGuard( m_aMutex
);
205 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
207 return makeResultSet();
211 void SAL_CALL
OPreparedStatement::setBoolean( sal_Int32 parameterIndex
, sal_Bool x
)
213 setParameter(parameterIndex
,static_cast<bool>(x
));
216 void SAL_CALL
OPreparedStatement::setByte( sal_Int32 parameterIndex
, sal_Int8 x
)
218 setParameter(parameterIndex
,x
);
222 void SAL_CALL
OPreparedStatement::setDate( sal_Int32 parameterIndex
, const util::Date
& aData
)
224 setParameter(parameterIndex
,DBTypeConversion::toDouble(aData
));
227 void SAL_CALL
OPreparedStatement::setTime( sal_Int32 parameterIndex
, const util::Time
& aVal
)
229 setParameter(parameterIndex
,DBTypeConversion::toDouble(aVal
));
233 void SAL_CALL
OPreparedStatement::setTimestamp( sal_Int32 parameterIndex
, const util::DateTime
& aVal
)
235 setParameter(parameterIndex
,DBTypeConversion::toDouble(aVal
));
239 void SAL_CALL
OPreparedStatement::setDouble( sal_Int32 parameterIndex
, double x
)
241 setParameter(parameterIndex
,x
);
245 void SAL_CALL
OPreparedStatement::setFloat( sal_Int32 parameterIndex
, float x
)
247 setParameter(parameterIndex
,x
);
251 void SAL_CALL
OPreparedStatement::setInt( sal_Int32 parameterIndex
, sal_Int32 x
)
253 setParameter(parameterIndex
,x
);
257 void SAL_CALL
OPreparedStatement::setLong( sal_Int32
/*parameterIndex*/, sal_Int64
/*aVal*/ )
259 throwFeatureNotImplementedSQLException( u
"XParameters::setLong"_ustr
, *this );
263 void SAL_CALL
OPreparedStatement::setNull( sal_Int32 parameterIndex
, sal_Int32
/*sqlType*/ )
265 ::osl::MutexGuard
aGuard( m_aMutex
);
266 checkAndResizeParameters(parameterIndex
);
268 if ( m_aAssignValues
.is() )
269 (*m_aAssignValues
)[m_aParameterIndexes
[parameterIndex
]]->setNull();
271 (*m_aParameterRow
)[parameterIndex
]->setNull();
275 void SAL_CALL
OPreparedStatement::setClob( sal_Int32
/*parameterIndex*/, const Reference
< XClob
>& /*x*/ )
277 throwFeatureNotImplementedSQLException( u
"XParameters::setClob"_ustr
, *this );
281 void SAL_CALL
OPreparedStatement::setBlob( sal_Int32
/*parameterIndex*/, const Reference
< XBlob
>& /*x*/ )
283 throwFeatureNotImplementedSQLException( u
"XParameters::setBlob"_ustr
, *this );
287 void SAL_CALL
OPreparedStatement::setArray( sal_Int32
/*parameterIndex*/, const Reference
< XArray
>& /*x*/ )
289 throwFeatureNotImplementedSQLException( u
"XParameters::setArray"_ustr
, *this );
293 void SAL_CALL
OPreparedStatement::setRef( sal_Int32
/*parameterIndex*/, const Reference
< XRef
>& /*x*/ )
295 throwFeatureNotImplementedSQLException( u
"XParameters::setRef"_ustr
, *this );
299 void SAL_CALL
OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex
, const Any
& x
, sal_Int32 sqlType
, sal_Int32 scale
)
303 case DataType::DECIMAL
:
304 case DataType::NUMERIC
:
305 setString(parameterIndex
,::comphelper::getString(x
));
308 ::dbtools::setObjectWithInfo(this,parameterIndex
,x
,sqlType
,scale
);
314 void SAL_CALL
OPreparedStatement::setObjectNull( sal_Int32 parameterIndex
, sal_Int32 sqlType
, const OUString
& /*typeName*/ )
316 setNull(parameterIndex
,sqlType
);
320 void SAL_CALL
OPreparedStatement::setObject( sal_Int32 parameterIndex
, const Any
& x
)
322 if(!::dbtools::implSetObject(this,parameterIndex
,x
))
324 const OUString
sError( m_pConnection
->getResources().getResourceStringWithSubstitution(
325 STR_UNKNOWN_PARA_TYPE
,
326 "$position$", OUString::number(parameterIndex
)
328 ::dbtools::throwGenericSQLException(sError
,*this);
330 // setObject (parameterIndex, x, sqlType, 0);
334 void SAL_CALL
OPreparedStatement::setShort( sal_Int32 parameterIndex
, sal_Int16 x
)
336 setParameter(parameterIndex
,x
);
340 void SAL_CALL
OPreparedStatement::setBytes( sal_Int32 parameterIndex
, const Sequence
< sal_Int8
>& x
)
342 setParameter(parameterIndex
,x
);
346 void SAL_CALL
OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex
, const Reference
< css::io::XInputStream
>& x
, sal_Int32 length
)
348 setBinaryStream(parameterIndex
,x
,length
);
352 void SAL_CALL
OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex
, const Reference
< css::io::XInputStream
>& x
, sal_Int32 length
)
355 ::dbtools::throwFunctionSequenceException(*this);
357 Sequence
<sal_Int8
> aSeq
;
358 x
->readBytes(aSeq
,length
);
359 setParameter(parameterIndex
,aSeq
);
363 void SAL_CALL
OPreparedStatement::clearParameters( )
365 ::osl::MutexGuard
aGuard( m_aMutex
);
366 checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
368 m_aParameterRow
->clear();
369 m_aParameterRow
->push_back(new ORowSetValueDecorator(sal_Int32(0)) );
372 rtl::Reference
<OResultSet
> OPreparedStatement::createResultSet()
374 return new OResultSet(this,m_aSQLIterator
);
377 void OPreparedStatement::initResultSet(OResultSet
*pResultSet
)
379 // check if we got enough parameters
380 if ( (m_aParameterRow
.is() && ( m_aParameterRow
->size() -1 ) < m_xParamColumns
->size()) ||
381 (m_xParamColumns
.is() && !m_aParameterRow
.is() && !m_aParameterRow
->empty()) )
382 m_pConnection
->throwGenericSQLException(STR_INVALID_PARA_COUNT
,*this);
384 pResultSet
->OpenImpl();
385 pResultSet
->setMetaData(getMetaDataImpl());
388 void SAL_CALL
OPreparedStatement::acquire() noexcept
390 OStatement_BASE2::acquire();
393 void SAL_CALL
OPreparedStatement::release() noexcept
395 OStatement_BASE2::release();
398 void OPreparedStatement::checkAndResizeParameters(sal_Int32 parameterIndex
)
400 ::connectivity::checkDisposed(OStatement_BASE::rBHelper
.bDisposed
);
401 if ( m_aAssignValues
.is() && (parameterIndex
< 1 || o3tl::make_unsigned(parameterIndex
) >= m_aParameterIndexes
.size()) )
402 throwInvalidIndexException(*this);
403 else if ( static_cast<sal_Int32
>(m_aParameterRow
->size()) <= parameterIndex
)
405 sal_Int32 i
= m_aParameterRow
->size();
406 m_aParameterRow
->resize(parameterIndex
+1);
407 for ( ; i
<= parameterIndex
; ++i
)
409 if ( !(*m_aParameterRow
)[i
].is() )
410 (*m_aParameterRow
)[i
] = new ORowSetValueDecorator
;
415 void OPreparedStatement::setParameter(sal_Int32 parameterIndex
, const ORowSetValue
& x
)
417 ::osl::MutexGuard
aGuard( m_aMutex
);
418 checkAndResizeParameters(parameterIndex
);
420 if(m_aAssignValues
.is())
421 *(*m_aAssignValues
)[m_aParameterIndexes
[parameterIndex
]] = x
;
423 *((*m_aParameterRow
)[parameterIndex
]) = x
;
426 sal_uInt32
OPreparedStatement::AddParameter(OSQLParseNode
const * pParameter
, const Reference
<XPropertySet
>& _xCol
)
428 OSL_ENSURE(SQL_ISRULE(pParameter
,parameter
),"OResultSet::AddParameter: Argument is not a parameter");
429 OSL_ENSURE(pParameter
->count() > 0,"OResultSet: Error in Parse Tree");
431 OUString sParameterName
;
432 // set up Parameter-Column:
433 sal_Int32 eType
= DataType::VARCHAR
;
434 sal_uInt32 nPrecision
= 255;
435 sal_Int32 nScale
= 0;
436 sal_Int32 nNullable
= ColumnValue::NULLABLE
;
440 // Use type, precision, scale ... from the given column,
441 // because this Column will get a value assigned or
442 // with this Column the value will be compared.
443 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
)) >>= eType
;
444 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION
)) >>= nPrecision
;
445 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE
)) >>= nScale
;
446 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE
)) >>= nNullable
;
447 _xCol
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME
)) >>= sParameterName
;
450 Reference
<XPropertySet
> xParaColumn
= new connectivity::parse::OParseColumn(sParameterName
460 ,m_aSQLIterator
.isCaseSensitive()
464 m_xParamColumns
->push_back(xParaColumn
);
465 return m_xParamColumns
->size();
468 void OPreparedStatement::describeColumn(OSQLParseNode
const * _pParameter
, OSQLParseNode
const * _pNode
,const OSQLTable
& _xTable
)
470 Reference
<XPropertySet
> xProp
;
471 if(SQL_ISRULE(_pNode
,column_ref
))
473 OUString sColumnName
,sTableRange
;
474 m_aSQLIterator
.getColumnRange(_pNode
,sColumnName
,sTableRange
);
475 if ( !sColumnName
.isEmpty() )
477 Reference
<XNameAccess
> xNameAccess
= _xTable
->getColumns();
478 if(xNameAccess
->hasByName(sColumnName
))
479 xNameAccess
->getByName(sColumnName
) >>= xProp
;
480 AddParameter(_pParameter
,xProp
);
484 // AddParameter(_pParameter,xProp);
487 void OPreparedStatement::describeParameter()
489 std::vector
< OSQLParseNode
*> aParseNodes
;
490 scanParameter(m_pParseTree
,aParseNodes
);
491 if ( aParseNodes
.empty() )
494 // m_xParamColumns = new OSQLColumns();
495 const OSQLTables
& rTabs
= m_aSQLIterator
.getTables();
498 OSQLTable xTable
= rTabs
.begin()->second
;
499 for (auto const& parseNode
: aParseNodes
)
501 describeColumn(parseNode
,parseNode
->getParent()->getChild(0),xTable
);
505 void OPreparedStatement::initializeResultSet(OResultSet
* pRS
)
507 OStatement_Base::initializeResultSet(pRS
);
509 // Substitute parameter (AssignValues and criteria):
510 if (m_xParamColumns
->empty())
513 // begin with AssignValues
514 sal_uInt16 nParaCount
=0; // gives the current number of previously set Parameters
516 // search for parameters to be substituted:
517 size_t nCount
= m_aAssignValues
.is() ? m_aAssignValues
->size() : 1; // 1 is important for the Criteria
518 for (size_t j
= 1; j
< nCount
; j
++)
520 sal_uInt32 nParameter
= (*m_aAssignValues
).getParameterIndex(j
);
521 if (nParameter
== SQL_NO_PARAMETER
)
522 continue; // this AssignValue is no Parameter
524 ++nParaCount
; // now the Parameter is valid
527 if (m_aParameterRow
.is() && (m_xParamColumns
->size()+1) != m_aParameterRow
->size() )
529 sal_Int32 i
= m_aParameterRow
->size();
530 sal_Int32 nParamColumns
= m_xParamColumns
->size()+1;
531 m_aParameterRow
->resize(nParamColumns
);
532 for ( ;i
< nParamColumns
; ++i
)
534 if ( !(*m_aParameterRow
)[i
].is() )
535 (*m_aParameterRow
)[i
] = new ORowSetValueDecorator
;
538 if (m_aParameterRow
.is() && nParaCount
< m_aParameterRow
->size() )
539 m_pSQLAnalyzer
->bindParameterRow(m_aParameterRow
);
542 void OPreparedStatement::parseParamterElem(const OUString
& _sColumnName
, OSQLParseNode
* pRow_Value_Constructor_Elem
)
544 Reference
<XPropertySet
> xCol
;
545 m_xColNames
->getByName(_sColumnName
) >>= xCol
;
546 sal_Int32 nParameter
= -1;
547 if(m_xParamColumns
.is())
549 OSQLColumns::const_iterator aIter
= find(m_xParamColumns
->begin(),m_xParamColumns
->end(),_sColumnName
,::comphelper::UStringMixEqual(m_pTable
->isCaseSensitive()));
550 if(aIter
!= m_xParamColumns
->end())
551 nParameter
= m_xParamColumns
->size() - (m_xParamColumns
->end() - aIter
) + 1;// +1 because the rows start at 1
554 nParameter
= AddParameter(pRow_Value_Constructor_Elem
,xCol
);
555 // Save number of parameter in the variable:
556 SetAssignValue(_sColumnName
, OUString(), true, nParameter
);
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */