dpvoice: Add IDirectPlayVoiceClient Support.
[wine.git] / dlls / oledb32 / convert.c
blob36448d31e647f6ee2fa99feacfadd2ffd610b9f1
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
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "ole2.h"
30 #include "msdadc.h"
31 #include "oledberr.h"
33 #include "oledb_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
39 typedef struct
41 IDataConvert IDataConvert_iface;
42 IDCInfo IDCInfo_iface;
44 LONG ref;
46 UINT version; /* Set by IDCInfo_SetInfo */
47 } convert;
49 static inline convert *impl_from_IDataConvert(IDataConvert *iface)
51 return CONTAINING_RECORD(iface, convert, IDataConvert_iface);
54 static inline convert *impl_from_IDCInfo(IDCInfo *iface)
56 return CONTAINING_RECORD(iface, convert, IDCInfo_iface);
59 static HRESULT WINAPI convert_QueryInterface(IDataConvert* iface,
60 REFIID riid,
61 void **obj)
63 convert *This = impl_from_IDataConvert(iface);
64 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
66 *obj = NULL;
68 if(IsEqualIID(riid, &IID_IUnknown) ||
69 IsEqualIID(riid, &IID_IDataConvert))
71 *obj = iface;
73 else if(IsEqualIID(riid, &IID_IDCInfo))
75 *obj = &This->IDCInfo_iface;
77 else
79 FIXME("interface %s not implemented\n", debugstr_guid(riid));
80 return E_NOINTERFACE;
83 IDataConvert_AddRef(iface);
84 return S_OK;
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);
100 LONG ref;
102 TRACE("(%p)\n", This);
104 ref = InterlockedDecrement(&This->ref);
105 if(ref == 0)
106 heap_free(This);
108 return ref;
111 static int get_length(DBTYPE type)
113 switch(type)
115 case DBTYPE_I1:
116 case DBTYPE_UI1:
117 return 1;
118 case DBTYPE_I2:
119 case DBTYPE_UI2:
120 return 2;
121 case DBTYPE_BOOL:
122 return sizeof(VARIANT_BOOL);
123 case DBTYPE_I4:
124 case DBTYPE_UI4:
125 case DBTYPE_R4:
126 return 4;
127 case DBTYPE_I8:
128 case DBTYPE_UI8:
129 case DBTYPE_R8:
130 case DBTYPE_DATE:
131 return 8;
132 case DBTYPE_DBDATE:
133 return sizeof(DBDATE);
134 case DBTYPE_DBTIMESTAMP:
135 return sizeof(DBTIMESTAMP);
136 case DBTYPE_CY:
137 return sizeof(CY);
138 case DBTYPE_BSTR:
139 return sizeof(BSTR);
140 case DBTYPE_FILETIME:
141 return sizeof(FILETIME);
142 case DBTYPE_GUID:
143 return sizeof(GUID);
144 case DBTYPE_BYTES:
145 case DBTYPE_WSTR:
146 case DBTYPE_STR:
147 case DBTYPE_BYREF | DBTYPE_WSTR:
148 return 0;
149 case DBTYPE_VARIANT:
150 return sizeof(VARIANT);
151 default:
152 FIXME("Unhandled type %04x\n", type);
153 return 0;
157 static HRESULT WINAPI convert_DataConvert(IDataConvert* iface,
158 DBTYPE src_type, DBTYPE dst_type,
159 DBLENGTH src_len, DBLENGTH *dst_len,
160 void *src, void *dst,
161 DBLENGTH dst_max_len,
162 DBSTATUS src_status, DBSTATUS *dst_status,
163 BYTE precision, BYTE scale,
164 DBDATACONVERT flags)
166 convert *This = impl_from_IDataConvert(iface);
167 DBLENGTH dst_len_loc;
168 DBSTATUS dst_status_loc;
169 VARIANT tmp;
170 HRESULT hr;
172 TRACE("(%p)->(%d, %d, %ld, %p, %p, %p, %ld, %d, %p, %d, %d, %x)\n", This,
173 src_type, dst_type, src_len, dst_len, src, dst, dst_max_len,
174 src_status, dst_status, precision, scale, flags);
176 if (!dst_len) dst_len = &dst_len_loc;
177 if (!dst_status) dst_status = &dst_status_loc;
179 *dst_status = DBSTATUS_E_BADACCESSOR;
181 if(src_status == DBSTATUS_S_ISNULL)
183 *dst_status = DBSTATUS_S_ISNULL;
184 *dst_len = 0;
185 return S_OK;
188 if(IDataConvert_CanConvert(iface, src_type, dst_type) != S_OK)
190 return DB_E_UNSUPPORTEDCONVERSION;
193 if(dst == NULL && get_length(dst_type) != 0)
195 *dst_len = get_length(src_type);
196 *dst_status = DBSTATUS_S_OK;
197 return S_OK;
200 if(src_type == DBTYPE_STR)
202 BSTR b;
203 DWORD len;
205 if(flags & DBDATACONVERT_LENGTHFROMNTS)
206 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0) - 1;
207 else
208 len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
209 b = SysAllocStringLen(NULL, len);
210 if(!b) return E_OUTOFMEMORY;
211 if(flags & DBDATACONVERT_LENGTHFROMNTS)
212 MultiByteToWideChar(CP_ACP, 0, src, -1, b, len + 1);
213 else
214 MultiByteToWideChar(CP_ACP, 0, src, src_len, b, len);
216 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
217 &b, dst, dst_max_len, src_status, dst_status,
218 precision, scale, flags);
220 SysFreeString(b);
221 return hr;
224 if(src_type == DBTYPE_WSTR)
226 BSTR b;
228 if(flags & DBDATACONVERT_LENGTHFROMNTS)
229 b = SysAllocString(src);
230 else
231 b = SysAllocStringLen(src, src_len / 2);
232 if(!b) return E_OUTOFMEMORY;
233 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
234 &b, dst, dst_max_len, src_status, dst_status,
235 precision, scale, flags);
236 SysFreeString(b);
237 return hr;
240 switch(dst_type)
242 case DBTYPE_I2:
244 signed short *d = dst;
245 switch(src_type)
247 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
248 case DBTYPE_I2: *d = *(signed short*)src; hr = S_OK; break;
249 case DBTYPE_I4: hr = VarI2FromI4(*(signed int*)src, d); break;
250 case DBTYPE_R4: hr = VarI2FromR4(*(FLOAT*)src, d); break;
251 case DBTYPE_R8: hr = VarI2FromR8(*(double*)src, d); break;
252 case DBTYPE_CY: hr = VarI2FromCy(*(CY*)src, d); break;
253 case DBTYPE_DATE: hr = VarI2FromDate(*(DATE*)src, d); break;
254 case DBTYPE_BSTR: hr = VarI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
255 case DBTYPE_BOOL: hr = VarI2FromBool(*(VARIANT_BOOL*)src, d); break;
256 case DBTYPE_DECIMAL: hr = VarI2FromDec((DECIMAL*)src, d); break;
257 case DBTYPE_I1: hr = VarI2FromI1(*(signed char*)src, d); break;
258 case DBTYPE_UI1: hr = VarI2FromUI1(*(BYTE*)src, d); break;
259 case DBTYPE_UI2: hr = VarI2FromUI2(*(WORD*)src, d); break;
260 case DBTYPE_UI4: hr = VarI2FromUI4(*(DWORD*)src, d); break;
261 case DBTYPE_I8: hr = VarI2FromI8(*(LONGLONG*)src, d); break;
262 case DBTYPE_UI8: hr = VarI2FromUI8(*(ULONGLONG*)src, d); break;
263 case DBTYPE_VARIANT:
264 VariantInit(&tmp);
265 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_I2)) == S_OK)
266 *d = V_I2(&tmp);
267 break;
268 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type); return E_NOTIMPL;
270 break;
273 case DBTYPE_I4:
275 signed int *d = dst;
276 switch(src_type)
278 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
279 case DBTYPE_I2: hr = VarI4FromI2(*(signed short*)src, d); break;
280 case DBTYPE_I4: *d = *(signed int*)src; hr = S_OK; break;
281 case DBTYPE_R4: hr = VarI4FromR4(*(FLOAT*)src, d); break;
282 case DBTYPE_R8: hr = VarI4FromR8(*(double*)src, d); break;
283 case DBTYPE_CY: hr = VarI4FromCy(*(CY*)src, d); break;
284 case DBTYPE_DATE: hr = VarI4FromDate(*(DATE*)src, d); break;
285 case DBTYPE_BSTR: hr = VarI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
286 case DBTYPE_BOOL: hr = VarI4FromBool(*(VARIANT_BOOL*)src, d); break;
287 case DBTYPE_DECIMAL: hr = VarI4FromDec((DECIMAL*)src, d); break;
288 case DBTYPE_I1: hr = VarI4FromI1(*(signed char*)src, d); break;
289 case DBTYPE_UI1: hr = VarI4FromUI1(*(BYTE*)src, d); break;
290 case DBTYPE_UI2: hr = VarI4FromUI2(*(WORD*)src, d); break;
291 case DBTYPE_UI4: hr = VarI4FromUI4(*(DWORD*)src, d); break;
292 case DBTYPE_I8: hr = VarI4FromI8(*(LONGLONG*)src, d); break;
293 case DBTYPE_UI8: hr = VarI4FromUI8(*(ULONGLONG*)src, d); break;
294 case DBTYPE_VARIANT:
295 VariantInit(&tmp);
296 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_I4)) == S_OK)
297 *d = V_I4(&tmp);
298 break;
299 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type); return E_NOTIMPL;
301 break;
304 case DBTYPE_I8:
306 LONGLONG *d = dst;
307 switch(src_type)
309 case DBTYPE_BSTR: hr = VarI8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
310 case DBTYPE_I8: *d = *(LONGLONG*)src; hr = S_OK; break;
311 default: FIXME("Unimplemented conversion %04x -> I8\n", src_type); return E_NOTIMPL;
313 break;
316 case DBTYPE_R4:
318 FLOAT *d = dst;
319 switch(src_type)
321 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
322 case DBTYPE_I2: hr = VarR4FromI2(*(signed short*)src, d); break;
323 case DBTYPE_I4: hr = VarR4FromI4(*(signed int*)src, d); break;
324 case DBTYPE_R4: *d = *(FLOAT*)src; hr = S_OK; break;
325 case DBTYPE_R8: hr = VarR4FromR8(*(double*)src, d); break;
326 case DBTYPE_CY: hr = VarR4FromCy(*(CY*)src, d); break;
327 case DBTYPE_DATE: hr = VarR4FromDate(*(DATE*)src, d); break;
328 case DBTYPE_BSTR: hr = VarR4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
329 case DBTYPE_BOOL: hr = VarR4FromBool(*(VARIANT_BOOL*)src, d); break;
330 case DBTYPE_DECIMAL: hr = VarR4FromDec((DECIMAL*)src, d); break;
331 case DBTYPE_I1: hr = VarR4FromI1(*(signed char*)src, d); break;
332 case DBTYPE_UI1: hr = VarR4FromUI1(*(BYTE*)src, d); break;
333 case DBTYPE_UI2: hr = VarR4FromUI2(*(WORD*)src, d); break;
334 case DBTYPE_UI4: hr = VarR4FromUI4(*(DWORD*)src, d); break;
335 case DBTYPE_I8: hr = VarR4FromI8(*(LONGLONG*)src, d); break;
336 case DBTYPE_UI8: hr = VarR4FromUI8(*(ULONGLONG*)src, d); break;
337 default: FIXME("Unimplemented conversion %04x -> R4\n", src_type); return E_NOTIMPL;
339 break;
341 case DBTYPE_R8:
343 DOUBLE *d=dst;
344 switch (src_type)
346 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
347 case DBTYPE_I1: hr = VarR8FromI1(*(signed char*)src, d); break;
348 case DBTYPE_I2: hr = VarR8FromI2(*(signed short*)src, d); break;
349 case DBTYPE_I4: hr = VarR8FromI4(*(signed int*)src, d); break;
350 case DBTYPE_I8: hr = VarR8FromI8(*(LONGLONG*)src, d); break;
351 case DBTYPE_UI1: hr = VarR8FromUI1(*(BYTE*)src, d); break;
352 case DBTYPE_UI2: hr = VarR8FromUI2(*(WORD*)src, d); break;
353 case DBTYPE_UI4: hr = VarR8FromUI4(*(DWORD*)src, d); break;
354 case DBTYPE_UI8: hr = VarR8FromUI8(*(ULONGLONG*)src, d); break;
355 case DBTYPE_R4: hr = VarR8FromR4(*(FLOAT*)src, d); break;
356 case DBTYPE_R8: *d = *(DOUBLE*)src; hr = S_OK; break;
357 case DBTYPE_CY: hr = VarR8FromCy(*(CY*)src, d); break;
358 case DBTYPE_DATE: hr = VarR8FromDate(*(DATE*)src, d); break;
359 case DBTYPE_BSTR: hr = VarR8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
360 case DBTYPE_BOOL: hr = VarR8FromBool(*(VARIANT_BOOL*)src, d); break;
361 case DBTYPE_DECIMAL: hr = VarR8FromDec((DECIMAL*)src, d); break;
362 case DBTYPE_VARIANT:
363 VariantInit(&tmp);
364 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_R8)) == S_OK)
365 *d = V_R8(&tmp);
366 break;
367 default: FIXME("Unimplemented conversion %04x -> R8\n", src_type); return E_NOTIMPL;
369 break;
371 case DBTYPE_BOOL:
373 VARIANT_BOOL *d=dst;
374 switch (src_type)
376 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
377 case DBTYPE_I1: hr = VarBoolFromI1(*(signed char*)src, d); break;
378 case DBTYPE_I2: hr = VarBoolFromI2(*(signed short*)src, d); break;
379 case DBTYPE_I4: hr = VarBoolFromI4(*(signed int*)src, d); break;
380 case DBTYPE_I8: hr = VarBoolFromI8(*(LONGLONG*)src, d); break;
381 case DBTYPE_UI1: hr = VarBoolFromUI1(*(BYTE*)src, d); break;
382 case DBTYPE_UI2: hr = VarBoolFromUI2(*(WORD*)src, d); break;
383 case DBTYPE_UI4: hr = VarBoolFromUI4(*(DWORD*)src, d); break;
384 case DBTYPE_UI8: hr = VarBoolFromUI8(*(ULONGLONG*)src, d); break;
385 case DBTYPE_R4: hr = VarBoolFromR4(*(FLOAT*)src, d); break;
386 case DBTYPE_R8: hr = VarBoolFromR8(*(DOUBLE*)src, d); break;
387 case DBTYPE_CY: hr = VarBoolFromCy(*(CY*)src, d); break;
388 case DBTYPE_DATE: hr = VarBoolFromDate(*(DATE*)src, d); break;
389 case DBTYPE_BSTR: hr = VarBoolFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
390 case DBTYPE_BOOL: *d = *(VARIANT_BOOL*)src; hr = S_OK; break;
391 case DBTYPE_DECIMAL: hr = VarBoolFromDec((DECIMAL*)src, d); break;
392 default: FIXME("Unimplemented conversion %04x -> BOOL\n", src_type); return E_NOTIMPL;
394 break;
396 case DBTYPE_DATE:
398 DATE *d=dst;
399 switch (src_type)
401 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
402 case DBTYPE_I1: hr = VarDateFromI1(*(signed char*)src, d); break;
403 case DBTYPE_I2: hr = VarDateFromI2(*(signed short*)src, d); break;
404 case DBTYPE_I4: hr = VarDateFromI4(*(signed int*)src, d); break;
405 case DBTYPE_I8: hr = VarDateFromI8(*(LONGLONG*)src, d); break;
406 case DBTYPE_UI1: hr = VarDateFromUI1(*(BYTE*)src, d); break;
407 case DBTYPE_UI2: hr = VarDateFromUI2(*(WORD*)src, d); break;
408 case DBTYPE_UI4: hr = VarDateFromUI4(*(DWORD*)src, d); break;
409 case DBTYPE_UI8: hr = VarDateFromUI8(*(ULONGLONG*)src, d); break;
410 case DBTYPE_R4: hr = VarDateFromR4(*(FLOAT*)src, d); break;
411 case DBTYPE_R8: hr = VarDateFromR8(*(DOUBLE*)src, d); break;
412 case DBTYPE_CY: hr = VarDateFromCy(*(CY*)src, d); break;
413 case DBTYPE_DATE: *d = *(DATE*)src; hr = S_OK; break;
414 case DBTYPE_BSTR: hr = VarDateFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
415 case DBTYPE_BOOL: hr = VarDateFromBool(*(VARIANT_BOOL*)src, d); break;
416 case DBTYPE_DECIMAL: hr = VarDateFromDec((DECIMAL*)src, d); break;
417 case DBTYPE_DBTIMESTAMP:
419 SYSTEMTIME st;
420 DBTIMESTAMP *ts=(DBTIMESTAMP*)src;
422 st.wYear = ts->year;
423 st.wMonth = ts->month;
424 st.wDay = ts->day;
425 st.wHour = ts->hour;
426 st.wMinute = ts->minute;
427 st.wSecond = ts->second;
428 st.wMilliseconds = ts->fraction/1000000;
429 hr = (SystemTimeToVariantTime(&st, d) ? S_OK : E_FAIL);
430 break;
432 default: FIXME("Unimplemented conversion %04x -> DATE\n", src_type); return E_NOTIMPL;
434 break;
436 case DBTYPE_DBDATE:
438 DBDATE *d=dst;
439 switch (src_type)
441 case DBTYPE_DBDATE: memcpy(d, src, sizeof(DBDATE)); hr = S_OK; break;
442 case DBTYPE_BSTR:
444 VARIANT var;
445 BSTR s = *(WCHAR**)src;
447 VariantInit(&var);
448 V_VT(&var) = VT_BSTR;
449 V_BSTR(&var) = SysAllocString(s);
451 if ((hr = VariantChangeType(&var, &var, 0, VT_DATE)) == S_OK)
453 SYSTEMTIME st;
454 hr = (VariantTimeToSystemTime( V_DATE(&var), &st) ? S_OK : E_FAIL);
455 d->year = st.wYear;
456 d->month = st.wMonth;
457 d->day = st.wDay;
460 VariantClear(&var);
462 break;
463 case DBTYPE_VARIANT:
464 if( V_VT((VARIANT*)src) == VT_DATE)
466 SYSTEMTIME st;
467 hr = (VariantTimeToSystemTime( V_DATE((VARIANT*)src), &st) ? S_OK : E_FAIL);
468 d->year = st.wYear;
469 d->month = st.wMonth;
470 d->day = st.wDay;
472 else
474 FIXME("Unimplemented variant type %d -> DBDATE\n", V_VT((VARIANT*)src));
475 return E_NOTIMPL;
477 break;
478 default: FIXME("Unimplemented conversion %04x -> DBDATE\n", src_type); return E_NOTIMPL;
480 break;
482 case DBTYPE_DBTIMESTAMP:
484 DBTIMESTAMP *d=dst;
485 switch (src_type)
487 case DBTYPE_EMPTY: memset(d, 0, sizeof(DBTIMESTAMP)); hr = S_OK; break;
488 case DBTYPE_DBTIMESTAMP: memcpy(d, src, sizeof(DBTIMESTAMP)); hr = S_OK; break;
489 case DBTYPE_BSTR:
491 VARIANT var;
492 BSTR s = *(WCHAR**)src;
494 VariantInit(&var);
495 V_VT(&var) = VT_BSTR;
496 V_BSTR(&var) = SysAllocString(s);
498 if ((hr = VariantChangeType(&var, &var, 0, VT_DATE)) == S_OK)
500 SYSTEMTIME st;
502 hr = (VariantTimeToSystemTime( V_DATE(&var), &st) ? S_OK : E_FAIL);
503 d->year = st.wYear;
504 d->month = st.wMonth;
505 d->day = st.wDay;
506 d->hour = st.wHour;
507 d->minute = st.wMinute;
508 d->second = st.wSecond;
509 d->fraction = st.wMilliseconds * 1000000;
512 VariantClear(&var);
514 break;
515 case DBTYPE_DATE:
517 SYSTEMTIME st;
518 hr = (VariantTimeToSystemTime(*(double*)src, &st) ? S_OK : E_FAIL);
519 d->year = st.wYear;
520 d->month = st.wMonth;
521 d->day = st.wDay;
522 d->hour = st.wHour;
523 d->minute = st.wMinute;
524 d->second = st.wSecond;
525 d->fraction = st.wMilliseconds * 1000000;
526 break;
528 case DBTYPE_VARIANT:
529 if( V_VT((VARIANT*)src) == VT_DATE)
531 SYSTEMTIME st;
532 hr = (VariantTimeToSystemTime( V_DATE((VARIANT*)src), &st) ? S_OK : E_FAIL);
533 d->year = st.wYear;
534 d->month = st.wMonth;
535 d->day = st.wDay;
536 d->hour = st.wHour;
537 d->minute = st.wMinute;
538 d->second = st.wSecond;
539 d->fraction = st.wMilliseconds * 1000000;
541 else
543 FIXME("Unimplemented variant type %d -> DBTIMESTAMP\n", V_VT((VARIANT*)src));
544 return E_NOTIMPL;
546 break;
547 default: FIXME("Unimplemented conversion %04x -> DBTIMESTAMP\n", src_type); return E_NOTIMPL;
549 break;
552 case DBTYPE_CY:
554 CY *d = dst;
555 switch(src_type)
557 case DBTYPE_EMPTY: d->int64 = 0; hr = S_OK; break;
558 case DBTYPE_I2: hr = VarCyFromI2(*(signed short*)src, d); break;
559 case DBTYPE_I4: hr = VarCyFromI4(*(signed int*)src, d); break;
560 case DBTYPE_R4: hr = VarCyFromR4(*(FLOAT*)src, d); break;
561 case DBTYPE_R8: hr = VarCyFromR8(*(double*)src, d); break;
562 case DBTYPE_CY: *d = *(CY*)src; hr = S_OK; break;
563 case DBTYPE_DATE: hr = VarCyFromDate(*(DATE*)src, d); break;
564 case DBTYPE_BSTR: hr = VarCyFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
565 case DBTYPE_BOOL: hr = VarCyFromBool(*(VARIANT_BOOL*)src, d); break;
566 case DBTYPE_DECIMAL: hr = VarCyFromDec((DECIMAL*)src, d); break;
567 case DBTYPE_I1: hr = VarCyFromI1(*(signed char*)src, d); break;
568 case DBTYPE_UI1: hr = VarCyFromUI1(*(BYTE*)src, d); break;
569 case DBTYPE_UI2: hr = VarCyFromUI2(*(WORD*)src, d); break;
570 case DBTYPE_UI4: hr = VarCyFromUI4(*(DWORD*)src, d); break;
571 case DBTYPE_I8: hr = VarCyFromI8(*(LONGLONG*)src, d); break;
572 case DBTYPE_UI8: hr = VarCyFromUI8(*(ULONGLONG*)src, d); break;
573 default: FIXME("Unimplemented conversion %04x -> CY\n", src_type); return E_NOTIMPL;
575 break;
578 case DBTYPE_BSTR:
580 BSTR *d = dst;
581 switch(src_type)
583 case DBTYPE_EMPTY: *d = SysAllocStringLen(NULL, 0); hr = *d ? S_OK : E_OUTOFMEMORY; break;
584 case DBTYPE_I2: hr = VarBstrFromI2(*(signed short*)src, LOCALE_USER_DEFAULT, 0, d); break;
585 case DBTYPE_I4: hr = VarBstrFromI4(*(signed int*)src, LOCALE_USER_DEFAULT, 0, d); break;
586 case DBTYPE_R4: hr = VarBstrFromR4(*(FLOAT*)src, LOCALE_USER_DEFAULT, 0, d); break;
587 case DBTYPE_R8: hr = VarBstrFromR8(*(double*)src, LOCALE_USER_DEFAULT, 0, d); break;
588 case DBTYPE_CY: hr = VarBstrFromCy(*(CY*)src, LOCALE_USER_DEFAULT, 0, d); break;
589 case DBTYPE_DATE: hr = VarBstrFromDate(*(DATE*)src, LOCALE_USER_DEFAULT, 0, d); break;
590 case DBTYPE_BSTR: *d = SysAllocStringLen(*(BSTR*)src, SysStringLen(*(BSTR*)src)); hr = *d ? S_OK : E_OUTOFMEMORY; break;
591 case DBTYPE_BOOL: hr = VarBstrFromBool(*(VARIANT_BOOL*)src, LOCALE_USER_DEFAULT, 0, d); break;
592 case DBTYPE_DECIMAL: hr = VarBstrFromDec((DECIMAL*)src, LOCALE_USER_DEFAULT, 0, d); break;
593 case DBTYPE_I1: hr = VarBstrFromI1(*(signed char*)src, LOCALE_USER_DEFAULT, 0, d); break;
594 case DBTYPE_UI1: hr = VarBstrFromUI1(*(BYTE*)src, LOCALE_USER_DEFAULT, 0, d); break;
595 case DBTYPE_UI2: hr = VarBstrFromUI2(*(WORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
596 case DBTYPE_UI4: hr = VarBstrFromUI4(*(DWORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
597 case DBTYPE_I8: hr = VarBstrFromI8(*(LONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
598 case DBTYPE_UI8: hr = VarBstrFromUI8(*(ULONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
599 case DBTYPE_GUID:
601 WCHAR szBuff[39];
602 const GUID *id = (const GUID *)src;
603 WCHAR format[] = {
604 '{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
605 '%','0','2','X','%','0','2','X','-',
606 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
607 wsprintfW(szBuff, format,
608 id->Data1, id->Data2, id->Data3,
609 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
610 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
611 *d = SysAllocString(szBuff);
612 hr = *d ? S_OK : E_OUTOFMEMORY;
614 break;
615 case DBTYPE_BYTES:
617 *d = SysAllocStringLen(NULL, 2 * src_len);
618 if (*d == NULL)
619 hr = E_OUTOFMEMORY;
620 else
622 const char hexchars[] = "0123456789ABCDEF";
623 WCHAR *s = *d;
624 unsigned char *p = src;
625 while (src_len > 0)
627 *s++ = hexchars[(*p >> 4) & 0x0F];
628 *s++ = hexchars[(*p) & 0x0F];
629 src_len--; p++;
631 hr = S_OK;
634 break;
635 case DBTYPE_VARIANT:
636 if(V_VT((VARIANT*)src) == VT_NULL)
638 *dst_status = DBSTATUS_S_ISNULL;
639 *dst_len = get_length(DBTYPE_BSTR);
640 return S_OK;
642 else
644 VariantInit(&tmp);
645 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_BSTR)) == S_OK)
646 *d = V_BSTR(&tmp);
648 break;
649 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type); return E_NOTIMPL;
651 break;
654 case DBTYPE_UI1:
656 BYTE *d = dst;
657 switch(src_type)
659 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
660 case DBTYPE_I2: hr = VarUI1FromI2(*(signed short*)src, d); break;
661 case DBTYPE_I4: hr = VarUI1FromI4(*(signed int*)src, d); break;
662 case DBTYPE_R4: hr = VarUI1FromR4(*(FLOAT*)src, d); break;
663 case DBTYPE_R8: hr = VarUI1FromR8(*(double*)src, d); break;
664 case DBTYPE_CY: hr = VarUI1FromCy(*(CY*)src, d); break;
665 case DBTYPE_DATE: hr = VarUI1FromDate(*(DATE*)src, d); break;
666 case DBTYPE_BSTR: hr = VarUI1FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
667 case DBTYPE_BOOL: hr = VarUI1FromBool(*(VARIANT_BOOL*)src, d); break;
668 case DBTYPE_DECIMAL: hr = VarUI1FromDec((DECIMAL*)src, d); break;
669 case DBTYPE_I1: hr = VarUI1FromI1(*(signed char*)src, d); break;
670 case DBTYPE_UI1: *d = *(BYTE*)src; hr = S_OK; break;
671 case DBTYPE_UI2: hr = VarUI1FromUI2(*(WORD*)src, d); break;
672 case DBTYPE_UI4: hr = VarUI1FromUI4(*(DWORD*)src, d); break;
673 case DBTYPE_I8: hr = VarUI1FromI8(*(LONGLONG*)src, d); break;
674 case DBTYPE_UI8: hr = VarUI1FromUI8(*(ULONGLONG*)src, d); break;
675 default: FIXME("Unimplemented conversion %04x -> UI1\n", src_type); return E_NOTIMPL;
677 break;
679 case DBTYPE_UI2:
681 WORD *d = dst;
682 switch(src_type)
684 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
685 case DBTYPE_I2: hr = VarUI2FromI2(*(signed short*)src, d); break;
686 case DBTYPE_I4: hr = VarUI2FromI4(*(signed int*)src, d); break;
687 case DBTYPE_R4: hr = VarUI2FromR4(*(FLOAT*)src, d); break;
688 case DBTYPE_R8: hr = VarUI2FromR8(*(double*)src, d); break;
689 case DBTYPE_CY: hr = VarUI2FromCy(*(CY*)src, d); break;
690 case DBTYPE_DATE: hr = VarUI2FromDate(*(DATE*)src, d); break;
691 case DBTYPE_BSTR: hr = VarUI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
692 case DBTYPE_BOOL: hr = VarUI2FromBool(*(VARIANT_BOOL*)src, d); break;
693 case DBTYPE_DECIMAL: hr = VarUI2FromDec((DECIMAL*)src, d); break;
694 case DBTYPE_I1: hr = VarUI2FromI1(*(signed char*)src, d); break;
695 case DBTYPE_UI1: hr = VarUI2FromUI1(*(BYTE*)src, d); break;
696 case DBTYPE_UI2: *d = *(WORD*)src; hr = S_OK; break;
697 case DBTYPE_UI4: hr = VarUI2FromUI4(*(DWORD*)src, d); break;
698 case DBTYPE_I8: hr = VarUI2FromI8(*(LONGLONG*)src, d); break;
699 case DBTYPE_UI8: hr = VarUI2FromUI8(*(ULONGLONG*)src, d); break;
700 default: FIXME("Unimplemented conversion %04x -> UI2\n", src_type); return E_NOTIMPL;
702 break;
705 case DBTYPE_UI4:
707 DWORD *d = dst;
708 switch(src_type)
710 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
711 case DBTYPE_I2: hr = VarUI4FromI2(*(signed short*)src, d); break;
712 case DBTYPE_I4: hr = VarUI4FromI4(*(signed int*)src, d); break;
713 case DBTYPE_R4: hr = VarUI4FromR4(*(FLOAT*)src, d); break;
714 case DBTYPE_R8: hr = VarUI4FromR8(*(double*)src, d); break;
715 case DBTYPE_CY: hr = VarUI4FromCy(*(CY*)src, d); break;
716 case DBTYPE_DATE: hr = VarUI4FromDate(*(DATE*)src, d); break;
717 case DBTYPE_BSTR: hr = VarUI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
718 case DBTYPE_BOOL: hr = VarUI4FromBool(*(VARIANT_BOOL*)src, d); break;
719 case DBTYPE_DECIMAL: hr = VarUI4FromDec((DECIMAL*)src, d); break;
720 case DBTYPE_I1: hr = VarUI4FromI1(*(signed char*)src, d); break;
721 case DBTYPE_UI1: hr = VarUI4FromUI1(*(BYTE*)src, d); break;
722 case DBTYPE_UI2: hr = VarUI4FromUI2(*(WORD*)src, d); break;
723 case DBTYPE_UI4: *d = *(DWORD*)src; hr = S_OK; break;
724 case DBTYPE_I8: hr = VarUI4FromI8(*(LONGLONG*)src, d); break;
725 case DBTYPE_UI8: hr = VarUI4FromUI8(*(ULONGLONG*)src, d); break;
726 case DBTYPE_VARIANT:
727 VariantInit(&tmp);
728 if ((hr = VariantChangeType(&tmp, (VARIANT*)src, 0, VT_UI4)) == S_OK)
729 *d = V_UI4(&tmp);
730 break;
731 default: FIXME("Unimplemented conversion %04x -> UI4\n", src_type); return E_NOTIMPL;
733 break;
736 case DBTYPE_UI8:
738 ULONGLONG *d = dst;
739 switch(src_type)
741 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
742 case DBTYPE_I2: hr = VarUI8FromI2(*(signed short*)src, d); break;
743 case DBTYPE_I4: {LONGLONG s = *(signed int*)src; hr = VarUI8FromI8(s, d); break;}
744 case DBTYPE_R4: hr = VarUI8FromR4(*(FLOAT*)src, d); break;
745 case DBTYPE_R8: hr = VarUI8FromR8(*(double*)src, d); break;
746 case DBTYPE_CY: hr = VarUI8FromCy(*(CY*)src, d); break;
747 case DBTYPE_DATE: hr = VarUI8FromDate(*(DATE*)src, d); break;
748 case DBTYPE_BSTR: hr = VarUI8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
749 case DBTYPE_BOOL: hr = VarUI8FromBool(*(VARIANT_BOOL*)src, d); break;
750 case DBTYPE_DECIMAL: hr = VarUI8FromDec((DECIMAL*)src, d); break;
751 case DBTYPE_I1: hr = VarUI8FromI1(*(signed char*)src, d); break;
752 case DBTYPE_UI1: hr = VarUI8FromUI1(*(BYTE*)src, d); break;
753 case DBTYPE_UI2: hr = VarUI8FromUI2(*(WORD*)src, d); break;
754 case DBTYPE_UI4: hr = VarUI8FromUI4(*(DWORD*)src, d); break;
755 case DBTYPE_I8: hr = VarUI8FromI8(*(LONGLONG*)src, d); break;
756 case DBTYPE_UI8: *d = *(ULONGLONG*)src; hr = S_OK; break;
757 default: FIXME("Unimplemented conversion %04x -> UI8\n", src_type); return E_NOTIMPL;
759 break;
762 case DBTYPE_FILETIME:
764 FILETIME *d = dst;
765 switch(src_type)
767 case DBTYPE_EMPTY: d->dwLowDateTime = d->dwHighDateTime = 0; hr = S_OK; break;
768 case DBTYPE_FILETIME: *d = *(FILETIME*)src; hr = S_OK; break;
769 default: FIXME("Unimplemented conversion %04x -> FILETIME\n", src_type); return E_NOTIMPL;
771 break;
774 case DBTYPE_GUID:
776 GUID *d = dst;
777 switch(src_type)
779 case DBTYPE_EMPTY: *d = GUID_NULL; hr = S_OK; break;
780 case DBTYPE_GUID: *d = *(GUID*)src; hr = S_OK; break;
781 default: FIXME("Unimplemented conversion %04x -> GUID\n", src_type); return E_NOTIMPL;
783 break;
786 case DBTYPE_WSTR:
788 BSTR b;
789 DBLENGTH bstr_len;
790 INT bytes_to_copy;
791 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
792 src, &b, sizeof(BSTR), src_status, dst_status,
793 precision, scale, flags);
794 if(hr != S_OK || *dst_status == DBSTATUS_S_ISNULL)
795 return hr;
796 bstr_len = SysStringLen(b);
797 *dst_len = bstr_len * sizeof(WCHAR); /* Doesn't include size for '\0' */
798 *dst_status = DBSTATUS_S_OK;
799 bytes_to_copy = min(*dst_len + sizeof(WCHAR), dst_max_len);
800 if(dst)
802 if(bytes_to_copy >= sizeof(WCHAR))
804 memcpy(dst, b, bytes_to_copy - sizeof(WCHAR));
805 *((WCHAR*)dst + bytes_to_copy / sizeof(WCHAR) - 1) = 0;
806 if(bytes_to_copy < *dst_len + sizeof(WCHAR))
807 *dst_status = DBSTATUS_S_TRUNCATED;
809 else
811 *dst_status = DBSTATUS_E_DATAOVERFLOW;
812 hr = DB_E_ERRORSOCCURRED;
815 SysFreeString(b);
816 return hr;
818 case DBTYPE_STR:
820 BSTR b;
821 DBLENGTH bstr_len;
822 INT bytes_to_copy;
823 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
824 src, &b, sizeof(BSTR), src_status, dst_status,
825 precision, scale, flags);
826 if(hr != S_OK) return hr;
827 bstr_len = SysStringLen(b);
828 *dst_len = bstr_len * sizeof(char); /* Doesn't include size for '\0' */
829 *dst_status = DBSTATUS_S_OK;
830 bytes_to_copy = min(*dst_len + sizeof(char), dst_max_len);
831 if(dst)
833 if(bytes_to_copy >= sizeof(char))
835 WideCharToMultiByte(CP_ACP, 0, b, bytes_to_copy - sizeof(char), dst, dst_max_len, NULL, NULL);
836 *((char *)dst + bytes_to_copy / sizeof(char) - 1) = 0;
837 if(bytes_to_copy < *dst_len + sizeof(char))
838 *dst_status = DBSTATUS_S_TRUNCATED;
840 else
842 *dst_status = DBSTATUS_E_DATAOVERFLOW;
843 hr = DB_E_ERRORSOCCURRED;
846 SysFreeString(b);
847 return hr;
850 case DBTYPE_BYREF | DBTYPE_WSTR:
852 BSTR b;
853 WCHAR **d = dst;
854 DBLENGTH bstr_len;
855 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
856 src, &b, sizeof(BSTR), src_status, dst_status,
857 precision, scale, flags);
858 if(hr != S_OK) return hr;
860 bstr_len = SysStringLen(b) * sizeof(WCHAR);
861 *dst_len = bstr_len; /* Doesn't include size for '\0' */
863 *d = CoTaskMemAlloc(bstr_len + sizeof(WCHAR));
864 if(*d) memcpy(*d, b, bstr_len + sizeof(WCHAR));
865 else hr = E_OUTOFMEMORY;
866 SysFreeString(b);
867 return hr;
870 case DBTYPE_VARIANT:
872 VARIANT *v = dst;
874 switch(src_type)
876 case DBTYPE_BOOL:
877 V_VT(v) = VT_BOOL;
878 V_BOOL(v) = *(VARIANT_BOOL*)src;
879 hr = S_OK;
880 break;
881 case DBTYPE_I2:
882 V_VT(v) = VT_I2;
883 V_I2(v) = *(signed short*)src;
884 hr = S_OK;
885 break;
886 case DBTYPE_I4:
887 V_VT(v) = VT_I4;
888 V_I4(v) = *(signed int*)src;
889 hr = S_OK;
890 break;
891 case DBTYPE_I8:
892 V_VT(v) = VT_DECIMAL;
893 hr = VarDecFromI8( *(LONGLONG*)src, &V_DECIMAL(v));
894 break;
895 case DBTYPE_R4:
896 V_VT(v) = VT_R4;
897 V_R4(v) = *(FLOAT*)src;
898 hr = S_OK;
899 break;
900 case DBTYPE_R8:
901 V_VT(v) = VT_R8;
902 V_R8(v) = *(double*)src;
903 hr = S_OK;
904 break;
905 case DBTYPE_BSTR:
907 BSTR s = *(WCHAR**)src;
908 TRACE("%s\n", debugstr_w(s));
909 V_VT(v) = VT_BSTR;
910 V_BSTR(v) = SysAllocString(s);
911 hr = V_BSTR(v) ? S_OK : E_OUTOFMEMORY;
912 break;
914 case DBTYPE_DATE:
915 V_VT(v) = VT_DATE;
916 V_DATE(v) = *(DATE*)src;
917 hr = S_OK;
918 break;
919 case DBTYPE_DBDATE:
921 SYSTEMTIME st;
922 DBDATE *ts=(DBDATE*)src;
924 V_VT(v) = VT_DATE;
926 st.wYear = ts->year;
927 st.wMonth = ts->month;
928 st.wDay = ts->day;
929 st.wHour = 0;
930 st.wMinute = 0;
931 st.wSecond = 0;
932 st.wMilliseconds = 0;
933 hr = (SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL);
934 break;
936 case DBTYPE_DBTIMESTAMP:
938 SYSTEMTIME st;
939 DBTIMESTAMP *ts = (DBTIMESTAMP *)src;
941 V_VT(v) = VT_DATE;
943 st.wYear = ts->year;
944 st.wMonth = ts->month;
945 st.wDay = ts->day;
946 st.wHour = ts->hour;
947 st.wMinute = ts->minute;
948 st.wSecond = ts->second;
949 st.wMilliseconds = ts->fraction/1000000;
950 hr = SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL;
951 break;
953 case DBTYPE_CY:
954 V_VT(v) = VT_CY;
955 V_CY(v) = *(CY*)src;
956 hr = S_OK;
957 break;
958 case DBTYPE_BYTES:
960 LONG i;
961 SAFEARRAY *psa = NULL;
962 SAFEARRAYBOUND rgsabound[1];
963 unsigned char *p = src;
965 rgsabound[0].lLbound = 0;
966 rgsabound[0].cElements = src_len;
968 psa = SafeArrayCreate(VT_UI1,1,rgsabound);
969 for(i =0; i < src_len; i++,p++)
971 hr = SafeArrayPutElement(psa, &i, p);
972 if(FAILED(hr)) {
973 SafeArrayDestroy (psa);
974 return hr;
978 V_VT(v) = VT_ARRAY|VT_UI1;
979 V_ARRAY(v) = psa;
980 hr = S_OK;
981 break;
983 default: FIXME("Unimplemented conversion %04x -> VARIANT\n", src_type); return E_NOTIMPL;
985 break;
987 case DBTYPE_BYTES:
989 BYTE *d = dst;
991 switch(src_type)
993 case DBTYPE_BYTES:
994 if( src_len > dst_max_len)
995 *dst_status = DBSTATUS_S_TRUNCATED;
996 else
997 *dst_status = DBSTATUS_S_OK;
999 *dst_len = src_len;
1000 memcpy(d, src, min(src_len, dst_max_len));
1002 return S_OK;
1003 case DBTYPE_VARIANT:
1005 if(V_VT((VARIANT*)src) == VT_NULL)
1007 *dst_status = DBSTATUS_S_ISNULL;
1008 *dst_len = 0;
1009 return S_OK;
1011 else
1013 switch(V_VT((VARIANT*)src))
1015 case VT_UI1 | VT_ARRAY:
1017 LONG l;
1018 BYTE *data = NULL;
1020 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1021 if(FAILED(hr))
1022 return hr;
1024 hr = SafeArrayAccessData(V_ARRAY((VARIANT*)src), (VOID**)&data);
1025 if(FAILED(hr))
1027 ERR("SafeArrayAccessData Failed = 0x%08x\n", hr);
1028 return hr;
1031 *dst_len = l+1;
1032 *dst_status = DBSTATUS_S_OK;
1033 memcpy(d, data, *dst_len);
1035 SafeArrayUnaccessData(V_ARRAY((VARIANT*)src));
1036 return S_OK;
1038 break;
1039 default:
1040 FIXME("Unimplemented variant type %d -> BYTES\n", V_VT((VARIANT*)src));
1041 return E_NOTIMPL;
1045 break;
1046 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES\n", src_type); return E_NOTIMPL;
1048 break;
1050 case DBTYPE_BYTES | DBTYPE_BYREF:
1052 BYTE **d = dst;
1054 switch(src_type)
1056 case DBTYPE_BYTES:
1057 *d = CoTaskMemAlloc(src_len);
1058 if(*d) memcpy(*d, src, src_len);
1059 else hr = E_OUTOFMEMORY;
1061 *dst_len = src_len;
1062 *dst_status = DBSTATUS_S_OK;
1063 return S_OK;
1064 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES | DBTYPE_BYREF\n", src_type); return E_NOTIMPL;
1066 break;
1068 break;
1070 default:
1071 FIXME("Unimplemented conversion %04x -> %04x\n", src_type, dst_type);
1072 return E_NOTIMPL;
1075 if(hr == DISP_E_OVERFLOW)
1077 *dst_status = DBSTATUS_E_DATAOVERFLOW;
1078 *dst_len = get_length(dst_type);
1079 hr = DB_E_ERRORSOCCURRED;
1081 else if(hr == S_OK)
1083 *dst_status = DBSTATUS_S_OK;
1084 *dst_len = get_length(dst_type);
1087 return hr;
1090 static inline WORD get_dbtype_class(DBTYPE type)
1092 switch(type)
1094 case DBTYPE_I2:
1095 case DBTYPE_R4:
1096 case DBTYPE_R8:
1097 case DBTYPE_I1:
1098 case DBTYPE_UI1:
1099 case DBTYPE_UI2:
1100 return DBTYPE_I2;
1102 case DBTYPE_I4:
1103 case DBTYPE_UI4:
1104 return DBTYPE_I4;
1106 case DBTYPE_I8:
1107 case DBTYPE_UI8:
1108 return DBTYPE_I8;
1110 case DBTYPE_BSTR:
1111 case DBTYPE_STR:
1112 case DBTYPE_WSTR:
1113 return DBTYPE_BSTR;
1115 case DBTYPE_DBDATE:
1116 case DBTYPE_DBTIME:
1117 case DBTYPE_DBTIMESTAMP:
1118 return DBTYPE_DBDATE;
1120 return type;
1123 /* Many src types will convert to this group of dst types */
1124 static inline BOOL common_class(WORD dst_class)
1126 switch(dst_class)
1128 case DBTYPE_EMPTY:
1129 case DBTYPE_NULL:
1130 case DBTYPE_I2:
1131 case DBTYPE_I4:
1132 case DBTYPE_BSTR:
1133 case DBTYPE_BOOL:
1134 case DBTYPE_VARIANT:
1135 case DBTYPE_I8:
1136 case DBTYPE_CY:
1137 case DBTYPE_DECIMAL:
1138 case DBTYPE_NUMERIC:
1139 return TRUE;
1141 return FALSE;
1144 static inline BOOL array_type(DBTYPE type)
1146 return (type >= DBTYPE_I2 && type <= DBTYPE_UI4);
1149 static HRESULT WINAPI convert_CanConvert(IDataConvert* iface,
1150 DBTYPE src_type, DBTYPE dst_type)
1152 convert *This = impl_from_IDataConvert(iface);
1153 DBTYPE src_base_type = src_type & 0x1ff;
1154 DBTYPE dst_base_type = dst_type & 0x1ff;
1155 WORD dst_class = get_dbtype_class(dst_base_type);
1157 TRACE("(%p)->(%d, %d)\n", This, src_type, dst_type);
1159 if(src_type & DBTYPE_VECTOR || dst_type & DBTYPE_VECTOR) return S_FALSE;
1161 if(src_type & DBTYPE_ARRAY)
1163 if(!array_type(src_base_type)) return S_FALSE;
1164 if(dst_type & DBTYPE_ARRAY)
1166 if(src_type == dst_type) return S_OK;
1167 return S_FALSE;
1169 if(dst_type == DBTYPE_VARIANT) return S_OK;
1170 return S_FALSE;
1173 if(dst_type & DBTYPE_ARRAY)
1175 if(!array_type(dst_base_type)) return S_FALSE;
1176 if(src_type == DBTYPE_IDISPATCH || src_type == DBTYPE_VARIANT) return S_OK;
1177 return S_FALSE;
1180 if(dst_type & DBTYPE_BYREF)
1181 if(dst_base_type != DBTYPE_BYTES && dst_base_type != DBTYPE_STR && dst_base_type != DBTYPE_WSTR)
1182 return S_FALSE;
1184 switch(get_dbtype_class(src_base_type))
1186 case DBTYPE_EMPTY:
1187 if(common_class(dst_class)) return S_OK;
1188 switch(dst_class)
1190 case DBTYPE_DATE:
1191 case DBTYPE_GUID:
1192 case DBTYPE_FILETIME:
1193 return S_OK;
1194 default:
1195 if(dst_base_type == DBTYPE_DBTIMESTAMP) return S_OK;
1196 return S_FALSE;
1199 case DBTYPE_NULL:
1200 switch(dst_base_type)
1202 case DBTYPE_NULL:
1203 case DBTYPE_VARIANT:
1204 case DBTYPE_FILETIME: return S_OK;
1205 default: return S_FALSE;
1208 case DBTYPE_I4:
1209 if(dst_base_type == DBTYPE_BYTES) return S_OK;
1210 /* fall through */
1211 case DBTYPE_I2:
1212 if(dst_base_type == DBTYPE_DATE) return S_OK;
1213 /* fall through */
1214 case DBTYPE_DECIMAL:
1215 if(common_class(dst_class)) return S_OK;
1216 if(dst_class == DBTYPE_DBDATE) return S_OK;
1217 return S_FALSE;
1219 case DBTYPE_BOOL:
1220 if(dst_base_type == DBTYPE_DATE) return S_OK;
1221 /* fall through */
1222 case DBTYPE_NUMERIC:
1223 case DBTYPE_CY:
1224 if(common_class(dst_class)) return S_OK;
1225 return S_FALSE;
1227 case DBTYPE_I8:
1228 if(common_class(dst_class)) return S_OK;
1229 switch(dst_base_type)
1231 case DBTYPE_BYTES:
1232 case DBTYPE_FILETIME: return S_OK;
1233 default: return S_FALSE;
1236 case DBTYPE_DATE:
1237 switch(dst_class)
1239 case DBTYPE_EMPTY:
1240 case DBTYPE_NULL:
1241 case DBTYPE_I2:
1242 case DBTYPE_I4:
1243 case DBTYPE_BSTR:
1244 case DBTYPE_BOOL:
1245 case DBTYPE_VARIANT:
1246 case DBTYPE_I8:
1247 case DBTYPE_DATE:
1248 case DBTYPE_DBDATE:
1249 case DBTYPE_FILETIME:
1250 return S_OK;
1251 default: return S_FALSE;
1254 case DBTYPE_IDISPATCH:
1255 case DBTYPE_VARIANT:
1256 switch(dst_base_type)
1258 case DBTYPE_IDISPATCH:
1259 case DBTYPE_ERROR:
1260 case DBTYPE_IUNKNOWN:
1261 return S_OK;
1263 /* fall through */
1264 case DBTYPE_BSTR:
1265 if(common_class(dst_class)) return S_OK;
1266 switch(dst_class)
1268 case DBTYPE_DATE:
1269 case DBTYPE_GUID:
1270 case DBTYPE_BYTES:
1271 case DBTYPE_DBDATE:
1272 case DBTYPE_FILETIME:
1273 return S_OK;
1274 default: return S_FALSE;
1277 case DBTYPE_ERROR:
1278 switch(dst_base_type)
1280 case DBTYPE_BSTR:
1281 case DBTYPE_ERROR:
1282 case DBTYPE_VARIANT:
1283 case DBTYPE_WSTR:
1284 return S_OK;
1285 default: return S_FALSE;
1288 case DBTYPE_IUNKNOWN:
1289 switch(dst_base_type)
1291 case DBTYPE_EMPTY:
1292 case DBTYPE_NULL:
1293 case DBTYPE_IDISPATCH:
1294 case DBTYPE_VARIANT:
1295 case DBTYPE_IUNKNOWN:
1296 return S_OK;
1297 default: return S_FALSE;
1300 case DBTYPE_BYTES:
1301 if(dst_class == DBTYPE_I4 || dst_class == DBTYPE_I8) return S_OK;
1302 /* fall through */
1303 case DBTYPE_GUID:
1304 switch(dst_class)
1306 case DBTYPE_EMPTY:
1307 case DBTYPE_NULL:
1308 case DBTYPE_BSTR:
1309 case DBTYPE_VARIANT:
1310 case DBTYPE_GUID:
1311 case DBTYPE_BYTES:
1312 return S_OK;
1313 default: return S_FALSE;
1316 case DBTYPE_FILETIME:
1317 if(dst_class == DBTYPE_I8) return S_OK;
1318 /* fall through */
1319 case DBTYPE_DBDATE:
1320 switch(dst_class)
1322 case DBTYPE_EMPTY:
1323 case DBTYPE_NULL:
1324 case DBTYPE_DATE:
1325 case DBTYPE_BSTR:
1326 case DBTYPE_VARIANT:
1327 case DBTYPE_DBDATE:
1328 case DBTYPE_FILETIME:
1329 return S_OK;
1330 default: return S_FALSE;
1334 return S_FALSE;
1337 static HRESULT WINAPI convert_GetConversionSize(IDataConvert* iface,
1338 DBTYPE src_type, DBTYPE dst_type,
1339 DBLENGTH *src_len, DBLENGTH *dst_len,
1340 void *src)
1342 convert *This = impl_from_IDataConvert(iface);
1343 HRESULT hr;
1345 TRACE("(%p)->(%d, %d, %p, %p, %p)\n", This, src_type, dst_type, src_len, dst_len, src);
1347 hr = IDataConvert_CanConvert(iface, src_type, dst_type);
1348 if (hr != S_OK)
1349 return DB_E_UNSUPPORTEDCONVERSION;
1351 if (!dst_len)
1352 return E_INVALIDARG;
1354 /* for some types we don't need to look into source data */
1355 if ((*dst_len = get_length(dst_type)))
1356 return S_OK;
1358 switch (dst_type)
1360 case DBTYPE_STR:
1361 switch (src_type)
1363 case DBTYPE_VARIANT:
1365 VARIANT v;
1367 VariantInit(&v);
1368 if ((hr = VariantChangeType(&v, (VARIANT*)src, 0, VT_BSTR)) == S_OK)
1370 *dst_len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), -1, NULL, 0, NULL, NULL);
1371 VariantClear(&v);
1373 else
1374 return hr;
1376 break;
1377 default:
1378 FIXME("unimplemented for %04x -> DBTYPE_STR\n", src_type);
1379 return E_NOTIMPL;
1381 break;
1382 case DBTYPE_WSTR:
1383 switch (src_type)
1385 case DBTYPE_VARIANT:
1386 if(V_VT((VARIANT*)src) == VT_BSTR)
1387 *dst_len = (SysStringLen(V_BSTR((VARIANT*)src))+1) * sizeof(WCHAR);
1388 else
1389 WARN("DBTYPE_VARIANT(%d)->DBTYPE_WSTR unimplemented\n", V_VT((VARIANT*)src));
1390 break;
1391 case DBTYPE_STR:
1392 if(src_len)
1393 *dst_len = (*src_len + 1) * sizeof(WCHAR);
1394 else
1395 *dst_len = (strlen(src) + 1) * sizeof(WCHAR);
1396 break;
1397 case DBTYPE_WSTR:
1398 if(src_len)
1399 *dst_len = (*src_len) + sizeof(WCHAR);
1400 else
1401 *dst_len = (lstrlenW(src) + 1) * sizeof(WCHAR);
1402 break;
1403 default:
1404 FIXME("unimplemented for %04x -> DBTYPE_WSTR\n", src_type);
1405 return E_NOTIMPL;
1407 break;
1408 case DBTYPE_BYTES:
1409 switch (src_type)
1411 case DBTYPE_VARIANT:
1412 if(V_VT((VARIANT*)src) == VT_BSTR)
1413 *dst_len = (SysStringLen(V_BSTR((VARIANT*)src))) / sizeof(WCHAR);
1414 else
1416 switch(V_VT((VARIANT*)src))
1418 case VT_UI1 | VT_ARRAY:
1420 LONG l;
1422 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1423 if(FAILED(hr))
1424 return hr;
1425 *dst_len = l+1;
1427 break;
1429 default:
1430 WARN("DBTYPE_VARIANT(%d)->DBTYPE_BYTES unimplemented\n", V_VT((VARIANT*)src));
1433 break;
1434 default:
1435 FIXME("unimplemented for %04x -> DBTYPE_BYTES\n", src_type);
1436 return E_NOTIMPL;
1438 break;
1439 default:
1440 FIXME("unimplemented for conversion %d->%d\n", src_type, dst_type);
1441 return E_NOTIMPL;
1444 return S_OK;
1447 static const struct IDataConvertVtbl convert_vtbl =
1449 convert_QueryInterface,
1450 convert_AddRef,
1451 convert_Release,
1452 convert_DataConvert,
1453 convert_CanConvert,
1454 convert_GetConversionSize
1457 static HRESULT WINAPI dcinfo_QueryInterface(IDCInfo* iface, REFIID riid, void **obj)
1459 convert *This = impl_from_IDCInfo(iface);
1461 return IDataConvert_QueryInterface(&This->IDataConvert_iface, riid, obj);
1464 static ULONG WINAPI dcinfo_AddRef(IDCInfo* iface)
1466 convert *This = impl_from_IDCInfo(iface);
1468 return IDataConvert_AddRef(&This->IDataConvert_iface);
1471 static ULONG WINAPI dcinfo_Release(IDCInfo* iface)
1473 convert *This = impl_from_IDCInfo(iface);
1475 return IDataConvert_Release(&This->IDataConvert_iface);
1478 static HRESULT WINAPI dcinfo_GetInfo(IDCInfo *iface, ULONG num, DCINFOTYPE types[], DCINFO **info_ptr)
1480 convert *This = impl_from_IDCInfo(iface);
1481 ULONG i;
1482 DCINFO *infos;
1484 TRACE("(%p)->(%d, %p, %p)\n", This, num, types, info_ptr);
1486 *info_ptr = infos = CoTaskMemAlloc(num * sizeof(*infos));
1487 if(!infos) return E_OUTOFMEMORY;
1489 for(i = 0; i < num; i++)
1491 infos[i].eInfoType = types[i];
1492 VariantInit(&infos[i].vData);
1494 switch(types[i])
1496 case DCINFOTYPE_VERSION:
1497 V_VT(&infos[i].vData) = VT_UI4;
1498 V_UI4(&infos[i].vData) = This->version;
1499 break;
1503 return S_OK;
1506 static HRESULT WINAPI dcinfo_SetInfo(IDCInfo* iface, ULONG num, DCINFO info[])
1508 convert *This = impl_from_IDCInfo(iface);
1509 ULONG i;
1510 HRESULT hr = S_OK;
1512 TRACE("(%p)->(%d, %p)\n", This, num, info);
1514 for(i = 0; i < num; i++)
1516 switch(info[i].eInfoType)
1518 case DCINFOTYPE_VERSION:
1519 if(V_VT(&info[i].vData) != VT_UI4)
1521 FIXME("VERSION with vt %x\n", V_VT(&info[i].vData));
1522 hr = DB_S_ERRORSOCCURRED;
1523 break;
1525 This->version = V_UI4(&info[i].vData);
1526 break;
1528 default:
1529 FIXME("Unhandled info type %d (vt %x)\n", info[i].eInfoType, V_VT(&info[i].vData));
1532 return hr;
1535 static const struct IDCInfoVtbl dcinfo_vtbl =
1537 dcinfo_QueryInterface,
1538 dcinfo_AddRef,
1539 dcinfo_Release,
1540 dcinfo_GetInfo,
1541 dcinfo_SetInfo
1544 HRESULT create_oledb_convert(IUnknown *outer, void **obj)
1546 convert *This;
1548 TRACE("(%p, %p)\n", outer, obj);
1550 *obj = NULL;
1552 if(outer) return CLASS_E_NOAGGREGATION;
1554 This = heap_alloc(sizeof(*This));
1555 if(!This) return E_OUTOFMEMORY;
1557 This->IDataConvert_iface.lpVtbl = &convert_vtbl;
1558 This->IDCInfo_iface.lpVtbl = &dcinfo_vtbl;
1559 This->ref = 1;
1560 This->version = 0x110;
1562 *obj = &This->IDataConvert_iface;
1564 return S_OK;