1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <connectivity/sqlparse.hxx>
21 #include <ado/APreparedStatement.hxx>
22 #include <com/sun/star/sdbc/DataType.hpp>
23 #include <ado/AResultSetMetaData.hxx>
24 #include <ado/AResultSet.hxx>
25 #include <ado/ADriver.hxx>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <cppuhelper/queryinterface.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <comphelper/sequence.hxx>
31 #include <comphelper/types.hxx>
32 #include <connectivity/dbexception.hxx>
33 #include <connectivity/dbtools.hxx>
34 #include <rtl/ref.hxx>
35 #include <strings.hrc>
36
37 #include <limits>
38
39 #define CHECK_RETURN(x) \
40 if(!x) \
41 ADOS::ThrowException(*m_pConnection->getConnection(),*this);
42
43 #ifdef max
44 # undef max
45 #endif
46
47 using namespace connectivity::ado;
48 using namespace connectivity;
49 using namespace com::sun::star::uno;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::beans;
52 using namespace com::sun::star::sdbc;
53 using namespace com::sun::star::util;
54
55 IMPLEMENT_SERVICE_INFO(OPreparedStatement,"com.sun.star.sdbcx.APreparedStatement","com.sun.star.sdbc.PreparedStatement");
56
OPreparedStatement(OConnection * _pConnection,const OUString & sql)57 OPreparedStatement::OPreparedStatement( OConnection* _pConnection, const OUString& sql)
58 : OStatement_Base( _pConnection )
59 {
60 osl_atomic_increment( &m_refCount );
61
62 OSQLParser aParser(_pConnection->getDriver()->getContext());
63 OUString sErrorMessage;
64 OUString sNewSql;
65 std::unique_ptr<OSQLParseNode> pNode = aParser.parseTree(sErrorMessage,sql);
66 if(pNode)
67 { // special handling for parameters
68 // we recursive replace all occurrences of ? in the statement and
69 // replace them with name like "parame" */
70 sal_Int32 nParameterCount = 0;
71 replaceParameterNodeName(pNode.get(), "parame", nParameterCount);
72 pNode->parseNodeToStr( sNewSql, _pConnection );
73 }
74 else
75 sNewSql = sql;
76 CHECK_RETURN(m_Command.put_CommandText(sNewSql))
77 CHECK_RETURN(m_Command.put_Prepared(VARIANT_TRUE))
78 m_pParameters = m_Command.get_Parameters();
79 m_pParameters->AddRef();
80 m_pParameters->Refresh();
81
82 osl_atomic_decrement( &m_refCount );
83 }
84
~OPreparedStatement()85 OPreparedStatement::~OPreparedStatement()
86 {
87 if (m_pParameters)
88 {
89 OSL_FAIL( "OPreparedStatement::~OPreparedStatement: not disposed!" );
90 m_pParameters->Release();
91 m_pParameters = nullptr;
92 }
93 }
94
queryInterface(const Type & rType)95 Any SAL_CALL OPreparedStatement::queryInterface( const Type & rType )
96 {
97 Any aRet = OStatement_Base::queryInterface(rType);
98 return aRet.hasValue() ? aRet : ::cppu::queryInterface( rType,
99 static_cast< XPreparedStatement*>(this),
100 static_cast< XParameters*>(this),
101 static_cast< XResultSetMetaDataSupplier*>(this));
102 }
103
getTypes()104 css::uno::Sequence< css::uno::Type > SAL_CALL OPreparedStatement::getTypes( )
105 {
106 ::cppu::OTypeCollection aTypes( cppu::UnoType<XPreparedStatement>::get(),
107 cppu::UnoType<XParameters>::get(),
108 cppu::UnoType<XResultSetMetaDataSupplier>::get());
109
110 return ::comphelper::concatSequences(aTypes.getTypes(),OStatement_Base::getTypes());
111 }
112
getMetaData()113 Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData( )
114 {
115 if(!m_xMetaData.is() && m_RecordSet.IsValid())
116 m_xMetaData = new OResultSetMetaData(m_RecordSet);
117 return m_xMetaData;
118 }
119
disposing()120 void OPreparedStatement::disposing()
121 {
122 m_xMetaData.clear();
123 if (m_pParameters)
124 {
125 m_pParameters->Release();
126 m_pParameters = nullptr;
127 }
128 OStatement_Base::disposing();
129 }
130
close()131 void SAL_CALL OPreparedStatement::close( )
132 {
133
134 {
135 ::osl::MutexGuard aGuard( m_aMutex );
136 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
137
138 }
139 dispose();
140
141 }
142
execute()143 sal_Bool SAL_CALL OPreparedStatement::execute( )
144 {
145 ::osl::MutexGuard aGuard( m_aMutex );
146 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
147
148 SQLWarning warning;
149 clearWarnings ();
150
151 // Call SQLExecute
152 try {
153 ADORecordset* pSet=nullptr;
154 CHECK_RETURN(m_Command.Execute(m_RecordsAffected,m_Parameters,adCmdUnknown,&pSet))
155 m_RecordSet = WpADORecordset(pSet);
156 }
157 catch (SQLWarning& ex)
158 {
159 // Save pointer to warning and save with ResultSet
160 // object once it is created.
161
162 warning = ex;
163 }
164 return m_RecordSet.IsValid();
165 }
166
executeUpdate()167 sal_Int32 SAL_CALL OPreparedStatement::executeUpdate( )
168 {
169 ::osl::MutexGuard aGuard( m_aMutex );
170 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
171
172
173 ADORecordset* pSet=nullptr;
174 CHECK_RETURN(m_Command.Execute(m_RecordsAffected,m_Parameters,adCmdUnknown,&pSet))
175 if ( VT_ERROR == m_RecordsAffected.getType() )
176 {
177 ADOS::ThrowException(*m_pConnection->getConnection(),*this);
178 // to be sure that we get the error really thrown
179 throw SQLException();
180 }
181 m_RecordSet = WpADORecordset(pSet);
182 return m_RecordsAffected.getInt32();
183 }
184
setParameter(sal_Int32 parameterIndex,const DataTypeEnum & _eType,sal_Int32 _nSize,const OLEVariant & Val)185 void OPreparedStatement::setParameter(sal_Int32 parameterIndex, const DataTypeEnum& _eType,
186 sal_Int32 _nSize,const OLEVariant& Val)
187 {
188 ::osl::MutexGuard aGuard( m_aMutex );
189 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
190
191
192 sal_Int32 nCount = 0;
193 m_pParameters->get_Count(&nCount);
194 if(nCount < (parameterIndex-1))
195 {
196 OUString sDefaultName = "parame" + OUString::number(parameterIndex);
197 ADOParameter* pParam = m_Command.CreateParameter(sDefaultName,_eType,adParamInput,_nSize,Val);
198 if(pParam)
199 {
200 m_pParameters->Append(pParam);
201 }
202 }
203 else
204 {
205 ADOParameter* pParam = nullptr;
206 m_pParameters->get_Item(OLEVariant(sal_Int32(parameterIndex-1)),&pParam);
207 WpADOParameter aParam(pParam);
208 if(pParam)
209 {
210 DataTypeEnum eType = aParam.GetADOType();
211 if ( _eType != eType && _eType != adDBTimeStamp )
212 {
213 aParam.put_Type(_eType);
214 eType = _eType;
215 aParam.put_Size(_nSize);
216 }
217
218 if ( adVarBinary == eType && aParam.GetAttributes() == adParamLong )
219 {
220 aParam.AppendChunk(Val);
221 }
222 else
223 CHECK_RETURN(aParam.PutValue(Val));
224 }
225 }
226 ADOS::ThrowException(*m_pConnection->getConnection(),*this);
227 }
228
setString(sal_Int32 parameterIndex,const OUString & x)229 void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x )
230 {
231 setParameter( parameterIndex, adLongVarWChar, std::numeric_limits< sal_Int32 >::max(), x );
232 }
233
getConnection()234 Reference< XConnection > SAL_CALL OPreparedStatement::getConnection( )
235 {
236 ::osl::MutexGuard aGuard( m_aMutex );
237 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
238
239 return static_cast<Reference< XConnection >>(m_pConnection);
240 }
241
executeQuery()242 Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery( )
243 {
244 ::osl::MutexGuard aGuard( m_aMutex );
245 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
246
247 // first clear the old things
248 m_xMetaData.clear();
249 disposeResultSet();
250 if(m_RecordSet.IsValid())
251 m_RecordSet.Close();
252 m_RecordSet.clear();
253
254 // then create the new ones
255 m_RecordSet.Create();
256 OLEVariant aCmd;
257 aCmd.setIDispatch(m_Command);
258 OLEVariant aCon;
259 aCon.setNoArg();
260 CHECK_RETURN(m_RecordSet.put_CacheSize(m_nFetchSize))
261 CHECK_RETURN(m_RecordSet.put_MaxRecords(m_nMaxRows))
262 CHECK_RETURN(m_RecordSet.Open(aCmd,aCon,m_eCursorType,m_eLockType,adOpenUnspecified))
263 CHECK_RETURN(m_RecordSet.get_CacheSize(m_nFetchSize))
264 CHECK_RETURN(m_RecordSet.get_MaxRecords(m_nMaxRows))
265 CHECK_RETURN(m_RecordSet.get_CursorType(m_eCursorType))
266 CHECK_RETURN(m_RecordSet.get_LockType(m_eLockType))
267
268 rtl::Reference<OResultSet> pSet = new OResultSet(m_RecordSet,this);
269 pSet->construct();
270 pSet->setMetaData(getMetaData());
271 m_xResultSet = WeakReference<XResultSet>(pSet);
272
273 return pSet;
274 }
275
setBoolean(sal_Int32 parameterIndex,sal_Bool x)276 void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x )
277 {
278 setParameter(parameterIndex,adBoolean,sizeof(x),bool(x));
279 }
280
setByte(sal_Int32 parameterIndex,sal_Int8 x)281 void SAL_CALL OPreparedStatement::setByte( sal_Int32 parameterIndex, sal_Int8 x )
282 {
283 setParameter(parameterIndex,adTinyInt,sizeof(x),x);
284 }
285
setDate(sal_Int32 parameterIndex,const Date & x)286 void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const Date& x )
287 {
288 setParameter(parameterIndex,adDBDate,sizeof(x),x);
289 }
290
setTime(sal_Int32 parameterIndex,const css::util::Time & x)291 void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const css::util::Time& x )
292 {
293 setParameter(parameterIndex,adDBTime,sizeof(x),x);
294 }
295
setTimestamp(sal_Int32 parameterIndex,const DateTime & x)296 void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const DateTime& x )
297 {
298 setParameter(parameterIndex,adDBTimeStamp,sizeof(x),x);
299 }
300
setDouble(sal_Int32 parameterIndex,double x)301 void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x )
302 {
303 setParameter(parameterIndex,adDouble,sizeof(x),x);
304 }
305
setFloat(sal_Int32 parameterIndex,float x)306 void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x )
307 {
308 setParameter(parameterIndex,adSingle,sizeof(x),x);
309 }
310
setInt(sal_Int32 parameterIndex,sal_Int32 x)311 void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x )
312 {
313 setParameter(parameterIndex,adInteger,sizeof(x),x);
314 }
315
setLong(sal_Int32 parameterIndex,sal_Int64 x)316 void SAL_CALL OPreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x )
317 {
318 setParameter(parameterIndex,adBigInt,sizeof(x),x);
319 }
320
setNull(sal_Int32 parameterIndex,sal_Int32)321 void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, sal_Int32 /*sqlType*/ )
322 {
323 OLEVariant aVal;
324 aVal.setNull();
325 setParameter(parameterIndex,adEmpty,0,aVal);
326 }
327
setClob(sal_Int32,const Reference<XClob> &)328 void SAL_CALL OPreparedStatement::setClob( sal_Int32 /*parameterIndex*/, const Reference< XClob >& /*x*/ )
329 {
330 ::dbtools::throwFeatureNotImplementedSQLException( "XRowUpdate::setClob", *this );
331 }
332
setBlob(sal_Int32,const Reference<XBlob> &)333 void SAL_CALL OPreparedStatement::setBlob( sal_Int32 /*parameterIndex*/, const Reference< XBlob >& /*x*/ )
334 {
335 ::dbtools::throwFeatureNotImplementedSQLException( "XRowUpdate::setBlob", *this );
336 }
337
setArray(sal_Int32,const Reference<XArray> &)338 void SAL_CALL OPreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const Reference< XArray >& /*x*/ )
339 {
340 ::dbtools::throwFeatureNotImplementedSQLException( "XRowUpdate::setArray", *this );
341 }
342
setRef(sal_Int32,const Reference<XRef> &)343 void SAL_CALL OPreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const Reference< XRef >& /*x*/ )
344 {
345 ::dbtools::throwFeatureNotImplementedSQLException( "XRowUpdate::setRef", *this );
346 }
347
setObjectWithInfo(sal_Int32 parameterIndex,const Any & x,sal_Int32 sqlType,sal_Int32 scale)348 void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 sqlType, sal_Int32 scale )
349 {
350 switch(sqlType)
351 {
352 case DataType::DECIMAL:
353 case DataType::NUMERIC:
354 setString(parameterIndex,::comphelper::getString(x));
355 break;
356 default:
357 ::dbtools::setObjectWithInfo(this,parameterIndex,x,sqlType,scale);
358 break;
359 }
360 }
361
setObjectNull(sal_Int32 parameterIndex,sal_Int32 sqlType,const OUString &)362 void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const OUString& /*typeName*/ )
363 {
364 setNull(parameterIndex,sqlType);
365 }
366
setObject(sal_Int32 parameterIndex,const Any & x)367 void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x )
368 {
369 if(!::dbtools::implSetObject(this,parameterIndex,x))
370 {
371 const OUString sError( m_pConnection->getResources().getResourceStringWithSubstitution(
372 STR_UNKNOWN_PARA_TYPE,
373 "$position$", OUString::number(parameterIndex)
374 ) );
375 ::dbtools::throwGenericSQLException(sError,*this);
376 }
377 }
378
setShort(sal_Int32 parameterIndex,sal_Int16 x)379 void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x )
380 {
381 setParameter(parameterIndex,adSmallInt,sizeof(x),x);
382 }
383
setBytes(sal_Int32 parameterIndex,const Sequence<sal_Int8> & x)384 void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x )
385 {
386 setParameter(parameterIndex,adVarBinary,sizeof(sal_Int8)*x.getLength(),x);
387 }
388
setCharacterStream(sal_Int32,const Reference<css::io::XInputStream> &,sal_Int32)389 void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 /*parameterIndex*/, const Reference< css::io::XInputStream >& /*x*/, sal_Int32 /*length*/ )
390 {
391 ::dbtools::throwFeatureNotImplementedSQLException( "XParameters::setCharacterStream", *this );
392 }
393
setBinaryStream(sal_Int32 parameterIndex,const Reference<css::io::XInputStream> & x,sal_Int32 length)394 void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< css::io::XInputStream >& x, sal_Int32 length )
395 {
396 if(x.is())
397 {
398 Sequence< sal_Int8 > aData;
399 x->readBytes(aData,length);
400 setBytes(parameterIndex,aData);
401 }
402 }
403
clearParameters()404 void SAL_CALL OPreparedStatement::clearParameters( )
405 {
406 ::osl::MutexGuard aGuard( m_aMutex );
407 checkDisposed(OStatement_BASE::rBHelper.bDisposed);
408
409
410 if(m_pParameters)
411 {
412 sal_Int32 nCount = 0;
413 m_pParameters->get_Count(&nCount);
414 OLEVariant aVal;
415 aVal.setEmpty();
416 for(sal_Int32 i=0;i<nCount;++i)
417 {
418 ADOParameter* pParam = nullptr;
419 m_pParameters->get_Item(OLEVariant(i),&pParam);
420 WpADOParameter aParam(pParam);
421 if(pParam)
422 {
423 CHECK_RETURN(aParam.PutValue(aVal));
424 }
425 }
426 }
427 }
428
acquire()429 void SAL_CALL OPreparedStatement::acquire() noexcept
430 {
431 OStatement_Base::acquire();
432 }
433
release()434 void SAL_CALL OPreparedStatement::release() noexcept
435 {
436 OStatement_Base::release();
437 }
438
replaceParameterNodeName(OSQLParseNode const * _pNode,const OUString & _sDefaultName,sal_Int32 & _rParameterCount)439 void OPreparedStatement::replaceParameterNodeName(OSQLParseNode const * _pNode,
440 const OUString& _sDefaultName,
441 sal_Int32& _rParameterCount)
442 {
443 sal_Int32 nCount = _pNode->count();
444 for(sal_Int32 i=0;i < nCount;++i)
445 {
446 OSQLParseNode* pChildNode = _pNode->getChild(i);
447 if(SQL_ISRULE(pChildNode,parameter) && pChildNode->count() == 1)
448 {
449 OSQLParseNode* pNewNode = new OSQLParseNode(OUString(":") ,SQLNodeType::Punctuation,0);
450 delete pChildNode->replace(pChildNode->getChild(0),pNewNode);
451 OUString sParameterName = _sDefaultName + OUString::number(++_rParameterCount);
452 pChildNode->append(new OSQLParseNode( sParameterName,SQLNodeType::Name,0));
453 }
454 else
455 replaceParameterNodeName(pChildNode,_sDefaultName,_rParameterCount);
456
457 }
458 }
459
460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
461