summaryrefslogtreecommitdiffstats
path: root/connectivity/source/drivers/firebird/StatementCommonBase.cxx
blob: df4d971e89bc1c07e6f465e7df61387067c6ddab (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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 "StatementCommonBase.hxx"
#include "Util.hxx"

#include <sal/log.hxx>
#include <comphelper/sequence.hxx>
#include <cppuhelper/typeprovider.hxx>
#include <propertyids.hxx>
#include <vcl/svapp.hxx>
#include <TConnection.hxx>

#include <com/sun/star/sdbc/SQLException.hpp>

using namespace ::connectivity::firebird;

using namespace ::com::sun::star;
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;

using namespace ::comphelper;
using namespace ::osl;
using namespace ::std;

OStatementCommonBase::OStatementCommonBase(Connection* _pConnection)
    : OStatementCommonBase_Base(m_aMutex),
      OPropertySetHelper(OStatementCommonBase_Base::rBHelper),
      m_pConnection(_pConnection),
#if SAL_TYPES_SIZEOFPOINTER == 8
      m_aStatementHandle(0)
#else
      m_aStatementHandle(nullptr)
#endif
{
}

OStatementCommonBase::~OStatementCommonBase()
{
}

void OStatementCommonBase::disposeResultSet()
{
    uno::Reference< XComponent > xComp(m_xResultSet, UNO_QUERY);
    if (xComp.is())
        xComp->dispose();
    m_xResultSet.clear();
}

void OStatementCommonBase::freeStatementHandle()
{
    if (m_aStatementHandle)
    {
        isc_dsql_free_statement(m_statusVector,
                                &m_aStatementHandle,
                                DSQL_drop);
        evaluateStatusVector(m_statusVector,
                             u"isc_dsql_free_statement",
                             *this);
    }
}


Any SAL_CALL OStatementCommonBase::queryInterface( const Type & rType )
{
    Any aRet = OStatementCommonBase_Base::queryInterface(rType);
    if(!aRet.hasValue())
        aRet = OPropertySetHelper::queryInterface(rType);
    return aRet;
}

Sequence< Type > SAL_CALL OStatementCommonBase::getTypes(  )
{
    ::cppu::OTypeCollection aTypes(
        ::cppu::UnoType<XMultiPropertySet>::get(),
        ::cppu::UnoType<XFastPropertySet>::get(),
        ::cppu::UnoType<XPropertySet>::get());

    return concatSequences(aTypes.getTypes(),OStatementCommonBase_Base::getTypes());
}


void SAL_CALL OStatementCommonBase::cancel(  )
{
    MutexGuard aGuard(m_aMutex);
    checkDisposed(OStatementCommonBase_Base::rBHelper.bDisposed);
    // cancel the current sql statement
}

void SAL_CALL OStatementCommonBase::close()
{
    SAL_INFO("connectivity.firebird", "close");

    {
        MutexGuard aGuard(m_aMutex);
        checkDisposed(OStatementCommonBase_Base::rBHelper.bDisposed);
        disposeResultSet();
        freeStatementHandle();
    }

    dispose();
}

void OStatementCommonBase::prepareAndDescribeStatement(std::u16string_view sql, XSQLDA*& pOutSqlda)
{
    SolarMutexGuard g; // tdf#122129

    freeStatementHandle();

    if (!pOutSqlda)
    {
        pOutSqlda = static_cast<XSQLDA*>(calloc(1, XSQLDA_LENGTH(10)));
        pOutSqlda->version = SQLDA_VERSION1;
        pOutSqlda->sqln = 10;
    }

    ISC_STATUS aErr = isc_dsql_allocate_statement(m_statusVector,
                                       &m_pConnection->getDBHandle(),
                                       &m_aStatementHandle);

    if (aErr)
    {
        evaluateStatusVector(m_statusVector,
                             u"isc_dsql_allocate_statement",
                             *this);
    }
    else
    {
        aErr = isc_dsql_prepare(m_statusVector,
                                &m_pConnection->getTransaction(),
                                &m_aStatementHandle,
                                0,
                                OUStringToOString(sql, RTL_TEXTENCODING_UTF8).getStr(),
                                SQL_DIALECT_CURRENT,
                                pOutSqlda);

        if (aErr)
        {
            evaluateStatusVector(m_statusVector,
                                 u"isc_dsql_prepare",
                                 *this);
        }
        else
        {
            // Ensure we have enough space in pOutSqlda
            if (pOutSqlda->sqld > pOutSqlda->sqln)
            {
                int n = pOutSqlda->sqld;
                free(pOutSqlda);
                pOutSqlda = static_cast<XSQLDA*>(calloc(1, XSQLDA_LENGTH(n)));
                pOutSqlda->version = SQLDA_VERSION1;
                pOutSqlda->sqln = n;
                aErr = isc_dsql_describe(m_statusVector,
                                         &m_aStatementHandle,
                                         1,
                                         pOutSqlda);
            }

            // Process each XSQLVAR parameter structure in the output XSQLDA
            if (aErr)
            {
                evaluateStatusVector(m_statusVector,
                                     u"isc_dsql_describe",
                                     *this);
            }
            else
            {
                mallocSQLVAR(pOutSqlda);
            }
        }
        if(aErr)
        {
            freeStatementHandle();
        }
    }
    if(aErr)
    {
        free(pOutSqlda);
        pOutSqlda = nullptr;
    }
}

// ---- XMultipleResults - UNSUPPORTED ----------------------------------------
uno::Reference< XResultSet > SAL_CALL OStatementCommonBase::getResultSet()
{
    // TODO: verify we really can't support this
//     return uno::Reference< XResultSet >();
    MutexGuard aGuard(m_aMutex);
    checkDisposed(OStatementCommonBase_Base::rBHelper.bDisposed);

    return m_xResultSet;
}

sal_Bool SAL_CALL OStatementCommonBase::getMoreResults()
{
    // TODO: verify we really can't support this
    return false;
//     MutexGuard aGuard( m_aMutex );
//     checkDisposed(OStatementCommonBase_Base::rBHelper.bDisposed);
}

sal_Int32 SAL_CALL OStatementCommonBase::getUpdateCount()
{
    // TODO: verify we really can't support this
    return -1;
}


// ---- XWarningsSupplier - UNSUPPORTED ----------------------------------------
Any SAL_CALL OStatementCommonBase::getWarnings()
{
    return Any();
}

void SAL_CALL OStatementCommonBase::clearWarnings()
{
}

::cppu::IPropertyArrayHelper* OStatementCommonBase::createArrayHelper( ) const
{
    // this properties are define by the service statement
    // they must in alphabetic order
    return new ::cppu::OPropertyArrayHelper
    {
        {
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_CURSORNAME),
                PROPERTY_ID_CURSORNAME,
                cppu::UnoType<OUString>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ESCAPEPROCESSING),
                PROPERTY_ID_ESCAPEPROCESSING,
                cppu::UnoType<bool>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHDIRECTION),
                PROPERTY_ID_FETCHDIRECTION,
                cppu::UnoType<sal_Int32>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FETCHSIZE),
                PROPERTY_ID_FETCHSIZE,
                cppu::UnoType<sal_Int32>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXFIELDSIZE),
                PROPERTY_ID_MAXFIELDSIZE,
                cppu::UnoType<sal_Int32>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_MAXROWS),
                PROPERTY_ID_MAXROWS,
                cppu::UnoType<sal_Int32>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_QUERYTIMEOUT),
                PROPERTY_ID_QUERYTIMEOUT,
                cppu::UnoType<sal_Int32>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETCONCURRENCY),
                PROPERTY_ID_RESULTSETCONCURRENCY,
                cppu::UnoType<sal_Int32>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RESULTSETTYPE),
                PROPERTY_ID_RESULTSETTYPE,
                cppu::UnoType<sal_Int32>::get(),
                0
            },
            {
                ::connectivity::OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_USEBOOKMARKS),
                PROPERTY_ID_USEBOOKMARKS,
                cppu::UnoType<bool>::get(),
                0
            }
        }
    };
}


