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
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
33 #include "oledb_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(oledb
);
41 const struct IDataConvertVtbl
*lpVtbl
;
42 const struct IDCInfoVtbl
*lpDCInfoVtbl
;
46 UINT version
; /* Set by IDCInfo_SetInfo */
49 static inline convert
*impl_from_IDataConvert(IDataConvert
*iface
)
51 return (convert
*)((char*)iface
- FIELD_OFFSET(convert
, lpVtbl
));
54 static inline convert
*impl_from_IDCInfo(IDCInfo
*iface
)
56 return (convert
*)((char*)iface
- FIELD_OFFSET(convert
, lpDCInfoVtbl
));
59 static HRESULT WINAPI
convert_QueryInterface(IDataConvert
* iface
,
63 convert
*This
= impl_from_IDataConvert(iface
);
64 TRACE("(%p)->(%s, %p)\n", This
, debugstr_guid(riid
), obj
);
68 if(IsEqualIID(riid
, &IID_IUnknown
) ||
69 IsEqualIID(riid
, &IID_IDataConvert
))
73 else if(IsEqualIID(riid
, &IID_IDCInfo
))
75 *obj
= &This
->lpDCInfoVtbl
;
79 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
83 IDataConvert_AddRef(iface
);
88 static ULONG WINAPI
convert_AddRef(IDataConvert
* iface
)
90 convert
*This
= impl_from_IDataConvert(iface
);
91 TRACE("(%p)\n", This
);
93 return InterlockedIncrement(&This
->ref
);
97 static ULONG WINAPI
convert_Release(IDataConvert
* iface
)
99 convert
*This
= impl_from_IDataConvert(iface
);
102 TRACE("(%p)\n", This
);
104 ref
= InterlockedDecrement(&This
->ref
);
107 HeapFree(GetProcessHeap(), 0, This
);
113 static int get_length(DBTYPE type
)
134 case DBTYPE_FILETIME
:
135 return sizeof(FILETIME
);
140 case DBTYPE_BYREF
| DBTYPE_WSTR
:
143 FIXME("Unhandled type %04x\n", type
);
148 static HRESULT WINAPI
convert_DataConvert(IDataConvert
* iface
,
149 DBTYPE src_type
, DBTYPE dst_type
,
150 DBLENGTH src_len
, DBLENGTH
*dst_len
,
151 void *src
, void *dst
,
152 DBLENGTH dst_max_len
,
153 DBSTATUS src_status
, DBSTATUS
*dst_status
,
154 BYTE precision
, BYTE scale
,
157 convert
*This
= impl_from_IDataConvert(iface
);
160 TRACE("(%p)->(%d, %d, %d, %p, %p, %p, %d, %d, %p, %d, %d, %x)\n", This
,
161 src_type
, dst_type
, src_len
, dst_len
, src
, dst
, dst_max_len
,
162 src_status
, dst_status
, precision
, scale
, flags
);
164 *dst_status
= DBSTATUS_E_BADACCESSOR
;
166 if(IDataConvert_CanConvert(iface
, src_type
, dst_type
) != S_OK
)
168 return DB_E_UNSUPPORTEDCONVERSION
;
171 if(src_type
== DBTYPE_STR
)
176 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
177 len
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, NULL
, 0) - 1;
179 len
= MultiByteToWideChar(CP_ACP
, 0, src
, src_len
, NULL
, 0);
180 b
= SysAllocStringLen(NULL
, len
);
181 if(!b
) return E_OUTOFMEMORY
;
182 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
183 MultiByteToWideChar(CP_ACP
, 0, src
, -1, b
, len
+ 1);
185 MultiByteToWideChar(CP_ACP
, 0, src
, src_len
, b
, len
);
187 hr
= IDataConvert_DataConvert(iface
, DBTYPE_BSTR
, dst_type
, 0, dst_len
,
188 &b
, dst
, dst_max_len
, src_status
, dst_status
,
189 precision
, scale
, flags
);
195 if(src_type
== DBTYPE_WSTR
)
199 if(flags
& DBDATACONVERT_LENGTHFROMNTS
)
200 b
= SysAllocString(src
);
202 b
= SysAllocStringLen(src
, src_len
/ 2);
203 if(!b
) return E_OUTOFMEMORY
;
204 hr
= IDataConvert_DataConvert(iface
, DBTYPE_BSTR
, dst_type
, 0, dst_len
,
205 &b
, dst
, dst_max_len
, src_status
, dst_status
,
206 precision
, scale
, flags
);
215 signed short *d
= dst
;
218 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
219 case DBTYPE_I2
: *d
= *(signed short*)src
; hr
= S_OK
; break;
220 case DBTYPE_I4
: hr
= VarI2FromI4(*(signed int*)src
, d
); break;
221 case DBTYPE_R4
: hr
= VarI2FromR4(*(FLOAT
*)src
, d
); break;
222 case DBTYPE_R8
: hr
= VarI2FromR8(*(double*)src
, d
); break;
223 case DBTYPE_CY
: hr
= VarI2FromCy(*(CY
*)src
, d
); break;
224 case DBTYPE_DATE
: hr
= VarI2FromDate(*(DATE
*)src
, d
); break;
225 case DBTYPE_BSTR
: hr
= VarI2FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
226 case DBTYPE_BOOL
: hr
= VarI2FromBool(*(VARIANT_BOOL
*)src
, d
); break;
227 case DBTYPE_DECIMAL
: hr
= VarI2FromDec((DECIMAL
*)src
, d
); break;
228 case DBTYPE_I1
: hr
= VarI2FromI1(*(signed char*)src
, d
); break;
229 case DBTYPE_UI1
: hr
= VarI2FromUI1(*(BYTE
*)src
, d
); break;
230 case DBTYPE_UI2
: hr
= VarI2FromUI2(*(WORD
*)src
, d
); break;
231 case DBTYPE_UI4
: hr
= VarI2FromUI4(*(DWORD
*)src
, d
); break;
232 case DBTYPE_I8
: hr
= VarI2FromI8(*(LONGLONG
*)src
, d
); break;
233 case DBTYPE_UI8
: hr
= VarI2FromUI8(*(ULONGLONG
*)src
, d
); break;
234 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type
); return E_NOTIMPL
;
244 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
245 case DBTYPE_I2
: hr
= VarI4FromI2(*(signed short*)src
, d
); break;
246 case DBTYPE_I4
: *d
= *(signed int*)src
; hr
= S_OK
; break;
247 case DBTYPE_R4
: hr
= VarI4FromR4(*(FLOAT
*)src
, d
); break;
248 case DBTYPE_R8
: hr
= VarI4FromR8(*(double*)src
, d
); break;
249 case DBTYPE_CY
: hr
= VarI4FromCy(*(CY
*)src
, d
); break;
250 case DBTYPE_DATE
: hr
= VarI4FromDate(*(DATE
*)src
, d
); break;
251 case DBTYPE_BSTR
: hr
= VarI4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
252 case DBTYPE_BOOL
: hr
= VarI4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
253 case DBTYPE_DECIMAL
: hr
= VarI4FromDec((DECIMAL
*)src
, d
); break;
254 case DBTYPE_I1
: hr
= VarI4FromI1(*(signed char*)src
, d
); break;
255 case DBTYPE_UI1
: hr
= VarI4FromUI1(*(BYTE
*)src
, d
); break;
256 case DBTYPE_UI2
: hr
= VarI4FromUI2(*(WORD
*)src
, d
); break;
257 case DBTYPE_UI4
: hr
= VarI4FromUI4(*(DWORD
*)src
, d
); break;
258 case DBTYPE_I8
: hr
= VarI4FromI8(*(LONGLONG
*)src
, d
); break;
259 case DBTYPE_UI8
: hr
= VarI4FromUI8(*(ULONGLONG
*)src
, d
); break;
260 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type
); return E_NOTIMPL
;
270 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
271 case DBTYPE_I2
: hr
= VarR4FromI2(*(signed short*)src
, d
); break;
272 case DBTYPE_I4
: hr
= VarR4FromI4(*(signed int*)src
, d
); break;
273 case DBTYPE_R4
: *d
= *(FLOAT
*)src
; hr
= S_OK
; break;
274 case DBTYPE_R8
: hr
= VarR4FromR8(*(double*)src
, d
); break;
275 case DBTYPE_CY
: hr
= VarR4FromCy(*(CY
*)src
, d
); break;
276 case DBTYPE_DATE
: hr
= VarR4FromDate(*(DATE
*)src
, d
); break;
277 case DBTYPE_BSTR
: hr
= VarR4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
278 case DBTYPE_BOOL
: hr
= VarR4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
279 case DBTYPE_DECIMAL
: hr
= VarR4FromDec((DECIMAL
*)src
, d
); break;
280 case DBTYPE_I1
: hr
= VarR4FromI1(*(signed char*)src
, d
); break;
281 case DBTYPE_UI1
: hr
= VarR4FromUI1(*(BYTE
*)src
, d
); break;
282 case DBTYPE_UI2
: hr
= VarR4FromUI2(*(WORD
*)src
, d
); break;
283 case DBTYPE_UI4
: hr
= VarR4FromUI4(*(DWORD
*)src
, d
); break;
284 case DBTYPE_I8
: hr
= VarR4FromI8(*(LONGLONG
*)src
, d
); break;
285 case DBTYPE_UI8
: hr
= VarR4FromUI8(*(ULONGLONG
*)src
, d
); break;
286 default: FIXME("Unimplemented conversion %04x -> R4\n", src_type
); return E_NOTIMPL
;
296 case DBTYPE_EMPTY
: d
->int64
= 0; hr
= S_OK
; break;
297 case DBTYPE_I2
: hr
= VarCyFromI2(*(signed short*)src
, d
); break;
298 case DBTYPE_I4
: hr
= VarCyFromI4(*(signed int*)src
, d
); break;
299 case DBTYPE_R4
: hr
= VarCyFromR4(*(FLOAT
*)src
, d
); break;
300 case DBTYPE_R8
: hr
= VarCyFromR8(*(double*)src
, d
); break;
301 case DBTYPE_CY
: *d
= *(CY
*)src
; hr
= S_OK
; break;
302 case DBTYPE_DATE
: hr
= VarCyFromDate(*(DATE
*)src
, d
); break;
303 case DBTYPE_BSTR
: hr
= VarCyFromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
304 case DBTYPE_BOOL
: hr
= VarCyFromBool(*(VARIANT_BOOL
*)src
, d
); break;
305 case DBTYPE_DECIMAL
: hr
= VarCyFromDec((DECIMAL
*)src
, d
); break;
306 case DBTYPE_I1
: hr
= VarCyFromI1(*(signed char*)src
, d
); break;
307 case DBTYPE_UI1
: hr
= VarCyFromUI1(*(BYTE
*)src
, d
); break;
308 case DBTYPE_UI2
: hr
= VarCyFromUI2(*(WORD
*)src
, d
); break;
309 case DBTYPE_UI4
: hr
= VarCyFromUI4(*(DWORD
*)src
, d
); break;
310 case DBTYPE_I8
: hr
= VarCyFromI8(*(LONGLONG
*)src
, d
); break;
311 case DBTYPE_UI8
: hr
= VarCyFromUI8(*(ULONGLONG
*)src
, d
); break;
312 default: FIXME("Unimplemented conversion %04x -> CY\n", src_type
); return E_NOTIMPL
;
322 case DBTYPE_EMPTY
: *d
= SysAllocStringLen(NULL
, 0); hr
= *d
? S_OK
: E_OUTOFMEMORY
; break;
323 case DBTYPE_I2
: hr
= VarBstrFromI2(*(signed short*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
324 case DBTYPE_I4
: hr
= VarBstrFromI4(*(signed int*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
325 case DBTYPE_R4
: hr
= VarBstrFromR4(*(FLOAT
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
326 case DBTYPE_R8
: hr
= VarBstrFromR8(*(double*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
327 case DBTYPE_CY
: hr
= VarBstrFromCy(*(CY
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
328 case DBTYPE_DATE
: hr
= VarBstrFromDate(*(DATE
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
329 case DBTYPE_BSTR
: *d
= SysAllocStringLen(*(BSTR
*)src
, SysStringLen(*(BSTR
*)src
)); hr
= *d
? S_OK
: E_OUTOFMEMORY
; break;
330 case DBTYPE_BOOL
: hr
= VarBstrFromBool(*(VARIANT_BOOL
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
331 case DBTYPE_DECIMAL
: hr
= VarBstrFromDec((DECIMAL
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
332 case DBTYPE_I1
: hr
= VarBstrFromI1(*(signed char*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
333 case DBTYPE_UI1
: hr
= VarBstrFromUI1(*(BYTE
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
334 case DBTYPE_UI2
: hr
= VarBstrFromUI2(*(WORD
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
335 case DBTYPE_UI4
: hr
= VarBstrFromUI4(*(DWORD
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
336 case DBTYPE_I8
: hr
= VarBstrFromI8(*(LONGLONG
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
337 case DBTYPE_UI8
: hr
= VarBstrFromUI8(*(ULONGLONG
*)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
341 const GUID
*id
= (const GUID
*)src
;
343 '{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
344 '%','0','2','X','%','0','2','X','-',
345 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
346 wsprintfW(szBuff
, format
,
347 id
->Data1
, id
->Data2
, id
->Data3
,
348 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
349 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
350 *d
= SysAllocString(szBuff
);
351 hr
= *d
? S_OK
: E_OUTOFMEMORY
;
356 *d
= SysAllocStringLen(NULL
, 2 * src_len
);
361 const char hexchars
[] = "0123456789ABCDEF";
363 unsigned char *p
= src
;
366 *s
++ = hexchars
[(*p
>> 4) & 0x0F];
367 *s
++ = hexchars
[(*p
) & 0x0F];
374 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type
); return E_NOTIMPL
;
384 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
385 case DBTYPE_I2
: hr
= VarUI1FromI2(*(signed short*)src
, d
); break;
386 case DBTYPE_I4
: hr
= VarUI1FromI4(*(signed int*)src
, d
); break;
387 case DBTYPE_R4
: hr
= VarUI1FromR4(*(FLOAT
*)src
, d
); break;
388 case DBTYPE_R8
: hr
= VarUI1FromR8(*(double*)src
, d
); break;
389 case DBTYPE_CY
: hr
= VarUI1FromCy(*(CY
*)src
, d
); break;
390 case DBTYPE_DATE
: hr
= VarUI1FromDate(*(DATE
*)src
, d
); break;
391 case DBTYPE_BSTR
: hr
= VarUI1FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
392 case DBTYPE_BOOL
: hr
= VarUI1FromBool(*(VARIANT_BOOL
*)src
, d
); break;
393 case DBTYPE_DECIMAL
: hr
= VarUI1FromDec((DECIMAL
*)src
, d
); break;
394 case DBTYPE_I1
: hr
= VarUI1FromI1(*(signed char*)src
, d
); break;
395 case DBTYPE_UI1
: *d
= *(BYTE
*)src
; hr
= S_OK
; break;
396 case DBTYPE_UI2
: hr
= VarUI1FromUI2(*(WORD
*)src
, d
); break;
397 case DBTYPE_UI4
: hr
= VarUI1FromUI4(*(DWORD
*)src
, d
); break;
398 case DBTYPE_I8
: hr
= VarUI1FromI8(*(LONGLONG
*)src
, d
); break;
399 case DBTYPE_UI8
: hr
= VarUI1FromUI8(*(ULONGLONG
*)src
, d
); break;
400 default: FIXME("Unimplemented conversion %04x -> UI1\n", src_type
); return E_NOTIMPL
;
410 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
411 case DBTYPE_I2
: hr
= VarUI4FromI2(*(signed short*)src
, d
); break;
412 case DBTYPE_I4
: hr
= VarUI4FromI4(*(signed int*)src
, d
); break;
413 case DBTYPE_R4
: hr
= VarUI4FromR4(*(FLOAT
*)src
, d
); break;
414 case DBTYPE_R8
: hr
= VarUI4FromR8(*(double*)src
, d
); break;
415 case DBTYPE_CY
: hr
= VarUI4FromCy(*(CY
*)src
, d
); break;
416 case DBTYPE_DATE
: hr
= VarUI4FromDate(*(DATE
*)src
, d
); break;
417 case DBTYPE_BSTR
: hr
= VarUI4FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
418 case DBTYPE_BOOL
: hr
= VarUI4FromBool(*(VARIANT_BOOL
*)src
, d
); break;
419 case DBTYPE_DECIMAL
: hr
= VarUI4FromDec((DECIMAL
*)src
, d
); break;
420 case DBTYPE_I1
: hr
= VarUI4FromI1(*(signed char*)src
, d
); break;
421 case DBTYPE_UI1
: hr
= VarUI4FromUI1(*(BYTE
*)src
, d
); break;
422 case DBTYPE_UI2
: hr
= VarUI4FromUI2(*(WORD
*)src
, d
); break;
423 case DBTYPE_UI4
: *d
= *(DWORD
*)src
; hr
= S_OK
; break;
424 case DBTYPE_I8
: hr
= VarUI4FromI8(*(LONGLONG
*)src
, d
); break;
425 case DBTYPE_UI8
: hr
= VarUI4FromUI8(*(ULONGLONG
*)src
, d
); break;
426 default: FIXME("Unimplemented conversion %04x -> UI4\n", src_type
); return E_NOTIMPL
;
436 case DBTYPE_EMPTY
: *d
= 0; hr
= S_OK
; break;
437 case DBTYPE_I2
: hr
= VarUI8FromI2(*(signed short*)src
, d
); break;
438 case DBTYPE_I4
: {LONGLONG s
= *(signed int*)src
; hr
= VarUI8FromI8(s
, d
); break;}
439 case DBTYPE_R4
: hr
= VarUI8FromR4(*(FLOAT
*)src
, d
); break;
440 case DBTYPE_R8
: hr
= VarUI8FromR8(*(double*)src
, d
); break;
441 case DBTYPE_CY
: hr
= VarUI8FromCy(*(CY
*)src
, d
); break;
442 case DBTYPE_DATE
: hr
= VarUI8FromDate(*(DATE
*)src
, d
); break;
443 case DBTYPE_BSTR
: hr
= VarUI8FromStr(*(WCHAR
**)src
, LOCALE_USER_DEFAULT
, 0, d
); break;
444 case DBTYPE_BOOL
: hr
= VarUI8FromBool(*(VARIANT_BOOL
*)src
, d
); break;
445 case DBTYPE_DECIMAL
: hr
= VarUI8FromDec((DECIMAL
*)src
, d
); break;
446 case DBTYPE_I1
: hr
= VarUI8FromI1(*(signed char*)src
, d
); break;
447 case DBTYPE_UI1
: hr
= VarUI8FromUI1(*(BYTE
*)src
, d
); break;
448 case DBTYPE_UI2
: hr
= VarUI8FromUI2(*(WORD
*)src
, d
); break;
449 case DBTYPE_UI4
: hr
= VarUI8FromUI4(*(DWORD
*)src
, d
); break;
450 case DBTYPE_I8
: hr
= VarUI8FromI8(*(LONGLONG
*)src
, d
); break;
451 case DBTYPE_UI8
: *d
= *(ULONGLONG
*)src
; hr
= S_OK
; break;
452 default: FIXME("Unimplemented conversion %04x -> UI8\n", src_type
); return E_NOTIMPL
;
457 case DBTYPE_FILETIME
:
462 case DBTYPE_EMPTY
: d
->dwLowDateTime
= d
->dwHighDateTime
= 0; hr
= S_OK
; break;
463 case DBTYPE_FILETIME
: *d
= *(FILETIME
*)src
; hr
= S_OK
; break;
464 default: FIXME("Unimplemented conversion %04x -> FILETIME\n", src_type
); return E_NOTIMPL
;
474 case DBTYPE_EMPTY
: *d
= GUID_NULL
; hr
= S_OK
; break;
475 case DBTYPE_GUID
: *d
= *(GUID
*)src
; hr
= S_OK
; break;
476 default: FIXME("Unimplemented conversion %04x -> GUID\n", src_type
); return E_NOTIMPL
;
486 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &bstr_len
,
487 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
488 precision
, scale
, flags
);
489 if(hr
!= S_OK
) return hr
;
490 bstr_len
= SysStringLen(b
);
491 *dst_len
= bstr_len
* sizeof(WCHAR
); /* Doesn't include size for '\0' */
492 *dst_status
= DBSTATUS_S_OK
;
493 bytes_to_copy
= min(*dst_len
+ sizeof(WCHAR
), dst_max_len
);
496 if(bytes_to_copy
>= sizeof(WCHAR
))
498 memcpy(dst
, b
, bytes_to_copy
- sizeof(WCHAR
));
499 *((WCHAR
*)dst
+ bytes_to_copy
/ sizeof(WCHAR
) - 1) = 0;
500 if(bytes_to_copy
< *dst_len
+ sizeof(WCHAR
))
501 *dst_status
= DBSTATUS_S_TRUNCATED
;
505 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
506 hr
= DB_E_ERRORSOCCURRED
;
517 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &bstr_len
,
518 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
519 precision
, scale
, flags
);
520 if(hr
!= S_OK
) return hr
;
521 bstr_len
= SysStringLen(b
);
522 *dst_len
= bstr_len
* sizeof(char); /* Doesn't include size for '\0' */
523 *dst_status
= DBSTATUS_S_OK
;
524 bytes_to_copy
= min(*dst_len
+ sizeof(char), dst_max_len
);
527 if(bytes_to_copy
>= sizeof(char))
529 WideCharToMultiByte(CP_ACP
, 0, b
, bytes_to_copy
- sizeof(char), dst
, dst_max_len
, NULL
, NULL
);
530 *((char *)dst
+ bytes_to_copy
/ sizeof(char) - 1) = 0;
531 if(bytes_to_copy
< *dst_len
+ sizeof(char))
532 *dst_status
= DBSTATUS_S_TRUNCATED
;
536 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
537 hr
= DB_E_ERRORSOCCURRED
;
544 case DBTYPE_BYREF
| DBTYPE_WSTR
:
549 hr
= IDataConvert_DataConvert(iface
, src_type
, DBTYPE_BSTR
, src_len
, &bstr_len
,
550 src
, &b
, sizeof(BSTR
), src_status
, dst_status
,
551 precision
, scale
, flags
);
552 if(hr
!= S_OK
) return hr
;
554 bstr_len
= SysStringLen(b
) * sizeof(WCHAR
);
555 *dst_len
= bstr_len
; /* Doesn't include size for '\0' */
557 *d
= CoTaskMemAlloc(bstr_len
+ sizeof(WCHAR
));
558 if(*d
) memcpy(*d
, b
, bstr_len
+ sizeof(WCHAR
));
559 else hr
= E_OUTOFMEMORY
;
565 FIXME("Unimplemented conversion %04x -> %04x\n", src_type
, dst_type
);
570 if(hr
== DISP_E_OVERFLOW
)
572 *dst_status
= DBSTATUS_E_DATAOVERFLOW
;
573 *dst_len
= get_length(dst_type
);
574 hr
= DB_E_ERRORSOCCURRED
;
578 *dst_status
= DBSTATUS_S_OK
;
579 *dst_len
= get_length(dst_type
);
585 static inline WORD
get_dbtype_class(DBTYPE type
)
612 case DBTYPE_DBTIMESTAMP
:
613 return DBTYPE_DBDATE
;
618 /* Many src types will convert to this group of dst types */
619 static inline BOOL
common_class(WORD dst_class
)
639 static inline BOOL
array_type(DBTYPE type
)
641 return (type
>= DBTYPE_I2
&& type
<= DBTYPE_UI4
);
644 static HRESULT WINAPI
convert_CanConvert(IDataConvert
* iface
,
645 DBTYPE src_type
, DBTYPE dst_type
)
647 convert
*This
= impl_from_IDataConvert(iface
);
648 DBTYPE src_base_type
= src_type
& 0x1ff;
649 DBTYPE dst_base_type
= dst_type
& 0x1ff;
650 WORD dst_class
= get_dbtype_class(dst_base_type
);
652 TRACE("(%p)->(%d, %d)\n", This
, src_type
, dst_type
);
654 if(src_type
& DBTYPE_VECTOR
|| dst_type
& DBTYPE_VECTOR
) return S_FALSE
;
656 if(src_type
& DBTYPE_ARRAY
)
658 if(!array_type(src_base_type
)) return S_FALSE
;
659 if(dst_type
& DBTYPE_ARRAY
)
661 if(src_type
== dst_type
) return S_OK
;
664 if(dst_type
== DBTYPE_VARIANT
) return S_OK
;
668 if(dst_type
& DBTYPE_ARRAY
)
670 if(!array_type(dst_base_type
)) return S_FALSE
;
671 if(src_type
== DBTYPE_IDISPATCH
|| src_type
== DBTYPE_VARIANT
) return S_OK
;
675 if(dst_type
& DBTYPE_BYREF
)
676 if(dst_base_type
!= DBTYPE_BYTES
&& dst_base_type
!= DBTYPE_STR
&& dst_base_type
!= DBTYPE_WSTR
)
679 switch(get_dbtype_class(src_base_type
))
682 if(common_class(dst_class
)) return S_OK
;
687 case DBTYPE_FILETIME
:
690 if(dst_base_type
== DBTYPE_DBTIMESTAMP
) return S_OK
;
695 switch(dst_base_type
)
699 case DBTYPE_FILETIME
: return S_OK
;
700 default: return S_FALSE
;
704 if(dst_base_type
== DBTYPE_BYTES
) return S_OK
;
707 if(dst_base_type
== DBTYPE_DATE
) return S_OK
;
710 if(common_class(dst_class
)) return S_OK
;
711 if(dst_class
== DBTYPE_DBDATE
) return S_OK
;
715 if(dst_base_type
== DBTYPE_DATE
) return S_OK
;
718 if(common_class(dst_class
)) return S_OK
;
722 if(common_class(dst_class
)) return S_OK
;
723 switch(dst_base_type
)
726 case DBTYPE_FILETIME
: return S_OK
;
727 default: return S_FALSE
;
743 case DBTYPE_FILETIME
:
745 default: return S_FALSE
;
748 case DBTYPE_IDISPATCH
:
750 switch(dst_base_type
)
752 case DBTYPE_IDISPATCH
:
754 case DBTYPE_IUNKNOWN
:
759 if(common_class(dst_class
)) return S_OK
;
766 case DBTYPE_FILETIME
:
768 default: return S_FALSE
;
772 switch(dst_base_type
)
779 default: return S_FALSE
;
782 case DBTYPE_IUNKNOWN
:
783 switch(dst_base_type
)
787 case DBTYPE_IDISPATCH
:
789 case DBTYPE_IUNKNOWN
:
791 default: return S_FALSE
;
795 if(dst_class
== DBTYPE_I4
|| dst_class
== DBTYPE_I8
) return S_OK
;
807 default: return S_FALSE
;
810 case DBTYPE_FILETIME
:
811 if(dst_class
== DBTYPE_I8
) return S_OK
;
822 case DBTYPE_FILETIME
:
824 default: return S_FALSE
;
831 static HRESULT WINAPI
convert_GetConversionSize(IDataConvert
* iface
,
832 DBTYPE wSrcType
, DBTYPE wDstType
,
833 DBLENGTH
*pcbSrcLength
, DBLENGTH
*pcbDstLength
,
836 convert
*This
= impl_from_IDataConvert(iface
);
837 FIXME("(%p)->(%d, %d, %p, %p, %p): stub\n", This
, wSrcType
, wDstType
, pcbSrcLength
, pcbDstLength
, pSrc
);
842 static const struct IDataConvertVtbl convert_vtbl
=
844 convert_QueryInterface
,
849 convert_GetConversionSize
852 static HRESULT WINAPI
dcinfo_QueryInterface(IDCInfo
* iface
, REFIID riid
, void **obj
)
854 convert
*This
= impl_from_IDCInfo(iface
);
856 return IDataConvert_QueryInterface((IDataConvert
*)This
, riid
, obj
);
859 static ULONG WINAPI
dcinfo_AddRef(IDCInfo
* iface
)
861 convert
*This
= impl_from_IDCInfo(iface
);
863 return IDataConvert_AddRef((IDataConvert
*)This
);
866 static ULONG WINAPI
dcinfo_Release(IDCInfo
* iface
)
868 convert
*This
= impl_from_IDCInfo(iface
);
870 return IDataConvert_Release((IDataConvert
*)This
);
873 static HRESULT WINAPI
dcinfo_GetInfo(IDCInfo
*iface
, ULONG num
, DCINFOTYPE types
[], DCINFO
**info_ptr
)
875 convert
*This
= impl_from_IDCInfo(iface
);
879 TRACE("(%p)->(%d, %p, %p)\n", This
, num
, types
, info_ptr
);
881 *info_ptr
= infos
= CoTaskMemAlloc(num
* sizeof(*infos
));
882 if(!infos
) return E_OUTOFMEMORY
;
884 for(i
= 0; i
< num
; i
++)
886 infos
[i
].eInfoType
= types
[i
];
887 VariantInit(&infos
[i
].vData
);
891 case DCINFOTYPE_VERSION
:
892 V_VT(&infos
[i
].vData
) = VT_UI4
;
893 V_UI4(&infos
[i
].vData
) = This
->version
;
901 static HRESULT WINAPI
dcinfo_SetInfo(IDCInfo
* iface
, ULONG num
, DCINFO info
[])
903 convert
*This
= impl_from_IDCInfo(iface
);
907 TRACE("(%p)->(%d, %p)\n", This
, num
, info
);
909 for(i
= 0; i
< num
; i
++)
911 switch(info
[i
].eInfoType
)
913 case DCINFOTYPE_VERSION
:
914 if(V_VT(&info
[i
].vData
) != VT_UI4
)
916 FIXME("VERSION with vt %x\n", V_VT(&info
[i
].vData
));
917 hr
= DB_S_ERRORSOCCURRED
;
920 This
->version
= V_UI4(&info
[i
].vData
);
924 FIXME("Unhandled info type %d (vt %x)\n", info
[i
].eInfoType
, V_VT(&info
[i
].vData
));
930 static const struct IDCInfoVtbl dcinfo_vtbl
=
932 dcinfo_QueryInterface
,
939 HRESULT
create_oledb_convert(IUnknown
*outer
, void **obj
)
943 TRACE("(%p, %p)\n", outer
, obj
);
947 if(outer
) return CLASS_E_NOAGGREGATION
;
949 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
950 if(!This
) return E_OUTOFMEMORY
;
952 This
->lpVtbl
= &convert_vtbl
;
953 This
->lpDCInfoVtbl
= &dcinfo_vtbl
;
955 This
->version
= 0x110;
957 *obj
= &This
->lpVtbl
;