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"
34 #include "wine/heap.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb
);
40 IDataConvert IDataConvert_iface
;
41 IDCInfo IDCInfo_iface
;
45 UINT version
; /* Set by IDCInfo_SetInfo */
48 static inline convert
*impl_from_IDataConvert(IDataConvert
*iface
)
50 return CONTAINING_RECORD(iface
, convert
, IDataConvert_iface
);
53 static inline convert
*impl_from_IDCInfo(IDCInfo
*iface
)
55 return CONTAINING_RECORD(iface
, convert
, IDCInfo_iface
);
58 static HRESULT WINAPI
convert_QueryInterface(IDataConvert
* iface
,
62 convert
*This
= impl_from_IDataConvert(iface
);
63 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), obj
);
67 if(IsEqualIID(riid
, &IID_IUnknown
) ||
68 IsEqualIID(riid
, &IID_IDataConvert
))
72 else if(IsEqualIID(riid
, &IID_IDCInfo
))
74 *obj
= &This
->IDCInfo_iface
;
78 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
82 IDataConvert_AddRef(iface
);
87 static ULONG WINAPI
convert_AddRef(IDataConvert
* iface
)
89 convert
*This
= impl_from_IDataConvert(iface
);
90 TRACE("(%p)\n", This
);
92 return InterlockedIncrement(&This
->ref
);
96 static ULONG WINAPI
convert_Release(IDataConvert
* iface
)
98 convert
*This
= impl_from_IDataConvert(iface
);
101 TRACE("(%p)\n", This
);
103 ref
= InterlockedDecrement(&This
->ref
);
110 static int get_length(DBTYPE type
)
121 return sizeof(VARIANT_BOOL
);
132 return sizeof(DBDATE
);
133 case DBTYPE_DBTIMESTAMP
:
134 return sizeof(DBTIMESTAMP
);
139 case DBTYPE_FILETIME
:
140 return sizeof(FILETIME
);
144 return sizeof(DB_NUMERIC
);
148 case DBTYPE_BYREF
| DBTYPE_WSTR
:
151 return sizeof(VARIANT
);
153 FIXME("Unhandled type %04x\n", type
);
158 static HRESULT WINAPI
convert_DataConvert(IDataConvert
* iface
,
159 DBTYPE src_type
, DBTYPE dst_type
,
160 DBLENGTH src_len
, DBLENGTH
*dst_len
,
161 void *src
, void *dst
,
162 DBLENGTH dst_max_len
,
163 DBSTATUS src_status
, DBSTATUS
*dst_status
,
164 BYTE precision
, BYTE scale
,
167 convert
*This
= impl_from_IDataConvert(iface
);
168 DBLENGTH dst_len_loc
;
169 DBSTATUS dst_status_loc
;
173 TRACE("(%p)->(%d, %d, %ld, %p, %p, %p, %ld, %d, %p, %d, %d, %x)\n", This
,
174 src_type
, dst_type
, src_len
, dst_len
, src
, dst
, dst_max_len
,
175 src_status
, dst_status
, precision
, scale
, flags
);
177 if (!dst_len
) dst_len
= &dst_len_loc
;
178 if (!dst_status
) dst_status
= &dst_status_loc
;
180 *dst_status
= DBSTATUS_E_BADACCESSOR
;
182 if(src_status
== DBSTATUS_S_ISNULL
)
184 *dst_status
= DBSTATUS_S_ISNULL
;
189 if(src_type
== DBTYPE_VARIANT
&& V_VT((VARIANT
*)src
) == VT_NULL
)
191 if(dst_type
== DBTYPE_VARIANT
)
192 *dst_len
= sizeof(VARIANT
);
194 *dst_status
= DBSTATUS_S_ISNULL
;
198 if(IDataConvert_CanConvert(iface
, src_type
, dst_type
) != S_OK
)
200 return DB_E_UNSUPPORTEDCONVERSION
;
203 if(dst
== NULL
&& get_length(dst_type
) != 0)
205 *dst_len
= get_length(src_type
);
206 *dst_status
= DBSTATUS_S_OK
;
210 if(src_type
== DBTYPE_STR
)
215 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
216 len
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, NULL
, 0) - 1;
218 len
= MultiByteToWideChar(CP_ACP
, 0, src
, src_len
, NULL
, 0);
219 b
= SysAllocStringLen(NULL
, len
);
220 if(!b
) return E_OUTOFMEMORY
;
221 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
222 MultiByteToWideChar(CP_ACP
, 0, src
, -1, b
, len
+ 1);
224 MultiByteToWideChar(CP_ACP
, 0, src
, src_len
, b
, len
);
226 hr
= IDataConvert_DataConvert(iface
, DBTYPE_BSTR
, dst_type
, 0, dst_len
,
227 &b
, dst
, dst_max_len
, src_status
, dst_status
,
228 precision
, scale
, flags
);
234 if(src_type
== DBTYPE_WSTR
)
238 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
239 b
= SysAllocString(src
);
241 b
= SysAllocStringLen(src
, src_len
/ 2);
242 if(!b
) return E_OUTOFMEMORY
;
243 hr
= IDataConvert_DataConvert(iface
, DBTYPE_BSTR
, dst_type
, 0, dst_len
,
244 &b
, dst
, dst_max_len
, src_status
, dst_status
,
245 precision
, scale
, flags
);
254 signed char *d
= dst
;
257 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
258 case DBTYPE_I1
: *d
= *(signed char*)src
; hr
= S_OK
; break;
259 case DBTYPE_I2
: hr
= VarI1FromI2(*(signed short*)src
, d
); break;
260 case DBTYPE_I4
: hr
= VarI1FromI4(*(signed int*)src
, d
); break;
261 case DBTYPE_R4
: hr
= VarI1FromR4(*(FLOAT
*)src
, d
); break;
262 case DBTYPE_R8
: hr
= VarI1FromR8(*(double*)src
, d
); break;
263 case DBTYPE_CY
: hr
= VarI1FromCy(*(CY
*)src
, d
); break;
264 case DBTYPE_DATE
: hr
= VarI1FromDate(*(DATE
*)src
, d
); break;
265 case DBTYPE_BSTR
: hr
= VarI1FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
266 case DBTYPE_BOOL
: hr
= VarI1FromBool(*(VARIANT_BOOL
*)src
, d
); break;
267 case DBTYPE_DECIMAL
: hr
= VarI1FromDec(src
, d
); break;
268 case DBTYPE_UI1
: hr
= VarI1FromUI1(*(BYTE
*)src
, d
); break;
269 case DBTYPE_UI2
: hr
= VarI1FromUI2(*(WORD
*)src
, d
); break;
270 case DBTYPE_UI4
: hr
= VarI1FromUI4(*(DWORD
*)src
, d
); break;
271 case DBTYPE_I8
: hr
= VarI1FromI8(*(LONGLONG
*)src
, d
); break;
272 case DBTYPE_UI8
: hr
= VarI1FromUI8(*(ULONGLONG
*)src
, d
); break;
275 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_I1
)) == S_OK
)
278 default: FIXME("Unimplemented conversion %04x -> I1\n", src_type
); return E_NOTIMPL
;
285 signed short *d
= dst
;
288 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
289 case DBTYPE_I2
: *d
= *(signed short*)src
; hr
= S_OK
; break;
290 case DBTYPE_I4
: hr
= VarI2FromI4(*(signed int*)src
, d
); break;
291 case DBTYPE_R4
: hr
= VarI2FromR4(*(FLOAT
*)src
, d
); break;
292 case DBTYPE_R8
: hr
= VarI2FromR8(*(double*)src
, d
); break;
293 case DBTYPE_CY
: hr
= VarI2FromCy(*(CY
*)src
, d
); break;
294 case DBTYPE_DATE
: hr
= VarI2FromDate(*(DATE
*)src
, d
); break;
295 case DBTYPE_BSTR
: hr
= VarI2FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
296 case DBTYPE_BOOL
: hr
= VarI2FromBool(*(VARIANT_BOOL
*)src
, d
); break;
297 case DBTYPE_DECIMAL
: hr
= VarI2FromDec(src
, d
); break;
298 case DBTYPE_I1
: hr
= VarI2FromI1(*(signed char*)src
, d
); break;
299 case DBTYPE_UI1
: hr
= VarI2FromUI1(*(BYTE
*)src
, d
); break;
300 case DBTYPE_UI2
: hr
= VarI2FromUI2(*(WORD
*)src
, d
); break;
301 case DBTYPE_UI4
: hr
= VarI2FromUI4(*(DWORD
*)src
, d
); break;
302 case DBTYPE_I8
: hr
= VarI2FromI8(*(LONGLONG
*)src
, d
); break;
303 case DBTYPE_UI8
: hr
= VarI2FromUI8(*(ULONGLONG
*)src
, d
); break;
306 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_I2
)) == S_OK
)
309 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type
); return E_NOTIMPL
;
319 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
320 case DBTYPE_I2
: hr
= VarI4FromI2(*(signed short*)src
, d
); break;
321 case DBTYPE_I4
: *d
= *(signed int*)src
; hr
= S_OK
; break;
322 case DBTYPE_R4
: hr
= VarI4FromR4(*(FLOAT
*)src
, d
); break;
323 case DBTYPE_R8
: hr
= VarI4FromR8(*(double*)src
, d
); break;
324 case DBTYPE_CY
: hr
= VarI4FromCy(*(CY
*)src
, d
); break;
325 case DBTYPE_DATE
: hr
= VarI4FromDate(*(DATE
*)src
, d
); break;
326 case DBTYPE_BSTR
: hr
= VarI4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
327 case DBTYPE_BOOL
: hr
= VarI4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
328 case DBTYPE_DECIMAL
: hr
= VarI4FromDec(src
, d
); break;
329 case DBTYPE_I1
: hr
= VarI4FromI1(*(signed char*)src
, d
); break;
330 case DBTYPE_UI1
: hr
= VarI4FromUI1(*(BYTE
*)src
, d
); break;
331 case DBTYPE_UI2
: hr
= VarI4FromUI2(*(WORD
*)src
, d
); break;
332 case DBTYPE_UI4
: hr
= VarI4FromUI4(*(DWORD
*)src
, d
); break;
333 case DBTYPE_I8
: hr
= VarI4FromI8(*(LONGLONG
*)src
, d
); break;
334 case DBTYPE_UI8
: hr
= VarI4FromUI8(*(ULONGLONG
*)src
, d
); break;
337 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_I4
)) == S_OK
)
340 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type
); return E_NOTIMPL
;
350 case DBTYPE_BSTR
: hr
= VarI8FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
351 case DBTYPE_I8
: *d
= *(LONGLONG
*)src
; hr
= S_OK
; break;
352 default: FIXME("Unimplemented conversion %04x -> I8\n", src_type
); return E_NOTIMPL
;
362 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
363 case DBTYPE_I2
: hr
= VarR4FromI2(*(signed short*)src
, d
); break;
364 case DBTYPE_I4
: hr
= VarR4FromI4(*(signed int*)src
, d
); break;
365 case DBTYPE_R4
: *d
= *(FLOAT
*)src
; hr
= S_OK
; break;
366 case DBTYPE_R8
: hr
= VarR4FromR8(*(double*)src
, d
); break;
367 case DBTYPE_CY
: hr
= VarR4FromCy(*(CY
*)src
, d
); break;
368 case DBTYPE_DATE
: hr
= VarR4FromDate(*(DATE
*)src
, d
); break;
369 case DBTYPE_BSTR
: hr
= VarR4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
370 case DBTYPE_BOOL
: hr
= VarR4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
371 case DBTYPE_DECIMAL
: hr
= VarR4FromDec(src
, d
); break;
372 case DBTYPE_I1
: hr
= VarR4FromI1(*(signed char*)src
, d
); break;
373 case DBTYPE_UI1
: hr
= VarR4FromUI1(*(BYTE
*)src
, d
); break;
374 case DBTYPE_UI2
: hr
= VarR4FromUI2(*(WORD
*)src
, d
); break;
375 case DBTYPE_UI4
: hr
= VarR4FromUI4(*(DWORD
*)src
, d
); break;
376 case DBTYPE_I8
: hr
= VarR4FromI8(*(LONGLONG
*)src
, d
); break;
377 case DBTYPE_UI8
: hr
= VarR4FromUI8(*(ULONGLONG
*)src
, d
); break;
380 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_R4
)) == S_OK
)
383 default: FIXME("Unimplemented conversion %04x -> R4\n", src_type
); return E_NOTIMPL
;
392 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
393 case DBTYPE_I1
: hr
= VarR8FromI1(*(signed char*)src
, d
); break;
394 case DBTYPE_I2
: hr
= VarR8FromI2(*(signed short*)src
, d
); break;
395 case DBTYPE_I4
: hr
= VarR8FromI4(*(signed int*)src
, d
); break;
396 case DBTYPE_I8
: hr
= VarR8FromI8(*(LONGLONG
*)src
, d
); break;
397 case DBTYPE_UI1
: hr
= VarR8FromUI1(*(BYTE
*)src
, d
); break;
398 case DBTYPE_UI2
: hr
= VarR8FromUI2(*(WORD
*)src
, d
); break;
399 case DBTYPE_UI4
: hr
= VarR8FromUI4(*(DWORD
*)src
, d
); break;
400 case DBTYPE_UI8
: hr
= VarR8FromUI8(*(ULONGLONG
*)src
, d
); break;
401 case DBTYPE_R4
: hr
= VarR8FromR4(*(FLOAT
*)src
, d
); break;
402 case DBTYPE_R8
: *d
= *(DOUBLE
*)src
; hr
= S_OK
; break;
403 case DBTYPE_CY
: hr
= VarR8FromCy(*(CY
*)src
, d
); break;
404 case DBTYPE_DATE
: hr
= VarR8FromDate(*(DATE
*)src
, d
); break;
405 case DBTYPE_BSTR
: hr
= VarR8FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
406 case DBTYPE_BOOL
: hr
= VarR8FromBool(*(VARIANT_BOOL
*)src
, d
); break;
407 case DBTYPE_DECIMAL
: hr
= VarR8FromDec(src
, d
); break;
410 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_R8
)) == S_OK
)
413 default: FIXME("Unimplemented conversion %04x -> R8\n", src_type
); return E_NOTIMPL
;
422 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
423 case DBTYPE_I1
: hr
= VarBoolFromI1(*(signed char*)src
, d
); break;
424 case DBTYPE_I2
: hr
= VarBoolFromI2(*(signed short*)src
, d
); break;
425 case DBTYPE_I4
: hr
= VarBoolFromI4(*(signed int*)src
, d
); break;
426 case DBTYPE_I8
: hr
= VarBoolFromI8(*(LONGLONG
*)src
, d
); break;
427 case DBTYPE_UI1
: hr
= VarBoolFromUI1(*(BYTE
*)src
, d
); break;
428 case DBTYPE_UI2
: hr
= VarBoolFromUI2(*(WORD
*)src
, d
); break;
429 case DBTYPE_UI4
: hr
= VarBoolFromUI4(*(DWORD
*)src
, d
); break;
430 case DBTYPE_UI8
: hr
= VarBoolFromUI8(*(ULONGLONG
*)src
, d
); break;
431 case DBTYPE_R4
: hr
= VarBoolFromR4(*(FLOAT
*)src
, d
); break;
432 case DBTYPE_R8
: hr
= VarBoolFromR8(*(DOUBLE
*)src
, d
); break;
433 case DBTYPE_CY
: hr
= VarBoolFromCy(*(CY
*)src
, d
); break;
434 case DBTYPE_DATE
: hr
= VarBoolFromDate(*(DATE
*)src
, d
); break;
435 case DBTYPE_BSTR
: hr
= VarBoolFromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
436 case DBTYPE_BOOL
: *d
= *(VARIANT_BOOL
*)src
; hr
= S_OK
; break;
437 case DBTYPE_DECIMAL
: hr
= VarBoolFromDec(src
, d
); break;
438 default: FIXME("Unimplemented conversion %04x -> BOOL\n", src_type
); return E_NOTIMPL
;
447 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
448 case DBTYPE_I1
: hr
= VarDateFromI1(*(signed char*)src
, d
); break;
449 case DBTYPE_I2
: hr
= VarDateFromI2(*(signed short*)src
, d
); break;
450 case DBTYPE_I4
: hr
= VarDateFromI4(*(signed int*)src
, d
); break;
451 case DBTYPE_I8
: hr
= VarDateFromI8(*(LONGLONG
*)src
, d
); break;
452 case DBTYPE_UI1
: hr
= VarDateFromUI1(*(BYTE
*)src
, d
); break;
453 case DBTYPE_UI2
: hr
= VarDateFromUI2(*(WORD
*)src
, d
); break;
454 case DBTYPE_UI4
: hr
= VarDateFromUI4(*(DWORD
*)src
, d
); break;
455 case DBTYPE_UI8
: hr
= VarDateFromUI8(*(ULONGLONG
*)src
, d
); break;
456 case DBTYPE_R4
: hr
= VarDateFromR4(*(FLOAT
*)src
, d
); break;
457 case DBTYPE_R8
: hr
= VarDateFromR8(*(DOUBLE
*)src
, d
); break;
458 case DBTYPE_CY
: hr
= VarDateFromCy(*(CY
*)src
, d
); break;
459 case DBTYPE_DATE
: *d
= *(DATE
*)src
; hr
= S_OK
; break;
460 case DBTYPE_BSTR
: hr
= VarDateFromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
461 case DBTYPE_BOOL
: hr
= VarDateFromBool(*(VARIANT_BOOL
*)src
, d
); break;
462 case DBTYPE_DECIMAL
: hr
= VarDateFromDec(src
, d
); break;
463 case DBTYPE_DBTIMESTAMP
:
466 DBTIMESTAMP
*ts
= src
;
469 st
.wMonth
= ts
->month
;
472 st
.wMinute
= ts
->minute
;
473 st
.wSecond
= ts
->second
;
474 st
.wMilliseconds
= ts
->fraction
/1000000;
475 hr
= (SystemTimeToVariantTime(&st
, d
) ? S_OK
: E_FAIL
);
482 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_DATE
)) == S_OK
)
486 default: FIXME("Unimplemented conversion %04x -> DATE\n", src_type
); return E_NOTIMPL
;
495 case DBTYPE_DBDATE
: memcpy(d
, src
, sizeof(DBDATE
)); hr
= S_OK
; break;
499 BSTR s
= *(WCHAR
**)src
;
502 V_VT(&var
) = VT_BSTR
;
503 V_BSTR(&var
) = SysAllocString(s
);
505 if ((hr
= VariantChangeType(&var
, &var
, 0, VT_DATE
)) == S_OK
)
508 hr
= (VariantTimeToSystemTime( V_DATE(&var
), &st
) ? S_OK
: E_FAIL
);
510 d
->month
= st
.wMonth
;
521 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_DATE
)) == S_OK
)
524 hr
= (VariantTimeToSystemTime( V_DATE(&tmp
), &st
) ? S_OK
: E_FAIL
);
526 d
->month
= st
.wMonth
;
531 FIXME("Unimplemented variant type %d -> DBDATE\n", V_VT((VARIANT
*)src
));
536 default: FIXME("Unimplemented conversion %04x -> DBDATE\n", src_type
); return E_NOTIMPL
;
540 case DBTYPE_DBTIMESTAMP
:
545 case DBTYPE_EMPTY
: memset(d
, 0, sizeof(DBTIMESTAMP
)); hr
= S_OK
; break;
546 case DBTYPE_DBTIMESTAMP
: memcpy(d
, src
, sizeof(DBTIMESTAMP
)); hr
= S_OK
; break;
550 BSTR s
= *(WCHAR
**)src
;
553 V_VT(&var
) = VT_BSTR
;
554 V_BSTR(&var
) = SysAllocString(s
);
556 if ((hr
= VariantChangeType(&var
, &var
, 0, VT_DATE
)) == S_OK
)
560 hr
= (VariantTimeToSystemTime( V_DATE(&var
), &st
) ? S_OK
: E_FAIL
);
562 d
->month
= st
.wMonth
;
565 d
->minute
= st
.wMinute
;
566 d
->second
= st
.wSecond
;
567 d
->fraction
= st
.wMilliseconds
* 1000000;
571 if(swscanf( s
, L
"%d-%d-%d %d:%d:%d.%d", &d
->year
, &d
->month
, &d
->day
, &d
->hour
, &d
->minute
,
572 &d
->second
, &d
->fraction
) != 7)
574 hr
= DISP_E_TYPEMISMATCH
;
575 *dst_status
= DBSTATUS_E_CANTCONVERTVALUE
;
576 *dst_len
= get_length(dst_type
);
588 hr
= (VariantTimeToSystemTime(*(double*)src
, &st
) ? S_OK
: E_FAIL
);
590 d
->month
= st
.wMonth
;
593 d
->minute
= st
.wMinute
;
594 d
->second
= st
.wSecond
;
595 d
->fraction
= st
.wMilliseconds
* 1000000;
599 if( V_VT((VARIANT
*)src
) == VT_DATE
)
602 hr
= (VariantTimeToSystemTime( V_DATE((VARIANT
*)src
), &st
) ? S_OK
: E_FAIL
);
604 d
->month
= st
.wMonth
;
607 d
->minute
= st
.wMinute
;
608 d
->second
= st
.wSecond
;
609 d
->fraction
= st
.wMilliseconds
* 1000000;
613 FIXME("Unimplemented variant type %d -> DBTIMESTAMP\n", V_VT((VARIANT
*)src
));
617 default: FIXME("Unimplemented conversion %04x -> DBTIMESTAMP\n", src_type
); return E_NOTIMPL
;
627 case DBTYPE_EMPTY
: d
->int64
= 0; hr
= S_OK
; break;
628 case DBTYPE_I2
: hr
= VarCyFromI2(*(signed short*)src
, d
); break;
629 case DBTYPE_I4
: hr
= VarCyFromI4(*(signed int*)src
, d
); break;
630 case DBTYPE_R4
: hr
= VarCyFromR4(*(FLOAT
*)src
, d
); break;
631 case DBTYPE_R8
: hr
= VarCyFromR8(*(double*)src
, d
); break;
632 case DBTYPE_CY
: *d
= *(CY
*)src
; hr
= S_OK
; break;
633 case DBTYPE_DATE
: hr
= VarCyFromDate(*(DATE
*)src
, d
); break;
634 case DBTYPE_BSTR
: hr
= VarCyFromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
635 case DBTYPE_BOOL
: hr
= VarCyFromBool(*(VARIANT_BOOL
*)src
, d
); break;
636 case DBTYPE_DECIMAL
: hr
= VarCyFromDec(src
, d
); break;
637 case DBTYPE_I1
: hr
= VarCyFromI1(*(signed char*)src
, d
); break;
638 case DBTYPE_UI1
: hr
= VarCyFromUI1(*(BYTE
*)src
, d
); break;
639 case DBTYPE_UI2
: hr
= VarCyFromUI2(*(WORD
*)src
, d
); break;
640 case DBTYPE_UI4
: hr
= VarCyFromUI4(*(DWORD
*)src
, d
); break;
641 case DBTYPE_I8
: hr
= VarCyFromI8(*(LONGLONG
*)src
, d
); break;
642 case DBTYPE_UI8
: hr
= VarCyFromUI8(*(ULONGLONG
*)src
, d
); break;
643 default: FIXME("Unimplemented conversion %04x -> CY\n", src_type
); return E_NOTIMPL
;
653 case DBTYPE_EMPTY
: *d
= SysAllocStringLen(NULL
, 0); hr
= *d
? S_OK
: E_OUTOFMEMORY
; break;
654 case DBTYPE_I2
: hr
= VarBstrFromI2(*(signed short*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
655 case DBTYPE_I4
: hr
= VarBstrFromI4(*(signed int*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
656 case DBTYPE_R4
: hr
= VarBstrFromR4(*(FLOAT
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
657 case DBTYPE_R8
: hr
= VarBstrFromR8(*(double*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
658 case DBTYPE_CY
: hr
= VarBstrFromCy(*(CY
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
659 case DBTYPE_DATE
: hr
= VarBstrFromDate(*(DATE
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
660 case DBTYPE_BSTR
: *d
= SysAllocStringLen(*(BSTR
*)src
, SysStringLen(*(BSTR
*)src
)); hr
= *d
? S_OK
: E_OUTOFMEMORY
; break;
661 case DBTYPE_BOOL
: hr
= VarBstrFromBool(*(VARIANT_BOOL
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
662 case DBTYPE_DECIMAL
: hr
= VarBstrFromDec(src
, LOCALE_USER_DEFAULT
, 0, d
); break;
663 case DBTYPE_I1
: hr
= VarBstrFromI1(*(signed char*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
664 case DBTYPE_UI1
: hr
= VarBstrFromUI1(*(BYTE
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
665 case DBTYPE_UI2
: hr
= VarBstrFromUI2(*(WORD
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
666 case DBTYPE_UI4
: hr
= VarBstrFromUI4(*(DWORD
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
667 case DBTYPE_I8
: hr
= VarBstrFromI8(*(LONGLONG
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
668 case DBTYPE_UI8
: hr
= VarBstrFromUI8(*(ULONGLONG
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
672 const GUID
*id
= src
;
673 static const WCHAR format
[] = {
674 '{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
675 '%','0','2','X','%','0','2','X','-',
676 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
677 wsprintfW(szBuff
, format
,
678 id
->Data1
, id
->Data2
, id
->Data3
,
679 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
680 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
681 *d
= SysAllocString(szBuff
);
682 hr
= *d
? S_OK
: E_OUTOFMEMORY
;
687 *d
= SysAllocStringLen(NULL
, 2 * src_len
);
692 static const char hexchars
[] = "0123456789ABCDEF";
694 unsigned char *p
= src
;
697 *s
++ = hexchars
[(*p
>> 4) & 0x0F];
698 *s
++ = hexchars
[(*p
) & 0x0F];
705 case DBTYPE_DBTIMESTAMP
:
708 static const WCHAR format1
[] = {
709 '%','0','4','d','-','%','0','2','d','-','%','0','2','d',' ','%','0','2','d',':','%','0','2','d',
710 ':','%','0','2','d', 0};
711 static const WCHAR format2
[] = {
712 '%','0','4','d','-','%','0','2','d','-','%','0','2','d',' ','%','0','2','d',':','%','0','2','d',
713 ':','%','0','2','d','.','%','0','9','d', 0};
714 DBTIMESTAMP
*ts
= src
;
716 if(ts
->fraction
== 0)
717 wsprintfW(szBuff
, format1
, ts
->year
, ts
->month
, ts
->day
, ts
->hour
, ts
->minute
, ts
->second
);
719 wsprintfW(szBuff
, format2
, ts
->year
, ts
->month
, ts
->day
, ts
->hour
, ts
->minute
, ts
->second
, ts
->fraction
);
720 *d
= SysAllocString(szBuff
);
721 hr
= *d
? S_OK
: E_OUTOFMEMORY
;
726 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_BSTR
)) == S_OK
)
729 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type
); return E_NOTIMPL
;
739 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
740 case DBTYPE_I2
: hr
= VarUI1FromI2(*(signed short*)src
, d
); break;
741 case DBTYPE_I4
: hr
= VarUI1FromI4(*(signed int*)src
, d
); break;
742 case DBTYPE_R4
: hr
= VarUI1FromR4(*(FLOAT
*)src
, d
); break;
743 case DBTYPE_R8
: hr
= VarUI1FromR8(*(double*)src
, d
); break;
744 case DBTYPE_CY
: hr
= VarUI1FromCy(*(CY
*)src
, d
); break;
745 case DBTYPE_DATE
: hr
= VarUI1FromDate(*(DATE
*)src
, d
); break;
746 case DBTYPE_BSTR
: hr
= VarUI1FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
747 case DBTYPE_BOOL
: hr
= VarUI1FromBool(*(VARIANT_BOOL
*)src
, d
); break;
748 case DBTYPE_DECIMAL
: hr
= VarUI1FromDec(src
, d
); break;
749 case DBTYPE_I1
: hr
= VarUI1FromI1(*(signed char*)src
, d
); break;
750 case DBTYPE_UI1
: *d
= *(BYTE
*)src
; hr
= S_OK
; break;
751 case DBTYPE_UI2
: hr
= VarUI1FromUI2(*(WORD
*)src
, d
); break;
752 case DBTYPE_UI4
: hr
= VarUI1FromUI4(*(DWORD
*)src
, d
); break;
753 case DBTYPE_I8
: hr
= VarUI1FromI8(*(LONGLONG
*)src
, d
); break;
754 case DBTYPE_UI8
: hr
= VarUI1FromUI8(*(ULONGLONG
*)src
, d
); break;
755 default: FIXME("Unimplemented conversion %04x -> UI1\n", src_type
); return E_NOTIMPL
;
764 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
765 case DBTYPE_I2
: hr
= VarUI2FromI2(*(signed short*)src
, d
); break;
766 case DBTYPE_I4
: hr
= VarUI2FromI4(*(signed int*)src
, d
); break;
767 case DBTYPE_R4
: hr
= VarUI2FromR4(*(FLOAT
*)src
, d
); break;
768 case DBTYPE_R8
: hr
= VarUI2FromR8(*(double*)src
, d
); break;
769 case DBTYPE_CY
: hr
= VarUI2FromCy(*(CY
*)src
, d
); break;
770 case DBTYPE_DATE
: hr
= VarUI2FromDate(*(DATE
*)src
, d
); break;
771 case DBTYPE_BSTR
: hr
= VarUI2FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
772 case DBTYPE_BOOL
: hr
= VarUI2FromBool(*(VARIANT_BOOL
*)src
, d
); break;
773 case DBTYPE_DECIMAL
: hr
= VarUI2FromDec(src
, d
); break;
774 case DBTYPE_I1
: hr
= VarUI2FromI1(*(signed char*)src
, d
); break;
775 case DBTYPE_UI1
: hr
= VarUI2FromUI1(*(BYTE
*)src
, d
); break;
776 case DBTYPE_UI2
: *d
= *(WORD
*)src
; hr
= S_OK
; break;
777 case DBTYPE_UI4
: hr
= VarUI2FromUI4(*(DWORD
*)src
, d
); break;
778 case DBTYPE_I8
: hr
= VarUI2FromI8(*(LONGLONG
*)src
, d
); break;
779 case DBTYPE_UI8
: hr
= VarUI2FromUI8(*(ULONGLONG
*)src
, d
); break;
780 default: FIXME("Unimplemented conversion %04x -> UI2\n", src_type
); return E_NOTIMPL
;
790 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
791 case DBTYPE_I2
: hr
= VarUI4FromI2(*(signed short*)src
, d
); break;
792 case DBTYPE_I4
: hr
= VarUI4FromI4(*(signed int*)src
, d
); break;
793 case DBTYPE_R4
: hr
= VarUI4FromR4(*(FLOAT
*)src
, d
); break;
794 case DBTYPE_R8
: hr
= VarUI4FromR8(*(double*)src
, d
); break;
795 case DBTYPE_CY
: hr
= VarUI4FromCy(*(CY
*)src
, d
); break;
796 case DBTYPE_DATE
: hr
= VarUI4FromDate(*(DATE
*)src
, d
); break;
797 case DBTYPE_BSTR
: hr
= VarUI4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
798 case DBTYPE_BOOL
: hr
= VarUI4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
799 case DBTYPE_DECIMAL
: hr
= VarUI4FromDec(src
, d
); break;
800 case DBTYPE_I1
: hr
= VarUI4FromI1(*(signed char*)src
, d
); break;
801 case DBTYPE_UI1
: hr
= VarUI4FromUI1(*(BYTE
*)src
, d
); break;
802 case DBTYPE_UI2
: hr
= VarUI4FromUI2(*(WORD
*)src
, d
); break;
803 case DBTYPE_UI4
: *d
= *(DWORD
*)src
; hr
= S_OK
; break;
804 case DBTYPE_I8
: hr
= VarUI4FromI8(*(LONGLONG
*)src
, d
); break;
805 case DBTYPE_UI8
: hr
= VarUI4FromUI8(*(ULONGLONG
*)src
, d
); break;
808 if ((hr
= VariantChangeType(&tmp
, src
, 0, VT_UI4
)) == S_OK
)
811 default: FIXME("Unimplemented conversion %04x -> UI4\n", src_type
); return E_NOTIMPL
;
821 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
822 case DBTYPE_I2
: hr
= VarUI8FromI2(*(signed short*)src
, d
); break;
823 case DBTYPE_I4
: {LONGLONG s
= *(signed int*)src
; hr
= VarUI8FromI8(s
, d
); break;}
824 case DBTYPE_R4
: hr
= VarUI8FromR4(*(FLOAT
*)src
, d
); break;
825 case DBTYPE_R8
: hr
= VarUI8FromR8(*(double*)src
, d
); break;
826 case DBTYPE_CY
: hr
= VarUI8FromCy(*(CY
*)src
, d
); break;
827 case DBTYPE_DATE
: hr
= VarUI8FromDate(*(DATE
*)src
, d
); break;
828 case DBTYPE_BSTR
: hr
= VarUI8FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
829 case DBTYPE_BOOL
: hr
= VarUI8FromBool(*(VARIANT_BOOL
*)src
, d
); break;
830 case DBTYPE_DECIMAL
: hr
= VarUI8FromDec(src
, d
); break;
831 case DBTYPE_I1
: hr
= VarUI8FromI1(*(signed char*)src
, d
); break;
832 case DBTYPE_UI1
: hr
= VarUI8FromUI1(*(BYTE
*)src
, d
); break;
833 case DBTYPE_UI2
: hr
= VarUI8FromUI2(*(WORD
*)src
, d
); break;
834 case DBTYPE_UI4
: hr
= VarUI8FromUI4(*(DWORD
*)src
, d
); break;
835 case DBTYPE_I8
: hr
= VarUI8FromI8(*(LONGLONG
*)src
, d
); break;
836 case DBTYPE_UI8
: *d
= *(ULONGLONG
*)src
; hr
= S_OK
; break;
837 default: FIXME("Unimplemented conversion %04x -> UI8\n", src_type
); return E_NOTIMPL
;
842 case DBTYPE_FILETIME
:
847 case DBTYPE_EMPTY
: d
->dwLowDateTime
= d
->dwHighDateTime
= 0; hr
= S_OK
; break;
848 case DBTYPE_FILETIME
: *d
= *(FILETIME
*)src
; hr
= S_OK
; break;
849 default: FIXME("Unimplemented conversion %04x -> FILETIME\n", src_type
); return E_NOTIMPL
;
859 case DBTYPE_EMPTY
: *d
= GUID_NULL
; hr
= S_OK
; break;
860 case DBTYPE_GUID
: *d
= *(GUID
*)src
; hr
= S_OK
; break;
861 default: FIXME("Unimplemented conversion %04x -> GUID\n", src_type
); return E_NOTIMPL
;
871 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &bstr_len
,
872 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
873 precision
, scale
, flags
);
874 if(hr
!= S_OK
|| *dst_status
== DBSTATUS_S_ISNULL
)
876 bstr_len
= SysStringLen(b
);
877 *dst_len
= bstr_len
* sizeof(WCHAR
); /* Doesn't include size for '\0' */
878 *dst_status
= DBSTATUS_S_OK
;
879 bytes_to_copy
= min(*dst_len
+ sizeof(WCHAR
), dst_max_len
);
882 if(bytes_to_copy
>= sizeof(WCHAR
))
884 memcpy(dst
, b
, bytes_to_copy
- sizeof(WCHAR
));
885 *((WCHAR
*)dst
+ bytes_to_copy
/ sizeof(WCHAR
) - 1) = 0;
886 if(bytes_to_copy
< *dst_len
+ sizeof(WCHAR
))
887 *dst_status
= DBSTATUS_S_TRUNCATED
;
891 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
892 hr
= DB_E_ERRORSOCCURRED
;
904 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &length
,
905 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
906 precision
, scale
, flags
);
907 if(hr
!= S_OK
) return hr
;
908 length
= WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
), NULL
, 0, NULL
, NULL
);
909 *dst_len
= length
; /* Doesn't include size for '\0' */
910 *dst_status
= DBSTATUS_S_OK
;
911 bytes_to_copy
= min(length
+ 1, dst_max_len
);
914 if(bytes_to_copy
>= sizeof(char))
916 WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
), dst
, bytes_to_copy
- 1, NULL
, NULL
);
917 *((char *)dst
+ bytes_to_copy
- 1) = 0;
918 if(bytes_to_copy
< length
+ 1)
919 *dst_status
= DBSTATUS_S_TRUNCATED
;
923 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
924 hr
= DB_E_ERRORSOCCURRED
;
931 case DBTYPE_BYREF
| DBTYPE_WSTR
:
936 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &bstr_len
,
937 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
938 precision
, scale
, flags
);
939 if(hr
!= S_OK
) return hr
;
941 bstr_len
= SysStringLen(b
) * sizeof(WCHAR
);
942 *dst_len
= bstr_len
; /* Doesn't include size for '\0' */
944 *d
= CoTaskMemAlloc(bstr_len
+ sizeof(WCHAR
));
945 if(*d
) memcpy(*d
, b
, bstr_len
+ sizeof(WCHAR
));
946 else hr
= E_OUTOFMEMORY
;
951 case DBTYPE_BYREF
| DBTYPE_STR
:
956 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &length
,
957 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
958 precision
, scale
, flags
);
959 if(hr
!= S_OK
) return hr
;
961 length
= WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
) + 1, NULL
, 0, NULL
, NULL
);
962 *dst_len
= length
- 1; /* Doesn't include size for '\0' */
963 *dst_status
= DBSTATUS_S_OK
;
964 *d
= CoTaskMemAlloc(length
);
967 WideCharToMultiByte(CP_ACP
, 0, b
, SysStringLen(b
) + 1, *d
, length
, NULL
, NULL
);
983 V_BOOL(v
) = *(VARIANT_BOOL
*)src
;
988 V_I2(v
) = *(signed short*)src
;
993 V_I4(v
) = *(signed int*)src
;
997 V_VT(v
) = VT_DECIMAL
;
998 hr
= VarDecFromI8( *(LONGLONG
*)src
, &V_DECIMAL(v
));
1002 V_R4(v
) = *(FLOAT
*)src
;
1007 V_R8(v
) = *(double*)src
;
1012 BSTR s
= *(WCHAR
**)src
;
1013 TRACE("%s\n", debugstr_w(s
));
1015 V_BSTR(v
) = SysAllocString(s
);
1016 hr
= V_BSTR(v
) ? S_OK
: E_OUTOFMEMORY
;
1021 V_DATE(v
) = *(DATE
*)src
;
1031 st
.wYear
= ts
->year
;
1032 st
.wMonth
= ts
->month
;
1037 st
.wMilliseconds
= 0;
1038 hr
= (SystemTimeToVariantTime(&st
, &V_DATE(v
)) ? S_OK
: E_FAIL
);
1041 case DBTYPE_DBTIMESTAMP
:
1044 DBTIMESTAMP
*ts
= src
;
1048 st
.wYear
= ts
->year
;
1049 st
.wMonth
= ts
->month
;
1051 st
.wHour
= ts
->hour
;
1052 st
.wMinute
= ts
->minute
;
1053 st
.wSecond
= ts
->second
;
1054 st
.wMilliseconds
= ts
->fraction
/1000000;
1055 hr
= SystemTimeToVariantTime(&st
, &V_DATE(v
)) ? S_OK
: E_FAIL
;
1060 V_CY(v
) = *(CY
*)src
;
1066 SAFEARRAY
*psa
= NULL
;
1067 SAFEARRAYBOUND rgsabound
[1];
1068 unsigned char *p
= src
;
1070 rgsabound
[0].lLbound
= 0;
1071 rgsabound
[0].cElements
= src_len
;
1073 psa
= SafeArrayCreate(VT_UI1
,1,rgsabound
);
1074 for(i
=0; i
< src_len
; i
++,p
++)
1076 hr
= SafeArrayPutElement(psa
, &i
, p
);
1078 SafeArrayDestroy (psa
);
1083 V_VT(v
) = VT_ARRAY
|VT_UI1
;
1088 default: FIXME("Unimplemented conversion %04x -> VARIANT\n", src_type
); return E_NOTIMPL
;
1099 if( src_len
> dst_max_len
)
1100 *dst_status
= DBSTATUS_S_TRUNCATED
;
1102 *dst_status
= DBSTATUS_S_OK
;
1105 memcpy(d
, src
, min(src_len
, dst_max_len
));
1108 case DBTYPE_VARIANT
:
1110 switch(V_VT((VARIANT
*)src
))
1112 case VT_UI1
| VT_ARRAY
:
1117 hr
= SafeArrayGetUBound(V_ARRAY((VARIANT
*)src
), 1, &l
);
1121 hr
= SafeArrayAccessData(V_ARRAY((VARIANT
*)src
), (VOID
**)&data
);
1124 ERR("SafeArrayAccessData Failed = 0x%08x\n", hr
);
1129 *dst_status
= DBSTATUS_S_OK
;
1130 memcpy(d
, data
, *dst_len
);
1132 SafeArrayUnaccessData(V_ARRAY((VARIANT
*)src
));
1137 FIXME("Unimplemented variant type %d -> BYTES\n", V_VT((VARIANT
*)src
));
1142 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES\n", src_type
); return E_NOTIMPL
;
1146 case DBTYPE_BYTES
| DBTYPE_BYREF
:
1153 *d
= CoTaskMemAlloc(src_len
);
1154 if(*d
) memcpy(*d
, src
, src_len
);
1155 else hr
= E_OUTOFMEMORY
;
1158 *dst_status
= DBSTATUS_S_OK
;
1160 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES | DBTYPE_BYREF\n", src_type
); return E_NOTIMPL
;
1165 case DBTYPE_NUMERIC
:
1166 FIXME("Unimplemented conversion %04x -> DBTYPE_NUMERIC\n", src_type
);
1170 FIXME("Unimplemented conversion %04x -> %04x\n", src_type
, dst_type
);
1174 if(hr
== DISP_E_OVERFLOW
)
1176 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
1177 *dst_len
= get_length(dst_type
);
1178 hr
= DB_E_ERRORSOCCURRED
;
1182 *dst_status
= DBSTATUS_S_OK
;
1183 *dst_len
= get_length(dst_type
);
1189 static inline WORD
get_dbtype_class(DBTYPE type
)
1216 case DBTYPE_DBTIMESTAMP
:
1217 return DBTYPE_DBDATE
;
1222 /* Many src types will convert to this group of dst types */
1223 static inline BOOL
common_class(WORD dst_class
)
1233 case DBTYPE_VARIANT
:
1236 case DBTYPE_DECIMAL
:
1237 case DBTYPE_NUMERIC
:
1243 static inline BOOL
array_type(DBTYPE type
)
1245 return (type
>= DBTYPE_I2
&& type
<= DBTYPE_UI4
);
1248 static HRESULT WINAPI
convert_CanConvert(IDataConvert
* iface
,
1249 DBTYPE src_type
, DBTYPE dst_type
)
1251 convert
*This
= impl_from_IDataConvert(iface
);
1252 DBTYPE src_base_type
= src_type
& 0x1ff;
1253 DBTYPE dst_base_type
= dst_type
& 0x1ff;
1254 WORD dst_class
= get_dbtype_class(dst_base_type
);
1256 TRACE("(%p)->(%d, %d)\n", This
, src_type
, dst_type
);
1258 if(src_type
& DBTYPE_VECTOR
|| dst_type
& DBTYPE_VECTOR
) return S_FALSE
;
1260 if(src_type
& DBTYPE_ARRAY
)
1262 if(!array_type(src_base_type
)) return S_FALSE
;
1263 if(dst_type
& DBTYPE_ARRAY
)
1265 if(src_type
== dst_type
) return S_OK
;
1268 if(dst_type
== DBTYPE_VARIANT
) return S_OK
;
1272 if(dst_type
& DBTYPE_ARRAY
)
1274 if(!array_type(dst_base_type
)) return S_FALSE
;
1275 if(src_type
== DBTYPE_IDISPATCH
|| src_type
== DBTYPE_VARIANT
) return S_OK
;
1279 if(dst_type
& DBTYPE_BYREF
)
1280 if(dst_base_type
!= DBTYPE_BYTES
&& dst_base_type
!= DBTYPE_STR
&& dst_base_type
!= DBTYPE_WSTR
)
1283 switch(get_dbtype_class(src_base_type
))
1286 if(common_class(dst_class
)) return S_OK
;
1291 case DBTYPE_FILETIME
:
1294 if(dst_base_type
== DBTYPE_DBTIMESTAMP
) return S_OK
;
1299 switch(dst_base_type
)
1302 case DBTYPE_VARIANT
:
1303 case DBTYPE_FILETIME
: return S_OK
;
1304 default: return S_FALSE
;
1308 if(dst_base_type
== DBTYPE_BYTES
) return S_OK
;
1311 if(dst_base_type
== DBTYPE_DATE
) return S_OK
;
1313 case DBTYPE_DECIMAL
:
1314 if(common_class(dst_class
)) return S_OK
;
1315 if(dst_class
== DBTYPE_DBDATE
) return S_OK
;
1319 if(dst_base_type
== DBTYPE_DATE
) return S_OK
;
1321 case DBTYPE_NUMERIC
:
1323 if(common_class(dst_class
)) return S_OK
;
1327 if(common_class(dst_class
)) return S_OK
;
1328 switch(dst_base_type
)
1331 case DBTYPE_FILETIME
: return S_OK
;
1332 default: return S_FALSE
;
1344 case DBTYPE_VARIANT
:
1348 case DBTYPE_FILETIME
:
1350 default: return S_FALSE
;
1353 case DBTYPE_IDISPATCH
:
1354 case DBTYPE_VARIANT
:
1355 switch(dst_base_type
)
1357 case DBTYPE_IDISPATCH
:
1359 case DBTYPE_IUNKNOWN
:
1364 if(common_class(dst_class
)) return S_OK
;
1371 case DBTYPE_FILETIME
:
1373 default: return S_FALSE
;
1377 switch(dst_base_type
)
1381 case DBTYPE_VARIANT
:
1384 default: return S_FALSE
;
1387 case DBTYPE_IUNKNOWN
:
1388 switch(dst_base_type
)
1392 case DBTYPE_IDISPATCH
:
1393 case DBTYPE_VARIANT
:
1394 case DBTYPE_IUNKNOWN
:
1396 default: return S_FALSE
;
1400 if(dst_class
== DBTYPE_I4
|| dst_class
== DBTYPE_I8
) return S_OK
;
1408 case DBTYPE_VARIANT
:
1412 default: return S_FALSE
;
1415 case DBTYPE_FILETIME
:
1416 if(dst_class
== DBTYPE_I8
) return S_OK
;
1425 case DBTYPE_VARIANT
:
1427 case DBTYPE_FILETIME
:
1429 default: return S_FALSE
;
1436 static HRESULT WINAPI
convert_GetConversionSize(IDataConvert
* iface
,
1437 DBTYPE src_type
, DBTYPE dst_type
,
1438 DBLENGTH
*src_len
, DBLENGTH
*dst_len
,
1441 convert
*This
= impl_from_IDataConvert(iface
);
1444 TRACE("(%p)->(%d, %d, %p, %p, %p)\n", This
, src_type
, dst_type
, src_len
, dst_len
, src
);
1446 hr
= IDataConvert_CanConvert(iface
, src_type
, dst_type
);
1448 return DB_E_UNSUPPORTEDCONVERSION
;
1451 return E_INVALIDARG
;
1453 /* for some types we don't need to look into source data */
1454 if ((*dst_len
= get_length(dst_type
)))
1459 if(src_type
== DBTYPE_VARIANT
&& V_VT((VARIANT
*)src
) == VT_NULL
)
1467 case DBTYPE_VARIANT
:
1472 if ((hr
= VariantChangeType(&v
, src
, 0, VT_BSTR
)) == S_OK
)
1474 *dst_len
= WideCharToMultiByte(CP_ACP
, 0, V_BSTR(&v
), -1, NULL
, 0, NULL
, NULL
);
1482 case DBTYPE_DECIMAL
:
1493 case DBTYPE_DBTIMESTAMP
:
1497 FIXME("unimplemented for %04x -> DBTYPE_STR\n", src_type
);
1504 case DBTYPE_VARIANT
:
1509 if ((hr
= VariantChangeType(&v
, src
, 0, VT_BSTR
)) == S_OK
)
1511 *dst_len
= (SysStringLen(V_BSTR(&v
))+1) * sizeof(WCHAR
);
1518 *dst_len
= (*src_len
+ 1) * sizeof(WCHAR
);
1520 *dst_len
= (strlen(src
) + 1) * sizeof(WCHAR
);
1524 *dst_len
= (*src_len
) + sizeof(WCHAR
);
1526 *dst_len
= (lstrlenW(src
) + 1) * sizeof(WCHAR
);
1529 case DBTYPE_DECIMAL
:
1540 case DBTYPE_DBTIMESTAMP
:
1544 FIXME("unimplemented for %04x -> DBTYPE_WSTR\n", src_type
);
1551 case DBTYPE_VARIANT
:
1552 if(V_VT((VARIANT
*)src
) == VT_BSTR
)
1553 *dst_len
= (SysStringLen(V_BSTR((VARIANT
*)src
))) / sizeof(WCHAR
);
1556 switch(V_VT((VARIANT
*)src
))
1558 case VT_UI1
| VT_ARRAY
:
1562 hr
= SafeArrayGetUBound(V_ARRAY((VARIANT
*)src
), 1, &l
);
1570 WARN("DBTYPE_VARIANT(%d)->DBTYPE_BYTES unimplemented\n", V_VT((VARIANT
*)src
));
1575 FIXME("unimplemented for %04x -> DBTYPE_BYTES\n", src_type
);
1580 FIXME("unimplemented for conversion %d->%d\n", src_type
, dst_type
);
1587 static const struct IDataConvertVtbl convert_vtbl
=
1589 convert_QueryInterface
,
1592 convert_DataConvert
,
1594 convert_GetConversionSize
1597 static HRESULT WINAPI
dcinfo_QueryInterface(IDCInfo
* iface
, REFIID riid
, void **obj
)
1599 convert
*This
= impl_from_IDCInfo(iface
);
1601 return IDataConvert_QueryInterface(&This
->IDataConvert_iface
, riid
, obj
);
1604 static ULONG WINAPI
dcinfo_AddRef(IDCInfo
* iface
)
1606 convert
*This
= impl_from_IDCInfo(iface
);
1608 return IDataConvert_AddRef(&This
->IDataConvert_iface
);
1611 static ULONG WINAPI
dcinfo_Release(IDCInfo
* iface
)
1613 convert
*This
= impl_from_IDCInfo(iface
);
1615 return IDataConvert_Release(&This
->IDataConvert_iface
);
1618 static HRESULT WINAPI
dcinfo_GetInfo(IDCInfo
*iface
, ULONG num
, DCINFOTYPE types
[], DCINFO
**info_ptr
)
1620 convert
*This
= impl_from_IDCInfo(iface
);
1624 TRACE("(%p)->(%d, %p, %p)\n", This
, num
, types
, info_ptr
);
1626 *info_ptr
= infos
= CoTaskMemAlloc(num
* sizeof(*infos
));
1627 if(!infos
) return E_OUTOFMEMORY
;
1629 for(i
= 0; i
< num
; i
++)
1631 infos
[i
].eInfoType
= types
[i
];
1632 VariantInit(&infos
[i
].vData
);
1636 case DCINFOTYPE_VERSION
:
1637 V_VT(&infos
[i
].vData
) = VT_UI4
;
1638 V_UI4(&infos
[i
].vData
) = This
->version
;
1646 static HRESULT WINAPI
dcinfo_SetInfo(IDCInfo
* iface
, ULONG num
, DCINFO info
[])
1648 convert
*This
= impl_from_IDCInfo(iface
);
1652 TRACE("(%p)->(%d, %p)\n", This
, num
, info
);
1654 for(i
= 0; i
< num
; i
++)
1656 switch(info
[i
].eInfoType
)
1658 case DCINFOTYPE_VERSION
:
1659 if(V_VT(&info
[i
].vData
) != VT_UI4
)
1661 FIXME("VERSION with vt %x\n", V_VT(&info
[i
].vData
));
1662 hr
= DB_S_ERRORSOCCURRED
;
1665 This
->version
= V_UI4(&info
[i
].vData
);
1669 FIXME("Unhandled info type %d (vt %x)\n", info
[i
].eInfoType
, V_VT(&info
[i
].vData
));
1675 static const struct IDCInfoVtbl dcinfo_vtbl
=
1677 dcinfo_QueryInterface
,
1684 HRESULT
create_oledb_convert(IUnknown
*outer
, void **obj
)
1688 TRACE("(%p, %p)\n", outer
, obj
);
1692 if(outer
) return CLASS_E_NOAGGREGATION
;
1694 This
= heap_alloc(sizeof(*This
));
1695 if(!This
) return E_OUTOFMEMORY
;
1697 This
->IDataConvert_iface
.lpVtbl
= &convert_vtbl
;
1698 This
->IDCInfo_iface
.lpVtbl
= &dcinfo_vtbl
;
1700 This
->version
= 0x110;
1702 *obj
= &This
->IDataConvert_iface
;