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)
132 case DBTYPE_BYREF | DBTYPE_WSTR:
135 FIXME("Unhandled type %04x\n", type);
140 static HRESULT WINAPI convert_DataConvert(IDataConvert* iface,
141 DBTYPE src_type, DBTYPE dst_type,
142 DBLENGTH src_len, DBLENGTH *dst_len,
143 void *src, void *dst,
144 DBLENGTH dst_max_len,
145 DBSTATUS src_status, DBSTATUS *dst_status,
146 BYTE precision, BYTE scale,
149 convert *This = impl_from_IDataConvert(iface);
152 TRACE("(%p)->(%d, %d, %d, %p, %p, %p, %d, %d, %p, %d, %d, %x)\n", This,
153 src_type, dst_type, src_len, dst_len, src, dst, dst_max_len,
154 src_status, dst_status, precision, scale, flags);
156 *dst_len = get_length(dst_type);
157 *dst_status = DBSTATUS_E_BADACCESSOR;
159 if(IDataConvert_CanConvert(iface, src_type, dst_type) != S_OK)
161 return DB_E_UNSUPPORTEDCONVERSION;
164 if(src_type == DBTYPE_STR)
169 if(flags & DBDATACONVERT_LENGTHFROMNTS)
170 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0) - 1;
172 len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
173 b = SysAllocStringLen(NULL, len);
174 if(!b) return E_OUTOFMEMORY;
175 if(flags & DBDATACONVERT_LENGTHFROMNTS)
176 MultiByteToWideChar(CP_ACP, 0, src, -1, b, len + 1);
178 MultiByteToWideChar(CP_ACP, 0, src, src_len, b, len);
180 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
181 &b, dst, dst_max_len, src_status, dst_status,
182 precision, scale, flags);
188 if(src_type == DBTYPE_WSTR)
192 if(flags & DBDATACONVERT_LENGTHFROMNTS)
193 b = SysAllocString(src);
195 b = SysAllocStringLen(src, src_len / 2);
196 if(!b) return E_OUTOFMEMORY;
197 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
198 &b, dst, dst_max_len, src_status, dst_status,
199 precision, scale, flags);
208 signed short *d = dst;
211 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
212 case DBTYPE_I2: *d = *(signed short*)src; hr = S_OK; break;
213 case DBTYPE_I4: hr = VarI2FromI4(*(signed int*)src, d); break;
214 case DBTYPE_R4: hr = VarI2FromR4(*(FLOAT*)src, d); break;
215 case DBTYPE_R8: hr = VarI2FromR8(*(double*)src, d); break;
216 case DBTYPE_CY: hr = VarI2FromCy(*(CY*)src, d); break;
217 case DBTYPE_DATE: hr = VarI2FromDate(*(DATE*)src, d); break;
218 case DBTYPE_BSTR: hr = VarI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
219 case DBTYPE_BOOL: hr = VarI2FromBool(*(VARIANT_BOOL*)src, d); break;
220 case DBTYPE_DECIMAL: hr = VarI2FromDec((DECIMAL*)src, d); break;
221 case DBTYPE_I1: hr = VarI2FromI1(*(signed char*)src, d); break;
222 case DBTYPE_UI1: hr = VarI2FromUI1(*(BYTE*)src, d); break;
223 case DBTYPE_UI2: hr = VarI2FromUI2(*(WORD*)src, d); break;
224 case DBTYPE_UI4: hr = VarI2FromUI4(*(DWORD*)src, d); break;
225 case DBTYPE_I8: hr = VarI2FromI8(*(LONGLONG*)src, d); break;
226 case DBTYPE_UI8: hr = VarI2FromUI8(*(ULONGLONG*)src, d); break;
227 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type); return E_NOTIMPL;
237 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
238 case DBTYPE_I2: hr = VarI4FromI2(*(signed short*)src, d); break;
239 case DBTYPE_I4: *d = *(signed int*)src; hr = S_OK; break;
240 case DBTYPE_R4: hr = VarI4FromR4(*(FLOAT*)src, d); break;
241 case DBTYPE_R8: hr = VarI4FromR8(*(double*)src, d); break;
242 case DBTYPE_CY: hr = VarI4FromCy(*(CY*)src, d); break;
243 case DBTYPE_DATE: hr = VarI4FromDate(*(DATE*)src, d); break;
244 case DBTYPE_BSTR: hr = VarI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
245 case DBTYPE_BOOL: hr = VarI4FromBool(*(VARIANT_BOOL*)src, d); break;
246 case DBTYPE_DECIMAL: hr = VarI4FromDec((DECIMAL*)src, d); break;
247 case DBTYPE_I1: hr = VarI4FromI1(*(signed char*)src, d); break;
248 case DBTYPE_UI1: hr = VarI4FromUI1(*(BYTE*)src, d); break;
249 case DBTYPE_UI2: hr = VarI4FromUI2(*(WORD*)src, d); break;
250 case DBTYPE_UI4: hr = VarI4FromUI4(*(DWORD*)src, d); break;
251 case DBTYPE_I8: hr = VarI4FromI8(*(LONGLONG*)src, d); break;
252 case DBTYPE_UI8: hr = VarI4FromUI8(*(ULONGLONG*)src, d); break;
253 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type); return E_NOTIMPL;
263 case DBTYPE_EMPTY: *d = SysAllocStringLen(NULL, 0); hr = *d ? S_OK : E_OUTOFMEMORY; break;
264 case DBTYPE_I2: hr = VarBstrFromI2(*(signed short*)src, LOCALE_USER_DEFAULT, 0, d); break;
265 case DBTYPE_I4: hr = VarBstrFromI4(*(signed int*)src, LOCALE_USER_DEFAULT, 0, d); break;
266 case DBTYPE_R4: hr = VarBstrFromR4(*(FLOAT*)src, LOCALE_USER_DEFAULT, 0, d); break;
267 case DBTYPE_R8: hr = VarBstrFromR8(*(double*)src, LOCALE_USER_DEFAULT, 0, d); break;
268 case DBTYPE_CY: hr = VarBstrFromCy(*(CY*)src, LOCALE_USER_DEFAULT, 0, d); break;
269 case DBTYPE_DATE: hr = VarBstrFromDate(*(DATE*)src, LOCALE_USER_DEFAULT, 0, d); break;
270 case DBTYPE_BSTR: *d = SysAllocStringLen(*(BSTR*)src, SysStringLen(*(BSTR*)src)); hr = *d ? S_OK : E_OUTOFMEMORY; break;
271 case DBTYPE_BOOL: hr = VarBstrFromBool(*(VARIANT_BOOL*)src, LOCALE_USER_DEFAULT, 0, d); break;
272 case DBTYPE_DECIMAL: hr = VarBstrFromDec((DECIMAL*)src, LOCALE_USER_DEFAULT, 0, d); break;
273 case DBTYPE_I1: hr = VarBstrFromI1(*(signed char*)src, LOCALE_USER_DEFAULT, 0, d); break;
274 case DBTYPE_UI1: hr = VarBstrFromUI1(*(BYTE*)src, LOCALE_USER_DEFAULT, 0, d); break;
275 case DBTYPE_UI2: hr = VarBstrFromUI2(*(WORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
276 case DBTYPE_UI4: hr = VarBstrFromUI4(*(DWORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
277 case DBTYPE_I8: hr = VarBstrFromI8(*(LONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
278 case DBTYPE_UI8: hr = VarBstrFromUI8(*(ULONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
279 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type); return E_NOTIMPL;
289 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
290 src, &b, sizeof(BSTR), src_status, dst_status,
291 precision, scale, flags);
292 if(hr != S_OK) return hr;
293 bstr_len = SysStringLen(b);
294 *dst_len = bstr_len * sizeof(WCHAR); /* Doesn't include size for '\0' */
295 *dst_status = DBSTATUS_S_OK;
296 bytes_to_copy = min(*dst_len + sizeof(WCHAR), dst_max_len);
299 if(bytes_to_copy >= sizeof(WCHAR))
301 memcpy(dst, b, bytes_to_copy - sizeof(WCHAR));
302 *((WCHAR*)dst + bytes_to_copy / sizeof(WCHAR) - 1) = 0;
303 if(bytes_to_copy < *dst_len + sizeof(WCHAR))
304 *dst_status = DBSTATUS_S_TRUNCATED;
308 *dst_status = DBSTATUS_E_DATAOVERFLOW;
309 hr = DB_E_ERRORSOCCURRED;
316 case DBTYPE_BYREF | DBTYPE_WSTR:
321 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
322 src, &b, sizeof(BSTR), src_status, dst_status,
323 precision, scale, flags);
324 if(hr != S_OK) return hr;
326 bstr_len = SysStringLen(b) * sizeof(WCHAR);
327 *dst_len = bstr_len; /* Doesn't include size for '\0' */
329 *d = CoTaskMemAlloc(bstr_len + sizeof(WCHAR));
330 if(*d) memcpy(*d, b, bstr_len + sizeof(WCHAR));
331 else hr = E_OUTOFMEMORY;
337 FIXME("Unimplemented conversion %04x -> %04x\n", src_type, dst_type);
342 if(hr == DISP_E_OVERFLOW)
344 *dst_status = DBSTATUS_E_DATAOVERFLOW;
345 hr = DB_E_ERRORSOCCURRED;
348 *dst_status = DBSTATUS_S_OK;
353 static inline WORD get_dbtype_class(DBTYPE type)
380 case DBTYPE_DBTIMESTAMP:
381 return DBTYPE_DBDATE;
386 /* Many src types will convert to this group of dst types */
387 static inline BOOL common_class(WORD dst_class)
407 static inline BOOL array_type(DBTYPE type)
409 return (type >= DBTYPE_I2 && type <= DBTYPE_UI4);
412 static HRESULT WINAPI convert_CanConvert(IDataConvert* iface,
413 DBTYPE src_type, DBTYPE dst_type)
415 convert *This = impl_from_IDataConvert(iface);
416 DBTYPE src_base_type = src_type & 0x1ff;
417 DBTYPE dst_base_type = dst_type & 0x1ff;
418 WORD dst_class = get_dbtype_class(dst_base_type);
420 TRACE("(%p)->(%d, %d)\n", This, src_type, dst_type);
422 if(src_type & DBTYPE_VECTOR || dst_type & DBTYPE_VECTOR) return S_FALSE;
424 if(src_type & DBTYPE_ARRAY)
426 if(!array_type(src_base_type)) return S_FALSE;
427 if(dst_type & DBTYPE_ARRAY)
429 if(src_type == dst_type) return S_OK;
432 if(dst_type == DBTYPE_VARIANT) return S_OK;
436 if(dst_type & DBTYPE_ARRAY)
438 if(!array_type(dst_base_type)) return S_FALSE;
439 if(src_type == DBTYPE_IDISPATCH || src_type == DBTYPE_VARIANT) return S_OK;
443 if(dst_type & DBTYPE_BYREF)
444 if(dst_base_type != DBTYPE_BYTES && dst_base_type != DBTYPE_STR && dst_base_type != DBTYPE_WSTR)
447 switch(get_dbtype_class(src_base_type))
450 if(common_class(dst_class)) return S_OK;
457 if(dst_base_type == DBTYPE_DBTIMESTAMP) return S_OK;
462 switch(dst_base_type)
465 case DBTYPE_VARIANT: return S_OK;
466 default: return S_FALSE;
470 if(dst_base_type == DBTYPE_BYTES) return S_OK;
473 if(dst_base_type == DBTYPE_DATE) return S_OK;
476 if(common_class(dst_class)) return S_OK;
477 if(dst_class == DBTYPE_DBDATE) return S_OK;
481 if(dst_base_type == DBTYPE_DATE) return S_OK;
484 if(common_class(dst_class)) return S_OK;
488 if(common_class(dst_class)) return S_OK;
489 if(dst_base_type == DBTYPE_BYTES) return S_OK;
506 default: return S_FALSE;
509 case DBTYPE_IDISPATCH:
511 switch(dst_base_type)
513 case DBTYPE_IDISPATCH:
515 case DBTYPE_IUNKNOWN:
520 if(common_class(dst_class)) return S_OK;
528 default: return S_FALSE;
532 switch(dst_base_type)
539 default: return S_FALSE;
542 case DBTYPE_IUNKNOWN:
543 switch(dst_base_type)
547 case DBTYPE_IDISPATCH:
549 case DBTYPE_IUNKNOWN:
551 default: return S_FALSE;
555 if(dst_class == DBTYPE_I4 || dst_class == DBTYPE_I8) return S_OK;
567 default: return S_FALSE;
580 default: return S_FALSE;
587 static HRESULT WINAPI convert_GetConversionSize(IDataConvert* iface,
588 DBTYPE wSrcType, DBTYPE wDstType,
589 DBLENGTH *pcbSrcLength, DBLENGTH *pcbDstLength,
592 convert *This = impl_from_IDataConvert(iface);
593 FIXME("(%p)->(%d, %d, %p, %p, %p): stub\n", This, wSrcType, wDstType, pcbSrcLength, pcbDstLength, pSrc);
598 static const struct IDataConvertVtbl convert_vtbl =
600 convert_QueryInterface,
605 convert_GetConversionSize
608 static HRESULT WINAPI dcinfo_QueryInterface(IDCInfo* iface, REFIID riid, void **obj)
610 convert *This = impl_from_IDCInfo(iface);
612 return IDataConvert_QueryInterface((IDataConvert *)This, riid, obj);
615 static ULONG WINAPI dcinfo_AddRef(IDCInfo* iface)
617 convert *This = impl_from_IDCInfo(iface);
619 return IDataConvert_AddRef((IDataConvert *)This);
622 static ULONG WINAPI dcinfo_Release(IDCInfo* iface)
624 convert *This = impl_from_IDCInfo(iface);
626 return IDataConvert_Release((IDataConvert *)This);
629 static HRESULT WINAPI dcinfo_GetInfo(IDCInfo *iface, ULONG num, DCINFOTYPE types[], DCINFO **info_ptr)
631 convert *This = impl_from_IDCInfo(iface);
635 TRACE("(%p)->(%d, %p, %p)\n", This, num, types, info_ptr);
637 *info_ptr = infos = CoTaskMemAlloc(num * sizeof(*infos));
638 if(!infos) return E_OUTOFMEMORY;
640 for(i = 0; i < num; i++)
642 infos[i].eInfoType = types[i];
643 VariantInit(&infos[i].vData);
647 case DCINFOTYPE_VERSION:
648 V_VT(&infos[i].vData) = VT_UI4;
649 V_UI4(&infos[i].vData) = This->version;
657 static HRESULT WINAPI dcinfo_SetInfo(IDCInfo* iface, ULONG num, DCINFO info[])
659 convert *This = impl_from_IDCInfo(iface);
663 TRACE("(%p)->(%d, %p)\n", This, num, info);
665 for(i = 0; i < num; i++)
667 switch(info[i].eInfoType)
669 case DCINFOTYPE_VERSION:
670 if(V_VT(&info[i].vData) != VT_UI4)
672 FIXME("VERSION with vt %x\n", V_VT(&info[i].vData));
673 hr = DB_S_ERRORSOCCURRED;
676 This->version = V_UI4(&info[i].vData);
680 FIXME("Unhandled info type %d (vt %x)\n", info[i].eInfoType, V_VT(&info[i].vData));
686 static const struct IDCInfoVtbl dcinfo_vtbl =
688 dcinfo_QueryInterface,
695 HRESULT create_oledb_convert(IUnknown *outer, void **obj)
699 TRACE("(%p, %p)\n", outer, obj);
703 if(outer) return CLASS_E_NOAGGREGATION;
705 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
706 if(!This) return E_OUTOFMEMORY;
708 This->lpVtbl = &convert_vtbl;
709 This->lpDCInfoVtbl = &dcinfo_vtbl;
711 This->version = 0x110;
713 *obj = &This->lpVtbl;