1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <string.h>
#include <osl/diagnose.h>
#include <odbc/OPreparedStatement.hxx>
#include <odbc/OBoundParam.hxx>
#include <com/sun/star/io/IOException.hpp>
#include <com/sun/star/sdbc/DataType.hpp>
#include <odbc/OTools.hxx>
#include <odbc/OResultSet.hxx>
#include <odbc/OResultSetMetaData.hxx>
#include <comphelper/sequence.hxx>
#include <connectivity/dbtools.hxx>
#include <comphelper/types.hxx>
#include <connectivity/FValue.hxx>
#include <strings.hrc>
#include <memory>
#include <type_traits>
using namespace ::comphelper;
using namespace connectivity;
using namespace connectivity::odbc;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::sdbc;
using namespace com::sun::star::sdbcx;
using namespace com::sun::star::container;
using namespace com::sun::star::io;
using namespace com::sun::star::util;
IMPLEMENT_SERVICE_INFO(OPreparedStatement,"com.sun.star.sdbcx.OPreparedStatement","com.sun.star.sdbc.PreparedStatement");
namespace
{
// for now, never use wchar,
// but most of code is prepared to handle it
// in case we make this configurable
const bool bUseWChar = false;
}
OPreparedStatement::OPreparedStatement( OConnection* _pConnection,const OUString& sql)
:OStatement_BASE2(_pConnection)
,numParams(0)
,m_bPrepared(false)
{
m_sSqlStatement = sql;
}
OPreparedStatement::~OPreparedStatement()
{
}
void SAL_CALL OPreparedStatement::acquire() throw()
{
OStatement_BASE2::acquire();
}
void SAL_CALL OPreparedStatement::release() throw()
{
OStatement_BASE2::release();
}
Any SAL_CALL OPreparedStatement::queryInterface( const Type & rType )
{
Any aRet = OStatement_BASE2::queryInterface(rType);
return aRet.hasValue() ? aRet : OPreparedStatement_BASE::queryInterface(rType);
}
css::uno::Sequence< css::uno::Type > SAL_CALL OPreparedStatement::getTypes( )
{
return ::comphelper::concatSequences(OPreparedStatement_BASE::getTypes(),OStatement_BASE2::getTypes());
}
Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData( )
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
prepareStatement();
OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
if(!m_xMetaData.is())
m_xMetaData = new OResultSetMetaData(getOwnConnection(),m_aStatementHandle);
return m_xMetaData;
}
void SAL_CALL OPreparedStatement::close( )
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
// Close/clear our result set
clearMyResultSet ();
// Reset last warning message
try {
clearWarnings ();
OStatement_BASE2::close();
FreeParams();
}
catch (SQLException &) {
// If we get an error, ignore
}
// Remove this Statement object from the Connection object's
// list
}
sal_Bool SAL_CALL OPreparedStatement::execute( )
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
// Reset warnings
clearWarnings ();
// Reset the statement handle, warning and saved Resultset
reset();
// Call SQLExecute
prepareStatement();
OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
try
{
SQLRETURN nReturn = N3SQLExecute(m_aStatementHandle);
OTools::ThrowException(m_pConnection.get(),nReturn,m_aStatementHandle,SQL_HANDLE_STMT,*this);
bool needData = nReturn == SQL_NEED_DATA;
// Now loop while more data is needed (i.e. a data-at-
// execution parameter was given). For each parameter
// that needs data, put the data from the input stream.
while (needData) {
// Get the parameter number that requires data
sal_Int32* paramIndex = nullptr;
N3SQLParamData(m_aStatementHandle, reinterpret_cast<SQLPOINTER*>(¶mIndex));
// If the parameter index is -1, there is no
// more data required
if ( !paramIndex || ( *paramIndex == -1 ) )
needData = false;
else
{
// Now we have the proper parameter
// index, get the data from the input
// stream and do a SQLPutData
putParamData (*paramIndex);
}
}
}
catch (const SQLWarning&)
{
}
// Now determine if there is a result set associated with
// the SQL statement that was executed. Get the column
// count, and if it is not zero, there is a result set.
return getColumnCount() > 0;
}
sal_Int32 SAL_CALL OPreparedStatement::executeUpdate( )
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
sal_Int32 numRows = -1;
prepareStatement();
// Execute the statement. If execute returns sal_False, a
// row count exists.
if (!execute())
numRows = getUpdateCount ();
else
{
// No update count was produced (a ResultSet was). Raise
// an exception
m_pConnection->throwGenericSQLException(STR_NO_ROWCOUNT,*this);
}
return numRows;
}
void SAL_CALL OPreparedStatement::setString( sal_Int32 parameterIndex, const OUString& x )
{
setParameter(parameterIndex, DataType::CHAR, invalid_scale, x);
}
Reference< XConnection > SAL_CALL OPreparedStatement::getConnection( )
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
return Reference< XConnection >(m_pConnection.get());
}
Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery( )
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
Reference< XResultSet > rs;
prepareStatement();
if (execute())
rs = getResultSet(false);
else
{
// No ResultSet was produced. Raise an exception
m_pConnection->throwGenericSQLException(STR_NO_RESULTSET,*this);
}
return rs;
}
void SAL_CALL OPreparedStatement::setBoolean( sal_Int32 parameterIndex, sal_Bool x )
{
// Set the parameter as if it were an integer
setInt (parameterIndex, x ? 1 : 0 );
}
// The MutexGuard must _already_ be taken!
void OPreparedStatement::setParameterPre(sal_Int32 parameterIndex)
{
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
prepareStatement();
checkParameterIndex(parameterIndex);
OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
}
template <typename T> void OPreparedStatement::setScalarParameter(const sal_Int32 parameterIndex, const sal_Int32 i_nType, const SQLULEN i_nColSize, const T i_Value)
{
setScalarParameter(parameterIndex, i_nType, i_nColSize, invalid_scale, i_Value);
}
template <typename T> void OPreparedStatement::setScalarParameter(const sal_Int32 parameterIndex, const sal_Int32 i_nType, const SQLULEN i_nColSize, sal_Int32 i_nScale, const T i_Value)
{
::osl::MutexGuard aGuard( m_aMutex );
setParameterPre(parameterIndex);
typedef typename std::remove_reference<T>::type TnoRef;
TnoRef *bindBuf = static_cast< TnoRef* >( allocBindBuf(parameterIndex, sizeof(i_Value)) );
*bindBuf = i_Value;
setParameter(parameterIndex, i_nType, i_nColSize, i_nScale, bindBuf, sizeof(i_Value), sizeof(i_Value));
}
void OPreparedStatement::setParameter(const sal_Int32 parameterIndex, const sal_Int32 _nType, const sal_Int16 _nScale, const OUString &_sData)
{
::osl::MutexGuard aGuard( m_aMutex );
setParameterPre(parameterIndex);
assert (_nType == DataType::VARCHAR || _nType == DataType::CHAR || _nType == DataType::DECIMAL || _nType == DataType::NUMERIC);
sal_Int32 nCharLen;
sal_Int32 nByteLen;
void *pData;
if (bUseWChar)
{
/*
* On Windows, wchar is 16 bits (UTF-16 encoding), the ODBC "W" variants functions take UTF-16 encoded strings
* and character lengths are number of UTF-16 codepoints.
* Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/ms716246%28v=vs.85%29.aspx
* ODBC Programmer's reference > Developing Applications > Programming Considerations > Unicode > Unicode Function Arguments
* http://support.microsoft.com/kb/294169
*
* UnixODBC can be configured at compile-time so that the "W" variants expect
* UTF-16 or UTF-32 encoded strings, and character lengths are number of codepoints.
* However, UTF-16 is the default, what all/most distributions do
* and the established API that most drivers implement.
* As wchar is often 32 bits, this differs from C-style strings of wchar!
*
* On MacOS X, the "W" variants use wchar_t, which is UCS4
*
* Our internal OUString storage is always UTF-16, so no conversion to do here.
*/
static_assert(sizeof (SQLWCHAR) == 2 || sizeof (SQLWCHAR) == 4, "must be 2 or 4");
if (sizeof (SQLWCHAR) == 2)
{
nCharLen = _sData.getLength();
nByteLen = 2 * nCharLen;
pData = allocBindBuf(parameterIndex, nByteLen);
memcpy(pData, _sData.getStr(), nByteLen);
}
else
{
pData = allocBindBuf(parameterIndex, _sData.getLength() * 4);
sal_uInt32* pCursor = static_cast<sal_uInt32*>(pData);
nCharLen = 0;
for (sal_Int32 i = 0; i != _sData.getLength();)
{
*pCursor++ = _sData.iterateCodePoints(&i);
nCharLen += 1;
}
nByteLen = 4 * nCharLen;
}
}
else
{
assert(getOwnConnection()->getTextEncoding() != RTL_TEXTENCODING_UCS2 &&
getOwnConnection()->getTextEncoding() != RTL_TEXTENCODING_UCS4);
OString sOData(
OUStringToOString(_sData, getOwnConnection()->getTextEncoding()));
nCharLen = nByteLen = sOData.getLength();
pData = allocBindBuf(parameterIndex, nByteLen);
memcpy(pData, sOData.getStr(), nByteLen);
}
setParameter( parameterIndex, _nType, nCharLen, _nScale, pData, nByteLen, nByteLen );
}
void OPreparedStatement::setParameter(const sal_Int32 parameterIndex, const sal_Int32 _nType, const Sequence< sal_Int8 > &x)
{
::osl::MutexGuard aGuard( m_aMutex );
setParameterPre(parameterIndex);
assert(_nType == DataType::BINARY || _nType == DataType::VARBINARY);
// don't copy the sequence, just point the ODBC directly at the sequence's storage array
// Why BINARY/Sequence is treated differently than strings (which are copied), I'm not sure
OSL_VERIFY(allocBindBuf(parameterIndex, 0) == nullptr);
boundParams[parameterIndex-1].setSequence(x); // this ensures that the sequence stays alive
setParameter( parameterIndex, _nType, x.getLength(), invalid_scale, x.getConstArray(), x.getLength(), x.getLength() );
}
void OPreparedStatement::setParameter(const sal_Int32 parameterIndex, const sal_Int32 _nType, const SQLULEN _nColumnSize, const sal_Int32 _nScale, const void* const _pData, const SQLULEN _nDataLen, const SQLLEN _nDataAllocLen)
{
SQLSMALLINT fCType, fSqlType;
OTools::getBindTypes(bUseWChar, m_pConnection->useOldDateFormat(), OTools::jdbcTypeToOdbc(_nType), fCType, fSqlType);
SQLLEN& rDataLen = boundParams[parameterIndex-1].getBindLengthBuffer();
rDataLen = _nDataLen;
SQLRETURN nRetcode;
nRetcode = (*reinterpret_cast<T3SQLBindParameter>(m_pConnection->getOdbcFunction(ODBC3SQLFunctionId::BindParameter)))(
m_aStatementHandle,
// checkParameterIndex guarantees this is safe
static_cast<SQLUSMALLINT>(parameterIndex),
SQL_PARAM_INPUT,
fCType,
fSqlType,
_nColumnSize,
_nScale,
// we trust the ODBC driver not to touch it because SQL_PARAM_INPUT
const_cast<void*>(_pData),
_nDataAllocLen,
&rDataLen);
OTools::ThrowException(m_pConnection.get(), nRetcode, m_aStatementHandle, SQL_HANDLE_STMT, *this);
}
void SAL_CALL OPreparedStatement::setByte( const sal_Int32 parameterIndex, const sal_Int8 x )
{
setScalarParameter(parameterIndex, DataType::TINYINT, 3, 0, x);
}
void SAL_CALL OPreparedStatement::setDate( sal_Int32 parameterIndex, const Date& aData )
{
DATE_STRUCT x(OTools::DateToOdbcDate(aData));
setScalarParameter<DATE_STRUCT&>(parameterIndex, DataType::DATE, 10, x);
}
void SAL_CALL OPreparedStatement::setTime( sal_Int32 parameterIndex, const css::util::Time& aVal )
{
SQLULEN nColSize;
if(aVal.NanoSeconds == 0)
nColSize = 8;
else if(aVal.NanoSeconds % 100000000 == 0)
nColSize = 10;
else if(aVal.NanoSeconds % 10000000 == 0)
nColSize = 11;
else if(aVal.NanoSeconds % 1000000 == 0)
nColSize = 12;
else if(aVal.NanoSeconds % 100000 == 0)
nColSize = 13;
else if(aVal.NanoSeconds % 10000 == 0)
nColSize = 14;
else if(aVal.NanoSeconds % 1000 == 0)
nColSize = 15;
else if(aVal.NanoSeconds % 100 == 0)
nColSize = 16;
else if(aVal.NanoSeconds % 10 == 0)
nColSize = 17;
else
nColSize = 18;
TIME_STRUCT x(OTools::TimeToOdbcTime(aVal));
setScalarParameter<TIME_STRUCT&>(parameterIndex, DataType::TIME, nColSize, (nColSize == 8)? 0 : nColSize-9, x);
}
void SAL_CALL OPreparedStatement::setTimestamp( sal_Int32 parameterIndex, const DateTime& aVal )
{
SQLULEN nColSize;
if(aVal.NanoSeconds == 0)
{
if (aVal.Seconds == 0)
nColSize=16;
else
nColSize=19;
}
else if(aVal.NanoSeconds % 100000000 == 0)
nColSize = 21;
else if(aVal.NanoSeconds % 10000000 == 0)
nColSize = 22;
else if(aVal.NanoSeconds % 1000000 == 0)
nColSize = 23;
else if(aVal.NanoSeconds % 100000 == 0)
nColSize = 24;
else if(aVal.NanoSeconds % 10000 == 0)
nColSize = 25;
else if(aVal.NanoSeconds % 1000 == 0)
nColSize = 26;
else if(aVal.NanoSeconds % 100 == 0)
nColSize = 27;
else if(aVal.NanoSeconds % 10 == 0)
nColSize = 28;
else
nColSize = 29;
TIMESTAMP_STRUCT x(OTools::DateTimeToTimestamp(aVal));
setScalarParameter<TIMESTAMP_STRUCT&>(parameterIndex, DataType::TIMESTAMP, nColSize, (nColSize <= 19)? 0 : nColSize-20, x);
}
void SAL_CALL OPreparedStatement::setDouble( sal_Int32 parameterIndex, double x )
{
setScalarParameter(parameterIndex, DataType::DOUBLE, 15, x);
}
void SAL_CALL OPreparedStatement::setFloat( sal_Int32 parameterIndex, float x )
{
setScalarParameter(parameterIndex, DataType::FLOAT, 15, x);
}
void SAL_CALL OPreparedStatement::setInt( sal_Int32 parameterIndex, sal_Int32 x )
{
setScalarParameter(parameterIndex, DataType::INTEGER, 10, 0, x);
}
void SAL_CALL OPreparedStatement::setLong( sal_Int32 parameterIndex, sal_Int64 x )
{
try
{
setScalarParameter(parameterIndex, DataType::BIGINT, 19, 0, x);
}
catch(SQLException&)
{
setString(parameterIndex, ORowSetValue(x));
}
}
void SAL_CALL OPreparedStatement::setNull( sal_Int32 parameterIndex, const sal_Int32 _nType )
{
::osl::MutexGuard aGuard( m_aMutex );
setParameterPre(parameterIndex);
OSL_VERIFY(allocBindBuf(parameterIndex, 0) == nullptr);
SQLLEN * const lenBuf = getLengthBuf (parameterIndex);
*lenBuf = SQL_NULL_DATA;
SQLSMALLINT fCType;
SQLSMALLINT fSqlType;
OTools::getBindTypes( bUseWChar,
m_pConnection->useOldDateFormat(),
OTools::jdbcTypeToOdbc(_nType),
fCType,
fSqlType);
SQLRETURN nReturn = N3SQLBindParameter( m_aStatementHandle,
static_cast<SQLUSMALLINT>(parameterIndex),
SQL_PARAM_INPUT,
fCType,
fSqlType,
0,
0,
nullptr,
0,
lenBuf
);
OTools::ThrowException(m_pConnection.get(),nReturn,m_aStatementHandle,SQL_HANDLE_STMT,*this);
}
void SAL_CALL OPreparedStatement::setClob( sal_Int32 parameterIndex, const Reference< XClob >& x )
{
if ( x.is() )
setStream(parameterIndex, x->getCharacterStream(), x->length(), DataType::LONGVARCHAR);
}
void SAL_CALL OPreparedStatement::setBlob( sal_Int32 parameterIndex, const Reference< XBlob >& x )
{
if ( x.is() )
setStream(parameterIndex, x->getBinaryStream(), x->length(), DataType::LONGVARBINARY);
}
void SAL_CALL OPreparedStatement::setArray( sal_Int32 /*parameterIndex*/, const Reference< XArray >& /*x*/ )
{
::dbtools::throwFunctionNotSupportedSQLException( "XParameters::setArray", *this );
}
void SAL_CALL OPreparedStatement::setRef( sal_Int32 /*parameterIndex*/, const Reference< XRef >& /*x*/ )
{
::dbtools::throwFunctionNotSupportedSQLException( "XParameters::setRef", *this );
}
void SAL_CALL OPreparedStatement::setObjectWithInfo( sal_Int32 parameterIndex, const Any& x, sal_Int32 sqlType, sal_Int32 scale )
{
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
::osl::MutexGuard aGuard( m_aMutex );
prepareStatement();
// For each known SQL Type, call the appropriate
// set routine
switch (sqlType)
{
case DataType::CHAR:
case DataType::VARCHAR:
case DataType::LONGVARCHAR:
if(x.hasValue())
{
OUString sStr;
x >>= sStr;
setParameter(parameterIndex, sqlType, scale, sStr);
}
else
setNull(parameterIndex,sqlType);
break;
case DataType::DECIMAL:
case DataType::NUMERIC:
if(x.hasValue())
{
ORowSetValue aValue;
aValue.fill(x);
// TODO: make sure that this calls the string overload
setParameter(parameterIndex, sqlType, scale, aValue);
}
else
setNull(parameterIndex,sqlType);
break;
default:
::dbtools::setObjectWithInfo(this,parameterIndex,x,sqlType,scale);
}
}
void SAL_CALL OPreparedStatement::setObjectNull( sal_Int32 parameterIndex, sal_Int32 sqlType, const OUString& /*typeName*/ )
{
setNull(parameterIndex,sqlType);
}
void SAL_CALL OPreparedStatement::setObject( sal_Int32 parameterIndex, const Any& x )
{
if (!::dbtools::implSetObject(this, parameterIndex, x))
{ // there is no other setXXX call which can handle the value in x
throw SQLException();
}
}
void SAL_CALL OPreparedStatement::setShort( sal_Int32 parameterIndex, sal_Int16 x )
{
setScalarParameter(parameterIndex, DataType::SMALLINT, 5, 0, x);
}
void SAL_CALL OPreparedStatement::setBytes( sal_Int32 parameterIndex, const Sequence< sal_Int8 >& x )
{
setParameter(parameterIndex, DataType::BINARY, x);
}
void SAL_CALL OPreparedStatement::setCharacterStream( sal_Int32 parameterIndex, const Reference< css::io::XInputStream >& x, sal_Int32 length )
{
// LEM: It is quite unclear to me what the interface here is.
// The XInputStream provides *bytes*, not characters.
setStream(parameterIndex, x, length, DataType::LONGVARCHAR);
}
void SAL_CALL OPreparedStatement::setBinaryStream( sal_Int32 parameterIndex, const Reference< css::io::XInputStream >& x, sal_Int32 length )
{
setStream(parameterIndex, x, length, DataType::LONGVARBINARY);
}
void SAL_CALL OPreparedStatement::clearParameters( )
{
::osl::MutexGuard aGuard( m_aMutex );
prepareStatement();
OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
N3SQLFreeStmt (m_aStatementHandle, SQL_RESET_PARAMS);
N3SQLFreeStmt (m_aStatementHandle, SQL_UNBIND);
}
void SAL_CALL OPreparedStatement::clearBatch( )
{
::dbtools::throwFunctionNotSupportedSQLException( "XPreparedBatchExecution::clearBatch", *this );
// clearParameters( );
// m_aBatchVector.erase();
}
void SAL_CALL OPreparedStatement::addBatch( )
{
::dbtools::throwFunctionNotSupportedSQLException( "XPreparedBatchExecution::addBatch", *this );
}
Sequence< sal_Int32 > SAL_CALL OPreparedStatement::executeBatch( )
{
::dbtools::throwFunctionNotSupportedSQLException( "XPreparedBatchExecution::executeBatch", *this );
// not reached, but keep -Werror happy
return Sequence< sal_Int32 > ();
}
// methods
// initBoundParam
// Initialize the bound parameter objects
void OPreparedStatement::initBoundParam ()
{
OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
// Get the number of parameters
numParams = 0;
N3SQLNumParams (m_aStatementHandle,&numParams);
// There are parameter markers, allocate the bound
// parameter objects
if (numParams > 0)
{
boundParams.reset(new OBoundParam[numParams]);
}
}
// allocBindBuf
// Allocate storage for the permanent data buffer for the bound
// parameter.
void* OPreparedStatement::allocBindBuf( sal_Int32 index,sal_Int32 bufLen)
{
void* b = nullptr;
// Sanity check the parameter number
if ((index >= 1) && (index <= numParams))
{
b = boundParams[index - 1].allocBindDataBuffer(bufLen);
}
return b;
}
// getLengthBuf
// Gets the length buffer for the given parameter index
SQLLEN* OPreparedStatement::getLengthBuf (sal_Int32 index)
{
SQLLEN* b = nullptr;
// Sanity check the parameter number
if ((index >= 1) &&
(index <= numParams))
{
b = &boundParams[index - 1].getBindLengthBuffer ();
}
return b;
}
// putParamData
// Puts parameter data from a previously bound input stream. The
// input stream was bound using SQL_LEN_DATA_AT_EXEC.
void OPreparedStatement::putParamData (sal_Int32 index)
{
// Sanity check the parameter index
if ((index < 1) ||
(index > numParams))
{
return;
}
// We'll transfer up to MAX_PUT_DATA_LENGTH at a time
Sequence< sal_Int8 > buf( MAX_PUT_DATA_LENGTH );
// Get the information about the input stream
Reference< XInputStream> inputStream = boundParams[index - 1].getInputStream ();
if ( !inputStream.is() )
{
::connectivity::SharedResources aResources;
const OUString sError( aResources.getResourceString(STR_NO_INPUTSTREAM));
throw SQLException (sError, *this,OUString(),0,Any());
}
sal_Int32 maxBytesLeft = boundParams[index - 1].getInputStreamLen ();
// Loop while more data from the input stream
sal_Int32 haveRead = 0;
try
{
do
{
sal_Int32 toReadThisRound = std::min( MAX_PUT_DATA_LENGTH, maxBytesLeft );
// Read some data from the input stream
haveRead = inputStream->readBytes( buf, toReadThisRound );
OSL_ENSURE( haveRead == buf.getLength(), "OPreparedStatement::putParamData: inconsistency!" );
if ( !haveRead )
// no more data in the stream - the given stream length was a maximum which could not be
// fulfilled by the stream
break;
// Put the data
OSL_ENSURE( m_aStatementHandle, "OPreparedStatement::putParamData: StatementHandle is null!" );
N3SQLPutData ( m_aStatementHandle, buf.getArray(), buf.getLength() );
// decrement the number of bytes still needed
maxBytesLeft -= haveRead;
}
while ( maxBytesLeft > 0 );
}
catch (const IOException& ex)
{
// If an I/O exception was generated, turn
// it into a SQLException
throw SQLException(ex.Message,*this,OUString(),0,Any());
}
}
// setStream
// Sets an input stream as a parameter, using the given SQL type
void OPreparedStatement::setStream(
sal_Int32 ParameterIndex,
const Reference< XInputStream>& x,
SQLLEN length,
sal_Int32 _nType)
{
::osl::MutexGuard aGuard( m_aMutex );
checkDisposed(OStatement_BASE::rBHelper.bDisposed);
prepareStatement();
checkParameterIndex(ParameterIndex);
// Get the buffer needed for the length
SQLLEN * const lenBuf = getLengthBuf(ParameterIndex);
// Allocate a new buffer for the parameter data. This buffer
// will be returned by SQLParamData (it is set to the parameter
// number, a sal_Int32)
sal_Int32* dataBuf = static_cast<sal_Int32*>( allocBindBuf(ParameterIndex, sizeof(ParameterIndex)) );
*dataBuf = ParameterIndex;
// Bind the parameter with SQL_LEN_DATA_AT_EXEC
*lenBuf = SQL_LEN_DATA_AT_EXEC (length);
SQLSMALLINT fCType, fSqlType;
OTools::getBindTypes(bUseWChar, m_pConnection->useOldDateFormat(), OTools::jdbcTypeToOdbc(_nType), fCType, fSqlType);
OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
N3SQLBindParameter(m_aStatementHandle,
static_cast<SQLUSMALLINT>(ParameterIndex),
SQL_PARAM_INPUT,
fCType,
fSqlType,
length,
invalid_scale,
dataBuf,
sizeof(ParameterIndex),
lenBuf);
// Save the input stream
boundParams[ParameterIndex - 1].setInputStream (x, length);
}
void OPreparedStatement::FreeParams()
{
numParams = 0;
boundParams.reset();
}
void OPreparedStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue)
{
try
{
switch(nHandle)
{
case PROPERTY_ID_RESULTSETCONCURRENCY:
if(!isPrepared())
setResultSetConcurrency(comphelper::getINT32(rValue));
break;
case PROPERTY_ID_RESULTSETTYPE:
if(!isPrepared())
setResultSetType(comphelper::getINT32(rValue));
break;
case PROPERTY_ID_FETCHDIRECTION:
if(!isPrepared())
setFetchDirection(comphelper::getINT32(rValue));
break;
case PROPERTY_ID_USEBOOKMARKS:
if(!isPrepared())
setUsingBookmarks(comphelper::getBOOL(rValue));
break;
default:
OStatement_Base::setFastPropertyValue_NoBroadcast(nHandle,rValue);
}
}
catch(const SQLException&)
{
// throw Exception(e.Message,*this);
}
}
void OPreparedStatement::prepareStatement()
{
if(!isPrepared())
{
OSL_ENSURE(m_aStatementHandle,"StatementHandle is null!");
OString aSql(OUStringToOString(m_sSqlStatement,getOwnConnection()->getTextEncoding()));
SQLRETURN nReturn = N3SQLPrepare(m_aStatementHandle, reinterpret_cast<SDB_ODBC_CHAR *>(const_cast<char *>(aSql.getStr())), aSql.getLength());
OTools::ThrowException(m_pConnection.get(),nReturn,m_aStatementHandle,SQL_HANDLE_STMT,*this);
m_bPrepared = true;
initBoundParam();
}
}
void OPreparedStatement::checkParameterIndex(sal_Int32 _parameterIndex)
{
if( _parameterIndex > numParams ||
_parameterIndex < 1 ||
_parameterIndex > std::numeric_limits<SQLUSMALLINT>::max() )
{
::connectivity::SharedResources aResources;
const OUString sError( aResources.getResourceStringWithSubstitution(STR_WRONG_PARAM_INDEX,
"$pos$", OUString::number(_parameterIndex),
"$count$", OUString::number(numParams)
));
SQLException aNext(sError,*this, OUString(),0,Any());
::dbtools::throwInvalidIndexException(*this,makeAny(aNext));
}
}
OResultSet* OPreparedStatement::createResulSet()
{
OResultSet* pReturn = new OResultSet(m_aStatementHandle,this);
pReturn->setMetaData(getMetaData());
return pReturn;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|