/src/libreoffice/include/connectivity/dbconversion.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #pragma once |
21 | | |
22 | | #include <connectivity/dbtoolsdllapi.hxx> |
23 | | #include <rtl/ustring.hxx> |
24 | | |
25 | | namespace com::sun::star::uno { class Any; } |
26 | | namespace com::sun::star::uno { template <typename > class Reference; } |
27 | | namespace com::sun::star::util { struct Date; } |
28 | | |
29 | | namespace com::sun::star |
30 | | { |
31 | | namespace lang |
32 | | { |
33 | | struct Locale; |
34 | | } |
35 | | namespace sdb |
36 | | { |
37 | | class XColumn; |
38 | | class XColumnUpdate; |
39 | | } |
40 | | namespace beans |
41 | | { |
42 | | class XPropertySet; |
43 | | } |
44 | | namespace util |
45 | | { |
46 | | class XNumberFormatter; |
47 | | class XNumberFormatsSupplier; |
48 | | struct Time; |
49 | | struct DateTime; |
50 | | } |
51 | | namespace script |
52 | | { |
53 | | class XTypeConverter; |
54 | | } |
55 | | namespace sdbc |
56 | | { |
57 | | class XConnection; |
58 | | } |
59 | | } |
60 | | |
61 | | namespace dbtools::DBTypeConversion |
62 | | { |
63 | | OOO_DLLPUBLIC_DBTOOLS css::util::Date const & getStandardDate(); |
64 | | /// @throws css::lang::IllegalArgumentException |
65 | | OOO_DLLPUBLIC_DBTOOLS void setValue(const css::uno::Reference< css::sdb::XColumnUpdate>& xVariant, |
66 | | const css::uno::Reference< css::util::XNumberFormatter>& xFormatter, |
67 | | const css::util::Date& rNullDate, |
68 | | const OUString& rString, |
69 | | sal_Int32 nKey, |
70 | | sal_Int16 nFieldType, |
71 | | sal_Int16 nKeyType); |
72 | | |
73 | | /// @throws css::lang::IllegalArgumentException |
74 | | OOO_DLLPUBLIC_DBTOOLS void setValue(const css::uno::Reference< css::sdb::XColumnUpdate>& xVariant, |
75 | | const css::util::Date& rNullDate, |
76 | | const double& rValue, |
77 | | sal_Int16 nKeyType); |
78 | | |
79 | | OOO_DLLPUBLIC_DBTOOLS double getValue( const css::uno::Reference< css::sdb::XColumn>& xVariant, const css::util::Date& rNullDate ); |
80 | | |
81 | | // get the columnvalue as string with a default format given by the column or a default format |
82 | | // for the type |
83 | | OOO_DLLPUBLIC_DBTOOLS OUString getFormattedValue( |
84 | | const css::uno::Reference< css::beans::XPropertySet>& _xColumn, |
85 | | const css::uno::Reference< css::util::XNumberFormatter>& xFormatter, |
86 | | const css::lang::Locale& _rLocale, |
87 | | const css::util::Date& rNullDate); |
88 | | |
89 | | OOO_DLLPUBLIC_DBTOOLS OUString getFormattedValue( |
90 | | const css::uno::Reference< css::sdb::XColumn>& _xColumn, |
91 | | const css::uno::Reference< css::util::XNumberFormatter>& xFormatter, |
92 | | const css::util::Date& rNullDate, |
93 | | sal_Int32 nKey, |
94 | | sal_Int16 nKeyType); |
95 | | |
96 | | OOO_DLLPUBLIC_DBTOOLS css::util::Date toDate(double dVal, const css::util::Date& _rNullDate = getStandardDate()); |
97 | | OOO_DLLPUBLIC_DBTOOLS css::util::Date toDate(std::u16string_view _sSQLDate); |
98 | | OOO_DLLPUBLIC_DBTOOLS css::util::Time toTime(double dVal, short nDigits = 9); |
99 | | OOO_DLLPUBLIC_DBTOOLS css::util::Time toTime(std::u16string_view _sSQLDate); |
100 | | OOO_DLLPUBLIC_DBTOOLS css::util::DateTime toDateTime(double dVal, const css::util::Date& _rNullDate = getStandardDate()); |
101 | | OOO_DLLPUBLIC_DBTOOLS css::util::DateTime toDateTime(const OUString& _sSQLDate); |
102 | | |
103 | | OOO_DLLPUBLIC_DBTOOLS sal_Int64 getNsFromTime(const css::util::Time& rVal); |
104 | | |
105 | | OOO_DLLPUBLIC_DBTOOLS sal_Int32 toDays(const css::util::Date& _rVal, const css::util::Date& _rNullDate = getStandardDate()); |
106 | | |
107 | | OOO_DLLPUBLIC_DBTOOLS double toDouble(const css::util::Date& rVal, const css::util::Date& _rNullDate = getStandardDate()); |
108 | | OOO_DLLPUBLIC_DBTOOLS double toDouble(const css::util::Time& rVal); |
109 | | OOO_DLLPUBLIC_DBTOOLS double toDouble(const css::util::DateTime& rVal, const css::util::Date& _rNullDate = getStandardDate()); |
110 | | |
111 | | OOO_DLLPUBLIC_DBTOOLS css::util::Date toDate(sal_Int32 _nVal); |
112 | | OOO_DLLPUBLIC_DBTOOLS css::util::Time toTime(sal_Int64 _nVal); |
113 | | |
114 | | /** convert a double which is a date value relative to the standard db null date into a date value relative |
115 | | to a given fixed date. |
116 | | */ |
117 | 0 | inline double toNullDate(const css::util::Date& _rNullDate, double _rVal) { return _rVal - toDays(_rNullDate); } |
118 | | |
119 | | // return the date from the numberformatsupplier or the STANDARD_DATE (1899,12,30) |
120 | | OOO_DLLPUBLIC_DBTOOLS css::util::Date getNULLDate(const css::uno::Reference< css::util::XNumberFormatsSupplier > &xSupplier); |
121 | | |
122 | | // return the date in the format %04d-%02d-%02d |
123 | | OOO_DLLPUBLIC_DBTOOLS OUString toDateString(const css::util::Date& rDate); |
124 | | // return the time in the format %02d:%02d:%02d |
125 | | OOO_DLLPUBLIC_DBTOOLS OUString toTimeStringS(const css::util::Time& rTime); |
126 | | // return the time in the format %02d:%02d:%02d[.%d], the fraction (up to 9 |
127 | | // digits, trailing zeros trimmed) being omitted entirely when there is none. |
128 | | // The value's full precision is kept - callers that build a SQL literal for a |
129 | | // backend which cannot parse that many fractional digits must round the value |
130 | | // first, see roundTimeFraction() below (the trailing-zero trim here is what |
131 | | // makes such a rounded value actually render with fewer digits, rather than |
132 | | // the same digit count with zeros in the low positions). |
133 | | OOO_DLLPUBLIC_DBTOOLS OUString toTimeString(const css::util::Time& rTime); |
134 | | // return the DateTime in the format %04d-%02d-%02d, plus the time part as toTimeString() above |
135 | | OOO_DLLPUBLIC_DBTOOLS OUString toDateTimeString(const css::util::DateTime& _rDateTime); |
136 | | |
137 | | /** rounds rTime's fractional seconds to at most nMaxFractionDigits digits |
138 | | (nearest 10^(9-nMaxFractionDigits) nanoseconds), carrying into |
139 | | seconds/minutes/hours as needed. A value of 9 (or higher, or negative) is |
140 | | treated as "no cap" and leaves rTime untouched. |
141 | | |
142 | | Some backends (e.g. Firebird, whose native TIME/TIMESTAMP precision is |
143 | | 1/10000s) reject a literal carrying more fractional digits than that |
144 | | (tdf#153057) - see dbtools::DatabaseMetaData::getMaxDateTimeLiteralFractionDigits() |
145 | | for the driver-aware digit count to pass in. This is deliberately *not* |
146 | | done inside toTimeString()/toDateTimeString(), which several drivers use to |
147 | | render a value for parameter binding rather than to build a literal (e.g. |
148 | | PostgreSQL's setTime()/setTimestamp(), which support microsecond precision) |
149 | | - only code generating a SQL literal should opt in. |
150 | | |
151 | | @return whether the rounding carried past 23:59:59 into the next day. A bare |
152 | | Time has no date to absorb that, so its Hours wrap to 0; use |
153 | | roundDateTimeFraction() when a date is available. [[nodiscard]] because |
154 | | silently dropping this return is exactly how the day-carry regression |
155 | | this function was introduced to fix would resurface - a caller with a |
156 | | genuine Date in scope (as opposed to a bare Time for a SQL TIME value, |
157 | | which has nothing to advance) must check it. |
158 | | */ |
159 | | [[nodiscard]] OOO_DLLPUBLIC_DBTOOLS bool roundTimeFraction(css::util::Time& rTime, sal_Int32 nMaxFractionDigits); |
160 | | |
161 | | /** as roundTimeFraction(), but advances rDateTime's date part by a day if the |
162 | | rounding carries past 23:59:59. |
163 | | */ |
164 | | OOO_DLLPUBLIC_DBTOOLS void roundDateTimeFraction(css::util::DateTime& rDateTime, sal_Int32 nMaxFractionDigits); |
165 | | // return the any in an sql standard format, capping any fractional-seconds |
166 | | // digits to what _rxConnection's driver declares it can parse in a literal |
167 | | // (see dbtools::DatabaseMetaData::getMaxDateTimeLiteralFractionDigits()) |
168 | | OOO_DLLPUBLIC_DBTOOLS OUString toSQLString(sal_Int32 eType, const css::uno::Any& _rVal, |
169 | | const css::uno::Reference< css::script::XTypeConverter >& _rxTypeConverter, |
170 | | const css::uno::Reference< css::sdbc::XConnection >& _rxConnection); |
171 | | |
172 | | /** converts a Unicode string into a 8-bit string, using the given encoding |
173 | | |
174 | | @param _rSource |
175 | | the source string to convert |
176 | | @param _rDest |
177 | | the destination string |
178 | | @param _eEncoding |
179 | | the encoding to use for the conversion |
180 | | |
181 | | @throws css::sdbc::SQLException |
182 | | if the given string contains characters which are not convertible using the given encoding |
183 | | The SQLState of the exception will be set to 22018 ("Invalid character value for cast specification") |
184 | | |
185 | | @return |
186 | | the length of the converted string |
187 | | */ |
188 | | OOO_DLLPUBLIC_DBTOOLS sal_Int32 convertUnicodeString( |
189 | | const OUString& _rSource, |
190 | | OString& _rDest, |
191 | | rtl_TextEncoding _eEncoding |
192 | | ); |
193 | | |
194 | | /** converts a Unicode string into a 8-bit string, using the given encoding |
195 | | |
196 | | @param _rSource |
197 | | the source string to convert |
198 | | |
199 | | @param _rDest |
200 | | the destination string |
201 | | |
202 | | @param _nMaxLen |
203 | | the maximum length of the destination string |
204 | | |
205 | | @param _eEncoding |
206 | | the encoding to use for the conversion |
207 | | |
208 | | @throws css::sdbc::SQLException |
209 | | if convertUnicodeString, which is called internally, throws such an exception |
210 | | |
211 | | @throws css::sdbc::SQLException |
212 | | if the conversion results in a string which is longer than _nMaxLen |
213 | | |
214 | | @return |
215 | | the length of the converted string |
216 | | */ |
217 | | OOO_DLLPUBLIC_DBTOOLS sal_Int32 convertUnicodeStringToLength( |
218 | | const OUString& _rSource, |
219 | | OString& _rDest, |
220 | | sal_Int32 _nMaxLen, |
221 | | rtl_TextEncoding _eEncoding |
222 | | ); |
223 | | |
224 | | } // namespace dbtools::DBTypeConversion |
225 | | |
226 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |