1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _DBHELPER_DBCONVERSION_HXX_
25 #define _DBHELPER_DBCONVERSION_HXX_
26 
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/util/Date.hpp>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include "connectivity/dbtoolsdllapi.hxx"
31 
32 // forward declarations
33 namespace rtl
34 {
35 	class OUString;
36 }
37 namespace com
38 {
39 	namespace sun
40 	{
41 		namespace star
42 		{
43 			namespace lang
44 			{
45 				struct Locale;
46 			}
47 			namespace sdb
48 			{
49 				class XColumn;
50 				class XColumnUpdate;
51 			}
52 			namespace sdbc
53 			{
54 				class SQLException;
55 			}
56 			namespace beans
57 			{
58 				class XPropertySet;
59 			}
60 			namespace util
61 			{
62 				class XNumberFormatter;
63 				class XNumberFormatsSupplier;
64 				struct Time;
65 				struct DateTime;
66 			}
67 			namespace script
68 			{
69 				class XTypeConverter;
70 			}
71 		}
72 	}
73 }
74 
75 //.........................................................................
76 namespace dbtools
77 {
78 //.........................................................................
79 
80 	class OOO_DLLPUBLIC_DBTOOLS DBTypeConversion
81 	{
82 	public:
83 		static ::com::sun::star::util::Date getStandardDate();
84 		static void setValue(const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumnUpdate>& xVariant,
85 									const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
86 									const ::com::sun::star::util::Date& rNullDate,
87 									const ::rtl::OUString& rString,
88 									sal_Int32 nKey,
89 									sal_Int16 nFieldType,
90 									sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException);
91 
92 		static void setValue(const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumnUpdate>& xVariant,
93 							 const ::com::sun::star::util::Date& rNullDate,
94 							 const double& rValue,
95 							 sal_Int16 nKeyType) throw(::com::sun::star::lang::IllegalArgumentException);
96 
97 		static double getValue( const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn>& xVariant, const ::com::sun::star::util::Date& rNullDate );
98 
99 		// get the columnvalue as string with a default format given by the column or a default format
100 		// for the type
101 		static ::rtl::OUString getFormattedValue(
102                                         const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xColumn,
103 										const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
104 										const ::com::sun::star::lang::Locale& _rLocale,
105 										const ::com::sun::star::util::Date& rNullDate);
106 
107 		static ::rtl::OUString getFormattedValue(
108                                         const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::XColumn>& _xColumn,
109 										const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter>& xFormatter,
110 										const ::com::sun::star::util::Date& rNullDate,
111 										sal_Int32 nKey,
112 										sal_Int16 nKeyType);
113 
114 		static ::com::sun::star::util::Date		toDate(double dVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
115 		static ::com::sun::star::util::Date		toDate(const ::rtl::OUString& _sSQLDate);
116 		static ::com::sun::star::util::Time		toTime(double dVal);
117 		static ::com::sun::star::util::Time		toTime(const ::rtl::OUString& _sSQLDate);
118 		static ::com::sun::star::util::DateTime	toDateTime(double dVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
119 		static ::com::sun::star::util::DateTime	toDateTime(const ::rtl::OUString& _sSQLDate);
120 
121 
122 		/** return the given DateTime as JDBC compliant 64 bit value
123 		*/
124 		static sal_Int64 toINT64(const ::com::sun::star::util::DateTime& rVal);
125 		static sal_Int32 getMsFromTime(const ::com::sun::star::util::Time& rVal);
126 
127 		static sal_Int32 toDays(const ::com::sun::star::util::Date& _rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
128 
129 		static double	toDouble(const ::com::sun::star::util::Date& rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
130 		static double	toDouble(const ::com::sun::star::util::Time& rVal);
131 		static double	toDouble(const ::com::sun::star::util::DateTime& rVal, const ::com::sun::star::util::Date& _rNullDate = getStandardDate());
132 
133 		static sal_Int32	toINT32(const ::com::sun::star::util::Date& rVal);
134 		static sal_Int32	toINT32(const ::com::sun::star::util::Time& rVal);
135 
136 		static ::com::sun::star::util::Date	toDate(sal_Int32 _nVal);
137 		static ::com::sun::star::util::Time	toTime(sal_Int32 _nVal);
138 
139 		/** convert a double which is a date value relative to a given fixed date into a date value relative
140 			to the standard db null date.
141 		*/
toStandardDbDate(const::com::sun::star::util::Date & _rNullDate,double _rVal)142 		static double toStandardDbDate(const ::com::sun::star::util::Date& _rNullDate, double _rVal) { return _rVal + toDays(_rNullDate); }
143 		/** convert a double which is a date value relative to the standard db null date into a date value relative
144 			to a given fixed date.
145 		*/
toNullDate(const::com::sun::star::util::Date & _rNullDate,double _rVal)146 		static double toNullDate(const ::com::sun::star::util::Date& _rNullDate, double _rVal) { return _rVal - toDays(_rNullDate); }
147 
148 		// return the date from the numberformatsupplier or the STANDARD_DATE (1900,1,1)
149 		static ::com::sun::star::util::Date getNULLDate(const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier > &xSupplier);
150 
151 		// return the date in the format %04d-%02d-%02d
152 		static ::rtl::OUString toDateString(const ::com::sun::star::util::Date& rDate);
153 		// return the time in the format %02d:%02d:%02d
154 		static ::rtl::OUString toTimeString(const ::com::sun::star::util::Time& rTime);
155 		// return the DateTime in the format %04d-%02d-%02d %02d:%02d:%02d
156 		static ::rtl::OUString toDateTimeString(const ::com::sun::star::util::DateTime& _rDateTime);
157 		// return the any in an sql standard format
158 		static ::rtl::OUString toSQLString(sal_Int32 eType, const ::com::sun::star::uno::Any& _rVal, sal_Bool bQuote,
159 			const ::com::sun::star::uno::Reference< ::com::sun::star::script::XTypeConverter >&  _rxTypeConverter);
160 
161         /** converts a Unicode string into a 8-bit string, using the given encoding
162 
163             @param _rSource
164                 the source string to convert
165             @param _rDest
166                 the destination string
167             @param _eEncoding
168                 the encoding to use for the conversion
169 
170             @throws com::sun::star::sdbc::SQLException
171                 if the given string contains characters which are not convertible using the given encoding
172                 The SQLState of the exception will be set to 22018 ("Invalid character value for cast specification")
173 
174             @return
175                 the length of the converted string
176         */
177         static sal_Int32 convertUnicodeString(
178             const ::rtl::OUString& _rSource,
179             ::rtl::OString&  _rDest,
180             rtl_TextEncoding _eEncoding
181         )
182             SAL_THROW((::com::sun::star::sdbc::SQLException));
183 
184         /** converts a Unicode string into a 8-bit string, using the given encoding
185 
186             @param _rSource
187                 the source string to convert
188 
189             @param _rDest
190                 the destination string
191 
192             @param _nMaxLen
193                 the maximum length of the destination string
194 
195             @param _eEncoding
196                 the encoding to use for the conversion
197 
198             @throws com::sun::star::sdbc::SQLException
199                 if convertUnicodeString, which is called internally, throws such an exception
200 
201             @throws com::sun::star::sdbc::SQLException
202                 if the conversion results in a string which is longer than _nMaxLen
203 
204             @return
205                 the length of the converted string
206         */
207         static sal_Int32 convertUnicodeStringToLength(
208             const ::rtl::OUString& _rSource,
209             ::rtl::OString&  _rDest,
210             sal_Int32 _nMaxLen,
211             rtl_TextEncoding _eEncoding
212        )
213             SAL_THROW((::com::sun::star::sdbc::SQLException));
214 	};
215 
216 //.........................................................................
217 }	// namespace dbtools
218 //.........................................................................
219 
220 #endif // _DBHELPER_DBCONVERSION_HXX_
221 
222