d3d9: Fix DrawIndexedPrimitiveUP with non-zero min_vertex_idx.
[wine.git] / dlls / oledb32 / convert.c
blob0a1518d9a92d4e03b4fe624b3ea3fe5ec4be5540
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
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "ole2.h"
28 #include "msdadc.h"
29 #include "oledberr.h"
31 #include "oledb_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
37 typedef struct
39 IDataConvert IDataConvert_iface;
40 IDCInfo IDCInfo_iface;
42 LONG ref;
44 UINT version; /* Set by IDCInfo_SetInfo */
45 } convert;
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,
58 REFIID riid,
59 void **obj)
61 convert *This = impl_from_IDataConvert(iface);
62 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
64 *obj = NULL;
66 if(IsEqualIID(riid, &IID_IUnknown) ||
67 IsEqualIID(riid, &IID_IDataConvert))
69 *obj = iface;
71 else if(IsEqualIID(riid, &IID_IDCInfo))
73 *obj = &This->IDCInfo_iface;
75 else
77 FIXME("interface %s not implemented\n", debugstr_guid(riid));
78 return E_NOINTERFACE;
81 IDataConvert_AddRef(iface);
82 return S_OK;
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);
98 LONG ref;
100 TRACE("(%p)\n", This);
102 ref = InterlockedDecrement(&This->ref);
103 if(ref == 0)
104 heap_free(This);
106 return ref;
109 static int get_length(DBTYPE type)
111 switch(type)
113 case DBTYPE_I1:
114 case DBTYPE_UI1:
115 return 1;
116 case DBTYPE_I2:
117 case DBTYPE_UI2:
118 return 2;
119 case DBTYPE_BOOL:
120 return sizeof(VARIANT_BOOL);
121 case DBTYPE_I4:
122 case DBTYPE_UI4:
123 case DBTYPE_R4:
124 return 4;
125 case DBTYPE_I8:
126 case DBTYPE_UI8:
127 case DBTYPE_R8:
128 case DBTYPE_DATE:
129 return 8;
130 case DBTYPE_DBDATE:
131 return sizeof(DBDATE);
132 case DBTYPE_DBTIMESTAMP:
133 return sizeof(DBTIMESTAMP);
134 case DBTYPE_CY:
135 return sizeof(CY);
136 case DBTYPE_BSTR:
137 return sizeof(BSTR);
138 case DBTYPE_FILETIME:
139 return sizeof(FILETIME);
140 case DBTYPE_GUID:
141 return sizeof(GUID);
142 case DBTYPE_BYTES:
143 case DBTYPE_WSTR:
144 case DBTYPE_STR:
145 case DBTYPE_BYREF | DBTYPE_WSTR:
146 return 0;
147 case DBTYPE_VARIANT:
148 return sizeof(VARIANT);
149 default:
150 FIXME("Unhandled type %04x\n", type);
151 return 0;
155 static HRESULT WINAPI convert_DataConvert(IDataConvert* iface,
156 DBTYPE src_type, DBTYPE dst_type,
157 DBLENGTH src_len, DBLENGTH *dst_len,
158 void *src, void *dst,
159 DBLENGTH dst_max_len,
160 DBSTATUS src_status, DBSTATUS *dst_status,
161 BYTE precision, BYTE scale,
162 DBDATACONVERT flags)
164 convert *This = impl_from_IDataConvert(iface);
165 DBLENGTH dst_len_loc;
166 DBSTATUS dst_status_loc;
167 VARIANT tmp;
168 HRESULT hr;
170 TRACE("(%p)->(%d, %d, %ld, %p, %p, %p, %ld, %d, %p, %d, %d, %x)\n", This,
171 src_type, dst_type, src_len, dst_len, src, dst, dst_max_len,
172 src_status, dst_status, precision, scale, flags);
174 if (!dst_len) dst_len = &dst_len_loc;
175 if (!dst_status) dst_status = &dst_status_loc;
177 *dst_status = DBSTATUS_E_BADACCESSOR;
179 if(src_status == DBSTATUS_S_ISNULL)
181 *dst_status = DBSTATUS_S_ISNULL;
182 *dst_len = 0;
183 return S_OK;
186 if(src_type == DBTYPE_VARIANT && V_VT((VARIANT*)src) == VT_NULL)
188 if(dst_type == DBTYPE_VARIANT)
189 *dst_len = sizeof(VARIANT);
191 *dst_status = DBSTATUS_S_ISNULL;
192 return S_OK;
195 if(IDataConvert_CanConvert(iface, src_type, dst_type) != S_OK)
197 return DB_E_UNSUPPORTEDCONVERSION;
200 if(dst == NULL && get_length(dst_type) != 0)
202 *dst_len = get_length(src_type);
203 *dst_status = DBSTATUS_S_OK;
204 return S_OK;
207 if(src_type == DBTYPE_STR)
209 BSTR b;
210 DWORD len;
212 if(flags & DBDATACONVERT_LENGTHFROMNTS)
213 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0) - 1;
214 else
215 len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
216 b = SysAllocStringLen(NULL, len);
217 if(!b) return E_OUTOFMEMORY;
218 if(flags & DBDATACONVERT_LENGTHFROMNTS)
219 MultiByteToWideChar(CP_ACP, 0, src, -1, b, len + 1);
220 else
221 MultiByteToWideChar(CP_ACP, 0, src, src_len, b, len);
223 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
224 &b, dst, dst_max_len, src_status, dst_status,
225 precision, scale, flags);
227 SysFreeString(b);
228 return hr;
231 if(src_type == DBTYPE_WSTR)
233 BSTR b;
235 if(flags & DBDATACONVERT_LENGTHFROMNTS)
236 b = SysAllocString(src);
237 else
238 b = SysAllocStringLen(src, src_len / 2);
239 if(!b) return E_OUTOFMEMORY;
240 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
241 &b, dst, dst_max_len, src_status, dst_status,
242 precision, scale, flags);
243 SysFreeString(b);
244 return hr;
247 switch(dst_type)
249 case DBTYPE_I2:
251 signed short *d = dst;
252 switch(src_type)
254 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
255 case DBTYPE_I2: *d = *(signed short*)src; hr = S_OK; break;
256 case DBTYPE_I4: hr = VarI2FromI4(*(signed int*)src, d); break;
257 case DBTYPE_R4: hr = VarI2FromR4(*(FLOAT*)src, d); break;
258 case DBTYPE_R8: hr = VarI2FromR8(*(double*)src, d); break;
259 case DBTYPE_CY: hr = VarI2FromCy(*(CY*)src, d); break;
260 case DBTYPE_DATE: hr = VarI2FromDate(*(DATE*)src, d); break;
261 case DBTYPE_BSTR: hr = VarI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
262 case DBTYPE_BOOL: hr = VarI2FromBool(*(VARIANT_BOOL*)src, d); break;
263 case DBTYPE_DECIMAL: hr = VarI2FromDec((DECIMAL*)src, d); break;
264 case DBTYPE_I1: hr = VarI2FromI1(*(signed char*)src, d); break;
265 case DBTYPE_UI1: hr = VarI2FromUI1(*(BYTE*)src, d); break;
266 case DBTYPE_UI2: hr = VarI2FromUI2(*(WORD*)src, d); break;
267 case DBTYPE_UI4: hr = VarI2FromUI4(*(DWORD*)src, d); break;
268 case DBTYPE_I8: hr = VarI2FromI8(*(LONGLONG*)src, d); break;
269 case DBTYPE_UI8: hr = VarI2FromUI8(*(ULONGLONG*)src, d); break;
270 case DBTYPE_VARIANT:
271 VariantInit(&tmp);
272 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_I2)) == S_OK)
273 *d = V_I2(&tmp);
274 break;
275 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type); return E_NOTIMPL;
277 break;
280 case DBTYPE_I4:
282 signed int *d = dst;
283 switch(src_type)
285 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
286 case DBTYPE_I2: hr = VarI4FromI2(*(signed short*)src, d); break;
287 case DBTYPE_I4: *d = *(signed int*)src; hr = S_OK; break;
288 case DBTYPE_R4: hr = VarI4FromR4(*(FLOAT*)src, d); break;
289 case DBTYPE_R8: hr = VarI4FromR8(*(double*)src, d); break;
290 case DBTYPE_CY: hr = VarI4FromCy(*(CY*)src, d); break;
291 case DBTYPE_DATE: hr = VarI4FromDate(*(DATE*)src, d); break;
292 case DBTYPE_BSTR: hr = VarI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
293 case DBTYPE_BOOL: hr = VarI4FromBool(*(VARIANT_BOOL*)src, d); break;
294 case DBTYPE_DECIMAL: hr = VarI4FromDec((DECIMAL*)src, d); break;
295 case DBTYPE_I1: hr = VarI4FromI1(*(signed char*)src, d); break;
296 case DBTYPE_UI1: hr = VarI4FromUI1(*(BYTE*)src, d); break;
297 case DBTYPE_UI2: hr = VarI4FromUI2(*(WORD*)src, d); break;
298 case DBTYPE_UI4: hr = VarI4FromUI4(*(DWORD*)src, d); break;
299 case DBTYPE_I8: hr = VarI4FromI8(*(LONGLONG*)src, d); break;
300 case DBTYPE_UI8: hr = VarI4FromUI8(*(ULONGLONG*)src, d); break;
301 case DBTYPE_VARIANT:
302 VariantInit(&tmp);
303 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_I4)) == S_OK)
304 *d = V_I4(&tmp);
305 break;
306 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type); return E_NOTIMPL;
308 break;
311 case DBTYPE_I8:
313 LONGLONG *d = dst;
314 switch(src_type)
316 case DBTYPE_BSTR: hr = VarI8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
317 case DBTYPE_I8: *d = *(LONGLONG*)src; hr = S_OK; break;
318 default: FIXME("Unimplemented conversion %04x -> I8\n", src_type); return E_NOTIMPL;
320 break;
323 case DBTYPE_R4:
325 FLOAT *d = dst;
326 switch(src_type)
328 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
329 case DBTYPE_I2: hr = VarR4FromI2(*(signed short*)src, d); break;
330 case DBTYPE_I4: hr = VarR4FromI4(*(signed int*)src, d); break;
331 case DBTYPE_R4: *d = *(FLOAT*)src; hr = S_OK; break;
332 case DBTYPE_R8: hr = VarR4FromR8(*(double*)src, d); break;
333 case DBTYPE_CY: hr = VarR4FromCy(*(CY*)src, d); break;
334 case DBTYPE_DATE: hr = VarR4FromDate(*(DATE*)src, d); break;
335 case DBTYPE_BSTR: hr = VarR4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
336 case DBTYPE_BOOL: hr = VarR4FromBool(*(VARIANT_BOOL*)src, d); break;
337 case DBTYPE_DECIMAL: hr = VarR4FromDec((DECIMAL*)src, d); break;
338 case DBTYPE_I1: hr = VarR4FromI1(*(signed char*)src, d); break;
339 case DBTYPE_UI1: hr = VarR4FromUI1(*(BYTE*)src, d); break;
340 case DBTYPE_UI2: hr = VarR4FromUI2(*(WORD*)src, d); break;
341 case DBTYPE_UI4: hr = VarR4FromUI4(*(DWORD*)src, d); break;
342 case DBTYPE_I8: hr = VarR4FromI8(*(LONGLONG*)src, d); break;
343 case DBTYPE_UI8: hr = VarR4FromUI8(*(ULONGLONG*)src, d); break;
344 default: FIXME("Unimplemented conversion %04x -> R4\n", src_type); return E_NOTIMPL;
346 break;
348 case DBTYPE_R8:
350 DOUBLE *d=dst;
351 switch (src_type)
353 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
354 case DBTYPE_I1: hr = VarR8FromI1(*(signed char*)src, d); break;
355 case DBTYPE_I2: hr = VarR8FromI2(*(signed short*)src, d); break;
356 case DBTYPE_I4: hr = VarR8FromI4(*(signed int*)src, d); break;
357 case DBTYPE_I8: hr = VarR8FromI8(*(LONGLONG*)src, d); break;
358 case DBTYPE_UI1: hr = VarR8FromUI1(*(BYTE*)src, d); break;
359 case DBTYPE_UI2: hr = VarR8FromUI2(*(WORD*)src, d); break;
360 case DBTYPE_UI4: hr = VarR8FromUI4(*(DWORD*)src, d); break;
361 case DBTYPE_UI8: hr = VarR8FromUI8(*(ULONGLONG*)src, d); break;
362 case DBTYPE_R4: hr = VarR8FromR4(*(FLOAT*)src, d); break;
363 case DBTYPE_R8: *d = *(DOUBLE*)src; hr = S_OK; break;
364 case DBTYPE_CY: hr = VarR8FromCy(*(CY*)src, d); break;
365 case DBTYPE_DATE: hr = VarR8FromDate(*(DATE*)src, d); break;
366 case DBTYPE_BSTR: hr = VarR8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
367 case DBTYPE_BOOL: hr = VarR8FromBool(*(VARIANT_BOOL*)src, d); break;
368 case DBTYPE_DECIMAL: hr = VarR8FromDec((DECIMAL*)src, d); break;
369 case DBTYPE_VARIANT:
370 VariantInit(&tmp);
371 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_R8)) == S_OK)
372 *d = V_R8(&tmp);
373 break;
374 default: FIXME("Unimplemented conversion %04x -> R8\n", src_type); return E_NOTIMPL;
376 break;
378 case DBTYPE_BOOL:
380 VARIANT_BOOL *d=dst;
381 switch (src_type)
383 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
384 case DBTYPE_I1: hr = VarBoolFromI1(*(signed char*)src, d); break;
385 case DBTYPE_I2: hr = VarBoolFromI2(*(signed short*)src, d); break;
386 case DBTYPE_I4: hr = VarBoolFromI4(*(signed int*)src, d); break;
387 case DBTYPE_I8: hr = VarBoolFromI8(*(LONGLONG*)src, d); break;
388 case DBTYPE_UI1: hr = VarBoolFromUI1(*(BYTE*)src, d); break;
389 case DBTYPE_UI2: hr = VarBoolFromUI2(*(WORD*)src, d); break;
390 case DBTYPE_UI4: hr = VarBoolFromUI4(*(DWORD*)src, d); break;
391 case DBTYPE_UI8: hr = VarBoolFromUI8(*(ULONGLONG*)src, d); break;
392 case DBTYPE_R4: hr = VarBoolFromR4(*(FLOAT*)src, d); break;
393 case DBTYPE_R8: hr = VarBoolFromR8(*(DOUBLE*)src, d); break;
394 case DBTYPE_CY: hr = VarBoolFromCy(*(CY*)src, d); break;
395 case DBTYPE_DATE: hr = VarBoolFromDate(*(DATE*)src, d); break;
396 case DBTYPE_BSTR: hr = VarBoolFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
397 case DBTYPE_BOOL: *d = *(VARIANT_BOOL*)src; hr = S_OK; break;
398 case DBTYPE_DECIMAL: hr = VarBoolFromDec((DECIMAL*)src, d); break;
399 default: FIXME("Unimplemented conversion %04x -> BOOL\n", src_type); return E_NOTIMPL;
401 break;
403 case DBTYPE_DATE:
405 DATE *d=dst;
406 switch (src_type)
408 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
409 case DBTYPE_I1: hr = VarDateFromI1(*(signed char*)src, d); break;
410 case DBTYPE_I2: hr = VarDateFromI2(*(signed short*)src, d); break;
411 case DBTYPE_I4: hr = VarDateFromI4(*(signed int*)src, d); break;
412 case DBTYPE_I8: hr = VarDateFromI8(*(LONGLONG*)src, d); break;
413 case DBTYPE_UI1: hr = VarDateFromUI1(*(BYTE*)src, d); break;
414 case DBTYPE_UI2: hr = VarDateFromUI2(*(WORD*)src, d); break;
415 case DBTYPE_UI4: hr = VarDateFromUI4(*(DWORD*)src, d); break;
416 case DBTYPE_UI8: hr = VarDateFromUI8(*(ULONGLONG*)src, d); break;
417 case DBTYPE_R4: hr = VarDateFromR4(*(FLOAT*)src, d); break;
418 case DBTYPE_R8: hr = VarDateFromR8(*(DOUBLE*)src, d); break;
419 case DBTYPE_CY: hr = VarDateFromCy(*(CY*)src, d); break;
420 case DBTYPE_DATE: *d = *(DATE*)src; hr = S_OK; break;
421 case DBTYPE_BSTR: hr = VarDateFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
422 case DBTYPE_BOOL: hr = VarDateFromBool(*(VARIANT_BOOL*)src, d); break;
423 case DBTYPE_DECIMAL: hr = VarDateFromDec((DECIMAL*)src, d); break;
424 case DBTYPE_DBTIMESTAMP:
426 SYSTEMTIME st;
427 DBTIMESTAMP *ts=(DBTIMESTAMP*)src;
429 st.wYear = ts->year;
430 st.wMonth = ts->month;
431 st.wDay = ts->day;
432 st.wHour = ts->hour;
433 st.wMinute = ts->minute;
434 st.wSecond = ts->second;
435 st.wMilliseconds = ts->fraction/1000000;
436 hr = (SystemTimeToVariantTime(&st, d) ? S_OK : E_FAIL);
437 break;
439 default: FIXME("Unimplemented conversion %04x -> DATE\n", src_type); return E_NOTIMPL;
441 break;
443 case DBTYPE_DBDATE:
445 DBDATE *d=dst;
446 switch (src_type)
448 case DBTYPE_DBDATE: memcpy(d, src, sizeof(DBDATE)); hr = S_OK; break;
449 case DBTYPE_BSTR:
451 VARIANT var;
452 BSTR s = *(WCHAR**)src;
454 VariantInit(&var);
455 V_VT(&var) = VT_BSTR;
456 V_BSTR(&var) = SysAllocString(s);
458 if ((hr = VariantChangeType(&var, &var, 0, VT_DATE)) == S_OK)
460 SYSTEMTIME st;
461 hr = (VariantTimeToSystemTime( V_DATE(&var), &st) ? S_OK : E_FAIL);
462 d->year = st.wYear;
463 d->month = st.wMonth;
464 d->day = st.wDay;
467 VariantClear(&var);
469 break;
470 case DBTYPE_VARIANT:
471 if( V_VT((VARIANT*)src) == VT_DATE)
473 SYSTEMTIME st;
474 hr = (VariantTimeToSystemTime( V_DATE((VARIANT*)src), &st) ? S_OK : E_FAIL);
475 d->year = st.wYear;
476 d->month = st.wMonth;
477 d->day = st.wDay;
479 else
481 FIXME("Unimplemented variant type %d -> DBDATE\n", V_VT((VARIANT*)src));
482 return E_NOTIMPL;
484 break;
485 default: FIXME("Unimplemented conversion %04x -> DBDATE\n", src_type); return E_NOTIMPL;
487 break;
489 case DBTYPE_DBTIMESTAMP:
491 DBTIMESTAMP *d=dst;
492 switch (src_type)
494 case DBTYPE_EMPTY: memset(d, 0, sizeof(DBTIMESTAMP)); hr = S_OK; break;
495 case DBTYPE_DBTIMESTAMP: memcpy(d, src, sizeof(DBTIMESTAMP)); hr = S_OK; break;
496 case DBTYPE_BSTR:
498 VARIANT var;
499 BSTR s = *(WCHAR**)src;
501 VariantInit(&var);
502 V_VT(&var) = VT_BSTR;
503 V_BSTR(&var) = SysAllocString(s);
505 if ((hr = VariantChangeType(&var, &var, 0, VT_DATE)) == S_OK)
507 SYSTEMTIME st;
509 hr = (VariantTimeToSystemTime( V_DATE(&var), &st) ? S_OK : E_FAIL);
510 d->year = st.wYear;
511 d->month = st.wMonth;
512 d->day = st.wDay;
513 d->hour = st.wHour;
514 d->minute = st.wMinute;
515 d->second = st.wSecond;
516 d->fraction = st.wMilliseconds * 1000000;
519 VariantClear(&var);
521 break;
522 case DBTYPE_DATE:
524 SYSTEMTIME st;
525 hr = (VariantTimeToSystemTime(*(double*)src, &st) ? S_OK : E_FAIL);
526 d->year = st.wYear;
527 d->month = st.wMonth;
528 d->day = st.wDay;
529 d->hour = st.wHour;
530 d->minute = st.wMinute;
531 d->second = st.wSecond;
532 d->fraction = st.wMilliseconds * 1000000;
533 break;
535 case DBTYPE_VARIANT:
536 if( V_VT((VARIANT*)src) == VT_DATE)
538 SYSTEMTIME st;
539 hr = (VariantTimeToSystemTime( V_DATE((VARIANT*)src), &st) ? S_OK : E_FAIL);
540 d->year = st.wYear;
541 d->month = st.wMonth;
542 d->day = st.wDay;
543 d->hour = st.wHour;
544 d->minute = st.wMinute;
545 d->second = st.wSecond;
546 d->fraction = st.wMilliseconds * 1000000;
548 else
550 FIXME("Unimplemented variant type %d -> DBTIMESTAMP\n", V_VT((VARIANT*)src));
551 return E_NOTIMPL;
553 break;
554 default: FIXME("Unimplemented conversion %04x -> DBTIMESTAMP\n", src_type); return E_NOTIMPL;
556 break;
559 case DBTYPE_CY:
561 CY *d = dst;
562 switch(src_type)
564 case DBTYPE_EMPTY: d->int64 = 0; hr = S_OK; break;
565 case DBTYPE_I2: hr = VarCyFromI2(*(signed short*)src, d); break;
566 case DBTYPE_I4: hr = VarCyFromI4(*(signed int*)src, d); break;
567 case DBTYPE_R4: hr = VarCyFromR4(*(FLOAT*)src, d); break;
568 case DBTYPE_R8: hr = VarCyFromR8(*(double*)src, d); break;
569 case DBTYPE_CY: *d = *(CY*)src; hr = S_OK; break;
570 case DBTYPE_DATE: hr = VarCyFromDate(*(DATE*)src, d); break;
571 case DBTYPE_BSTR: hr = VarCyFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
572 case DBTYPE_BOOL: hr = VarCyFromBool(*(VARIANT_BOOL*)src, d); break;
573 case DBTYPE_DECIMAL: hr = VarCyFromDec((DECIMAL*)src, d); break;
574 case DBTYPE_I1: hr = VarCyFromI1(*(signed char*)src, d); break;
575 case DBTYPE_UI1: hr = VarCyFromUI1(*(BYTE*)src, d); break;
576 case DBTYPE_UI2: hr = VarCyFromUI2(*(WORD*)src, d); break;
577 case DBTYPE_UI4: hr = VarCyFromUI4(*(DWORD*)src, d); break;
578 case DBTYPE_I8: hr = VarCyFromI8(*(LONGLONG*)src, d); break;
579 case DBTYPE_UI8: hr = VarCyFromUI8(*(ULONGLONG*)src, d); break;
580 default: FIXME("Unimplemented conversion %04x -> CY\n", src_type); return E_NOTIMPL;
582 break;
585 case DBTYPE_BSTR:
587 BSTR *d = dst;
588 switch(src_type)
590 case DBTYPE_EMPTY: *d = SysAllocStringLen(NULL, 0); hr = *d ? S_OK : E_OUTOFMEMORY; break;
591 case DBTYPE_I2: hr = VarBstrFromI2(*(signed short*)src, LOCALE_USER_DEFAULT, 0, d); break;
592 case DBTYPE_I4: hr = VarBstrFromI4(*(signed int*)src, LOCALE_USER_DEFAULT, 0, d); break;
593 case DBTYPE_R4: hr = VarBstrFromR4(*(FLOAT*)src, LOCALE_USER_DEFAULT, 0, d); break;
594 case DBTYPE_R8: hr = VarBstrFromR8(*(double*)src, LOCALE_USER_DEFAULT, 0, d); break;
595 case DBTYPE_CY: hr = VarBstrFromCy(*(CY*)src, LOCALE_USER_DEFAULT, 0, d); break;
596 case DBTYPE_DATE: hr = VarBstrFromDate(*(DATE*)src, LOCALE_USER_DEFAULT, 0, d); break;
597 case DBTYPE_BSTR: *d = SysAllocStringLen(*(BSTR*)src, SysStringLen(*(BSTR*)src)); hr = *d ? S_OK : E_OUTOFMEMORY; break;
598 case DBTYPE_BOOL: hr = VarBstrFromBool(*(VARIANT_BOOL*)src, LOCALE_USER_DEFAULT, 0, d); break;
599 case DBTYPE_DECIMAL: hr = VarBstrFromDec((DECIMAL*)src, LOCALE_USER_DEFAULT, 0, d); break;
600 case DBTYPE_I1: hr = VarBstrFromI1(*(signed char*)src, LOCALE_USER_DEFAULT, 0, d); break;
601 case DBTYPE_UI1: hr = VarBstrFromUI1(*(BYTE*)src, LOCALE_USER_DEFAULT, 0, d); break;
602 case DBTYPE_UI2: hr = VarBstrFromUI2(*(WORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
603 case DBTYPE_UI4: hr = VarBstrFromUI4(*(DWORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
604 case DBTYPE_I8: hr = VarBstrFromI8(*(LONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
605 case DBTYPE_UI8: hr = VarBstrFromUI8(*(ULONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
606 case DBTYPE_GUID:
608 WCHAR szBuff[39];
609 const GUID *id = (const GUID *)src;
610 WCHAR format[] = {
611 '{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
612 '%','0','2','X','%','0','2','X','-',
613 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
614 wsprintfW(szBuff, format,
615 id->Data1, id->Data2, id->Data3,
616 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
617 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
618 *d = SysAllocString(szBuff);
619 hr = *d ? S_OK : E_OUTOFMEMORY;
621 break;
622 case DBTYPE_BYTES:
624 *d = SysAllocStringLen(NULL, 2 * src_len);
625 if (*d == NULL)
626 hr = E_OUTOFMEMORY;
627 else
629 const char hexchars[] = "0123456789ABCDEF";
630 WCHAR *s = *d;
631 unsigned char *p = src;
632 while (src_len > 0)
634 *s++ = hexchars[(*p >> 4) & 0x0F];
635 *s++ = hexchars[(*p) & 0x0F];
636 src_len--; p++;
638 hr = S_OK;
641 break;
642 case DBTYPE_VARIANT:
643 VariantInit(&tmp);
644 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_BSTR)) == S_OK)
645 *d = V_BSTR(&tmp);
646 break;
647 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type); return E_NOTIMPL;
649 break;
652 case DBTYPE_UI1:
654 BYTE *d = dst;
655 switch(src_type)
657 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
658 case DBTYPE_I2: hr = VarUI1FromI2(*(signed short*)src, d); break;
659 case DBTYPE_I4: hr = VarUI1FromI4(*(signed int*)src, d); break;
660 case DBTYPE_R4: hr = VarUI1FromR4(*(FLOAT*)src, d); break;
661 case DBTYPE_R8: hr = VarUI1FromR8(*(double*)src, d); break;
662 case DBTYPE_CY: hr = VarUI1FromCy(*(CY*)src, d); break;
663 case DBTYPE_DATE: hr = VarUI1FromDate(*(DATE*)src, d); break;
664 case DBTYPE_BSTR: hr = VarUI1FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
665 case DBTYPE_BOOL: hr = VarUI1FromBool(*(VARIANT_BOOL*)src, d); break;
666 case DBTYPE_DECIMAL: hr = VarUI1FromDec((DECIMAL*)src, d); break;
667 case DBTYPE_I1: hr = VarUI1FromI1(*(signed char*)src, d); break;
668 case DBTYPE_UI1: *d = *(BYTE*)src; hr = S_OK; break;
669 case DBTYPE_UI2: hr = VarUI1FromUI2(*(WORD*)src, d); break;
670 case DBTYPE_UI4: hr = VarUI1FromUI4(*(DWORD*)src, d); break;
671 case DBTYPE_I8: hr = VarUI1FromI8(*(LONGLONG*)src, d); break;
672 case DBTYPE_UI8: hr = VarUI1FromUI8(*(ULONGLONG*)src, d); break;
673 default: FIXME("Unimplemented conversion %04x -> UI1\n", src_type); return E_NOTIMPL;
675 break;
677 case DBTYPE_UI2:
679 WORD *d = dst;
680 switch(src_type)
682 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
683 case DBTYPE_I2: hr = VarUI2FromI2(*(signed short*)src, d); break;
684 case DBTYPE_I4: hr = VarUI2FromI4(*(signed int*)src, d); break;
685 case DBTYPE_R4: hr = VarUI2FromR4(*(FLOAT*)src, d); break;
686 case DBTYPE_R8: hr = VarUI2FromR8(*(double*)src, d); break;
687 case DBTYPE_CY: hr = VarUI2FromCy(*(CY*)src, d); break;
688 case DBTYPE_DATE: hr = VarUI2FromDate(*(DATE*)src, d); break;
689 case DBTYPE_BSTR: hr = VarUI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
690 case DBTYPE_BOOL: hr = VarUI2FromBool(*(VARIANT_BOOL*)src, d); break;
691 case DBTYPE_DECIMAL: hr = VarUI2FromDec((DECIMAL*)src, d); break;
692 case DBTYPE_I1: hr = VarUI2FromI1(*(signed char*)src, d); break;
693 case DBTYPE_UI1: hr = VarUI2FromUI1(*(BYTE*)src, d); break;
694 case DBTYPE_UI2: *d = *(WORD*)src; hr = S_OK; break;
695 case DBTYPE_UI4: hr = VarUI2FromUI4(*(DWORD*)src, d); break;
696 case DBTYPE_I8: hr = VarUI2FromI8(*(LONGLONG*)src, d); break;
697 case DBTYPE_UI8: hr = VarUI2FromUI8(*(ULONGLONG*)src, d); break;
698 default: FIXME("Unimplemented conversion %04x -> UI2\n", src_type); return E_NOTIMPL;
700 break;
703 case DBTYPE_UI4:
705 DWORD *d = dst;
706 switch(src_type)
708 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
709 case DBTYPE_I2: hr = VarUI4FromI2(*(signed short*)src, d); break;
710 case DBTYPE_I4: hr = VarUI4FromI4(*(signed int*)src, d); break;
711 case DBTYPE_R4: hr = VarUI4FromR4(*(FLOAT*)src, d); break;
712 case DBTYPE_R8: hr = VarUI4FromR8(*(double*)src, d); break;
713 case DBTYPE_CY: hr = VarUI4FromCy(*(CY*)src, d); break;
714 case DBTYPE_DATE: hr = VarUI4FromDate(*(DATE*)src, d); break;
715 case DBTYPE_BSTR: hr = VarUI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
716 case DBTYPE_BOOL: hr = VarUI4FromBool(*(VARIANT_BOOL*)src, d); break;
717 case DBTYPE_DECIMAL: hr = VarUI4FromDec((DECIMAL*)src, d); break;
718 case DBTYPE_I1: hr = VarUI4FromI1(*(signed char*)src, d); break;
719 case DBTYPE_UI1: hr = VarUI4FromUI1(*(BYTE*)src, d); break;
720 case DBTYPE_UI2: hr = VarUI4FromUI2(*(WORD*)src, d); break;
721 case DBTYPE_UI4: *d = *(DWORD*)src; hr = S_OK; break;
722 case DBTYPE_I8: hr = VarUI4FromI8(*(LONGLONG*)src, d); break;
723 case DBTYPE_UI8: hr = VarUI4FromUI8(*(ULONGLONG*)src, d); break;
724 case DBTYPE_VARIANT:
725 VariantInit(&tmp);
726 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_UI4)) == S_OK)
727 *d = V_UI4(&tmp);
728 break;
729 default: FIXME("Unimplemented conversion %04x -> UI4\n", src_type); return E_NOTIMPL;
731 break;
734 case DBTYPE_UI8:
736 ULONGLONG *d = dst;
737 switch(src_type)
739 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
740 case DBTYPE_I2: hr = VarUI8FromI2(*(signed short*)src, d); break;
741 case DBTYPE_I4: {LONGLONG s = *(signed int*)src; hr = VarUI8FromI8(s, d); break;}
742 case DBTYPE_R4: hr = VarUI8FromR4(*(FLOAT*)src, d); break;
743 case DBTYPE_R8: hr = VarUI8FromR8(*(double*)src, d); break;
744 case DBTYPE_CY: hr = VarUI8FromCy(*(CY*)src, d); break;
745 case DBTYPE_DATE: hr = VarUI8FromDate(*(DATE*)src, d); break;
746 case DBTYPE_BSTR: hr = VarUI8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
747 case DBTYPE_BOOL: hr = VarUI8FromBool(*(VARIANT_BOOL*)src, d); break;
748 case DBTYPE_DECIMAL: hr = VarUI8FromDec((DECIMAL*)src, d); break;
749 case DBTYPE_I1: hr = VarUI8FromI1(*(signed char*)src, d); break;
750 case DBTYPE_UI1: hr = VarUI8FromUI1(*(BYTE*)src, d); break;
751 case DBTYPE_UI2: hr = VarUI8FromUI2(*(WORD*)src, d); break;
752 case DBTYPE_UI4: hr = VarUI8FromUI4(*(DWORD*)src, d); break;
753 case DBTYPE_I8: hr = VarUI8FromI8(*(LONGLONG*)src, d); break;
754 case DBTYPE_UI8: *d = *(ULONGLONG*)src; hr = S_OK; break;
755 default: FIXME("Unimplemented conversion %04x -> UI8\n", src_type); return E_NOTIMPL;
757 break;
760 case DBTYPE_FILETIME:
762 FILETIME *d = dst;
763 switch(src_type)
765 case DBTYPE_EMPTY: d->dwLowDateTime = d->dwHighDateTime = 0; hr = S_OK; break;
766 case DBTYPE_FILETIME: *d = *(FILETIME*)src; hr = S_OK; break;
767 default: FIXME("Unimplemented conversion %04x -> FILETIME\n", src_type); return E_NOTIMPL;
769 break;
772 case DBTYPE_GUID:
774 GUID *d = dst;
775 switch(src_type)
777 case DBTYPE_EMPTY: *d = GUID_NULL; hr = S_OK; break;
778 case DBTYPE_GUID: *d = *(GUID*)src; hr = S_OK; break;
779 default: FIXME("Unimplemented conversion %04x -> GUID\n", src_type); return E_NOTIMPL;
781 break;
784 case DBTYPE_WSTR:
786 BSTR b;
787 DBLENGTH bstr_len;
788 INT bytes_to_copy;
789 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
790 src, &b, sizeof(BSTR), src_status, dst_status,
791 precision, scale, flags);
792 if(hr != S_OK || *dst_status == DBSTATUS_S_ISNULL)
793 return hr;
794 bstr_len = SysStringLen(b);
795 *dst_len = bstr_len * sizeof(WCHAR); /* Doesn't include size for '\0' */
796 *dst_status = DBSTATUS_S_OK;
797 bytes_to_copy = min(*dst_len + sizeof(WCHAR), dst_max_len);
798 if(dst)
800 if(bytes_to_copy >= sizeof(WCHAR))
802 memcpy(dst, b, bytes_to_copy - sizeof(WCHAR));
803 *((WCHAR*)dst + bytes_to_copy / sizeof(WCHAR) - 1) = 0;
804 if(bytes_to_copy < *dst_len + sizeof(WCHAR))
805 *dst_status = DBSTATUS_S_TRUNCATED;
807 else
809 *dst_status = DBSTATUS_E_DATAOVERFLOW;
810 hr = DB_E_ERRORSOCCURRED;
813 SysFreeString(b);
814 return hr;
816 case DBTYPE_STR:
818 BSTR b;
819 DBLENGTH bstr_len;
820 INT bytes_to_copy;
821 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
822 src, &b, sizeof(BSTR), src_status, dst_status,
823 precision, scale, flags);
824 if(hr != S_OK) return hr;
825 bstr_len = SysStringLen(b);
826 *dst_len = bstr_len * sizeof(char); /* Doesn't include size for '\0' */
827 *dst_status = DBSTATUS_S_OK;
828 bytes_to_copy = min(*dst_len + sizeof(char), dst_max_len);
829 if(dst)
831 if(bytes_to_copy >= sizeof(char))
833 WideCharToMultiByte(CP_ACP, 0, b, bytes_to_copy - sizeof(char), dst, dst_max_len, NULL, NULL);
834 *((char *)dst + bytes_to_copy / sizeof(char) - 1) = 0;
835 if(bytes_to_copy < *dst_len + sizeof(char))
836 *dst_status = DBSTATUS_S_TRUNCATED;
838 else
840 *dst_status = DBSTATUS_E_DATAOVERFLOW;
841 hr = DB_E_ERRORSOCCURRED;
844 SysFreeString(b);
845 return hr;
848 case DBTYPE_BYREF | DBTYPE_WSTR:
850 BSTR b;
851 WCHAR **d = dst;
852 DBLENGTH bstr_len;
853 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
854 src, &b, sizeof(BSTR), src_status, dst_status,
855 precision, scale, flags);
856 if(hr != S_OK) return hr;
858 bstr_len = SysStringLen(b) * sizeof(WCHAR);
859 *dst_len = bstr_len; /* Doesn't include size for '\0' */
861 *d = CoTaskMemAlloc(bstr_len + sizeof(WCHAR));
862 if(*d) memcpy(*d, b, bstr_len + sizeof(WCHAR));
863 else hr = E_OUTOFMEMORY;
864 SysFreeString(b);
865 return hr;
868 case DBTYPE_VARIANT:
870 VARIANT *v = dst;
872 switch(src_type)
874 case DBTYPE_BOOL:
875 V_VT(v) = VT_BOOL;
876 V_BOOL(v) = *(VARIANT_BOOL*)src;
877 hr = S_OK;
878 break;
879 case DBTYPE_I2:
880 V_VT(v) = VT_I2;
881 V_I2(v) = *(signed short*)src;
882 hr = S_OK;
883 break;
884 case DBTYPE_I4:
885 V_VT(v) = VT_I4;
886 V_I4(v) = *(signed int*)src;
887 hr = S_OK;
888 break;
889 case DBTYPE_I8:
890 V_VT(v) = VT_DECIMAL;
891 hr = VarDecFromI8( *(LONGLONG*)src, &V_DECIMAL(v));
892 break;
893 case DBTYPE_R4:
894 V_VT(v) = VT_R4;
895 V_R4(v) = *(FLOAT*)src;
896 hr = S_OK;
897 break;
898 case DBTYPE_R8:
899 V_VT(v) = VT_R8;
900 V_R8(v) = *(double*)src;
901 hr = S_OK;
902 break;
903 case DBTYPE_BSTR:
905 BSTR s = *(WCHAR**)src;
906 TRACE("%s\n", debugstr_w(s));
907 V_VT(v) = VT_BSTR;
908 V_BSTR(v) = SysAllocString(s);
909 hr = V_BSTR(v) ? S_OK : E_OUTOFMEMORY;
910 break;
912 case DBTYPE_DATE:
913 V_VT(v) = VT_DATE;
914 V_DATE(v) = *(DATE*)src;
915 hr = S_OK;
916 break;
917 case DBTYPE_DBDATE:
919 SYSTEMTIME st;
920 DBDATE *ts=(DBDATE*)src;
922 V_VT(v) = VT_DATE;
924 st.wYear = ts->year;
925 st.wMonth = ts->month;
926 st.wDay = ts->day;
927 st.wHour = 0;
928 st.wMinute = 0;
929 st.wSecond = 0;
930 st.wMilliseconds = 0;
931 hr = (SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL);
932 break;
934 case DBTYPE_DBTIMESTAMP:
936 SYSTEMTIME st;
937 DBTIMESTAMP *ts = (DBTIMESTAMP *)src;
939 V_VT(v) = VT_DATE;
941 st.wYear = ts->year;
942 st.wMonth = ts->month;
943 st.wDay = ts->day;
944 st.wHour = ts->hour;
945 st.wMinute = ts->minute;
946 st.wSecond = ts->second;
947 st.wMilliseconds = ts->fraction/1000000;
948 hr = SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL;
949 break;
951 case DBTYPE_CY:
952 V_VT(v) = VT_CY;
953 V_CY(v) = *(CY*)src;
954 hr = S_OK;
955 break;
956 case DBTYPE_BYTES:
958 LONG i;
959 SAFEARRAY *psa = NULL;
960 SAFEARRAYBOUND rgsabound[1];
961 unsigned char *p = src;
963 rgsabound[0].lLbound = 0;
964 rgsabound[0].cElements = src_len;
966 psa = SafeArrayCreate(VT_UI1,1,rgsabound);
967 for(i =0; i < src_len; i++,p++)
969 hr = SafeArrayPutElement(psa, &i, p);
970 if(FAILED(hr)) {
971 SafeArrayDestroy (psa);
972 return hr;
976 V_VT(v) = VT_ARRAY|VT_UI1;
977 V_ARRAY(v) = psa;
978 hr = S_OK;
979 break;
981 default: FIXME("Unimplemented conversion %04x -> VARIANT\n", src_type); return E_NOTIMPL;
983 break;
985 case DBTYPE_BYTES:
987 BYTE *d = dst;
989 switch(src_type)
991 case DBTYPE_BYTES:
992 if( src_len > dst_max_len)
993 *dst_status = DBSTATUS_S_TRUNCATED;
994 else
995 *dst_status = DBSTATUS_S_OK;
997 *dst_len = src_len;
998 memcpy(d, src, min(src_len, dst_max_len));
1000 return S_OK;
1001 case DBTYPE_VARIANT:
1003 switch(V_VT((VARIANT*)src))
1005 case VT_UI1 | VT_ARRAY:
1007 LONG l;
1008 BYTE *data = NULL;
1010 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1011 if(FAILED(hr))
1012 return hr;
1014 hr = SafeArrayAccessData(V_ARRAY((VARIANT*)src), (VOID**)&data);
1015 if(FAILED(hr))
1017 ERR("SafeArrayAccessData Failed = 0x%08x\n", hr);
1018 return hr;
1021 *dst_len = l+1;
1022 *dst_status = DBSTATUS_S_OK;
1023 memcpy(d, data, *dst_len);
1025 SafeArrayUnaccessData(V_ARRAY((VARIANT*)src));
1026 return S_OK;
1028 break;
1029 default:
1030 FIXME("Unimplemented variant type %d -> BYTES\n", V_VT((VARIANT*)src));
1031 return E_NOTIMPL;
1034 break;
1035 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES\n", src_type); return E_NOTIMPL;
1037 break;
1039 case DBTYPE_BYTES | DBTYPE_BYREF:
1041 BYTE **d = dst;
1043 switch(src_type)
1045 case DBTYPE_BYTES:
1046 *d = CoTaskMemAlloc(src_len);
1047 if(*d) memcpy(*d, src, src_len);
1048 else hr = E_OUTOFMEMORY;
1050 *dst_len = src_len;
1051 *dst_status = DBSTATUS_S_OK;
1052 return S_OK;
1053 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES | DBTYPE_BYREF\n", src_type); return E_NOTIMPL;
1055 break;
1057 break;
1059 default:
1060 FIXME("Unimplemented conversion %04x -> %04x\n", src_type, dst_type);
1061 return E_NOTIMPL;
1064 if(hr == DISP_E_OVERFLOW)
1066 *dst_status = DBSTATUS_E_DATAOVERFLOW;
1067 *dst_len = get_length(dst_type);
1068 hr = DB_E_ERRORSOCCURRED;
1070 else if(hr == S_OK)
1072 *dst_status = DBSTATUS_S_OK;
1073 *dst_len = get_length(dst_type);
1076 return hr;
1079 static inline WORD get_dbtype_class(DBTYPE type)
1081 switch(type)
1083 case DBTYPE_I2:
1084 case DBTYPE_R4:
1085 case DBTYPE_R8:
1086 case DBTYPE_I1:
1087 case DBTYPE_UI1:
1088 case DBTYPE_UI2:
1089 return DBTYPE_I2;
1091 case DBTYPE_I4:
1092 case DBTYPE_UI4:
1093 return DBTYPE_I4;
1095 case DBTYPE_I8:
1096 case DBTYPE_UI8:
1097 return DBTYPE_I8;
1099 case DBTYPE_BSTR:
1100 case DBTYPE_STR:
1101 case DBTYPE_WSTR:
1102 return DBTYPE_BSTR;
1104 case DBTYPE_DBDATE:
1105 case DBTYPE_DBTIME:
1106 case DBTYPE_DBTIMESTAMP:
1107 return DBTYPE_DBDATE;
1109 return type;
1112 /* Many src types will convert to this group of dst types */
1113 static inline BOOL common_class(WORD dst_class)
1115 switch(dst_class)
1117 case DBTYPE_EMPTY:
1118 case DBTYPE_NULL:
1119 case DBTYPE_I2:
1120 case DBTYPE_I4:
1121 case DBTYPE_BSTR:
1122 case DBTYPE_BOOL:
1123 case DBTYPE_VARIANT:
1124 case DBTYPE_I8:
1125 case DBTYPE_CY:
1126 case DBTYPE_DECIMAL:
1127 case DBTYPE_NUMERIC:
1128 return TRUE;
1130 return FALSE;
1133 static inline BOOL array_type(DBTYPE type)
1135 return (type >= DBTYPE_I2 && type <= DBTYPE_UI4);
1138 static HRESULT WINAPI convert_CanConvert(IDataConvert* iface,
1139 DBTYPE src_type, DBTYPE dst_type)
1141 convert *This = impl_from_IDataConvert(iface);
1142 DBTYPE src_base_type = src_type & 0x1ff;
1143 DBTYPE dst_base_type = dst_type & 0x1ff;
1144 WORD dst_class = get_dbtype_class(dst_base_type);
1146 TRACE("(%p)->(%d, %d)\n", This, src_type, dst_type);
1148 if(src_type & DBTYPE_VECTOR || dst_type & DBTYPE_VECTOR) return S_FALSE;
1150 if(src_type & DBTYPE_ARRAY)
1152 if(!array_type(src_base_type)) return S_FALSE;
1153 if(dst_type & DBTYPE_ARRAY)
1155 if(src_type == dst_type) return S_OK;
1156 return S_FALSE;
1158 if(dst_type == DBTYPE_VARIANT) return S_OK;
1159 return S_FALSE;
1162 if(dst_type & DBTYPE_ARRAY)
1164 if(!array_type(dst_base_type)) return S_FALSE;
1165 if(src_type == DBTYPE_IDISPATCH || src_type == DBTYPE_VARIANT) return S_OK;
1166 return S_FALSE;
1169 if(dst_type & DBTYPE_BYREF)
1170 if(dst_base_type != DBTYPE_BYTES && dst_base_type != DBTYPE_STR && dst_base_type != DBTYPE_WSTR)
1171 return S_FALSE;
1173 switch(get_dbtype_class(src_base_type))
1175 case DBTYPE_EMPTY:
1176 if(common_class(dst_class)) return S_OK;
1177 switch(dst_class)
1179 case DBTYPE_DATE:
1180 case DBTYPE_GUID:
1181 case DBTYPE_FILETIME:
1182 return S_OK;
1183 default:
1184 if(dst_base_type == DBTYPE_DBTIMESTAMP) return S_OK;
1185 return S_FALSE;
1188 case DBTYPE_NULL:
1189 switch(dst_base_type)
1191 case DBTYPE_NULL:
1192 case DBTYPE_VARIANT:
1193 case DBTYPE_FILETIME: return S_OK;
1194 default: return S_FALSE;
1197 case DBTYPE_I4:
1198 if(dst_base_type == DBTYPE_BYTES) return S_OK;
1199 /* fall through */
1200 case DBTYPE_I2:
1201 if(dst_base_type == DBTYPE_DATE) return S_OK;
1202 /* fall through */
1203 case DBTYPE_DECIMAL:
1204 if(common_class(dst_class)) return S_OK;
1205 if(dst_class == DBTYPE_DBDATE) return S_OK;
1206 return S_FALSE;
1208 case DBTYPE_BOOL:
1209 if(dst_base_type == DBTYPE_DATE) return S_OK;
1210 /* fall through */
1211 case DBTYPE_NUMERIC:
1212 case DBTYPE_CY:
1213 if(common_class(dst_class)) return S_OK;
1214 return S_FALSE;
1216 case DBTYPE_I8:
1217 if(common_class(dst_class)) return S_OK;
1218 switch(dst_base_type)
1220 case DBTYPE_BYTES:
1221 case DBTYPE_FILETIME: return S_OK;
1222 default: return S_FALSE;
1225 case DBTYPE_DATE:
1226 switch(dst_class)
1228 case DBTYPE_EMPTY:
1229 case DBTYPE_NULL:
1230 case DBTYPE_I2:
1231 case DBTYPE_I4:
1232 case DBTYPE_BSTR:
1233 case DBTYPE_BOOL:
1234 case DBTYPE_VARIANT:
1235 case DBTYPE_I8:
1236 case DBTYPE_DATE:
1237 case DBTYPE_DBDATE:
1238 case DBTYPE_FILETIME:
1239 return S_OK;
1240 default: return S_FALSE;
1243 case DBTYPE_IDISPATCH:
1244 case DBTYPE_VARIANT:
1245 switch(dst_base_type)
1247 case DBTYPE_IDISPATCH:
1248 case DBTYPE_ERROR:
1249 case DBTYPE_IUNKNOWN:
1250 return S_OK;
1252 /* fall through */
1253 case DBTYPE_BSTR:
1254 if(common_class(dst_class)) return S_OK;
1255 switch(dst_class)
1257 case DBTYPE_DATE:
1258 case DBTYPE_GUID:
1259 case DBTYPE_BYTES:
1260 case DBTYPE_DBDATE:
1261 case DBTYPE_FILETIME:
1262 return S_OK;
1263 default: return S_FALSE;
1266 case DBTYPE_ERROR:
1267 switch(dst_base_type)
1269 case DBTYPE_BSTR:
1270 case DBTYPE_ERROR:
1271 case DBTYPE_VARIANT:
1272 case DBTYPE_WSTR:
1273 return S_OK;
1274 default: return S_FALSE;
1277 case DBTYPE_IUNKNOWN:
1278 switch(dst_base_type)
1280 case DBTYPE_EMPTY:
1281 case DBTYPE_NULL:
1282 case DBTYPE_IDISPATCH:
1283 case DBTYPE_VARIANT:
1284 case DBTYPE_IUNKNOWN:
1285 return S_OK;
1286 default: return S_FALSE;
1289 case DBTYPE_BYTES:
1290 if(dst_class == DBTYPE_I4 || dst_class == DBTYPE_I8) return S_OK;
1291 /* fall through */
1292 case DBTYPE_GUID:
1293 switch(dst_class)
1295 case DBTYPE_EMPTY:
1296 case DBTYPE_NULL:
1297 case DBTYPE_BSTR:
1298 case DBTYPE_VARIANT:
1299 case DBTYPE_GUID:
1300 case DBTYPE_BYTES:
1301 return S_OK;
1302 default: return S_FALSE;
1305 case DBTYPE_FILETIME:
1306 if(dst_class == DBTYPE_I8) return S_OK;
1307 /* fall through */
1308 case DBTYPE_DBDATE:
1309 switch(dst_class)
1311 case DBTYPE_EMPTY:
1312 case DBTYPE_NULL:
1313 case DBTYPE_DATE:
1314 case DBTYPE_BSTR:
1315 case DBTYPE_VARIANT:
1316 case DBTYPE_DBDATE:
1317 case DBTYPE_FILETIME:
1318 return S_OK;
1319 default: return S_FALSE;
1323 return S_FALSE;
1326 static HRESULT WINAPI convert_GetConversionSize(IDataConvert* iface,
1327 DBTYPE src_type, DBTYPE dst_type,
1328 DBLENGTH *src_len, DBLENGTH *dst_len,
1329 void *src)
1331 convert *This = impl_from_IDataConvert(iface);
1332 HRESULT hr;
1334 TRACE("(%p)->(%d, %d, %p, %p, %p)\n", This, src_type, dst_type, src_len, dst_len, src);
1336 hr = IDataConvert_CanConvert(iface, src_type, dst_type);
1337 if (hr != S_OK)
1338 return DB_E_UNSUPPORTEDCONVERSION;
1340 if (!dst_len)
1341 return E_INVALIDARG;
1343 /* for some types we don't need to look into source data */
1344 if ((*dst_len = get_length(dst_type)))
1345 return S_OK;
1347 if(src_type == DBTYPE_VARIANT && V_VT((VARIANT*)src) == VT_NULL)
1348 return S_OK;
1350 switch (dst_type)
1352 case DBTYPE_STR:
1353 switch (src_type)
1355 case DBTYPE_VARIANT:
1357 VARIANT v;
1359 VariantInit(&v);
1360 if ((hr = VariantChangeType(&v, (VARIANT*)src, 0, VT_BSTR)) == S_OK)
1362 *dst_len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), -1, NULL, 0, NULL, NULL);
1363 VariantClear(&v);
1365 else
1366 return hr;
1368 break;
1369 default:
1370 FIXME("unimplemented for %04x -> DBTYPE_STR\n", src_type);
1371 return E_NOTIMPL;
1373 break;
1374 case DBTYPE_WSTR:
1375 switch (src_type)
1377 case DBTYPE_VARIANT:
1379 VARIANT v;
1381 VariantInit(&v);
1382 if ((hr = VariantChangeType(&v, (VARIANT*)src, 0, VT_BSTR)) == S_OK)
1384 *dst_len = (SysStringLen(V_BSTR(&v))+1) * sizeof(WCHAR);
1385 VariantClear(&v);
1388 break;
1389 case DBTYPE_STR:
1390 if(src_len)
1391 *dst_len = (*src_len + 1) * sizeof(WCHAR);
1392 else
1393 *dst_len = (strlen(src) + 1) * sizeof(WCHAR);
1394 break;
1395 case DBTYPE_WSTR:
1396 if(src_len)
1397 *dst_len = (*src_len) + sizeof(WCHAR);
1398 else
1399 *dst_len = (lstrlenW(src) + 1) * sizeof(WCHAR);
1400 break;
1401 default:
1402 FIXME("unimplemented for %04x -> DBTYPE_WSTR\n", src_type);
1403 return E_NOTIMPL;
1405 break;
1406 case DBTYPE_BYTES:
1407 switch (src_type)
1409 case DBTYPE_VARIANT:
1410 if(V_VT((VARIANT*)src) == VT_BSTR)
1411 *dst_len = (SysStringLen(V_BSTR((VARIANT*)src))) / sizeof(WCHAR);
1412 else
1414 switch(V_VT((VARIANT*)src))
1416 case VT_UI1 | VT_ARRAY:
1418 LONG l;
1420 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1421 if(FAILED(hr))
1422 return hr;
1423 *dst_len = l+1;
1425 break;
1427 default:
1428 WARN("DBTYPE_VARIANT(%d)->DBTYPE_BYTES unimplemented\n", V_VT((VARIANT*)src));
1431 break;
1432 default:
1433 FIXME("unimplemented for %04x -> DBTYPE_BYTES\n", src_type);
1434 return E_NOTIMPL;
1436 break;
1437 default:
1438 FIXME("unimplemented for conversion %d->%d\n", src_type, dst_type);
1439 return E_NOTIMPL;
1442 return S_OK;
1445 static const struct IDataConvertVtbl convert_vtbl =
1447 convert_QueryInterface,
1448 convert_AddRef,
1449 convert_Release,
1450 convert_DataConvert,
1451 convert_CanConvert,
1452 convert_GetConversionSize
1455 static HRESULT WINAPI dcinfo_QueryInterface(IDCInfo* iface, REFIID riid, void **obj)
1457 convert *This = impl_from_IDCInfo(iface);
1459 return IDataConvert_QueryInterface(&This->IDataConvert_iface, riid, obj);
1462 static ULONG WINAPI dcinfo_AddRef(IDCInfo* iface)
1464 convert *This = impl_from_IDCInfo(iface);
1466 return IDataConvert_AddRef(&This->IDataConvert_iface);
1469 static ULONG WINAPI dcinfo_Release(IDCInfo* iface)
1471 convert *This = impl_from_IDCInfo(iface);
1473 return IDataConvert_Release(&This->IDataConvert_iface);
1476 static HRESULT WINAPI dcinfo_GetInfo(IDCInfo *iface, ULONG num, DCINFOTYPE types[], DCINFO **info_ptr)
1478 convert *This = impl_from_IDCInfo(iface);
1479 ULONG i;
1480 DCINFO *infos;
1482 TRACE("(%p)->(%d, %p, %p)\n", This, num, types, info_ptr);
1484 *info_ptr = infos = CoTaskMemAlloc(num * sizeof(*infos));
1485 if(!infos) return E_OUTOFMEMORY;
1487 for(i = 0; i < num; i++)
1489 infos[i].eInfoType = types[i];
1490 VariantInit(&infos[i].vData);
1492 switch(types[i])
1494 case DCINFOTYPE_VERSION:
1495 V_VT(&infos[i].vData) = VT_UI4;
1496 V_UI4(&infos[i].vData) = This->version;
1497 break;
1501 return S_OK;
1504 static HRESULT WINAPI dcinfo_SetInfo(IDCInfo* iface, ULONG num, DCINFO info[])
1506 convert *This = impl_from_IDCInfo(iface);
1507 ULONG i;
1508 HRESULT hr = S_OK;
1510 TRACE("(%p)->(%d, %p)\n", This, num, info);
1512 for(i = 0; i < num; i++)
1514 switch(info[i].eInfoType)
1516 case DCINFOTYPE_VERSION:
1517 if(V_VT(&info[i].vData) != VT_UI4)
1519 FIXME("VERSION with vt %x\n", V_VT(&info[i].vData));
1520 hr = DB_S_ERRORSOCCURRED;
1521 break;
1523 This->version = V_UI4(&info[i].vData);
1524 break;
1526 default:
1527 FIXME("Unhandled info type %d (vt %x)\n", info[i].eInfoType, V_VT(&info[i].vData));
1530 return hr;
1533 static const struct IDCInfoVtbl dcinfo_vtbl =
1535 dcinfo_QueryInterface,
1536 dcinfo_AddRef,
1537 dcinfo_Release,
1538 dcinfo_GetInfo,
1539 dcinfo_SetInfo
1542 HRESULT create_oledb_convert(IUnknown *outer, void **obj)
1544 convert *This;
1546 TRACE("(%p, %p)\n", outer, obj);
1548 *obj = NULL;
1550 if(outer) return CLASS_E_NOAGGREGATION;
1552 This = heap_alloc(sizeof(*This));
1553 if(!This) return E_OUTOFMEMORY;
1555 This->IDataConvert_iface.lpVtbl = &convert_vtbl;
1556 This->IDCInfo_iface.lpVtbl = &dcinfo_vtbl;
1557 This->ref = 1;
1558 This->version = 0x110;
1560 *obj = &This->IDataConvert_iface;
1562 return S_OK;