summaryrefslogtreecommitdiff
path: root/include/connectivity/dbconversion.hxx
blob: ebd06b12dfdaa833f1db7b2cca01ffa8f05c6cad (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
/* -*- 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 .
 */

#ifndef INCLUDED_CONNECTIVITY_DBCONVERSION_HXX
#define INCLUDED_CONNECTIVITY_DBCONVERSION_HXX

#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/util/Date.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <connectivity/dbtoolsdllapi.hxx>

// forward declarations
namespace com
{
    namespace sun
    {
        namespace star
        {
            namespace lang
            {
                struct Locale;
            }
            namespace sdb
            {
                class XColumn;
                class XColumnUpdate;
            }
            namespace sdbc
            {
                class SQLException;
            }
            namespace beans
            {
                class XPropertySet;
            }
            namespace util
            {
                class XNumberFormatter;
                class XNumberFormatsSupplier;
                struct Time;
                struct DateTime;
            }
            namespace script
            {
                class XTypeConverter;
            }
        }
    }
}


namespace dbtools
{


    namespace DBTypeConversion
    {
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Date getStandardDate();
        OOO_DLLPUBLIC_DBTOOLS void setValue(const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumnUpdate>& xVariant,
                                    const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
                                    const ::com::sun::star::util::Date& rNullDate,
                                    const OUString& rString,
                                    sal_Int32 nKey,
                                    sal_Int16 nFieldType,
                                    sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException);

        OOO_DLLPUBLIC_DBTOOLS void setValue(const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumnUpdate>& xVariant,
                             const ::com::sun::star::util::Date& rNullDate,
                             const double& rValue,
                             sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException);

        OOO_DLLPUBLIC_DBTOOLS double getValue( const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn>& xVariant, const ::com::sun::star::util::Date& rNullDate );

        // get the columnvalue as string with a default format given by the column or a default format
        // for the type
        OOO_DLLPUBLIC_DBTOOLS OUString getFormattedValue(
                                        const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xColumn,
                                        const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
                                        const ::com::sun::star::lang::Locale& _rLocale,
                                        const ::com::sun::star::util::Date& rNullDate);

        OOO_DLLPUBLIC_DBTOOLS OUString getFormattedValue(
                                        const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn>& _xColumn,
                                        const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
                                        const ::com::sun::star::util::Date& rNullDate,
                                        sal_Int32 nKey,
                                        sal_Int16 nKeyType);

        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Date     toDate(double dVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Date     toDate(const OUString& _sSQLDate);
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Time     toTime(double dVal, short nDigits = 9);
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Time     toTime(const OUString& _sSQLDate);
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::DateTime toDateTime(double dVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::DateTime toDateTime(const OUString& _sSQLDate);

        OOO_DLLPUBLIC_DBTOOLS sal_Int64 getNsFromTime(const ::com::sun::star::util::Time& rVal);

        OOO_DLLPUBLIC_DBTOOLS sal_Int32 toDays(const ::com::sun::star::util::Date& _rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());

        OOO_DLLPUBLIC_DBTOOLS double   toDouble(const ::com::sun::star::util::Date& rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
        OOO_DLLPUBLIC_DBTOOLS double   toDouble(const ::com::sun::star::util::Time& rVal);
        OOO_DLLPUBLIC_DBTOOLS double   toDouble(const ::com::sun::star::util::DateTime& rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());

        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Date toDate(sal_Int32 _nVal);
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Time toTime(sal_Int64 _nVal);

        /** convert a double which is a date value relative to a given fixed date into a date value relative
            to the standard db null date.
        */
        inline double toStandardDbDate(const ::com::sun::star::util::Date& _rNullDate, double _rVal) { return _rVal + toDays(_rNullDate); }
        /** convert a double which is a date value relative to the standard db null date into a date value relative
            to a given fixed date.
        */
        inline double toNullDate(const ::com::sun::star::util::Date& _rNullDate, double _rVal) { return _rVal - toDays(_rNullDate); }

        // return the date from the numberformatsupplier or the STANDARD_DATE (1900,1,1)
        OOO_DLLPUBLIC_DBTOOLS ::com::sun::star::util::Date getNULLDate(const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier > &xSupplier);

        // return the date in the format %04d-%02d-%02d
        OOO_DLLPUBLIC_DBTOOLS OUString toDateString(const ::com::sun::star::util::Date& rDate);
        // return the time in the format %02d:%02d:%02d
        OOO_DLLPUBLIC_DBTOOLS OUString toTimeStringS(const ::com::sun::star::util::Time& rTime);
        // return the time in the format %02d:%02d:%02d.%09d
        OOO_DLLPUBLIC_DBTOOLS OUString toTimeString(const ::com::sun::star::util::Time& rTime);
        // return the DateTime in the format %04d-%02d-%02d %02d:%02d:%02d.%09d
        OOO_DLLPUBLIC_DBTOOLS OUString toDateTimeString(const ::com::sun::star::util::DateTime& _rDateTime);
        // return the any in an sql standard format
        OOO_DLLPUBLIC_DBTOOLS OUString toSQLString(sal_Int32 eType, const ::com::sun::star::uno::Any& _rVal, bool bQuote,
            const ::com::sun::star::uno::Reference< ::com::sun::star::script::XTypeConverter >&  _rxTypeConverter);

        /** converts a Unicode string into a 8-bit string, using the given encoding

            @param _rSource
                the source string to convert
            @param _rDest
                the destination string
            @param _eEncoding
                the encoding to use for the conversion

            @throws com::sun::star::sdbc::SQLException
                if the given string contains characters which are not convertible using the given encoding
                The SQLState of the exception will be set to 22018 ("Invalid character value for cast specification")

            @return
                the length of the converted string
        */
        OOO_DLLPUBLIC_DBTOOLS sal_Int32 convertUnicodeString(
            const OUString& _rSource,
            OString&  _rDest,
            rtl_TextEncoding _eEncoding
        );

        /** converts a Unicode string into a 8-bit string, using the given encoding

            @param _rSource
                the source string to convert

            @param _rDest
                the destination string

            @param _nMaxLen
                the maximum length of the destination string

            @param _eEncoding
                the encoding to use for the conversion

            @throws com::sun::star::sdbc::SQLException
                if convertUnicodeString, which is called internally, throws such an exception

            @throws com::sun::star::sdbc::SQLException
                if the conversion results in a string which is longer than _nMaxLen

            @return
                the length of the converted string
        */
        OOO_DLLPUBLIC_DBTOOLS sal_Int32 convertUnicodeStringToLength(
            const OUString& _rSource,
            OString&  _rDest,
            sal_Int32 _nMaxLen,
            rtl_TextEncoding _eEncoding
       );
    }


}   // namespace dbtools


#endif // INCLUDED_CONNECTIVITY_DBCONVERSION_HXX

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