summaryrefslogtreecommitdiff
path: root/connectivity/source/drivers/firebird/Util.hxx
blob: 4cbf49879465268d17ca2555c15bbd0a799dad1b (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
/* -*- 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/.
 */

#pragma once

#include <ibase.h>

#include <rtl/ustring.hxx>

#include <com/sun/star/uno/XInterface.hpp>
#include <utility>

namespace connectivity::firebird
    {
        // Type Blob has 2 subtypes values
        // 0 for BLOB, 1 for CLOB
        // see http://www.firebirdfaq.org/faq48/
        // User-defined subtypes are negative.
        // Use a number for image which is very unlikely to be defined by a
        // user.
        enum class BlobSubtype {
            Blob = 0,
            Clob = 1,
            Image = -9546
        };

        /**
         * Numeric and decimal types can be identified by their subtype in
         * Firebird API. 1 for NUMERIC, 2 for DECIMAL.
         */
        enum class NumberSubType {
            Other = 0,
            Numeric = 1,
            Decimal = 2
        };

        class ColumnTypeInfo {
private:
            short m_aType;
            short m_aSubType;
            short m_nScale;
            OUString m_sCharsetName;
public:
            /**
             * @param tType SQL type of column defined by Firebird (e.g.
             * SQL_DOUBLE)
             * @param aSubType SQL sub type as in firebird API. See
             * NumberSubType.
             * @param scale: Scale of the number. It is ignored in case it's not
             * a number. Scale obtained from the Firebird API is negative, so
             * that should be negated before passing to this constructor.
             *
             */
            explicit ColumnTypeInfo( short aType, short aSubType = 0,
                    short nScale = 0, OUString sCharset = OUString() )
                : m_aType(aType)
                , m_aSubType(aSubType)
                , m_nScale(nScale)
                , m_sCharsetName(std::move(sCharset)) {}
            explicit ColumnTypeInfo(const XSQLVAR& var, OUString sCharset)
                : ColumnTypeInfo(var.sqltype, var.sqlsubtype, -var.sqlscale, std::move(sCharset)) {}
            explicit ColumnTypeInfo(const XSQLDA* pXSQLDA, sal_Int32 column, OUString sCharset = {})
                : ColumnTypeInfo(pXSQLDA->sqlvar[column-1], std::move(sCharset)) {}
            short getType() const { return m_aType; }
            short getSubType() const { return m_aSubType; }
            short getScale() const { return m_nScale; }
            OUString const & getCharacterSet() const { return m_sCharsetName; }

            sal_Int32 getSdbcType() const;
            OUString getColumnTypeName() const;

        };

        /**
         * Make sure an identifier is safe to use within the database. Currently
         * firebird seems to return identifiers with 93 character (instead of
         * 31), whereby the name is simply padded with trailing whitespace.
         * This removes all trailing whitespace (i.e. if necessary so that
         * the length is below 31 characters). Firebird automatically compensates
         * for such shorter strings, however any trailing padding makes the gui
         * editing of such names harder, hence we remove all trailing whitespace.
         */
        OUString sanitizeIdentifier(std::u16string_view rIdentifier);

        inline bool IndicatesError(const ISC_STATUS_ARRAY& rStatusVector)
        {
            return rStatusVector[0]==1 && rStatusVector[1]; // indicates error;
        }

        OUString StatusVectorToString(const ISC_STATUS_ARRAY& rStatusVector,
                                    std::u16string_view rCause);

        /**
         * Evaluate a firebird status vector and throw exceptions as necessary.
         * The content of the status vector is included in the thrown exception.
         *
         * @throws css::sdbc::SQLException
         */
        void evaluateStatusVector(const ISC_STATUS_ARRAY& rStatusVector,
                                  std::u16string_view aCause,
                                  const css::uno::Reference< css::uno::XInterface >& _rxContext);

        /**
         * Internally (i.e. in RDB$FIELD_TYPE) firebird stores the data type
         * for a column as defined in blr_*, however in the firebird
         * api the SQL_* types are used, hence we need to be able to convert
         * between the two when retrieving column metadata.
         */
        short getFBTypeFromBlrType(short blrType);

        void mallocSQLVAR(XSQLDA* pSqlda);

        void freeSQLVAR(XSQLDA* pSqlda);

        sal_Int64 pow10Integer( int nDecimalCount );

}

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