summaryrefslogtreecommitdiff
path: root/ucbhelper/source/provider/resultsetmetadata.cxx
blob: 0b389eb4d4acdd2ee3453b2e289c00bfc0fcf9c2 (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
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
/* -*- 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 .
 */


/**************************************************************************
                                TODO
 **************************************************************************

 *************************************************************************/

#include "osl/diagnose.h"
#include <com/sun/star/beans/Property.hpp>
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sdbc/DataType.hpp>
#include <com/sun/star/sdbc/XArray.hpp>
#include <com/sun/star/sdbc/XBlob.hpp>
#include <com/sun/star/sdbc/XClob.hpp>
#include <com/sun/star/sdbc/XRef.hpp>
#include <com/sun/star/util/Date.hpp>
#include <com/sun/star/util/Time.hpp>
#include <com/sun/star/util/DateTime.hpp>
#include <com/sun/star/ucb/PropertiesManager.hpp>
#include <ucbhelper/resultsetmetadata.hxx>

using namespace com::sun::star::beans;
using namespace com::sun::star::io;
using namespace com::sun::star::lang;
using namespace com::sun::star::sdbc;
using namespace com::sun::star::ucb;
using namespace com::sun::star::uno;
using namespace com::sun::star::util;


namespace ucbhelper_impl {

struct ResultSetMetaData_Impl
{
    osl::Mutex                                      m_aMutex;
    std::vector< ::ucbhelper::ResultSetColumnData > m_aColumnData;
    bool                                        m_bObtainedTypes;
    bool                                        m_bGlobalReadOnlyValue;

    ResultSetMetaData_Impl( sal_Int32 nSize )
    : m_aColumnData( nSize ), m_bObtainedTypes( false ),
      m_bGlobalReadOnlyValue( true ) {}

    ResultSetMetaData_Impl(
        const std::vector< ::ucbhelper::ResultSetColumnData >& rColumnData )
    : m_aColumnData( rColumnData ), m_bObtainedTypes( false ),
      m_bGlobalReadOnlyValue( false ) {}
};

}

using namespace ucbhelper_impl;

namespace ucbhelper {




// ResultSetMetaData Implementation.




ResultSetMetaData::ResultSetMetaData(
                        const Reference< XComponentContext >& rxContext,
                        const Sequence< Property >& rProps,
                        bool bReadOnly )
: m_pImpl( new ResultSetMetaData_Impl( rProps.getLength() ) ),
  m_xContext( rxContext ),
  m_aProps( rProps ),
  m_bReadOnly( bReadOnly )
{
}


ResultSetMetaData::ResultSetMetaData(
                        const Reference< XComponentContext >& rxContext,
                        const Sequence< Property >& rProps,
                        const std::vector< ResultSetColumnData >& rColumnData )
: m_pImpl( new ResultSetMetaData_Impl( rColumnData ) ),
  m_xContext( rxContext ),
  m_aProps( rProps ),
  m_bReadOnly( true )
{
    OSL_ENSURE( rColumnData.size() == sal_uInt32( rProps.getLength() ),
                "ResultSetMetaData ctor - different array sizes!" );
}


// virtual
ResultSetMetaData::~ResultSetMetaData()
{
    delete m_pImpl;
}



// XInterface methods.

void SAL_CALL ResultSetMetaData::acquire()
    throw()
{
    OWeakObject::acquire();
}

void SAL_CALL ResultSetMetaData::release()
    throw()
{
    OWeakObject::release();
}

css::uno::Any SAL_CALL ResultSetMetaData::queryInterface( const css::uno::Type & rType )
    throw( css::uno::RuntimeException, std::exception )
{
    css::uno::Any aRet = cppu::queryInterface( rType,
                                               (static_cast< XTypeProvider* >(this)),
                                               (static_cast< XResultSetMetaData* >(this))
                                               );
    return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
}

// XTypeProvider methods.



XTYPEPROVIDER_IMPL_2( ResultSetMetaData,
                      XTypeProvider,
                      XResultSetMetaData );



// XResultSetMetaData methods.



// virtual
sal_Int32 SAL_CALL ResultSetMetaData::getColumnCount()
    throw( SQLException, RuntimeException, std::exception )
{
    return m_aProps.getLength();
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isAutoIncrement( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Checks whether column is automatically numbered, which makes it
        read-only.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_False;

    return m_pImpl->m_aColumnData[ column - 1 ].isAutoIncrement;
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isCaseSensitive( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_False;

    return m_pImpl->m_aColumnData[ column - 1 ].isCaseSensitive;
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isSearchable( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Checks whether the value stored in column can be used in a
        WHERE clause.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_False;

    return m_pImpl->m_aColumnData[ column - 1 ].isSearchable;
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isCurrency( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Checks whether column is a cash value.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_False;

    return m_pImpl->m_aColumnData[ column - 1 ].isCurrency;
}


// virtual
sal_Int32 SAL_CALL ResultSetMetaData::isNullable( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Checks whether a NULL can be stored in column.
        Possible values: see com/sun/star/sdbc/ColumnValue.idl
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return ColumnValue::NULLABLE;

    return m_pImpl->m_aColumnData[ column - 1 ].isNullable;
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isSigned( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Checks whether the value stored in column is a signed number.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_False;

    return m_pImpl->m_aColumnData[ column - 1 ].isSigned;
}


// virtual
sal_Int32 SAL_CALL ResultSetMetaData::getColumnDisplaySize( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the normal maximum width in characters for column.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return 16;

    return m_pImpl->m_aColumnData[ column - 1 ].columnDisplaySize;
}


// virtual
OUString SAL_CALL ResultSetMetaData::getColumnLabel( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the suggested column title for column, to be used in print-
        outs and displays.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return OUString();

    OUString aLabel = m_pImpl->m_aColumnData[ column - 1 ].columnLabel;
    if ( !aLabel.isEmpty() )
        return aLabel;

    return m_aProps.getConstArray()[ column - 1 ].Name;
}


// virtual
OUString SAL_CALL ResultSetMetaData::getColumnName( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the name of column.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return OUString();

    return m_aProps.getConstArray()[ column - 1 ].Name;
}


// virtual
OUString SAL_CALL ResultSetMetaData::getSchemaName( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the schema name for the table from which column of this
        result set was derived.
        Because this feature is not widely supported, the return value
        for many DBMSs will be an empty string.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return OUString();

    return m_pImpl->m_aColumnData[ column - 1 ].schemaName;
}


// virtual
sal_Int32 SAL_CALL ResultSetMetaData::getPrecision( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        For number types, getprecision gets the number of decimal digits
        in column.
        For character types, it gets the maximum length in characters for
        column.
        For binary types, it gets the maximum length in bytes for column.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return -1;

    return m_pImpl->m_aColumnData[ column - 1 ].precision;
}


// virtual
sal_Int32 SAL_CALL ResultSetMetaData::getScale( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the number of digits to the right of the decimal point for
        values in column.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return 0;

    return m_pImpl->m_aColumnData[ column - 1 ].scale;
}


// virtual
OUString SAL_CALL ResultSetMetaData::getTableName( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the name of the table from which column of this result set
        was derived or "" if there is none (for example, for a join).
        Because this feature is not widely supported, the return value
        for many DBMSs will be an empty string.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return OUString();

    return m_pImpl->m_aColumnData[ column - 1 ].tableName;
}


// virtual
OUString SAL_CALL ResultSetMetaData::getCatalogName( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the catalog name for the table from which column of this
        result set was derived.
        Because this feature is not widely supported, the return value
        for many DBMSs will be an empty string.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return OUString();

    return m_pImpl->m_aColumnData[ column - 1 ].catalogName;
}


// virtual
sal_Int32 SAL_CALL ResultSetMetaData::getColumnType( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the JDBC type for the value stored in column. ... The STRUCT
        and DISTINCT type codes are always returned for structured and
        distinct types, regardless of whether the value will be mapped
        according to the standard mapping or be a custom mapping.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return DataType::SQLNULL;

    if ( m_aProps.getConstArray()[ column - 1 ].Type
            == getCppuVoidType() )
    {
        // No type given. Try UCB's Properties Manager...

        osl::Guard< osl::Mutex > aGuard( m_pImpl->m_aMutex );

        if ( !m_pImpl->m_bObtainedTypes )
        {
            try
            {
                Reference< XPropertySetInfo > xInfo = PropertiesManager::create( m_xContext );
    // Less (remote) calls...

                Sequence< Property > aProps = xInfo->getProperties();
                const Property* pProps1 = aProps.getConstArray();
                sal_Int32 nCount1 = aProps.getLength();

                sal_Int32 nCount = m_aProps.getLength();
                Property* pProps = m_aProps.getArray();
                for ( sal_Int32 n = 0; n < nCount; ++n )
                {
                    Property& rProp = pProps[ n ];

                    for ( sal_Int32 m = 0; m < nCount1; ++m )
                    {
                        const Property& rProp1 = pProps1[ m ];
                        if ( rProp.Name == rProp1.Name )
                        {
                            // Found...
                            rProp.Type = rProp1.Type;
                            break;
                        }
                    }
                }
            }
            catch ( RuntimeException& )
            {
                throw;
            }
            catch ( Exception& )
            {
                // createInstance
            }

            m_pImpl->m_bObtainedTypes = true;
        }
    }

    const Type& rType = m_aProps.getConstArray()[ column - 1 ].Type;
    sal_Int32 nType = DataType::OTHER;

    if ( rType == cppu::UnoType<OUString>::get() )
        nType = DataType::VARCHAR;  // XRow::getString
    else if ( rType == getCppuBooleanType() )
        nType = DataType::BIT;      // XRow::getBoolean
    else if ( rType == cppu::UnoType<sal_Int32>::get() )
        nType = DataType::INTEGER;  // XRow::getInt
    else if ( rType == cppu::UnoType<sal_Int64>::get() )
        nType = DataType::BIGINT;   // XRow::getLong
    else if ( rType == cppu::UnoType<sal_Int16>::get() )
        nType = DataType::SMALLINT; // XRow::getShort
    else if ( rType == cppu::UnoType<sal_Int8>::get() )
        nType = DataType::TINYINT;  // XRow::getByte
    else if ( rType == cppu::UnoType<float>::get() )
        nType = DataType::REAL;     // XRow::getFloat
    else if ( rType == cppu::UnoType<double>::get() )
        nType = DataType::DOUBLE;   // XRow::getDouble
    else if ( rType == getCppuType( static_cast< const Sequence< sal_Int8 > * >( 0 ) ) )
        nType = DataType::VARBINARY;// XRow::getBytes
    else if ( rType == cppu::UnoType<Date>::get() )
        nType = DataType::DATE;     // XRow::getDate
    else if ( rType == cppu::UnoType<Time>::get() )
        nType = DataType::TIME;     // XRow::getTime
    else if ( rType == cppu::UnoType<DateTime>::get() )
        nType = DataType::TIMESTAMP;// XRow::getTimestamp
    else if ( rType == cppu::UnoType<XInputStream>::get() )
        nType = DataType::LONGVARBINARY;    // XRow::getBinaryStream
//      nType = DataType::LONGVARCHAR;      // XRow::getCharacterStream
    else if ( rType == cppu::UnoType<XClob>::get() )
        nType = DataType::CLOB; // XRow::getClob
    else if ( rType == cppu::UnoType<XBlob>::get() )
        nType = DataType::BLOB; // XRow::getBlob
    else if ( rType == cppu::UnoType<XArray>::get() )
        nType = DataType::ARRAY;// XRow::getArray
    else if ( rType == cppu::UnoType<XRef>::get() )
        nType = DataType::REF;// XRow::getRef
    else
        nType = DataType::OBJECT;// XRow::getObject

    return nType;
}


// virtual
OUString SAL_CALL ResultSetMetaData::getColumnTypeName( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
        Gets the type name used by this particular data source for the
        values stored in column. If the type code for the type of value
        stored in column is STRUCT, DISTINCT or JAVA_OBJECT, this method
        returns a fully-qualified SQL type name.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return OUString();

    return m_pImpl->m_aColumnData[ column - 1 ].columnTypeName;
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isReadOnly( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    if ( m_pImpl->m_bGlobalReadOnlyValue )
        return m_bReadOnly;

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_True;

    // autoincrement==true => readonly
    return m_pImpl->m_aColumnData[ column - 1 ].isAutoIncrement ||
           m_pImpl->m_aColumnData[ column - 1 ].isReadOnly;
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isWritable( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    if ( m_pImpl->m_bGlobalReadOnlyValue )
        return !m_bReadOnly;

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_False;

    return m_pImpl->m_aColumnData[ column - 1 ].isWritable;
}


// virtual
sal_Bool SAL_CALL ResultSetMetaData::isDefinitelyWritable( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    if ( m_pImpl->m_bGlobalReadOnlyValue )
        return !m_bReadOnly;

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return sal_False;

    return m_pImpl->m_aColumnData[ column - 1 ].isDefinitelyWritable;
}


// virtual
OUString SAL_CALL ResultSetMetaData::getColumnServiceName( sal_Int32 column )
    throw( SQLException, RuntimeException, std::exception )
{
    /*
          Returns the fully-qualified name of the service whose instances
         are manufactured if XResultSet::getObject is called to retrieve
        a value from the column.
     */

    if ( ( column < 1 ) || ( column > m_aProps.getLength() ) )
        return OUString();

    return m_pImpl->m_aColumnData[ column - 1 ].columnServiceName;
}

} // namespace ucbhelper

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */