1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef _DBHELPER_DBCONVERSION_HXX_
21 #define _DBHELPER_DBCONVERSION_HXX_
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/util/Date.hpp>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include "connectivity/dbtoolsdllapi.hxx"
28 // forward declarations
54 class XNumberFormatter
;
55 class XNumberFormatsSupplier
;
67 //.........................................................................
70 //.........................................................................
72 class OOO_DLLPUBLIC_DBTOOLS DBTypeConversion
75 static ::com::sun::star::util::Date
getStandardDate();
76 static void setValue(const ::com::sun::star::uno::Reference
< ::com::sun::star::sdb::XColumnUpdate
>& xVariant
,
77 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
>& xFormatter
,
78 const ::com::sun::star::util::Date
& rNullDate
,
79 const OUString
& rString
,
82 sal_Int16 nKeyType
) throw(::com::sun::star::lang::IllegalArgumentException
);
84 static void setValue(const ::com::sun::star::uno::Reference
< ::com::sun::star::sdb::XColumnUpdate
>& xVariant
,
85 const ::com::sun::star::util::Date
& rNullDate
,
87 sal_Int16 nKeyType
) throw(::com::sun::star::lang::IllegalArgumentException
);
89 static double getValue( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdb::XColumn
>& xVariant
, const ::com::sun::star::util::Date
& rNullDate
);
91 // get the columnvalue as string with a default format given by the column or a default format
93 static OUString
getFormattedValue(
94 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _xColumn
,
95 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
>& xFormatter
,
96 const ::com::sun::star::lang::Locale
& _rLocale
,
97 const ::com::sun::star::util::Date
& rNullDate
);
99 static OUString
getFormattedValue(
100 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdb::XColumn
>& _xColumn
,
101 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
>& xFormatter
,
102 const ::com::sun::star::util::Date
& rNullDate
,
106 static ::com::sun::star::util::Date
toDate(double dVal
, const ::com::sun::star::util::Date
& _rNullDate
= getStandardDate());
107 static ::com::sun::star::util::Date
toDate(const OUString
& _sSQLDate
);
108 static ::com::sun::star::util::Time
toTime(double dVal
, short nDigits
= 9);
109 static ::com::sun::star::util::Time
toTime(const OUString
& _sSQLDate
);
110 static ::com::sun::star::util::DateTime
toDateTime(double dVal
, const ::com::sun::star::util::Date
& _rNullDate
= getStandardDate());
111 static ::com::sun::star::util::DateTime
toDateTime(const OUString
& _sSQLDate
);
114 // TODO: consider removing getMsFromTime
115 static sal_Int32
getMsFromTime(const ::com::sun::star::util::Time
& rVal
);
116 static sal_Int64
getNsFromTime(const ::com::sun::star::util::Time
& rVal
);
118 static sal_Int32
toDays(const ::com::sun::star::util::Date
& _rVal
, const ::com::sun::star::util::Date
& _rNullDate
= getStandardDate());
120 static double toDouble(const ::com::sun::star::util::Date
& rVal
, const ::com::sun::star::util::Date
& _rNullDate
= getStandardDate());
121 static double toDouble(const ::com::sun::star::util::Time
& rVal
);
122 static double toDouble(const ::com::sun::star::util::DateTime
& rVal
, const ::com::sun::star::util::Date
& _rNullDate
= getStandardDate());
124 static sal_Int32
toINT32(const ::com::sun::star::util::Date
& rVal
);
125 static sal_Int64
toINT64(const ::com::sun::star::util::Time
& rVal
);
127 static ::com::sun::star::util::Date
toDate(sal_Int32 _nVal
);
128 static ::com::sun::star::util::Time
toTime(sal_Int64 _nVal
);
130 /** convert a double which is a date value relative to a given fixed date into a date value relative
131 to the standard db null date.
133 static double toStandardDbDate(const ::com::sun::star::util::Date
& _rNullDate
, double _rVal
) { return _rVal
+ toDays(_rNullDate
); }
134 /** convert a double which is a date value relative to the standard db null date into a date value relative
135 to a given fixed date.
137 static double toNullDate(const ::com::sun::star::util::Date
& _rNullDate
, double _rVal
) { return _rVal
- toDays(_rNullDate
); }
139 // return the date from the numberformatsupplier or the STANDARD_DATE (1900,1,1)
140 static ::com::sun::star::util::Date
getNULLDate(const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatsSupplier
> &xSupplier
);
142 // return the date in the format %04d-%02d-%02d
143 static OUString
toDateString(const ::com::sun::star::util::Date
& rDate
);
144 // return the time in the format %02d:%02d:%02d.%09d
145 static OUString
toTimeString(const ::com::sun::star::util::Time
& rTime
);
146 // return the DateTime in the format %04d-%02d-%02d %02d:%02d:%02d.%09d
147 static OUString
toDateTimeString(const ::com::sun::star::util::DateTime
& _rDateTime
);
148 // return the any in an sql standard format
149 static OUString
toSQLString(sal_Int32 eType
, const ::com::sun::star::uno::Any
& _rVal
, sal_Bool bQuote
,
150 const ::com::sun::star::uno::Reference
< ::com::sun::star::script::XTypeConverter
>& _rxTypeConverter
);
152 /** converts a Unicode string into a 8-bit string, using the given encoding
155 the source string to convert
157 the destination string
159 the encoding to use for the conversion
161 @throws com::sun::star::sdbc::SQLException
162 if the given string contains characters which are not convertible using the given encoding
163 The SQLState of the exception will be set to 22018 ("Invalid character value for cast specification")
166 the length of the converted string
168 static sal_Int32
convertUnicodeString(
169 const OUString
& _rSource
,
171 rtl_TextEncoding _eEncoding
173 SAL_THROW((::com::sun::star::sdbc::SQLException
));
175 /** converts a Unicode string into a 8-bit string, using the given encoding
178 the source string to convert
181 the destination string
184 the maximum length of the destination string
187 the encoding to use for the conversion
189 @throws com::sun::star::sdbc::SQLException
190 if convertUnicodeString, which is called internally, throws such an exception
192 @throws com::sun::star::sdbc::SQLException
193 if the conversion results in a string which is longer than _nMaxLen
196 the length of the converted string
198 static sal_Int32
convertUnicodeStringToLength(
199 const OUString
& _rSource
,
202 rtl_TextEncoding _eEncoding
204 SAL_THROW((::com::sun::star::sdbc::SQLException
));
207 //.........................................................................
208 } // namespace dbtools
209 //.........................................................................
211 #endif // _DBHELPER_DBCONVERSION_HXX_
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */