Line data Source code
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 : #ifndef _DBHELPER_DBCONVERSION_HXX_
21 : #define _DBHELPER_DBCONVERSION_HXX_
22 :
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"
27 :
28 : // forward declarations
29 : namespace rtl
30 : {
31 : class OUString;
32 : }
33 : namespace com
34 : {
35 : namespace sun
36 : {
37 : namespace star
38 : {
39 : namespace lang
40 : {
41 : struct Locale;
42 : }
43 : namespace sdb
44 : {
45 : class XColumn;
46 : class XColumnUpdate;
47 : }
48 : namespace sdbc
49 : {
50 : class SQLException;
51 : }
52 : namespace beans
53 : {
54 : class XPropertySet;
55 : }
56 : namespace util
57 : {
58 : class XNumberFormatter;
59 : class XNumberFormatsSupplier;
60 : struct Time;
61 : struct DateTime;
62 : }
63 : namespace script
64 : {
65 : class XTypeConverter;
66 : }
67 : }
68 : }
69 : }
70 :
71 : //.........................................................................
72 : namespace dbtools
73 : {
74 : //.........................................................................
75 :
76 : class OOO_DLLPUBLIC_DBTOOLS DBTypeConversion
77 : {
78 : public:
79 : static ::com::sun::star::util::Date getStandardDate();
80 : static void setValue(const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumnUpdate>& xVariant,
81 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
82 : const ::com::sun::star::util::Date& rNullDate,
83 : const ::rtl::OUString& rString,
84 : sal_Int32 nKey,
85 : sal_Int16 nFieldType,
86 : sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException);
87 :
88 : static void setValue(const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumnUpdate>& xVariant,
89 : const ::com::sun::star::util::Date& rNullDate,
90 : const double& rValue,
91 : sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException);
92 :
93 : static double getValue( const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn>& xVariant, const ::com::sun::star::util::Date& rNullDate );
94 :
95 : // get the columnvalue as string with a default format given by the column or a default format
96 : // for the type
97 : static ::rtl::OUString getFormattedValue(
98 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xColumn,
99 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
100 : const ::com::sun::star::lang::Locale& _rLocale,
101 : const ::com::sun::star::util::Date& rNullDate);
102 :
103 : static ::rtl::OUString getFormattedValue(
104 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn>& _xColumn,
105 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
106 : const ::com::sun::star::util::Date& rNullDate,
107 : sal_Int32 nKey,
108 : sal_Int16 nKeyType);
109 :
110 : static ::com::sun::star::util::Date toDate(double dVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
111 : static ::com::sun::star::util::Date toDate(const ::rtl::OUString& _sSQLDate);
112 : static ::com::sun::star::util::Time toTime(double dVal);
113 : static ::com::sun::star::util::Time toTime(const ::rtl::OUString& _sSQLDate);
114 : static ::com::sun::star::util::DateTime toDateTime(double dVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
115 : static ::com::sun::star::util::DateTime toDateTime(const ::rtl::OUString& _sSQLDate);
116 :
117 :
118 : static sal_Int32 getMsFromTime(const ::com::sun::star::util::Time& rVal);
119 :
120 : static sal_Int32 toDays(const ::com::sun::star::util::Date& _rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
121 :
122 : static double toDouble(const ::com::sun::star::util::Date& rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
123 : static double toDouble(const ::com::sun::star::util::Time& rVal);
124 : static double toDouble(const ::com::sun::star::util::DateTime& rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
125 :
126 : static sal_Int32 toINT32(const ::com::sun::star::util::Date& rVal);
127 : static sal_Int32 toINT32(const ::com::sun::star::util::Time& rVal);
128 :
129 : static ::com::sun::star::util::Date toDate(sal_Int32 _nVal);
130 : static ::com::sun::star::util::Time toTime(sal_Int32 _nVal);
131 :
132 : /** convert a double which is a date value relative to a given fixed date into a date value relative
133 : to the standard db null date.
134 : */
135 : static double toStandardDbDate(const ::com::sun::star::util::Date& _rNullDate, double _rVal) { return _rVal + toDays(_rNullDate); }
136 : /** convert a double which is a date value relative to the standard db null date into a date value relative
137 : to a given fixed date.
138 : */
139 0 : static double toNullDate(const ::com::sun::star::util::Date& _rNullDate, double _rVal) { return _rVal - toDays(_rNullDate); }
140 :
141 : // return the date from the numberformatsupplier or the STANDARD_DATE (1900,1,1)
142 : static ::com::sun::star::util::Date getNULLDate(const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier > &xSupplier);
143 :
144 : // return the date in the format %04d-%02d-%02d
145 : static ::rtl::OUString toDateString(const ::com::sun::star::util::Date& rDate);
146 : // return the time in the format %02d:%02d:%02d
147 : static ::rtl::OUString toTimeString(const ::com::sun::star::util::Time& rTime);
148 : // return the DateTime in the format %04d-%02d-%02d %02d:%02d:%02d
149 : static ::rtl::OUString toDateTimeString(const ::com::sun::star::util::DateTime& _rDateTime);
150 : // return the any in an sql standard format
151 : static ::rtl::OUString toSQLString(sal_Int32 eType, const ::com::sun::star::uno::Any& _rVal, sal_Bool bQuote,
152 : const ::com::sun::star::uno::Reference< ::com::sun::star::script::XTypeConverter >& _rxTypeConverter);
153 :
154 : /** converts a Unicode string into a 8-bit string, using the given encoding
155 :
156 : @param _rSource
157 : the source string to convert
158 : @param _rDest
159 : the destination string
160 : @param _eEncoding
161 : the encoding to use for the conversion
162 :
163 : @throws com::sun::star::sdbc::SQLException
164 : if the given string contains characters which are not convertible using the given encoding
165 : The SQLState of the exception will be set to 22018 ("Invalid character value for cast specification")
166 :
167 : @return
168 : the length of the converted string
169 : */
170 : static sal_Int32 convertUnicodeString(
171 : const ::rtl::OUString& _rSource,
172 : ::rtl::OString& _rDest,
173 : rtl_TextEncoding _eEncoding
174 : )
175 : SAL_THROW((::com::sun::star::sdbc::SQLException));
176 :
177 : /** converts a Unicode string into a 8-bit string, using the given encoding
178 :
179 : @param _rSource
180 : the source string to convert
181 :
182 : @param _rDest
183 : the destination string
184 :
185 : @param _nMaxLen
186 : the maximum length of the destination string
187 :
188 : @param _eEncoding
189 : the encoding to use for the conversion
190 :
191 : @throws com::sun::star::sdbc::SQLException
192 : if convertUnicodeString, which is called internally, throws such an exception
193 :
194 : @throws com::sun::star::sdbc::SQLException
195 : if the conversion results in a string which is longer than _nMaxLen
196 :
197 : @return
198 : the length of the converted string
199 : */
200 : static sal_Int32 convertUnicodeStringToLength(
201 : const ::rtl::OUString& _rSource,
202 : ::rtl::OString& _rDest,
203 : sal_Int32 _nMaxLen,
204 : rtl_TextEncoding _eEncoding
205 : )
206 : SAL_THROW((::com::sun::star::sdbc::SQLException));
207 : };
208 :
209 : //.........................................................................
210 : } // namespace dbtools
211 : //.........................................................................
212 :
213 : #endif // _DBHELPER_DBCONVERSION_HXX_
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|