::cppu::IPropertyArrayHelper & OStatementCommonBase::getInfoHelper()
{
    return *getArrayHelper();
}

sal_Bool OStatementCommonBase::convertFastPropertyValue(
                            Any &,
                            Any &,
                            sal_Int32,
                            const Any& )
{
    // here we have to try to convert
    return false;
}

void OStatementCommonBase::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any&)
{
    // set the value to whatever is necessary
    switch(nHandle)
    {
        case PROPERTY_ID_QUERYTIMEOUT:
        case PROPERTY_ID_MAXFIELDSIZE:
        case PROPERTY_ID_MAXROWS:
        case PROPERTY_ID_CURSORNAME:
        case PROPERTY_ID_RESULTSETCONCURRENCY:
        case PROPERTY_ID_RESULTSETTYPE:
        case PROPERTY_ID_FETCHDIRECTION:
        case PROPERTY_ID_FETCHSIZE:
        case PROPERTY_ID_ESCAPEPROCESSING:
        case PROPERTY_ID_USEBOOKMARKS:
        default:
            ;
    }
}

void OStatementCommonBase::getFastPropertyValue(Any&,sal_Int32 nHandle) const
{
    switch(nHandle)
    {
        case PROPERTY_ID_QUERYTIMEOUT:
        case PROPERTY_ID_MAXFIELDSIZE:
        case PROPERTY_ID_MAXROWS:
        case PROPERTY_ID_CURSORNAME:
        case PROPERTY_ID_RESULTSETCONCURRENCY:
        case PROPERTY_ID_RESULTSETTYPE:
        case PROPERTY_ID_FETCHDIRECTION:
        case PROPERTY_ID_FETCHSIZE:
        case PROPERTY_ID_ESCAPEPROCESSING:
        case PROPERTY_ID_USEBOOKMARKS:
        default:
            ;
    }
}

