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 FIXME("Unhandled type %04x\n", type);
137 static HRESULT WINAPI convert_DataConvert(IDataConvert* iface,
138 DBTYPE src_type, DBTYPE dst_type,
139 DBLENGTH src_len, DBLENGTH *dst_len,
140 void *src, void *dst,
141 DBLENGTH dst_max_len,
142 DBSTATUS src_status, DBSTATUS *dst_status,
143 BYTE precision, BYTE scale,
146 convert *This = impl_from_IDataConvert(iface);
149 TRACE("(%p)->(%d, %d, %d, %p, %p, %p, %d, %d, %p, %d, %d, %x)\n", This,
150 src_type, dst_type, src_len, dst_len, src, dst, dst_max_len,
151 src_status, dst_status, precision, scale, flags);
153 *dst_len = get_length(dst_type);
154 *dst_status = DBSTATUS_E_BADACCESSOR;
156 if(IDataConvert_CanConvert(iface, src_type, dst_type) != S_OK)
158 return DB_E_UNSUPPORTEDCONVERSION;
161 if(src_type == DBTYPE_STR)
166 if(flags & DBDATACONVERT_LENGTHFROMNTS)
167 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0) - 1;
169 len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
170 b = SysAllocStringLen(NULL, len);
171 if(!b) return E_OUTOFMEMORY;
172 if(flags & DBDATACONVERT_LENGTHFROMNTS)
173 MultiByteToWideChar(CP_ACP, 0, src, -1, b, len + 1);
175 MultiByteToWideChar(CP_ACP, 0, src, src_len, b, len);
177 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
178 &b, dst, dst_max_len, src_status, dst_status,
179 precision, scale, flags);
185 if(src_type == DBTYPE_WSTR)
189 if(flags & DBDATACONVERT_LENGTHFROMNTS)
190 b = SysAllocString(src);
192 b = SysAllocStringLen(src, src_len / 2);
193 if(!b) return E_OUTOFMEMORY;
194 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
195 &b, dst, dst_max_len, src_status, dst_status,
196 precision, scale, flags);
205 signed short *d = dst;
208 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
209 case DBTYPE_I2: *d = *(signed short*)src; hr = S_OK; break;
210 case DBTYPE_I4: hr = VarI2FromI4(*(signed int*)src, d); break;
211 case DBTYPE_R4: hr = VarI2FromR4(*(FLOAT*)src, d); break;
212 case DBTYPE_R8: hr = VarI2FromR8(*(double*)src, d); break;
213 case DBTYPE_CY: hr = VarI2FromCy(*(CY*)src, d); break;
214 case DBTYPE_DATE: hr = VarI2FromDate(*(DATE*)src, d); break;
215 case DBTYPE_BSTR: hr = VarI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
216 case DBTYPE_BOOL: hr = VarI2FromBool(*(VARIANT_BOOL*)src, d); break;
217 case DBTYPE_DECIMAL: hr = VarI2FromDec((DECIMAL*)src, d); break;
218 case DBTYPE_I1: hr = VarI2FromI1(*(signed char*)src, d); break;
219 case DBTYPE_UI1: hr = VarI2FromUI1(*(BYTE*)src, d); break;
220 case DBTYPE_UI2: hr = VarI2FromUI2(*(WORD*)src, d); break;
221 case DBTYPE_UI4: hr = VarI2FromUI4(*(DWORD*)src, d); break;
222 case DBTYPE_I8: hr = VarI2FromI8(*(LONGLONG*)src, d); break;
223 case DBTYPE_UI8: hr = VarI2FromUI8(*(ULONGLONG*)src, d); break;
224 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type); return E_NOTIMPL;
234 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
235 case DBTYPE_I2: hr = VarI4FromI2(*(signed short*)src, d); break;
236 case DBTYPE_I4: *d = *(signed int*)src; hr = S_OK; break;
237 case DBTYPE_R4: hr = VarI4FromR4(*(FLOAT*)src, d); break;
238 case DBTYPE_R8: hr = VarI4FromR8(*(double*)src, d); break;
239 case DBTYPE_CY: hr = VarI4FromCy(*(CY*)src, d); break;
240 case DBTYPE_DATE: hr = VarI4FromDate(*(DATE*)src, d); break;
241 case DBTYPE_BSTR: hr = VarI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
242 case DBTYPE_BOOL: hr = VarI4FromBool(*(VARIANT_BOOL*)src, d); break;
243 case DBTYPE_DECIMAL: hr = VarI4FromDec((DECIMAL*)src, d); break;
244 case DBTYPE_I1: hr = VarI4FromI1(*(signed char*)src, d); break;
245 case DBTYPE_UI1: hr = VarI4FromUI1(*(BYTE*)src, d); break;
246 case DBTYPE_UI2: hr = VarI4FromUI2(*(WORD*)src, d); break;
247 case DBTYPE_UI4: hr = VarI4FromUI4(*(DWORD*)src, d); break;
248 case DBTYPE_I8: hr = VarI4FromI8(*(LONGLONG*)src, d); break;
249 case DBTYPE_UI8: hr = VarI4FromUI8(*(ULONGLONG*)src, d); break;
250 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type); return E_NOTIMPL;
260 case DBTYPE_EMPTY: *d = SysAllocStringLen(NULL, 0); hr = *d ? S_OK : E_OUTOFMEMORY; break;
261 case DBTYPE_I2: hr = VarBstrFromI2(*(signed short*)src, LOCALE_USER_DEFAULT, 0, d); break;
262 case DBTYPE_I4: hr = VarBstrFromI4(*(signed int*)src, LOCALE_USER_DEFAULT, 0, d); break;
263 case DBTYPE_R4: hr = VarBstrFromR4(*(FLOAT*)src, LOCALE_USER_DEFAULT, 0, d); break;
264 case DBTYPE_R8: hr = VarBstrFromR8(*(double*)src, LOCALE_USER_DEFAULT, 0, d); break;
265 case DBTYPE_CY: hr = VarBstrFromCy(*(CY*)src, LOCALE_USER_DEFAULT, 0, d); break;
266 case DBTYPE_DATE: hr = VarBstrFromDate(*(DATE*)src, LOCALE_USER_DEFAULT, 0, d); break;
267 case DBTYPE_BSTR: *d = SysAllocStringLen(*(BSTR*)src, SysStringLen(*(BSTR*)src)); hr = *d ? S_OK : E_OUTOFMEMORY; break;
268 case DBTYPE_BOOL: hr = VarBstrFromBool(*(VARIANT_BOOL*)src, LOCALE_USER_DEFAULT, 0, d); break;
269 case DBTYPE_DECIMAL: hr = VarBstrFromDec((DECIMAL*)src, LOCALE_USER_DEFAULT, 0, d); break;
270 case DBTYPE_I1: hr = VarBstrFromI1(*(signed char*)src, LOCALE_USER_DEFAULT, 0, d); break;
271 case DBTYPE_UI1: hr = VarBstrFromUI1(*(BYTE*)src, LOCALE_USER_DEFAULT, 0, d); break;
272 case DBTYPE_UI2: hr = VarBstrFromUI2(*(WORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
273 case DBTYPE_UI4: hr = VarBstrFromUI4(*(DWORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
274 case DBTYPE_I8: hr = VarBstrFromI8(*(LONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
275 case DBTYPE_UI8: hr = VarBstrFromUI8(*(ULONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
276 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type); return E_NOTIMPL;
281 FIXME("Unimplemented conversion %04x -> %04x\n", src_type, dst_type);
286 if(hr == DISP_E_OVERFLOW)
288 *dst_status = DBSTATUS_E_DATAOVERFLOW;
289 hr = DB_E_ERRORSOCCURRED;
292 *dst_status = DBSTATUS_S_OK;
297 static inline WORD get_dbtype_class(DBTYPE type)
324 case DBTYPE_DBTIMESTAMP:
325 return DBTYPE_DBDATE;
330 /* Many src types will convert to this group of dst types */
331 static inline BOOL common_class(WORD dst_class)
351 static inline BOOL array_type(DBTYPE type)
353 return (type >= DBTYPE_I2 && type <= DBTYPE_UI4);
356 static HRESULT WINAPI convert_CanConvert(IDataConvert* iface,
357 DBTYPE src_type, DBTYPE dst_type)
359 convert *This = impl_from_IDataConvert(iface);
360 DBTYPE src_base_type = src_type & 0x1ff;
361 DBTYPE dst_base_type = dst_type & 0x1ff;
362 WORD dst_class = get_dbtype_class(dst_base_type);
364 TRACE("(%p)->(%d, %d)\n", This, src_type, dst_type);
366 if(src_type & DBTYPE_VECTOR || dst_type & DBTYPE_VECTOR) return S_FALSE;
368 if(src_type & DBTYPE_ARRAY)
370 if(!array_type(src_base_type)) return S_FALSE;
371 if(dst_type & DBTYPE_ARRAY)
373 if(src_type == dst_type) return S_OK;
376 if(dst_type == DBTYPE_VARIANT) return S_OK;
380 if(dst_type & DBTYPE_ARRAY)
382 if(!array_type(dst_base_type)) return S_FALSE;
383 if(src_type == DBTYPE_IDISPATCH || src_type == DBTYPE_VARIANT) return S_OK;
387 if(dst_type & DBTYPE_BYREF)
388 if(dst_base_type != DBTYPE_BYTES && dst_base_type != DBTYPE_STR && dst_base_type != DBTYPE_WSTR)
391 switch(get_dbtype_class(src_base_type))
394 if(common_class(dst_class)) return S_OK;
401 if(dst_base_type == DBTYPE_DBTIMESTAMP) return S_OK;
406 switch(dst_base_type)
409 case DBTYPE_VARIANT: return S_OK;
410 default: return S_FALSE;
414 if(dst_base_type == DBTYPE_BYTES) return S_OK;
417 if(dst_base_type == DBTYPE_DATE) return S_OK;
420 if(common_class(dst_class)) return S_OK;
421 if(dst_class == DBTYPE_DBDATE) return S_OK;
425 if(dst_base_type == DBTYPE_DATE) return S_OK;
428 if(common_class(dst_class)) return S_OK;
432 if(common_class(dst_class)) return S_OK;
433 if(dst_base_type == DBTYPE_BYTES) return S_OK;
450 default: return S_FALSE;
453 case DBTYPE_IDISPATCH:
455 switch(dst_base_type)
457 case DBTYPE_IDISPATCH:
459 case DBTYPE_IUNKNOWN:
464 if(common_class(dst_class)) return S_OK;
472 default: return S_FALSE;
476 switch(dst_base_type)
483 default: return S_FALSE;
486 case DBTYPE_IUNKNOWN:
487 switch(dst_base_type)
491 case DBTYPE_IDISPATCH:
493 case DBTYPE_IUNKNOWN:
495 default: return S_FALSE;
499 if(dst_class == DBTYPE_I4 || dst_class == DBTYPE_I8) return S_OK;
511 default: return S_FALSE;
524 default: return S_FALSE;
531 static HRESULT WINAPI convert_GetConversionSize(IDataConvert* iface,
532 DBTYPE wSrcType, DBTYPE wDstType,
533 DBLENGTH *pcbSrcLength, DBLENGTH *pcbDstLength,
536 convert *This = impl_from_IDataConvert(iface);
537 FIXME("(%p)->(%d, %d, %p, %p, %p): stub\n", This, wSrcType, wDstType, pcbSrcLength, pcbDstLength, pSrc);
542 static const struct IDataConvertVtbl convert_vtbl =
544 convert_QueryInterface,
549 convert_GetConversionSize
552 static HRESULT WINAPI dcinfo_QueryInterface(IDCInfo* iface, REFIID riid, void **obj)
554 convert *This = impl_from_IDCInfo(iface);
556 return IDataConvert_QueryInterface((IDataConvert *)This, riid, obj);
559 static ULONG WINAPI dcinfo_AddRef(IDCInfo* iface)
561 convert *This = impl_from_IDCInfo(iface);
563 return IDataConvert_AddRef((IDataConvert *)This);
566 static ULONG WINAPI dcinfo_Release(IDCInfo* iface)
568 convert *This = impl_from_IDCInfo(iface);
570 return IDataConvert_Release((IDataConvert *)This);
573 static HRESULT WINAPI dcinfo_GetInfo(IDCInfo *iface, ULONG num, DCINFOTYPE types[], DCINFO **info_ptr)
575 convert *This = impl_from_IDCInfo(iface);
579 TRACE("(%p)->(%d, %p, %p)\n", This, num, types, info_ptr);
581 *info_ptr = infos = CoTaskMemAlloc(num * sizeof(*infos));
582 if(!infos) return E_OUTOFMEMORY;
584 for(i = 0; i < num; i++)
586 infos[i].eInfoType = types[i];
587 VariantInit(&infos[i].vData);
591 case DCINFOTYPE_VERSION:
592 V_VT(&infos[i].vData) = VT_UI4;
593 V_UI4(&infos[i].vData) = This->version;
601 static HRESULT WINAPI dcinfo_SetInfo(IDCInfo* iface, ULONG num, DCINFO info[])
603 convert *This = impl_from_IDCInfo(iface);
607 TRACE("(%p)->(%d, %p)\n", This, num, info);
609 for(i = 0; i < num; i++)
611 switch(info[i].eInfoType)
613 case DCINFOTYPE_VERSION:
614 if(V_VT(&info[i].vData) != VT_UI4)
616 FIXME("VERSION with vt %x\n", V_VT(&info[i].vData));
617 hr = DB_S_ERRORSOCCURRED;
620 This->version = V_UI4(&info[i].vData);
624 FIXME("Unhandled info type %d (vt %x)\n", info[i].eInfoType, V_VT(&info[i].vData));
630 static const struct IDCInfoVtbl dcinfo_vtbl =
632 dcinfo_QueryInterface,
639 HRESULT create_oledb_convert(IUnknown *outer, void **obj)
643 TRACE("(%p, %p)\n", outer, obj);
647 if(outer) return CLASS_E_NOAGGREGATION;
649 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
650 if(!This) return E_OUTOFMEMORY;
652 This->lpVtbl = &convert_vtbl;
653 This->lpDCInfoVtbl = &dcinfo_vtbl;
655 This->version = 0x110;
657 *obj = &This->lpVtbl;