1 /* OLE DB Conversion library
3 * Copyright 2009 Huw Davies
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "oledb_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(oledb
);
39 IDataConvert IDataConvert_iface
;
40 IDCInfo IDCInfo_iface
;
44 UINT version
; /* Set by IDCInfo_SetInfo */
47 static inline convert
*impl_from_IDataConvert(IDataConvert
*iface
)
49 return CONTAINING_RECORD(iface
, convert
, IDataConvert_iface
);
52 static inline convert
*impl_from_IDCInfo(IDCInfo
*iface
)
54 return CONTAINING_RECORD(iface
, convert
, IDCInfo_iface
);
57 static HRESULT WINAPI
convert_QueryInterface(IDataConvert
* iface
,
61 convert
*This
= impl_from_IDataConvert(iface
);
62 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), obj
);
66 if(IsEqualIID(riid
, &IID_IUnknown
) ||
67 IsEqualIID(riid
, &IID_IDataConvert
))
71 else if(IsEqualIID(riid
, &IID_IDCInfo
))
73 *obj
= &This
->IDCInfo_iface
;
77 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
81 IDataConvert_AddRef(iface
);
86 static ULONG WINAPI
convert_AddRef(IDataConvert
* iface
)
88 convert
*This
= impl_from_IDataConvert(iface
);
89 TRACE("(%p)\n", This
);
91 return InterlockedIncrement(&This
->ref
);
95 static ULONG WINAPI
convert_Release(IDataConvert
* iface
)
97 convert
*This
= impl_from_IDataConvert(iface
);
100 TRACE("(%p)\n", This
);
102 ref
= InterlockedDecrement(&This
->ref
);
109 static int get_length(DBTYPE type
)
120 return sizeof(VARIANT_BOOL
);
131 return sizeof(DBDATE
);
132 case DBTYPE_DBTIMESTAMP
:
133 return sizeof(DBTIMESTAMP
);
138 case DBTYPE_FILETIME
:
139 return sizeof(FILETIME
);
143 return sizeof(DB_NUMERIC
);
147 case DBTYPE_BYREF
| DBTYPE_WSTR
:
150 return sizeof(VARIANT
);
152 FIXME("Unhandled type %04x\n", type
);
157 static HRESULT WINAPI
convert_DataConvert(IDataConvert
* iface
,
158 DBTYPE src_type
, DBTYPE dst_type
,
159 DBLENGTH src_len
, DBLENGTH
*dst_len
,
160 void *src
, void *dst
,
161 DBLENGTH dst_max_len
,
162 DBSTATUS src_status
, DBSTATUS
*dst_status
,
163 BYTE precision
, BYTE scale
,
166 convert
*This
= impl_from_IDataConvert(iface
);
167 DBLENGTH dst_len_loc
;
168 DBSTATUS dst_status_loc
;
172 TRACE("(%p)->(%d, %d, %ld, %p, %p, %p, %ld, %d, %p, %d, %d, %x)\n", This
,
173 src_type
, dst_type
, src_len
, dst_len
, src
, dst
, dst_max_len
,
174 src_status
, dst_status
, precision
, scale
, flags
);
176 if (!dst_len
) dst_len
= &dst_len_loc
;
177 if (!dst_status
) dst_status
= &dst_status_loc
;
179 *dst_status
= DBSTATUS_E_BADACCESSOR
;
181 if(src_status
== DBSTATUS_S_ISNULL
)
183 *dst_status
= DBSTATUS_S_ISNULL
;
188 if(src_type
== DBTYPE_VARIANT
&& V_VT((VARIANT
*)src
) == VT_NULL
)
190 if(dst_type
== DBTYPE_VARIANT
)
191 *dst_len
= sizeof(VARIANT
);
193 *dst_status
= DBSTATUS_S_ISNULL
;
197 if(IDataConvert_CanConvert(iface
, src_type
, dst_type
) != S_OK
)
199 return DB_E_UNSUPPORTEDCONVERSION
;
202 if(dst
== NULL
&& get_length(dst_type
) != 0)
204 *dst_len
= get_length(src_type
);
205 *dst_status
= DBSTATUS_S_OK
;
209 if(src_type
== DBTYPE_STR
)
214 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
215 len
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, NULL
, 0) - 1;
217 len
= MultiByteToWideChar(CP_ACP
, 0, src
, src_len
, NULL
, 0);
218 b
= SysAllocStringLen(NULL
, len
);
219 if(!b
) return E_OUTOFMEMORY
;
220 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
221 MultiByteToWideChar(CP_ACP
, 0, src
, -1, b
, len
+ 1);
223 MultiByteToWideChar(CP_ACP
, 0, src
, src_len
, b
, len
);
225 hr
= IDataConvert_DataConvert(iface
, DBTYPE_BSTR
, dst_type
, 0, dst_len
,
226 &b
, dst
, dst_max_len
, src_status
, dst_status
,
227 precision
, scale
, flags
);
233 if(src_type
== DBTYPE_WSTR
)
237 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
238 b
= SysAllocString(src
);
240 b
= SysAllocStringLen(src
, src_len
/ 2);
241 if(!b
) return E_OUTOFMEMORY
;
242 hr
= IDataConvert_DataConvert(iface
, DBTYPE_BSTR
, dst_type
, 0, dst_len
,
243 &b
, dst
, dst_max_len
, src_status
, dst_status
,
244 precision
, scale
, flags
);
253 signed short *d
= dst
;
256 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
257 case DBTYPE_I2
: *d
= *(signed short*)src
; hr
= S_OK
; break;
258 case DBTYPE_I4
: hr
= VarI2FromI4(*(signed int*)src
, d
); break;
259 case DBTYPE_R4
: hr
= VarI2FromR4(*(FLOAT
*)src
, d
); break;
260 case DBTYPE_R8
: hr
= VarI2FromR8(*(double*)src
, d
); break;
261 case DBTYPE_CY
: hr
= VarI2FromCy(*(CY
*)src
, d
); break;
262 case DBTYPE_DATE
: hr
= VarI2FromDate(*(DATE
*)src
, d
); break;
263 case DBTYPE_BSTR
: hr
= VarI2FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
264 case DBTYPE_BOOL
: hr
= VarI2FromBool(*(VARIANT_BOOL
*)src
, d
); break;
265 case DBTYPE_DECIMAL
: hr
= VarI2FromDec((DECIMAL
*)src
, d
); break;
266 case DBTYPE_I1
: hr
= VarI2FromI1(*(signed char*)src
, d
); break;
267 case DBTYPE_UI1
: hr
= VarI2FromUI1(*(BYTE
*)src
, d
); break;
268 case DBTYPE_UI2
: hr
= VarI2FromUI2(*(WORD
*)src
, d
); break;
269 case DBTYPE_UI4
: hr
= VarI2FromUI4(*(DWORD
*)src
, d
); break;
270 case DBTYPE_I8
: hr
= VarI2FromI8(*(LONGLONG
*)src
, d
); break;
271 case DBTYPE_UI8
: hr
= VarI2FromUI8(*(ULONGLONG
*)src
, d
); break;
274 if ((hr
= VariantChangeType(&tmp
, (VARIANT
*)src
, 0, VT_I2
)) == S_OK
)
277 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type
); return E_NOTIMPL
;
287 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
288 case DBTYPE_I2
: hr
= VarI4FromI2(*(signed short*)src
, d
); break;
289 case DBTYPE_I4
: *d
= *(signed int*)src
; hr
= S_OK
; break;
290 case DBTYPE_R4
: hr
= VarI4FromR4(*(FLOAT
*)src
, d
); break;
291 case DBTYPE_R8
: hr
= VarI4FromR8(*(double*)src
, d
); break;
292 case DBTYPE_CY
: hr
= VarI4FromCy(*(CY
*)src
, d
); break;
293 case DBTYPE_DATE
: hr
= VarI4FromDate(*(DATE
*)src
, d
); break;
294 case DBTYPE_BSTR
: hr
= VarI4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
295 case DBTYPE_BOOL
: hr
= VarI4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
296 case DBTYPE_DECIMAL
: hr
= VarI4FromDec((DECIMAL
*)src
, d
); break;
297 case DBTYPE_I1
: hr
= VarI4FromI1(*(signed char*)src
, d
); break;
298 case DBTYPE_UI1
: hr
= VarI4FromUI1(*(BYTE
*)src
, d
); break;
299 case DBTYPE_UI2
: hr
= VarI4FromUI2(*(WORD
*)src
, d
); break;
300 case DBTYPE_UI4
: hr
= VarI4FromUI4(*(DWORD
*)src
, d
); break;
301 case DBTYPE_I8
: hr
= VarI4FromI8(*(LONGLONG
*)src
, d
); break;
302 case DBTYPE_UI8
: hr
= VarI4FromUI8(*(ULONGLONG
*)src
, d
); break;
305 if ((hr
= VariantChangeType(&tmp
, (VARIANT
*)src
, 0, VT_I4
)) == S_OK
)
308 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type
); return E_NOTIMPL
;
318 case DBTYPE_BSTR
: hr
= VarI8FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
319 case DBTYPE_I8
: *d
= *(LONGLONG
*)src
; hr
= S_OK
; break;
320 default: FIXME("Unimplemented conversion %04x -> I8\n", src_type
); return E_NOTIMPL
;
330 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
331 case DBTYPE_I2
: hr
= VarR4FromI2(*(signed short*)src
, d
); break;
332 case DBTYPE_I4
: hr
= VarR4FromI4(*(signed int*)src
, d
); break;
333 case DBTYPE_R4
: *d
= *(FLOAT
*)src
; hr
= S_OK
; break;
334 case DBTYPE_R8
: hr
= VarR4FromR8(*(double*)src
, d
); break;
335 case DBTYPE_CY
: hr
= VarR4FromCy(*(CY
*)src
, d
); break;
336 case DBTYPE_DATE
: hr
= VarR4FromDate(*(DATE
*)src
, d
); break;
337 case DBTYPE_BSTR
: hr
= VarR4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
338 case DBTYPE_BOOL
: hr
= VarR4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
339 case DBTYPE_DECIMAL
: hr
= VarR4FromDec((DECIMAL
*)src
, d
); break;
340 case DBTYPE_I1
: hr
= VarR4FromI1(*(signed char*)src
, d
); break;
341 case DBTYPE_UI1
: hr
= VarR4FromUI1(*(BYTE
*)src
, d
); break;
342 case DBTYPE_UI2
: hr
= VarR4FromUI2(*(WORD
*)src
, d
); break;
343 case DBTYPE_UI4
: hr
= VarR4FromUI4(*(DWORD
*)src
, d
); break;
344 case DBTYPE_I8
: hr
= VarR4FromI8(*(LONGLONG
*)src
, d
); break;
345 case DBTYPE_UI8
: hr
= VarR4FromUI8(*(ULONGLONG
*)src
, d
); break;
346 default: FIXME("Unimplemented conversion %04x -> R4\n", src_type
); return E_NOTIMPL
;
355 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
356 case DBTYPE_I1
: hr
= VarR8FromI1(*(signed char*)src
, d
); break;
357 case DBTYPE_I2
: hr
= VarR8FromI2(*(signed short*)src
, d
); break;
358 case DBTYPE_I4
: hr
= VarR8FromI4(*(signed int*)src
, d
); break;
359 case DBTYPE_I8
: hr
= VarR8FromI8(*(LONGLONG
*)src
, d
); break;
360 case DBTYPE_UI1
: hr
= VarR8FromUI1(*(BYTE
*)src
, d
); break;
361 case DBTYPE_UI2
: hr
= VarR8FromUI2(*(WORD
*)src
, d
); break;
362 case DBTYPE_UI4
: hr
= VarR8FromUI4(*(DWORD
*)src
, d
); break;
363 case DBTYPE_UI8
: hr
= VarR8FromUI8(*(ULONGLONG
*)src
, d
); break;
364 case DBTYPE_R4
: hr
= VarR8FromR4(*(FLOAT
*)src
, d
); break;
365 case DBTYPE_R8
: *d
= *(DOUBLE
*)src
; hr
= S_OK
; break;
366 case DBTYPE_CY
: hr
= VarR8FromCy(*(CY
*)src
, d
); break;
367 case DBTYPE_DATE
: hr
= VarR8FromDate(*(DATE
*)src
, d
); break;
368 case DBTYPE_BSTR
: hr
= VarR8FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
369 case DBTYPE_BOOL
: hr
= VarR8FromBool(*(VARIANT_BOOL
*)src
, d
); break;
370 case DBTYPE_DECIMAL
: hr
= VarR8FromDec((DECIMAL
*)src
, d
); break;
373 if ((hr
= VariantChangeType(&tmp
, (VARIANT
*)src
, 0, VT_R8
)) == S_OK
)
376 default: FIXME("Unimplemented conversion %04x -> R8\n", src_type
); return E_NOTIMPL
;
385 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
386 case DBTYPE_I1
: hr
= VarBoolFromI1(*(signed char*)src
, d
); break;
387 case DBTYPE_I2
: hr
= VarBoolFromI2(*(signed short*)src
, d
); break;
388 case DBTYPE_I4
: hr
= VarBoolFromI4(*(signed int*)src
, d
); break;
389 case DBTYPE_I8
: hr
= VarBoolFromI8(*(LONGLONG
*)src
, d
); break;
390 case DBTYPE_UI1
: hr
= VarBoolFromUI1(*(BYTE
*)src
, d
); break;
391 case DBTYPE_UI2
: hr
= VarBoolFromUI2(*(WORD
*)src
, d
); break;
392 case DBTYPE_UI4
: hr
= VarBoolFromUI4(*(DWORD
*)src
, d
); break;
393 case DBTYPE_UI8
: hr
= VarBoolFromUI8(*(ULONGLONG
*)src
, d
); break;
394 case DBTYPE_R4
: hr
= VarBoolFromR4(*(FLOAT
*)src
, d
); break;
395 case DBTYPE_R8
: hr
= VarBoolFromR8(*(DOUBLE
*)src
, d
); break;
396 case DBTYPE_CY
: hr
= VarBoolFromCy(*(CY
*)src
, d
); break;
397 case DBTYPE_DATE
: hr
= VarBoolFromDate(*(DATE
*)src
, d
); break;
398 case DBTYPE_BSTR
: hr
= VarBoolFromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
399 case DBTYPE_BOOL
: *d
= *(VARIANT_BOOL
*)src
; hr
= S_OK
; break;
400 case DBTYPE_DECIMAL
: hr
= VarBoolFromDec((DECIMAL
*)src
, d
); break;
401 default: FIXME("Unimplemented conversion %04x -> BOOL\n", src_type
); return E_NOTIMPL
;
410 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
411 case DBTYPE_I1
: hr
= VarDateFromI1(*(signed char*)src
, d
); break;
412 case DBTYPE_I2
: hr
= VarDateFromI2(*(signed short*)src
, d
); break;
413 case DBTYPE_I4
: hr
= VarDateFromI4(*(signed int*)src
, d
); break;
414 case DBTYPE_I8
: hr
= VarDateFromI8(*(LONGLONG
*)src
, d
); break;
415 case DBTYPE_UI1
: hr
= VarDateFromUI1(*(BYTE
*)src
, d
); break;
416 case DBTYPE_UI2
: hr
= VarDateFromUI2(*(WORD
*)src
, d
); break;
417 case DBTYPE_UI4
: hr
= VarDateFromUI4(*(DWORD
*)src
, d
); break;
418 case DBTYPE_UI8
: hr
= VarDateFromUI8(*(ULONGLONG
*)src
, d
); break;
419 case DBTYPE_R4
: hr
= VarDateFromR4(*(FLOAT
*)src
, d
); break;
420 case DBTYPE_R8
: hr
= VarDateFromR8(*(DOUBLE
*)src
, d
); break;
421 case DBTYPE_CY
: hr
= VarDateFromCy(*(CY
*)src
, d
); break;
422 case DBTYPE_DATE
: *d
= *(DATE
*)src
; hr
= S_OK
; break;
423 case DBTYPE_BSTR
: hr
= VarDateFromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
424 case DBTYPE_BOOL
: hr
= VarDateFromBool(*(VARIANT_BOOL
*)src
, d
); break;
425 case DBTYPE_DECIMAL
: hr
= VarDateFromDec((DECIMAL
*)src
, d
); break;
426 case DBTYPE_DBTIMESTAMP
:
429 DBTIMESTAMP
*ts
=(DBTIMESTAMP
*)src
;
432 st
.wMonth
= ts
->month
;
435 st
.wMinute
= ts
->minute
;
436 st
.wSecond
= ts
->second
;
437 st
.wMilliseconds
= ts
->fraction
/1000000;
438 hr
= (SystemTimeToVariantTime(&st
, d
) ? S_OK
: E_FAIL
);
441 default: FIXME("Unimplemented conversion %04x -> DATE\n", src_type
); return E_NOTIMPL
;
450 case DBTYPE_DBDATE
: memcpy(d
, src
, sizeof(DBDATE
)); hr
= S_OK
; break;
454 BSTR s
= *(WCHAR
**)src
;
457 V_VT(&var
) = VT_BSTR
;
458 V_BSTR(&var
) = SysAllocString(s
);
460 if ((hr
= VariantChangeType(&var
, &var
, 0, VT_DATE
)) == S_OK
)
463 hr
= (VariantTimeToSystemTime( V_DATE(&var
), &st
) ? S_OK
: E_FAIL
);
465 d
->month
= st
.wMonth
;
473 if( V_VT((VARIANT
*)src
) == VT_DATE
)
476 hr
= (VariantTimeToSystemTime( V_DATE((VARIANT
*)src
), &st
) ? S_OK
: E_FAIL
);
478 d
->month
= st
.wMonth
;
483 FIXME("Unimplemented variant type %d -> DBDATE\n", V_VT((VARIANT
*)src
));
487 default: FIXME("Unimplemented conversion %04x -> DBDATE\n", src_type
); return E_NOTIMPL
;
491 case DBTYPE_DBTIMESTAMP
:
496 case DBTYPE_EMPTY
: memset(d
, 0, sizeof(DBTIMESTAMP
)); hr
= S_OK
; break;
497 case DBTYPE_DBTIMESTAMP
: memcpy(d
, src
, sizeof(DBTIMESTAMP
)); hr
= S_OK
; break;
501 BSTR s
= *(WCHAR
**)src
;
504 V_VT(&var
) = VT_BSTR
;
505 V_BSTR(&var
) = SysAllocString(s
);
507 if ((hr
= VariantChangeType(&var
, &var
, 0, VT_DATE
)) == S_OK
)
511 hr
= (VariantTimeToSystemTime( V_DATE(&var
), &st
) ? S_OK
: E_FAIL
);
513 d
->month
= st
.wMonth
;
516 d
->minute
= st
.wMinute
;
517 d
->second
= st
.wSecond
;
518 d
->fraction
= st
.wMilliseconds
* 1000000;
527 hr
= (VariantTimeToSystemTime(*(double*)src
, &st
) ? S_OK
: E_FAIL
);
529 d
->month
= st
.wMonth
;
532 d
->minute
= st
.wMinute
;
533 d
->second
= st
.wSecond
;
534 d
->fraction
= st
.wMilliseconds
* 1000000;
538 if( V_VT((VARIANT
*)src
) == VT_DATE
)
541 hr
= (VariantTimeToSystemTime( V_DATE((VARIANT
*)src
), &st
) ? S_OK
: E_FAIL
);
543 d
->month
= st
.wMonth
;
546 d
->minute
= st
.wMinute
;
547 d
->second
= st
.wSecond
;
548 d
->fraction
= st
.wMilliseconds
* 1000000;
552 FIXME("Unimplemented variant type %d -> DBTIMESTAMP\n", V_VT((VARIANT
*)src
));
556 default: FIXME("Unimplemented conversion %04x -> DBTIMESTAMP\n", src_type
); return E_NOTIMPL
;
566 case DBTYPE_EMPTY
: d
->int64
= 0; hr
= S_OK
; break;
567 case DBTYPE_I2
: hr
= VarCyFromI2(*(signed short*)src
, d
); break;
568 case DBTYPE_I4
: hr
= VarCyFromI4(*(signed int*)src
, d
); break;
569 case DBTYPE_R4
: hr
= VarCyFromR4(*(FLOAT
*)src
, d
); break;
570 case DBTYPE_R8
: hr
= VarCyFromR8(*(double*)src
, d
); break;
571 case DBTYPE_CY
: *d
= *(CY
*)src
; hr
= S_OK
; break;
572 case DBTYPE_DATE
: hr
= VarCyFromDate(*(DATE
*)src
, d
); break;
573 case DBTYPE_BSTR
: hr
= VarCyFromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
574 case DBTYPE_BOOL
: hr
= VarCyFromBool(*(VARIANT_BOOL
*)src
, d
); break;
575 case DBTYPE_DECIMAL
: hr
= VarCyFromDec((DECIMAL
*)src
, d
); break;
576 case DBTYPE_I1
: hr
= VarCyFromI1(*(signed char*)src
, d
); break;
577 case DBTYPE_UI1
: hr
= VarCyFromUI1(*(BYTE
*)src
, d
); break;
578 case DBTYPE_UI2
: hr
= VarCyFromUI2(*(WORD
*)src
, d
); break;
579 case DBTYPE_UI4
: hr
= VarCyFromUI4(*(DWORD
*)src
, d
); break;
580 case DBTYPE_I8
: hr
= VarCyFromI8(*(LONGLONG
*)src
, d
); break;
581 case DBTYPE_UI8
: hr
= VarCyFromUI8(*(ULONGLONG
*)src
, d
); break;
582 default: FIXME("Unimplemented conversion %04x -> CY\n", src_type
); return E_NOTIMPL
;
592 case DBTYPE_EMPTY
: *d
= SysAllocStringLen(NULL
, 0); hr
= *d
? S_OK
: E_OUTOFMEMORY
; break;
593 case DBTYPE_I2
: hr
= VarBstrFromI2(*(signed short*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
594 case DBTYPE_I4
: hr
= VarBstrFromI4(*(signed int*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
595 case DBTYPE_R4
: hr
= VarBstrFromR4(*(FLOAT
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
596 case DBTYPE_R8
: hr
= VarBstrFromR8(*(double*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
597 case DBTYPE_CY
: hr
= VarBstrFromCy(*(CY
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
598 case DBTYPE_DATE
: hr
= VarBstrFromDate(*(DATE
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
599 case DBTYPE_BSTR
: *d
= SysAllocStringLen(*(BSTR
*)src
, SysStringLen(*(BSTR
*)src
)); hr
= *d
? S_OK
: E_OUTOFMEMORY
; break;
600 case DBTYPE_BOOL
: hr
= VarBstrFromBool(*(VARIANT_BOOL
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
601 case DBTYPE_DECIMAL
: hr
= VarBstrFromDec((DECIMAL
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
602 case DBTYPE_I1
: hr
= VarBstrFromI1(*(signed char*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
603 case DBTYPE_UI1
: hr
= VarBstrFromUI1(*(BYTE
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
604 case DBTYPE_UI2
: hr
= VarBstrFromUI2(*(WORD
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
605 case DBTYPE_UI4
: hr
= VarBstrFromUI4(*(DWORD
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
606 case DBTYPE_I8
: hr
= VarBstrFromI8(*(LONGLONG
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
607 case DBTYPE_UI8
: hr
= VarBstrFromUI8(*(ULONGLONG
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
611 const GUID
*id
= (const GUID
*)src
;
613 '{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
614 '%','0','2','X','%','0','2','X','-',
615 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
616 wsprintfW(szBuff
, format
,
617 id
->Data1
, id
->Data2
, id
->Data3
,
618 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
619 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
620 *d
= SysAllocString(szBuff
);
621 hr
= *d
? S_OK
: E_OUTOFMEMORY
;
626 *d
= SysAllocStringLen(NULL
, 2 * src_len
);
631 const char hexchars
[] = "0123456789ABCDEF";
633 unsigned char *p
= src
;
636 *s
++ = hexchars
[(*p
>> 4) & 0x0F];
637 *s
++ = hexchars
[(*p
) & 0x0F];
644 case DBTYPE_DBTIMESTAMP
:
647 static const WCHAR format1
[] = {
648 '%','0','4','d','-','%','0','2','d','-','%','0','2','d',' ','%','0','2','d',':','%','0','2','d',
649 ':','%','0','2','d', 0};
650 static const WCHAR format2
[] = {
651 '%','0','4','d','-','%','0','2','d','-','%','0','2','d',' ','%','0','2','d',':','%','0','2','d',
652 ':','%','0','2','d','.','%','0','9','d', 0};
653 DBTIMESTAMP
*ts
= (DBTIMESTAMP
*)src
;
655 if(ts
->fraction
== 0)
656 wsprintfW(szBuff
, format1
, ts
->year
, ts
->month
, ts
->day
, ts
->hour
, ts
->minute
, ts
->second
);
658 wsprintfW(szBuff
, format2
, ts
->year
, ts
->month
, ts
->day
, ts
->hour
, ts
->minute
, ts
->second
, ts
->fraction
);
659 *d
= SysAllocString(szBuff
);
660 hr
= *d
? S_OK
: E_OUTOFMEMORY
;
665 if ((hr
= VariantChangeType(&tmp
, (VARIANT
*)src
, 0, VT_BSTR
)) == S_OK
)
668 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type
); return E_NOTIMPL
;
678 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
679 case DBTYPE_I2
: hr
= VarUI1FromI2(*(signed short*)src
, d
); break;
680 case DBTYPE_I4
: hr
= VarUI1FromI4(*(signed int*)src
, d
); break;
681 case DBTYPE_R4
: hr
= VarUI1FromR4(*(FLOAT
*)src
, d
); break;
682 case DBTYPE_R8
: hr
= VarUI1FromR8(*(double*)src
, d
); break;
683 case DBTYPE_CY
: hr
= VarUI1FromCy(*(CY
*)src
, d
); break;
684 case DBTYPE_DATE
: hr
= VarUI1FromDate(*(DATE
*)src
, d
); break;
685 case DBTYPE_BSTR
: hr
= VarUI1FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
686 case DBTYPE_BOOL
: hr
= VarUI1FromBool(*(VARIANT_BOOL
*)src
, d
); break;
687 case DBTYPE_DECIMAL
: hr
= VarUI1FromDec((DECIMAL
*)src
, d
); break;
688 case DBTYPE_I1
: hr
= VarUI1FromI1(*(signed char*)src
, d
); break;
689 case DBTYPE_UI1
: *d
= *(BYTE
*)src
; hr
= S_OK
; break;
690 case DBTYPE_UI2
: hr
= VarUI1FromUI2(*(WORD
*)src
, d
); break;
691 case DBTYPE_UI4
: hr
= VarUI1FromUI4(*(DWORD
*)src
, d
); break;
692 case DBTYPE_I8
: hr
= VarUI1FromI8(*(LONGLONG
*)src
, d
); break;
693 case DBTYPE_UI8
: hr
= VarUI1FromUI8(*(ULONGLONG
*)src
, d
); break;
694 default: FIXME("Unimplemented conversion %04x -> UI1\n", src_type
); return E_NOTIMPL
;
703 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
704 case DBTYPE_I2
: hr
= VarUI2FromI2(*(signed short*)src
, d
); break;
705 case DBTYPE_I4
: hr
= VarUI2FromI4(*(signed int*)src
, d
); break;
706 case DBTYPE_R4
: hr
= VarUI2FromR4(*(FLOAT
*)src
, d
); break;
707 case DBTYPE_R8
: hr
= VarUI2FromR8(*(double*)src
, d
); break;
708 case DBTYPE_CY
: hr
= VarUI2FromCy(*(CY
*)src
, d
); break;
709 case DBTYPE_DATE
: hr
= VarUI2FromDate(*(DATE
*)src
, d
); break;
710 case DBTYPE_BSTR
: hr
= VarUI2FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
711 case DBTYPE_BOOL
: hr
= VarUI2FromBool(*(VARIANT_BOOL
*)src
, d
); break;
712 case DBTYPE_DECIMAL
: hr
= VarUI2FromDec((DECIMAL
*)src
, d
); break;
713 case DBTYPE_I1
: hr
= VarUI2FromI1(*(signed char*)src
, d
); break;
714 case DBTYPE_UI1
: hr
= VarUI2FromUI1(*(BYTE
*)src
, d
); break;
715 case DBTYPE_UI2
: *d
= *(WORD
*)src
; hr
= S_OK
; break;
716 case DBTYPE_UI4
: hr
= VarUI2FromUI4(*(DWORD
*)src
, d
); break;
717 case DBTYPE_I8
: hr
= VarUI2FromI8(*(LONGLONG
*)src
, d
); break;
718 case DBTYPE_UI8
: hr
= VarUI2FromUI8(*(ULONGLONG
*)src
, d
); break;
719 default: FIXME("Unimplemented conversion %04x -> UI2\n", src_type
); return E_NOTIMPL
;
729 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
730 case DBTYPE_I2
: hr
= VarUI4FromI2(*(signed short*)src
, d
); break;
731 case DBTYPE_I4
: hr
= VarUI4FromI4(*(signed int*)src
, d
); break;
732 case DBTYPE_R4
: hr
= VarUI4FromR4(*(FLOAT
*)src
, d
); break;
733 case DBTYPE_R8
: hr
= VarUI4FromR8(*(double*)src
, d
); break;
734 case DBTYPE_CY
: hr
= VarUI4FromCy(*(CY
*)src
, d
); break;
735 case DBTYPE_DATE
: hr
= VarUI4FromDate(*(DATE
*)src
, d
); break;
736 case DBTYPE_BSTR
: hr
= VarUI4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
737 case DBTYPE_BOOL
: hr
= VarUI4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
738 case DBTYPE_DECIMAL
: hr
= VarUI4FromDec((DECIMAL
*)src
, d
); break;
739 case DBTYPE_I1
: hr
= VarUI4FromI1(*(signed char*)src
, d
); break;
740 case DBTYPE_UI1
: hr
= VarUI4FromUI1(*(BYTE
*)src
, d
); break;
741 case DBTYPE_UI2
: hr
= VarUI4FromUI2(*(WORD
*)src
, d
); break;
742 case DBTYPE_UI4
: *d
= *(DWORD
*)src
; hr
= S_OK
; break;
743 case DBTYPE_I8
: hr
= VarUI4FromI8(*(LONGLONG
*)src
, d
); break;
744 case DBTYPE_UI8
: hr
= VarUI4FromUI8(*(ULONGLONG
*)src
, d
); break;
747 if ((hr
= VariantChangeType(&tmp
, (VARIANT
*)src
, 0, VT_UI4
)) == S_OK
)
750 default: FIXME("Unimplemented conversion %04x -> UI4\n", src_type
); return E_NOTIMPL
;
760 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
761 case DBTYPE_I2
: hr
= VarUI8FromI2(*(signed short*)src
, d
); break;
762 case DBTYPE_I4
: {LONGLONG s
= *(signed int*)src
; hr
= VarUI8FromI8(s
, d
); break;}
763 case DBTYPE_R4
: hr
= VarUI8FromR4(*(FLOAT
*)src
, d
); break;
764 case DBTYPE_R8
: hr
= VarUI8FromR8(*(double*)src
, d
); break;
765 case DBTYPE_CY
: hr
= VarUI8FromCy(*(CY
*)src
, d
); break;
766 case DBTYPE_DATE
: hr
= VarUI8FromDate(*(DATE
*)src
, d
); break;
767 case DBTYPE_BSTR
: hr
= VarUI8FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
768 case DBTYPE_BOOL
: hr
= VarUI8FromBool(*(VARIANT_BOOL
*)src
, d
); break;
769 case DBTYPE_DECIMAL
: hr
= VarUI8FromDec((DECIMAL
*)src
, d
); break;
770 case DBTYPE_I1
: hr
= VarUI8FromI1(*(signed char*)src
, d
); break;
771 case DBTYPE_UI1
: hr
= VarUI8FromUI1(*(BYTE
*)src
, d
); break;
772 case DBTYPE_UI2
: hr
= VarUI8FromUI2(*(WORD
*)src
, d
); break;
773 case DBTYPE_UI4
: hr
= VarUI8FromUI4(*(DWORD
*)src
, d
); break;
774 case DBTYPE_I8
: hr
= VarUI8FromI8(*(LONGLONG
*)src
, d
); break;
775 case DBTYPE_UI8
: *d
= *(ULONGLONG
*)src
; hr
= S_OK
; break;
776 default: FIXME("Unimplemented conversion %04x -> UI8\n", src_type
); return E_NOTIMPL
;
781 case DBTYPE_FILETIME
:
786 case DBTYPE_EMPTY
: d
->dwLowDateTime
= d
->dwHighDateTime
= 0; hr
= S_OK
; break;
787 case DBTYPE_FILETIME
: *d
= *(FILETIME
*)src
; hr
= S_OK
; break;
788 default: FIXME("Unimplemented conversion %04x -> FILETIME\n", src_type
); return E_NOTIMPL
;
798 case DBTYPE_EMPTY
: *d
= GUID_NULL
; hr
= S_OK
; break;
799 case DBTYPE_GUID
: *d
= *(GUID
*)src
; hr
= S_OK
; break;
800 default: FIXME("Unimplemented conversion %04x -> GUID\n", src_type
); return E_NOTIMPL
;
810 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &bstr_len
,
811 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
812 precision
, scale
, flags
);
813 if(hr
!= S_OK
|| *dst_status
== DBSTATUS_S_ISNULL
)
815 bstr_len
= SysStringLen(b
);
816 *dst_len
= bstr_len
* sizeof(WCHAR
); /* Doesn't include size for '\0' */
817 *dst_status
= DBSTATUS_S_OK
;
818 bytes_to_copy
= min(*dst_len
+ sizeof(WCHAR
), dst_max_len
);
821 if(bytes_to_copy
>= sizeof(WCHAR
))
823 memcpy(dst
, b
, bytes_to_copy
- sizeof(WCHAR
));
824 *((WCHAR
*)dst
+ bytes_to_copy
/ sizeof(WCHAR
) - 1) = 0;
825 if(bytes_to_copy
< *dst_len
+ sizeof(WCHAR
))
826 *dst_status
= DBSTATUS_S_TRUNCATED
;
830 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
831 hr
= DB_E_ERRORSOCCURRED
;
843 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &length
,
844 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
845 precision
, scale
, flags
);
846 if(hr
!= S_OK
) return hr
;
847 length
= WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
), NULL
, 0, NULL
, NULL
);
848 *dst_len
= length
; /* Doesn't include size for '\0' */
849 *dst_status
= DBSTATUS_S_OK
;
850 bytes_to_copy
= min(length
+ 1, dst_max_len
);
853 if(bytes_to_copy
>= sizeof(char))
855 WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
), dst
, bytes_to_copy
- 1, NULL
, NULL
);
856 *((char *)dst
+ bytes_to_copy
- 1) = 0;
857 if(bytes_to_copy
< length
+ 1)
858 *dst_status
= DBSTATUS_S_TRUNCATED
;
862 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
863 hr
= DB_E_ERRORSOCCURRED
;
870 case DBTYPE_BYREF
| DBTYPE_WSTR
:
875 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &bstr_len
,
876 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
877 precision
, scale
, flags
);
878 if(hr
!= S_OK
) return hr
;
880 bstr_len
= SysStringLen(b
) * sizeof(WCHAR
);
881 *dst_len
= bstr_len
; /* Doesn't include size for '\0' */
883 *d
= CoTaskMemAlloc(bstr_len
+ sizeof(WCHAR
));
884 if(*d
) memcpy(*d
, b
, bstr_len
+ sizeof(WCHAR
));
885 else hr
= E_OUTOFMEMORY
;
890 case DBTYPE_BYREF
| DBTYPE_STR
:
895 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &length
,
896 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
897 precision
, scale
, flags
);
898 if(hr
!= S_OK
) return hr
;
900 length
= WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
) + 1, NULL
, 0, NULL
, NULL
);
901 *dst_len
= length
- 1; /* Doesn't include size for '\0' */
902 *dst_status
= DBSTATUS_S_OK
;
903 *d
= CoTaskMemAlloc(length
);
906 WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
) + 1, *d
, length
, NULL
, NULL
);
922 V_BOOL(v
) = *(VARIANT_BOOL
*)src
;
927 V_I2(v
) = *(signed short*)src
;
932 V_I4(v
) = *(signed int*)src
;
936 V_VT(v
) = VT_DECIMAL
;
937 hr
= VarDecFromI8( *(LONGLONG
*)src
, &V_DECIMAL(v
));
941 V_R4(v
) = *(FLOAT
*)src
;
946 V_R8(v
) = *(double*)src
;
951 BSTR s
= *(WCHAR
**)src
;
952 TRACE("%s\n", debugstr_w(s
));
954 V_BSTR(v
) = SysAllocString(s
);
955 hr
= V_BSTR(v
) ? S_OK
: E_OUTOFMEMORY
;
960 V_DATE(v
) = *(DATE
*)src
;
966 DBDATE
*ts
=(DBDATE
*)src
;
971 st
.wMonth
= ts
->month
;
976 st
.wMilliseconds
= 0;
977 hr
= (SystemTimeToVariantTime(&st
, &V_DATE(v
)) ? S_OK
: E_FAIL
);
980 case DBTYPE_DBTIMESTAMP
:
983 DBTIMESTAMP
*ts
= (DBTIMESTAMP
*)src
;
988 st
.wMonth
= ts
->month
;
991 st
.wMinute
= ts
->minute
;
992 st
.wSecond
= ts
->second
;
993 st
.wMilliseconds
= ts
->fraction
/1000000;
994 hr
= SystemTimeToVariantTime(&st
, &V_DATE(v
)) ? S_OK
: E_FAIL
;
1005 SAFEARRAY
*psa
= NULL
;
1006 SAFEARRAYBOUND rgsabound
[1];
1007 unsigned char *p
= src
;
1009 rgsabound
[0].lLbound
= 0;
1010 rgsabound
[0].cElements
= src_len
;
1012 psa
= SafeArrayCreate(VT_UI1
,1,rgsabound
);
1013 for(i
=0; i
< src_len
; i
++,p
++)
1015 hr
= SafeArrayPutElement(psa
, &i
, p
);
1017 SafeArrayDestroy (psa
);
1022 V_VT(v
) = VT_ARRAY
|VT_UI1
;
1027 default: FIXME("Unimplemented conversion %04x -> VARIANT\n", src_type
); return E_NOTIMPL
;
1038 if( src_len
> dst_max_len
)
1039 *dst_status
= DBSTATUS_S_TRUNCATED
;
1041 *dst_status
= DBSTATUS_S_OK
;
1044 memcpy(d
, src
, min(src_len
, dst_max_len
));
1047 case DBTYPE_VARIANT
:
1049 switch(V_VT((VARIANT
*)src
))
1051 case VT_UI1
| VT_ARRAY
:
1056 hr
= SafeArrayGetUBound(V_ARRAY((VARIANT
*)src
), 1, &l
);
1060 hr
= SafeArrayAccessData(V_ARRAY((VARIANT
*)src
), (VOID
**)&data
);
1063 ERR("SafeArrayAccessData Failed = 0x%08x\n", hr
);
1068 *dst_status
= DBSTATUS_S_OK
;
1069 memcpy(d
, data
, *dst_len
);
1071 SafeArrayUnaccessData(V_ARRAY((VARIANT
*)src
));
1076 FIXME("Unimplemented variant type %d -> BYTES\n", V_VT((VARIANT
*)src
));
1081 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES\n", src_type
); return E_NOTIMPL
;
1085 case DBTYPE_BYTES
| DBTYPE_BYREF
:
1092 *d
= CoTaskMemAlloc(src_len
);
1093 if(*d
) memcpy(*d
, src
, src_len
);
1094 else hr
= E_OUTOFMEMORY
;
1097 *dst_status
= DBSTATUS_S_OK
;
1099 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES | DBTYPE_BYREF\n", src_type
); return E_NOTIMPL
;
1104 case DBTYPE_NUMERIC
:
1105 FIXME("Unimplemented conversion %04x -> DBTYPE_NUMERIC\n", src_type
);
1109 FIXME("Unimplemented conversion %04x -> %04x\n", src_type
, dst_type
);
1113 if(hr
== DISP_E_OVERFLOW
)
1115 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
1116 *dst_len
= get_length(dst_type
);
1117 hr
= DB_E_ERRORSOCCURRED
;
1121 *dst_status
= DBSTATUS_S_OK
;
1122 *dst_len
= get_length(dst_type
);
1128 static inline WORD
get_dbtype_class(DBTYPE type
)
1155 case DBTYPE_DBTIMESTAMP
:
1156 return DBTYPE_DBDATE
;
1161 /* Many src types will convert to this group of dst types */
1162 static inline BOOL
common_class(WORD dst_class
)
1172 case DBTYPE_VARIANT
:
1175 case DBTYPE_DECIMAL
:
1176 case DBTYPE_NUMERIC
:
1182 static inline BOOL
array_type(DBTYPE type
)
1184 return (type
>= DBTYPE_I2
&& type
<= DBTYPE_UI4
);
1187 static HRESULT WINAPI
convert_CanConvert(IDataConvert
* iface
,
1188 DBTYPE src_type
, DBTYPE dst_type
)
1190 convert
*This
= impl_from_IDataConvert(iface
);
1191 DBTYPE src_base_type
= src_type
& 0x1ff;
1192 DBTYPE dst_base_type
= dst_type
& 0x1ff;
1193 WORD dst_class
= get_dbtype_class(dst_base_type
);
1195 TRACE("(%p)->(%d, %d)\n", This
, src_type
, dst_type
);
1197 if(src_type
& DBTYPE_VECTOR
|| dst_type
& DBTYPE_VECTOR
) return S_FALSE
;
1199 if(src_type
& DBTYPE_ARRAY
)
1201 if(!array_type(src_base_type
)) return S_FALSE
;
1202 if(dst_type
& DBTYPE_ARRAY
)
1204 if(src_type
== dst_type
) return S_OK
;
1207 if(dst_type
== DBTYPE_VARIANT
) return S_OK
;
1211 if(dst_type
& DBTYPE_ARRAY
)
1213 if(!array_type(dst_base_type
)) return S_FALSE
;
1214 if(src_type
== DBTYPE_IDISPATCH
|| src_type
== DBTYPE_VARIANT
) return S_OK
;
1218 if(dst_type
& DBTYPE_BYREF
)
1219 if(dst_base_type
!= DBTYPE_BYTES
&& dst_base_type
!= DBTYPE_STR
&& dst_base_type
!= DBTYPE_WSTR
)
1222 switch(get_dbtype_class(src_base_type
))
1225 if(common_class(dst_class
)) return S_OK
;
1230 case DBTYPE_FILETIME
:
1233 if(dst_base_type
== DBTYPE_DBTIMESTAMP
) return S_OK
;
1238 switch(dst_base_type
)
1241 case DBTYPE_VARIANT
:
1242 case DBTYPE_FILETIME
: return S_OK
;
1243 default: return S_FALSE
;
1247 if(dst_base_type
== DBTYPE_BYTES
) return S_OK
;
1250 if(dst_base_type
== DBTYPE_DATE
) return S_OK
;
1252 case DBTYPE_DECIMAL
:
1253 if(common_class(dst_class
)) return S_OK
;
1254 if(dst_class
== DBTYPE_DBDATE
) return S_OK
;
1258 if(dst_base_type
== DBTYPE_DATE
) return S_OK
;
1260 case DBTYPE_NUMERIC
:
1262 if(common_class(dst_class
)) return S_OK
;
1266 if(common_class(dst_class
)) return S_OK
;
1267 switch(dst_base_type
)
1270 case DBTYPE_FILETIME
: return S_OK
;
1271 default: return S_FALSE
;
1283 case DBTYPE_VARIANT
:
1287 case DBTYPE_FILETIME
:
1289 default: return S_FALSE
;
1292 case DBTYPE_IDISPATCH
:
1293 case DBTYPE_VARIANT
:
1294 switch(dst_base_type
)
1296 case DBTYPE_IDISPATCH
:
1298 case DBTYPE_IUNKNOWN
:
1303 if(common_class(dst_class
)) return S_OK
;
1310 case DBTYPE_FILETIME
:
1312 default: return S_FALSE
;
1316 switch(dst_base_type
)
1320 case DBTYPE_VARIANT
:
1323 default: return S_FALSE
;
1326 case DBTYPE_IUNKNOWN
:
1327 switch(dst_base_type
)
1331 case DBTYPE_IDISPATCH
:
1332 case DBTYPE_VARIANT
:
1333 case DBTYPE_IUNKNOWN
:
1335 default: return S_FALSE
;
1339 if(dst_class
== DBTYPE_I4
|| dst_class
== DBTYPE_I8
) return S_OK
;
1347 case DBTYPE_VARIANT
:
1351 default: return S_FALSE
;
1354 case DBTYPE_FILETIME
:
1355 if(dst_class
== DBTYPE_I8
) return S_OK
;
1364 case DBTYPE_VARIANT
:
1366 case DBTYPE_FILETIME
:
1368 default: return S_FALSE
;
1375 static HRESULT WINAPI
convert_GetConversionSize(IDataConvert
* iface
,
1376 DBTYPE src_type
, DBTYPE dst_type
,
1377 DBLENGTH
*src_len
, DBLENGTH
*dst_len
,
1380 convert
*This
= impl_from_IDataConvert(iface
);
1383 TRACE("(%p)->(%d, %d, %p, %p, %p)\n", This
, src_type
, dst_type
, src_len
, dst_len
, src
);
1385 hr
= IDataConvert_CanConvert(iface
, src_type
, dst_type
);
1387 return DB_E_UNSUPPORTEDCONVERSION
;
1390 return E_INVALIDARG
;
1392 /* for some types we don't need to look into source data */
1393 if ((*dst_len
= get_length(dst_type
)))
1398 if(src_type
== DBTYPE_VARIANT
&& V_VT((VARIANT
*)src
) == VT_NULL
)
1406 case DBTYPE_VARIANT
:
1411 if ((hr
= VariantChangeType(&v
, (VARIANT
*)src
, 0, VT_BSTR
)) == S_OK
)
1413 *dst_len
= WideCharToMultiByte(CP_ACP
, 0, V_BSTR(&v
), -1, NULL
, 0, NULL
, NULL
);
1421 case DBTYPE_DECIMAL
:
1432 case DBTYPE_DBTIMESTAMP
:
1436 FIXME("unimplemented for %04x -> DBTYPE_STR\n", src_type
);
1443 case DBTYPE_VARIANT
:
1448 if ((hr
= VariantChangeType(&v
, (VARIANT
*)src
, 0, VT_BSTR
)) == S_OK
)
1450 *dst_len
= (SysStringLen(V_BSTR(&v
))+1) * sizeof(WCHAR
);
1457 *dst_len
= (*src_len
+ 1) * sizeof(WCHAR
);
1459 *dst_len
= (strlen(src
) + 1) * sizeof(WCHAR
);
1463 *dst_len
= (*src_len
) + sizeof(WCHAR
);
1465 *dst_len
= (lstrlenW(src
) + 1) * sizeof(WCHAR
);
1468 case DBTYPE_DECIMAL
:
1479 case DBTYPE_DBTIMESTAMP
:
1483 FIXME("unimplemented for %04x -> DBTYPE_WSTR\n", src_type
);
1490 case DBTYPE_VARIANT
:
1491 if(V_VT((VARIANT
*)src
) == VT_BSTR
)
1492 *dst_len
= (SysStringLen(V_BSTR((VARIANT
*)src
))) / sizeof(WCHAR
);
1495 switch(V_VT((VARIANT
*)src
))
1497 case VT_UI1
| VT_ARRAY
:
1501 hr
= SafeArrayGetUBound(V_ARRAY((VARIANT
*)src
), 1, &l
);
1509 WARN("DBTYPE_VARIANT(%d)->DBTYPE_BYTES unimplemented\n", V_VT((VARIANT
*)src
));
1514 FIXME("unimplemented for %04x -> DBTYPE_BYTES\n", src_type
);
1519 FIXME("unimplemented for conversion %d->%d\n", src_type
, dst_type
);
1526 static const struct IDataConvertVtbl convert_vtbl
=
1528 convert_QueryInterface
,
1531 convert_DataConvert
,
1533 convert_GetConversionSize
1536 static HRESULT WINAPI
dcinfo_QueryInterface(IDCInfo
* iface
, REFIID riid
, void **obj
)
1538 convert
*This
= impl_from_IDCInfo(iface
);
1540 return IDataConvert_QueryInterface(&This
->IDataConvert_iface
, riid
, obj
);
1543 static ULONG WINAPI
dcinfo_AddRef(IDCInfo
* iface
)
1545 convert
*This
= impl_from_IDCInfo(iface
);
1547 return IDataConvert_AddRef(&This
->IDataConvert_iface
);
1550 static ULONG WINAPI
dcinfo_Release(IDCInfo
* iface
)
1552 convert
*This
= impl_from_IDCInfo(iface
);
1554 return IDataConvert_Release(&This
->IDataConvert_iface
);
1557 static HRESULT WINAPI
dcinfo_GetInfo(IDCInfo
*iface
, ULONG num
, DCINFOTYPE types
[], DCINFO
**info_ptr
)
1559 convert
*This
= impl_from_IDCInfo(iface
);
1563 TRACE("(%p)->(%d, %p, %p)\n", This
, num
, types
, info_ptr
);
1565 *info_ptr
= infos
= CoTaskMemAlloc(num
* sizeof(*infos
));
1566 if(!infos
) return E_OUTOFMEMORY
;
1568 for(i
= 0; i
< num
; i
++)
1570 infos
[i
].eInfoType
= types
[i
];
1571 VariantInit(&infos
[i
].vData
);
1575 case DCINFOTYPE_VERSION
:
1576 V_VT(&infos
[i
].vData
) = VT_UI4
;
1577 V_UI4(&infos
[i
].vData
) = This
->version
;
1585 static HRESULT WINAPI
dcinfo_SetInfo(IDCInfo
* iface
, ULONG num
, DCINFO info
[])
1587 convert
*This
= impl_from_IDCInfo(iface
);
1591 TRACE("(%p)->(%d, %p)\n", This
, num
, info
);
1593 for(i
= 0; i
< num
; i
++)
1595 switch(info
[i
].eInfoType
)
1597 case DCINFOTYPE_VERSION
:
1598 if(V_VT(&info
[i
].vData
) != VT_UI4
)
1600 FIXME("VERSION with vt %x\n", V_VT(&info
[i
].vData
));
1601 hr
= DB_S_ERRORSOCCURRED
;
1604 This
->version
= V_UI4(&info
[i
].vData
);
1608 FIXME("Unhandled info type %d (vt %x)\n", info
[i
].eInfoType
, V_VT(&info
[i
].vData
));
1614 static const struct IDCInfoVtbl dcinfo_vtbl
=
1616 dcinfo_QueryInterface
,
1623 HRESULT
create_oledb_convert(IUnknown
*outer
, void **obj
)
1627 TRACE("(%p, %p)\n", outer
, obj
);
1631 if(outer
) return CLASS_E_NOAGGREGATION
;
1633 This
= heap_alloc(sizeof(*This
));
1634 if(!This
) return E_OUTOFMEMORY
;
1636 This
->IDataConvert_iface
.lpVtbl
= &convert_vtbl
;
1637 This
->IDCInfo_iface
.lpVtbl
= &dcinfo_vtbl
;
1639 This
->version
= 0x110;
1641 *obj
= &This
->IDataConvert_iface
;