void SAL_CALL OStatementCommonBase::acquire() noexcept
{
    OStatementCommonBase_Base::acquire();
}

void SAL_CALL OStatementCommonBase::release() noexcept
{
    OStatementCommonBase_Base::release();
}

uno::Reference< css::beans::XPropertySetInfo > SAL_CALL OStatementCommonBase::getPropertySetInfo(  )
{
    return ::cppu::OPropertySetHelper::createPropertySetInfo(getInfoHelper());
}

short OStatementCommonBase::getSqlInfoItem(char aInfoItem)
{
    ISC_STATUS_ARRAY aStatusVector;
    ISC_STATUS aErr;

    char aInfoItems[] = {aInfoItem};
    char aResultsBuffer[8];

    aErr = isc_dsql_sql_info(aStatusVector,
                             &m_aStatementHandle,
                             sizeof(aInfoItems),
                             aInfoItems,
                             sizeof(aResultsBuffer),
                             aResultsBuffer);

    if (!aErr && aResultsBuffer[0] == aInfoItem)
    {
        const short aBytes = static_cast<short>(isc_vax_integer(aResultsBuffer+1, 2));
        return static_cast<short>(isc_vax_integer(aResultsBuffer+3, aBytes));
    }

    evaluateStatusVector(aStatusVector,
                         u"isc_dsq_sql_info",
                         *this);
    return 0;
}

bool OStatementCommonBase::isDDLStatement()
{
    return getSqlInfoItem(isc_info_sql_stmt_type) == isc_info_sql_stmt_ddl;
}

sal_Int32 OStatementCommonBase::getStatementChangeCount()
{
    const short aStatementType = getSqlInfoItem(isc_info_sql_stmt_type);


    ISC_STATUS_ARRAY aStatusVector;
    ISC_STATUS aErr;

    // This is somewhat undocumented so I'm just guessing and hoping for the best.
    char aInfoItems[] = {isc_info_sql_records};
    char aResultsBuffer[1024];

    aErr = isc_dsql_sql_info(aStatusVector,
                             &m_aStatementHandle,
                             sizeof(aInfoItems),
                             aInfoItems,
                             sizeof(aResultsBuffer),
                             aResultsBuffer);

    if (aErr)
    {
        evaluateStatusVector(aStatusVector,
                             u"isc_dsq_sql_info",
                             *this);
        return 0;
    }

    short aDesiredInfoType = 0;
    switch (aStatementType)
    {
        case isc_info_sql_stmt_select:
            aDesiredInfoType = isc_info_req_select_count;
            break;
        case isc_info_sql_stmt_insert:
            aDesiredInfoType = isc_info_req_insert_count;
            break;
        case isc_info_sql_stmt_update:
            aDesiredInfoType = isc_info_req_update_count;
            break;
        case isc_info_sql_stmt_delete:
            aDesiredInfoType = isc_info_req_delete_count;
            break;
        case isc_info_sql_stmt_exec_procedure:
            return 0; // cannot determine
        default:
            throw SQLException(); // TODO: better error message?
    }

    char* pResults = aResultsBuffer;
    if (static_cast<short>(*pResults++) != isc_info_sql_records)
        return 0;

//         const short aTotalLength = (short) isc_vax_integer(pResults, 2);
    pResults += 2;

    // Seems to be of form TOKEN (1 byte), LENGTH (2 bytes), DATA (LENGTH bytes)
    while (*pResults != isc_info_rsb_end)
    {
        const char aToken = *pResults;
        const short aLength =  static_cast<short>(isc_vax_integer(pResults+1, 2));

        if (aToken == aDesiredInfoType)
        {
            return isc_vax_integer(pResults + 3, aLength);
        }

        pResults += (3 + aLength);
    }

    return 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */