explorerframe/tests: Use CRT memory allocation functions.
[wine.git] / dlls / oledb32 / convert.c
blobb8db8f06d8b302b9bfb5006571a75bf1115e7b95
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"
34 #include "wine/heap.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
38 typedef struct
40 IDataConvert IDataConvert_iface;
41 IDCInfo IDCInfo_iface;
43 LONG ref;
45 UINT version; /* Set by IDCInfo_SetInfo */
46 } convert;
48 static inline convert *impl_from_IDataConvert(IDataConvert *iface)
50 return CONTAINING_RECORD(iface, convert, IDataConvert_iface);
53 static inline convert *impl_from_IDCInfo(IDCInfo *iface)
55 return CONTAINING_RECORD(iface, convert, IDCInfo_iface);
58 static HRESULT WINAPI convert_QueryInterface(IDataConvert* iface,
59 REFIID riid,
60 void **obj)
62 convert *This = impl_from_IDataConvert(iface);
63 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
65 *obj = NULL;
67 if(IsEqualIID(riid, &IID_IUnknown) ||
68 IsEqualIID(riid, &IID_IDataConvert))
70 *obj = iface;
72 else if(IsEqualIID(riid, &IID_IDCInfo))
74 *obj = &This->IDCInfo_iface;
76 else
78 FIXME("interface %s not implemented\n", debugstr_guid(riid));
79 return E_NOINTERFACE;
82 IDataConvert_AddRef(iface);
83 return S_OK;
87 static ULONG WINAPI convert_AddRef(IDataConvert* iface)
89 convert *This = impl_from_IDataConvert(iface);
90 TRACE("(%p)\n", This);
92 return InterlockedIncrement(&This->ref);
96 static ULONG WINAPI convert_Release(IDataConvert* iface)
98 convert *This = impl_from_IDataConvert(iface);
99 LONG ref;
101 TRACE("(%p)\n", This);
103 ref = InterlockedDecrement(&This->ref);
104 if(ref == 0)
105 heap_free(This);
107 return ref;
110 static int get_length(DBTYPE type)
112 switch(type)
114 case DBTYPE_I1:
115 case DBTYPE_UI1:
116 return 1;
117 case DBTYPE_I2:
118 case DBTYPE_UI2:
119 return 2;
120 case DBTYPE_BOOL:
121 return sizeof(VARIANT_BOOL);
122 case DBTYPE_I4:
123 case DBTYPE_UI4:
124 case DBTYPE_R4:
125 return 4;
126 case DBTYPE_I8:
127 case DBTYPE_UI8:
128 case DBTYPE_R8:
129 case DBTYPE_DATE:
130 return 8;
131 case DBTYPE_DBDATE:
132 return sizeof(DBDATE);
133 case DBTYPE_DBTIMESTAMP:
134 return sizeof(DBTIMESTAMP);
135 case DBTYPE_CY:
136 return sizeof(CY);
137 case DBTYPE_BSTR:
138 return sizeof(BSTR);
139 case DBTYPE_FILETIME:
140 return sizeof(FILETIME);
141 case DBTYPE_GUID:
142 return sizeof(GUID);
143 case DBTYPE_NUMERIC:
144 return sizeof(DB_NUMERIC);
145 case DBTYPE_BYTES:
146 case DBTYPE_WSTR:
147 case DBTYPE_STR:
148 case DBTYPE_BYREF | DBTYPE_WSTR:
149 return 0;
150 case DBTYPE_VARIANT:
151 return sizeof(VARIANT);
152 default:
153 FIXME("Unhandled type %04x\n", type);
154 return 0;
158 static HRESULT WINAPI convert_DataConvert(IDataConvert* iface,
159 DBTYPE src_type, DBTYPE dst_type,
160 DBLENGTH src_len, DBLENGTH *dst_len,
161 void *src, void *dst,
162 DBLENGTH dst_max_len,
163 DBSTATUS src_status, DBSTATUS *dst_status,
164 BYTE precision, BYTE scale,
165 DBDATACONVERT flags)
167 convert *This = impl_from_IDataConvert(iface);
168 DBLENGTH dst_len_loc;
169 DBSTATUS dst_status_loc;
170 VARIANT tmp;
171 HRESULT hr;
173 TRACE("(%p)->(%d, %d, %Id, %p, %p, %p, %Id, %ld, %p, %d, %d, %lx)\n", This,
174 src_type, dst_type, src_len, dst_len, src, dst, dst_max_len,
175 src_status, dst_status, precision, scale, flags);
177 if (!dst_len) dst_len = &dst_len_loc;
178 if (!dst_status) dst_status = &dst_status_loc;
180 *dst_status = DBSTATUS_E_BADACCESSOR;
182 if(src_status == DBSTATUS_S_ISNULL)
184 *dst_status = DBSTATUS_S_ISNULL;
185 *dst_len = 0;
186 return S_OK;
189 if(src_type == DBTYPE_VARIANT && V_VT((VARIANT*)src) == VT_NULL)
191 if(dst_type == DBTYPE_VARIANT)
192 *dst_len = sizeof(VARIANT);
194 *dst_status = DBSTATUS_S_ISNULL;
195 return S_OK;
198 if(IDataConvert_CanConvert(iface, src_type, dst_type) != S_OK)
200 return DB_E_UNSUPPORTEDCONVERSION;
203 if(dst == NULL && get_length(dst_type) != 0)
205 *dst_len = get_length(src_type);
206 *dst_status = DBSTATUS_S_OK;
207 return S_OK;
210 if(src_type == DBTYPE_STR)
212 BSTR b;
213 DWORD len;
215 if(flags & DBDATACONVERT_LENGTHFROMNTS)
216 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0) - 1;
217 else
218 len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
219 b = SysAllocStringLen(NULL, len);
220 if(!b) return E_OUTOFMEMORY;
221 if(flags & DBDATACONVERT_LENGTHFROMNTS)
222 MultiByteToWideChar(CP_ACP, 0, src, -1, b, len + 1);
223 else
224 MultiByteToWideChar(CP_ACP, 0, src, src_len, b, len);
226 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
227 &b, dst, dst_max_len, src_status, dst_status,
228 precision, scale, flags);
230 SysFreeString(b);
231 return hr;
234 if(src_type == DBTYPE_WSTR)
236 BSTR b;
238 if(flags & DBDATACONVERT_LENGTHFROMNTS)
239 b = SysAllocString(src);
240 else
241 b = SysAllocStringLen(src, src_len / 2);
242 if(!b) return E_OUTOFMEMORY;
243 hr = IDataConvert_DataConvert(iface, DBTYPE_BSTR, dst_type, 0, dst_len,
244 &b, dst, dst_max_len, src_status, dst_status,
245 precision, scale, flags);
246 SysFreeString(b);
247 return hr;
250 switch(dst_type)
252 case DBTYPE_I1:
254 signed char *d = dst;
255 switch(src_type)
257 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
258 case DBTYPE_I1: *d = *(signed char*)src; hr = S_OK; break;
259 case DBTYPE_I2: hr = VarI1FromI2(*(signed short*)src, d); break;
260 case DBTYPE_I4: hr = VarI1FromI4(*(signed int*)src, d); break;
261 case DBTYPE_R4: hr = VarI1FromR4(*(FLOAT*)src, d); break;
262 case DBTYPE_R8: hr = VarI1FromR8(*(double*)src, d); break;
263 case DBTYPE_CY: hr = VarI1FromCy(*(CY*)src, d); break;
264 case DBTYPE_DATE: hr = VarI1FromDate(*(DATE*)src, d); break;
265 case DBTYPE_BSTR: hr = VarI1FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
266 case DBTYPE_BOOL: hr = VarI1FromBool(*(VARIANT_BOOL*)src, d); break;
267 case DBTYPE_DECIMAL: hr = VarI1FromDec(src, d); break;
268 case DBTYPE_UI1: hr = VarI1FromUI1(*(BYTE*)src, d); break;
269 case DBTYPE_UI2: hr = VarI1FromUI2(*(WORD*)src, d); break;
270 case DBTYPE_UI4: hr = VarI1FromUI4(*(DWORD*)src, d); break;
271 case DBTYPE_I8: hr = VarI1FromI8(*(LONGLONG*)src, d); break;
272 case DBTYPE_UI8: hr = VarI1FromUI8(*(ULONGLONG*)src, d); break;
273 case DBTYPE_VARIANT:
274 VariantInit(&tmp);
275 if ((hr = VariantChangeType(&tmp, src, 0, VT_I1)) == S_OK)
276 *d = V_I1(&tmp);
277 break;
278 default: FIXME("Unimplemented conversion %04x -> I1\n", src_type); return E_NOTIMPL;
280 break;
283 case DBTYPE_I2:
285 signed short *d = dst;
286 switch(src_type)
288 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
289 case DBTYPE_I2: *d = *(signed short*)src; hr = S_OK; break;
290 case DBTYPE_I4: hr = VarI2FromI4(*(signed int*)src, d); break;
291 case DBTYPE_R4: hr = VarI2FromR4(*(FLOAT*)src, d); break;
292 case DBTYPE_R8: hr = VarI2FromR8(*(double*)src, d); break;
293 case DBTYPE_CY: hr = VarI2FromCy(*(CY*)src, d); break;
294 case DBTYPE_DATE: hr = VarI2FromDate(*(DATE*)src, d); break;
295 case DBTYPE_BSTR: hr = VarI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
296 case DBTYPE_BOOL: hr = VarI2FromBool(*(VARIANT_BOOL*)src, d); break;
297 case DBTYPE_DECIMAL: hr = VarI2FromDec(src, d); break;
298 case DBTYPE_I1: hr = VarI2FromI1(*(signed char*)src, d); break;
299 case DBTYPE_UI1: hr = VarI2FromUI1(*(BYTE*)src, d); break;
300 case DBTYPE_UI2: hr = VarI2FromUI2(*(WORD*)src, d); break;
301 case DBTYPE_UI4: hr = VarI2FromUI4(*(DWORD*)src, d); break;
302 case DBTYPE_I8: hr = VarI2FromI8(*(LONGLONG*)src, d); break;
303 case DBTYPE_UI8: hr = VarI2FromUI8(*(ULONGLONG*)src, d); break;
304 case DBTYPE_VARIANT:
305 VariantInit(&tmp);
306 if ((hr = VariantChangeType(&tmp, src, 0, VT_I2)) == S_OK)
307 *d = V_I2(&tmp);
308 break;
309 default: FIXME("Unimplemented conversion %04x -> I2\n", src_type); return E_NOTIMPL;
311 break;
314 case DBTYPE_I4:
316 LONG *d = dst;
317 switch(src_type)
319 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
320 case DBTYPE_I2: hr = VarI4FromI2(*(signed short*)src, d); break;
321 case DBTYPE_I4: *d = *(signed int*)src; hr = S_OK; break;
322 case DBTYPE_R4: hr = VarI4FromR4(*(FLOAT*)src, d); break;
323 case DBTYPE_R8: hr = VarI4FromR8(*(double*)src, d); break;
324 case DBTYPE_CY: hr = VarI4FromCy(*(CY*)src, d); break;
325 case DBTYPE_DATE: hr = VarI4FromDate(*(DATE*)src, d); break;
326 case DBTYPE_BSTR: hr = VarI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
327 case DBTYPE_BOOL: hr = VarI4FromBool(*(VARIANT_BOOL*)src, d); break;
328 case DBTYPE_DECIMAL: hr = VarI4FromDec(src, d); break;
329 case DBTYPE_I1: hr = VarI4FromI1(*(signed char*)src, d); break;
330 case DBTYPE_UI1: hr = VarI4FromUI1(*(BYTE*)src, d); break;
331 case DBTYPE_UI2: hr = VarI4FromUI2(*(WORD*)src, d); break;
332 case DBTYPE_UI4: hr = VarI4FromUI4(*(DWORD*)src, d); break;
333 case DBTYPE_I8: hr = VarI4FromI8(*(LONGLONG*)src, d); break;
334 case DBTYPE_UI8: hr = VarI4FromUI8(*(ULONGLONG*)src, d); break;
335 case DBTYPE_VARIANT:
336 VariantInit(&tmp);
337 if ((hr = VariantChangeType(&tmp, src, 0, VT_I4)) == S_OK)
338 *d = V_I4(&tmp);
339 break;
340 default: FIXME("Unimplemented conversion %04x -> I4\n", src_type); return E_NOTIMPL;
342 break;
345 case DBTYPE_I8:
347 LONGLONG *d = dst;
348 switch(src_type)
350 case DBTYPE_BSTR: hr = VarI8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
351 case DBTYPE_I8: *d = *(LONGLONG*)src; hr = S_OK; break;
352 default: FIXME("Unimplemented conversion %04x -> I8\n", src_type); return E_NOTIMPL;
354 break;
357 case DBTYPE_R4:
359 FLOAT *d = dst;
360 switch(src_type)
362 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
363 case DBTYPE_I2: hr = VarR4FromI2(*(signed short*)src, d); break;
364 case DBTYPE_I4: hr = VarR4FromI4(*(signed int*)src, d); break;
365 case DBTYPE_R4: *d = *(FLOAT*)src; hr = S_OK; break;
366 case DBTYPE_R8: hr = VarR4FromR8(*(double*)src, d); break;
367 case DBTYPE_CY: hr = VarR4FromCy(*(CY*)src, d); break;
368 case DBTYPE_DATE: hr = VarR4FromDate(*(DATE*)src, d); break;
369 case DBTYPE_BSTR: hr = VarR4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
370 case DBTYPE_BOOL: hr = VarR4FromBool(*(VARIANT_BOOL*)src, d); break;
371 case DBTYPE_DECIMAL: hr = VarR4FromDec(src, d); break;
372 case DBTYPE_I1: hr = VarR4FromI1(*(signed char*)src, d); break;
373 case DBTYPE_UI1: hr = VarR4FromUI1(*(BYTE*)src, d); break;
374 case DBTYPE_UI2: hr = VarR4FromUI2(*(WORD*)src, d); break;
375 case DBTYPE_UI4: hr = VarR4FromUI4(*(DWORD*)src, d); break;
376 case DBTYPE_I8: hr = VarR4FromI8(*(LONGLONG*)src, d); break;
377 case DBTYPE_UI8: hr = VarR4FromUI8(*(ULONGLONG*)src, d); break;
378 case DBTYPE_VARIANT:
379 VariantInit(&tmp);
380 if ((hr = VariantChangeType(&tmp, src, 0, VT_R4)) == S_OK)
381 *d = V_R4(&tmp);
382 break;
383 default: FIXME("Unimplemented conversion %04x -> R4\n", src_type); return E_NOTIMPL;
385 break;
387 case DBTYPE_R8:
389 DOUBLE *d=dst;
390 switch (src_type)
392 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
393 case DBTYPE_I1: hr = VarR8FromI1(*(signed char*)src, d); break;
394 case DBTYPE_I2: hr = VarR8FromI2(*(signed short*)src, d); break;
395 case DBTYPE_I4: hr = VarR8FromI4(*(signed int*)src, d); break;
396 case DBTYPE_I8: hr = VarR8FromI8(*(LONGLONG*)src, d); break;
397 case DBTYPE_UI1: hr = VarR8FromUI1(*(BYTE*)src, d); break;
398 case DBTYPE_UI2: hr = VarR8FromUI2(*(WORD*)src, d); break;
399 case DBTYPE_UI4: hr = VarR8FromUI4(*(DWORD*)src, d); break;
400 case DBTYPE_UI8: hr = VarR8FromUI8(*(ULONGLONG*)src, d); break;
401 case DBTYPE_R4: hr = VarR8FromR4(*(FLOAT*)src, d); break;
402 case DBTYPE_R8: *d = *(DOUBLE*)src; hr = S_OK; break;
403 case DBTYPE_CY: hr = VarR8FromCy(*(CY*)src, d); break;
404 case DBTYPE_DATE: hr = VarR8FromDate(*(DATE*)src, d); break;
405 case DBTYPE_BSTR: hr = VarR8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
406 case DBTYPE_BOOL: hr = VarR8FromBool(*(VARIANT_BOOL*)src, d); break;
407 case DBTYPE_DECIMAL: hr = VarR8FromDec(src, d); break;
408 case DBTYPE_VARIANT:
409 VariantInit(&tmp);
410 if ((hr = VariantChangeType(&tmp, src, 0, VT_R8)) == S_OK)
411 *d = V_R8(&tmp);
412 break;
413 default: FIXME("Unimplemented conversion %04x -> R8\n", src_type); return E_NOTIMPL;
415 break;
417 case DBTYPE_BOOL:
419 VARIANT_BOOL *d=dst;
420 switch (src_type)
422 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
423 case DBTYPE_I1: hr = VarBoolFromI1(*(signed char*)src, d); break;
424 case DBTYPE_I2: hr = VarBoolFromI2(*(signed short*)src, d); break;
425 case DBTYPE_I4: hr = VarBoolFromI4(*(signed int*)src, d); break;
426 case DBTYPE_I8: hr = VarBoolFromI8(*(LONGLONG*)src, d); break;
427 case DBTYPE_UI1: hr = VarBoolFromUI1(*(BYTE*)src, d); break;
428 case DBTYPE_UI2: hr = VarBoolFromUI2(*(WORD*)src, d); break;
429 case DBTYPE_UI4: hr = VarBoolFromUI4(*(DWORD*)src, d); break;
430 case DBTYPE_UI8: hr = VarBoolFromUI8(*(ULONGLONG*)src, d); break;
431 case DBTYPE_R4: hr = VarBoolFromR4(*(FLOAT*)src, d); break;
432 case DBTYPE_R8: hr = VarBoolFromR8(*(DOUBLE*)src, d); break;
433 case DBTYPE_CY: hr = VarBoolFromCy(*(CY*)src, d); break;
434 case DBTYPE_DATE: hr = VarBoolFromDate(*(DATE*)src, d); break;
435 case DBTYPE_BSTR: hr = VarBoolFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
436 case DBTYPE_BOOL: *d = *(VARIANT_BOOL*)src; hr = S_OK; break;
437 case DBTYPE_DECIMAL: hr = VarBoolFromDec(src, d); break;
438 default: FIXME("Unimplemented conversion %04x -> BOOL\n", src_type); return E_NOTIMPL;
440 break;
442 case DBTYPE_DATE:
444 DATE *d=dst;
445 switch (src_type)
447 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
448 case DBTYPE_I1: hr = VarDateFromI1(*(signed char*)src, d); break;
449 case DBTYPE_I2: hr = VarDateFromI2(*(signed short*)src, d); break;
450 case DBTYPE_I4: hr = VarDateFromI4(*(signed int*)src, d); break;
451 case DBTYPE_I8: hr = VarDateFromI8(*(LONGLONG*)src, d); break;
452 case DBTYPE_UI1: hr = VarDateFromUI1(*(BYTE*)src, d); break;
453 case DBTYPE_UI2: hr = VarDateFromUI2(*(WORD*)src, d); break;
454 case DBTYPE_UI4: hr = VarDateFromUI4(*(DWORD*)src, d); break;
455 case DBTYPE_UI8: hr = VarDateFromUI8(*(ULONGLONG*)src, d); break;
456 case DBTYPE_R4: hr = VarDateFromR4(*(FLOAT*)src, d); break;
457 case DBTYPE_R8: hr = VarDateFromR8(*(DOUBLE*)src, d); break;
458 case DBTYPE_CY: hr = VarDateFromCy(*(CY*)src, d); break;
459 case DBTYPE_DATE: *d = *(DATE*)src; hr = S_OK; break;
460 case DBTYPE_BSTR: hr = VarDateFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
461 case DBTYPE_BOOL: hr = VarDateFromBool(*(VARIANT_BOOL*)src, d); break;
462 case DBTYPE_DECIMAL: hr = VarDateFromDec(src, d); break;
463 case DBTYPE_DBTIMESTAMP:
465 SYSTEMTIME st;
466 DBTIMESTAMP *ts = src;
468 st.wYear = ts->year;
469 st.wMonth = ts->month;
470 st.wDay = ts->day;
471 st.wHour = ts->hour;
472 st.wMinute = ts->minute;
473 st.wSecond = ts->second;
474 st.wMilliseconds = ts->fraction/1000000;
475 hr = (SystemTimeToVariantTime(&st, d) ? S_OK : E_FAIL);
476 break;
478 case DBTYPE_VARIANT:
480 VARIANT tmp;
481 VariantInit(&tmp);
482 if ((hr = VariantChangeType(&tmp, src, 0, VT_DATE)) == S_OK)
483 *d = V_DATE(&tmp);
484 break;
486 default: FIXME("Unimplemented conversion %04x -> DATE\n", src_type); return E_NOTIMPL;
488 break;
490 case DBTYPE_DBDATE:
492 DBDATE *d=dst;
493 switch (src_type)
495 case DBTYPE_DBDATE: memcpy(d, src, sizeof(DBDATE)); 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;
508 hr = (VariantTimeToSystemTime( V_DATE(&var), &st) ? S_OK : E_FAIL);
509 d->year = st.wYear;
510 d->month = st.wMonth;
511 d->day = st.wDay;
514 VariantClear(&var);
516 break;
517 case DBTYPE_VARIANT:
519 VariantInit(&tmp);
521 if ((hr = VariantChangeType(&tmp, src, 0, VT_DATE)) == S_OK)
523 SYSTEMTIME st;
524 hr = (VariantTimeToSystemTime( V_DATE(&tmp), &st) ? S_OK : E_FAIL);
525 d->year = st.wYear;
526 d->month = st.wMonth;
527 d->day = st.wDay;
529 else
531 FIXME("Unimplemented variant type %d -> DBDATE\n", V_VT((VARIANT*)src));
532 return E_NOTIMPL;
534 break;
536 default: FIXME("Unimplemented conversion %04x -> DBDATE\n", src_type); return E_NOTIMPL;
538 break;
540 case DBTYPE_DBTIMESTAMP:
542 DBTIMESTAMP *d=dst;
543 switch (src_type)
545 case DBTYPE_EMPTY: memset(d, 0, sizeof(DBTIMESTAMP)); hr = S_OK; break;
546 case DBTYPE_DBTIMESTAMP: memcpy(d, src, sizeof(DBTIMESTAMP)); hr = S_OK; break;
547 case DBTYPE_BSTR:
549 VARIANT var;
550 BSTR s = *(WCHAR**)src;
552 VariantInit(&var);
553 V_VT(&var) = VT_BSTR;
554 V_BSTR(&var) = SysAllocString(s);
556 if ((hr = VariantChangeType(&var, &var, 0, VT_DATE)) == S_OK)
558 SYSTEMTIME st;
560 hr = (VariantTimeToSystemTime( V_DATE(&var), &st) ? S_OK : E_FAIL);
561 d->year = st.wYear;
562 d->month = st.wMonth;
563 d->day = st.wDay;
564 d->hour = st.wHour;
565 d->minute = st.wMinute;
566 d->second = st.wSecond;
567 d->fraction = st.wMilliseconds * 1000000;
569 else
571 if(swscanf( s, L"%d-%d-%d %d:%d:%d.%d", &d->year, &d->month, &d->day, &d->hour, &d->minute,
572 &d->second, &d->fraction) != 7)
574 hr = DISP_E_TYPEMISMATCH;
575 *dst_status = DBSTATUS_E_CANTCONVERTVALUE;
576 *dst_len = get_length(dst_type);
578 else
579 hr = S_OK;
582 VariantClear(&var);
584 break;
585 case DBTYPE_DATE:
587 SYSTEMTIME st;
588 hr = (VariantTimeToSystemTime(*(double*)src, &st) ? S_OK : E_FAIL);
589 d->year = st.wYear;
590 d->month = st.wMonth;
591 d->day = st.wDay;
592 d->hour = st.wHour;
593 d->minute = st.wMinute;
594 d->second = st.wSecond;
595 d->fraction = st.wMilliseconds * 1000000;
596 break;
598 case DBTYPE_VARIANT:
599 if( V_VT((VARIANT*)src) == VT_DATE)
601 SYSTEMTIME st;
602 hr = (VariantTimeToSystemTime( V_DATE((VARIANT*)src), &st) ? S_OK : E_FAIL);
603 d->year = st.wYear;
604 d->month = st.wMonth;
605 d->day = st.wDay;
606 d->hour = st.wHour;
607 d->minute = st.wMinute;
608 d->second = st.wSecond;
609 d->fraction = st.wMilliseconds * 1000000;
611 else
613 FIXME("Unimplemented variant type %d -> DBTIMESTAMP\n", V_VT((VARIANT*)src));
614 return E_NOTIMPL;
616 break;
617 default: FIXME("Unimplemented conversion %04x -> DBTIMESTAMP\n", src_type); return E_NOTIMPL;
619 break;
622 case DBTYPE_CY:
624 CY *d = dst;
625 switch(src_type)
627 case DBTYPE_EMPTY: d->int64 = 0; hr = S_OK; break;
628 case DBTYPE_I2: hr = VarCyFromI2(*(signed short*)src, d); break;
629 case DBTYPE_I4: hr = VarCyFromI4(*(signed int*)src, d); break;
630 case DBTYPE_R4: hr = VarCyFromR4(*(FLOAT*)src, d); break;
631 case DBTYPE_R8: hr = VarCyFromR8(*(double*)src, d); break;
632 case DBTYPE_CY: *d = *(CY*)src; hr = S_OK; break;
633 case DBTYPE_DATE: hr = VarCyFromDate(*(DATE*)src, d); break;
634 case DBTYPE_BSTR: hr = VarCyFromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
635 case DBTYPE_BOOL: hr = VarCyFromBool(*(VARIANT_BOOL*)src, d); break;
636 case DBTYPE_DECIMAL: hr = VarCyFromDec(src, d); break;
637 case DBTYPE_I1: hr = VarCyFromI1(*(signed char*)src, d); break;
638 case DBTYPE_UI1: hr = VarCyFromUI1(*(BYTE*)src, d); break;
639 case DBTYPE_UI2: hr = VarCyFromUI2(*(WORD*)src, d); break;
640 case DBTYPE_UI4: hr = VarCyFromUI4(*(DWORD*)src, d); break;
641 case DBTYPE_I8: hr = VarCyFromI8(*(LONGLONG*)src, d); break;
642 case DBTYPE_UI8: hr = VarCyFromUI8(*(ULONGLONG*)src, d); break;
643 default: FIXME("Unimplemented conversion %04x -> CY\n", src_type); return E_NOTIMPL;
645 break;
648 case DBTYPE_BSTR:
650 BSTR *d = dst;
651 switch(src_type)
653 case DBTYPE_EMPTY: *d = SysAllocStringLen(NULL, 0); hr = *d ? S_OK : E_OUTOFMEMORY; break;
654 case DBTYPE_I2: hr = VarBstrFromI2(*(signed short*)src, LOCALE_USER_DEFAULT, 0, d); break;
655 case DBTYPE_I4: hr = VarBstrFromI4(*(signed int*)src, LOCALE_USER_DEFAULT, 0, d); break;
656 case DBTYPE_R4: hr = VarBstrFromR4(*(FLOAT*)src, LOCALE_USER_DEFAULT, 0, d); break;
657 case DBTYPE_R8: hr = VarBstrFromR8(*(double*)src, LOCALE_USER_DEFAULT, 0, d); break;
658 case DBTYPE_CY: hr = VarBstrFromCy(*(CY*)src, LOCALE_USER_DEFAULT, 0, d); break;
659 case DBTYPE_DATE: hr = VarBstrFromDate(*(DATE*)src, LOCALE_USER_DEFAULT, 0, d); break;
660 case DBTYPE_BSTR: *d = SysAllocStringLen(*(BSTR*)src, SysStringLen(*(BSTR*)src)); hr = *d ? S_OK : E_OUTOFMEMORY; break;
661 case DBTYPE_BOOL: hr = VarBstrFromBool(*(VARIANT_BOOL*)src, LOCALE_USER_DEFAULT, 0, d); break;
662 case DBTYPE_DECIMAL: hr = VarBstrFromDec(src, LOCALE_USER_DEFAULT, 0, d); break;
663 case DBTYPE_I1: hr = VarBstrFromI1(*(signed char*)src, LOCALE_USER_DEFAULT, 0, d); break;
664 case DBTYPE_UI1: hr = VarBstrFromUI1(*(BYTE*)src, LOCALE_USER_DEFAULT, 0, d); break;
665 case DBTYPE_UI2: hr = VarBstrFromUI2(*(WORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
666 case DBTYPE_UI4: hr = VarBstrFromUI4(*(DWORD*)src, LOCALE_USER_DEFAULT, 0, d); break;
667 case DBTYPE_I8: hr = VarBstrFromI8(*(LONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
668 case DBTYPE_UI8: hr = VarBstrFromUI8(*(ULONGLONG*)src, LOCALE_USER_DEFAULT, 0, d); break;
669 case DBTYPE_GUID:
671 WCHAR szBuff[39];
672 const GUID *id = src;
673 wsprintfW(szBuff, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
674 id->Data1, id->Data2, id->Data3,
675 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
676 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
677 *d = SysAllocString(szBuff);
678 hr = *d ? S_OK : E_OUTOFMEMORY;
680 break;
681 case DBTYPE_BYTES:
683 *d = SysAllocStringLen(NULL, 2 * src_len);
684 if (*d == NULL)
685 hr = E_OUTOFMEMORY;
686 else
688 static const char hexchars[] = "0123456789ABCDEF";
689 WCHAR *s = *d;
690 unsigned char *p = src;
691 while (src_len > 0)
693 *s++ = hexchars[(*p >> 4) & 0x0F];
694 *s++ = hexchars[(*p) & 0x0F];
695 src_len--; p++;
697 hr = S_OK;
700 break;
701 case DBTYPE_DBTIMESTAMP:
703 WCHAR szBuff[64];
704 DBTIMESTAMP *ts = src;
706 if(ts->fraction == 0)
707 wsprintfW(szBuff, L"%04d-%02d-%02d %02d:%02d:%02d", ts->year, ts->month, ts->day, ts->hour,
708 ts->minute, ts->second);
709 else
710 wsprintfW(szBuff, L"%04d-%02d-%02d %02d:%02d:%02d.%09d", ts->year, ts->month, ts->day, ts->hour,
711 ts->minute, ts->second, ts->fraction );
712 *d = SysAllocString(szBuff);
713 hr = *d ? S_OK : E_OUTOFMEMORY;
714 break;
716 case DBTYPE_VARIANT:
717 VariantInit(&tmp);
718 if ((hr = VariantChangeType(&tmp, src, 0, VT_BSTR)) == S_OK)
719 *d = V_BSTR(&tmp);
720 break;
721 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type); return E_NOTIMPL;
723 break;
726 case DBTYPE_UI1:
728 BYTE *d = dst;
729 switch(src_type)
731 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
732 case DBTYPE_I2: hr = VarUI1FromI2(*(signed short*)src, d); break;
733 case DBTYPE_I4: hr = VarUI1FromI4(*(signed int*)src, d); break;
734 case DBTYPE_R4: hr = VarUI1FromR4(*(FLOAT*)src, d); break;
735 case DBTYPE_R8: hr = VarUI1FromR8(*(double*)src, d); break;
736 case DBTYPE_CY: hr = VarUI1FromCy(*(CY*)src, d); break;
737 case DBTYPE_DATE: hr = VarUI1FromDate(*(DATE*)src, d); break;
738 case DBTYPE_BSTR: hr = VarUI1FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
739 case DBTYPE_BOOL: hr = VarUI1FromBool(*(VARIANT_BOOL*)src, d); break;
740 case DBTYPE_DECIMAL: hr = VarUI1FromDec(src, d); break;
741 case DBTYPE_I1: hr = VarUI1FromI1(*(signed char*)src, d); break;
742 case DBTYPE_UI1: *d = *(BYTE*)src; hr = S_OK; break;
743 case DBTYPE_UI2: hr = VarUI1FromUI2(*(WORD*)src, d); break;
744 case DBTYPE_UI4: hr = VarUI1FromUI4(*(DWORD*)src, d); break;
745 case DBTYPE_I8: hr = VarUI1FromI8(*(LONGLONG*)src, d); break;
746 case DBTYPE_UI8: hr = VarUI1FromUI8(*(ULONGLONG*)src, d); break;
747 default: FIXME("Unimplemented conversion %04x -> UI1\n", src_type); return E_NOTIMPL;
749 break;
751 case DBTYPE_UI2:
753 WORD *d = dst;
754 switch(src_type)
756 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
757 case DBTYPE_I2: hr = VarUI2FromI2(*(signed short*)src, d); break;
758 case DBTYPE_I4: hr = VarUI2FromI4(*(signed int*)src, d); break;
759 case DBTYPE_R4: hr = VarUI2FromR4(*(FLOAT*)src, d); break;
760 case DBTYPE_R8: hr = VarUI2FromR8(*(double*)src, d); break;
761 case DBTYPE_CY: hr = VarUI2FromCy(*(CY*)src, d); break;
762 case DBTYPE_DATE: hr = VarUI2FromDate(*(DATE*)src, d); break;
763 case DBTYPE_BSTR: hr = VarUI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
764 case DBTYPE_BOOL: hr = VarUI2FromBool(*(VARIANT_BOOL*)src, d); break;
765 case DBTYPE_DECIMAL: hr = VarUI2FromDec(src, d); break;
766 case DBTYPE_I1: hr = VarUI2FromI1(*(signed char*)src, d); break;
767 case DBTYPE_UI1: hr = VarUI2FromUI1(*(BYTE*)src, d); break;
768 case DBTYPE_UI2: *d = *(WORD*)src; hr = S_OK; break;
769 case DBTYPE_UI4: hr = VarUI2FromUI4(*(DWORD*)src, d); break;
770 case DBTYPE_I8: hr = VarUI2FromI8(*(LONGLONG*)src, d); break;
771 case DBTYPE_UI8: hr = VarUI2FromUI8(*(ULONGLONG*)src, d); break;
772 default: FIXME("Unimplemented conversion %04x -> UI2\n", src_type); return E_NOTIMPL;
774 break;
777 case DBTYPE_UI4:
779 DWORD *d = dst;
780 switch(src_type)
782 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
783 case DBTYPE_I2: hr = VarUI4FromI2(*(signed short*)src, d); break;
784 case DBTYPE_I4: hr = VarUI4FromI4(*(signed int*)src, d); break;
785 case DBTYPE_R4: hr = VarUI4FromR4(*(FLOAT*)src, d); break;
786 case DBTYPE_R8: hr = VarUI4FromR8(*(double*)src, d); break;
787 case DBTYPE_CY: hr = VarUI4FromCy(*(CY*)src, d); break;
788 case DBTYPE_DATE: hr = VarUI4FromDate(*(DATE*)src, d); break;
789 case DBTYPE_BSTR: hr = VarUI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
790 case DBTYPE_BOOL: hr = VarUI4FromBool(*(VARIANT_BOOL*)src, d); break;
791 case DBTYPE_DECIMAL: hr = VarUI4FromDec(src, d); break;
792 case DBTYPE_I1: hr = VarUI4FromI1(*(signed char*)src, d); break;
793 case DBTYPE_UI1: hr = VarUI4FromUI1(*(BYTE*)src, d); break;
794 case DBTYPE_UI2: hr = VarUI4FromUI2(*(WORD*)src, d); break;
795 case DBTYPE_UI4: *d = *(DWORD*)src; hr = S_OK; break;
796 case DBTYPE_I8: hr = VarUI4FromI8(*(LONGLONG*)src, d); break;
797 case DBTYPE_UI8: hr = VarUI4FromUI8(*(ULONGLONG*)src, d); break;
798 case DBTYPE_VARIANT:
799 VariantInit(&tmp);
800 if ((hr = VariantChangeType(&tmp, src, 0, VT_UI4)) == S_OK)
801 *d = V_UI4(&tmp);
802 break;
803 default: FIXME("Unimplemented conversion %04x -> UI4\n", src_type); return E_NOTIMPL;
805 break;
808 case DBTYPE_UI8:
810 ULONGLONG *d = dst;
811 switch(src_type)
813 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
814 case DBTYPE_I2: hr = VarUI8FromI2(*(signed short*)src, d); break;
815 case DBTYPE_I4: {LONGLONG s = *(signed int*)src; hr = VarUI8FromI8(s, d); break;}
816 case DBTYPE_R4: hr = VarUI8FromR4(*(FLOAT*)src, d); break;
817 case DBTYPE_R8: hr = VarUI8FromR8(*(double*)src, d); break;
818 case DBTYPE_CY: hr = VarUI8FromCy(*(CY*)src, d); break;
819 case DBTYPE_DATE: hr = VarUI8FromDate(*(DATE*)src, d); break;
820 case DBTYPE_BSTR: hr = VarUI8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
821 case DBTYPE_BOOL: hr = VarUI8FromBool(*(VARIANT_BOOL*)src, d); break;
822 case DBTYPE_DECIMAL: hr = VarUI8FromDec(src, d); break;
823 case DBTYPE_I1: hr = VarUI8FromI1(*(signed char*)src, d); break;
824 case DBTYPE_UI1: hr = VarUI8FromUI1(*(BYTE*)src, d); break;
825 case DBTYPE_UI2: hr = VarUI8FromUI2(*(WORD*)src, d); break;
826 case DBTYPE_UI4: hr = VarUI8FromUI4(*(DWORD*)src, d); break;
827 case DBTYPE_I8: hr = VarUI8FromI8(*(LONGLONG*)src, d); break;
828 case DBTYPE_UI8: *d = *(ULONGLONG*)src; hr = S_OK; break;
829 default: FIXME("Unimplemented conversion %04x -> UI8\n", src_type); return E_NOTIMPL;
831 break;
834 case DBTYPE_FILETIME:
836 FILETIME *d = dst;
837 switch(src_type)
839 case DBTYPE_EMPTY: d->dwLowDateTime = d->dwHighDateTime = 0; hr = S_OK; break;
840 case DBTYPE_FILETIME: *d = *(FILETIME*)src; hr = S_OK; break;
841 default: FIXME("Unimplemented conversion %04x -> FILETIME\n", src_type); return E_NOTIMPL;
843 break;
846 case DBTYPE_GUID:
848 GUID *d = dst;
849 switch(src_type)
851 case DBTYPE_EMPTY: *d = GUID_NULL; hr = S_OK; break;
852 case DBTYPE_GUID: *d = *(GUID*)src; hr = S_OK; break;
853 default: FIXME("Unimplemented conversion %04x -> GUID\n", src_type); return E_NOTIMPL;
855 break;
858 case DBTYPE_WSTR:
860 BSTR b;
861 DBLENGTH bstr_len;
862 INT bytes_to_copy;
863 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
864 src, &b, sizeof(BSTR), src_status, dst_status,
865 precision, scale, flags);
866 if(hr != S_OK || *dst_status == DBSTATUS_S_ISNULL)
867 return hr;
868 bstr_len = SysStringLen(b);
869 *dst_len = bstr_len * sizeof(WCHAR); /* Doesn't include size for '\0' */
870 *dst_status = DBSTATUS_S_OK;
871 bytes_to_copy = min(*dst_len + sizeof(WCHAR), dst_max_len);
872 if(dst)
874 if(bytes_to_copy >= sizeof(WCHAR))
876 memcpy(dst, b, bytes_to_copy - sizeof(WCHAR));
877 *((WCHAR*)dst + bytes_to_copy / sizeof(WCHAR) - 1) = 0;
878 if(bytes_to_copy < *dst_len + sizeof(WCHAR))
879 *dst_status = DBSTATUS_S_TRUNCATED;
881 else
883 *dst_status = DBSTATUS_E_DATAOVERFLOW;
884 hr = DB_E_ERRORSOCCURRED;
887 SysFreeString(b);
888 return hr;
891 case DBTYPE_STR:
893 BSTR b;
894 DBLENGTH length;
895 INT bytes_to_copy;
896 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &length,
897 src, &b, sizeof(BSTR), src_status, dst_status,
898 precision, scale, flags);
899 if(hr != S_OK) return hr;
900 length = WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b), NULL, 0, NULL, NULL);
901 *dst_len = length; /* Doesn't include size for '\0' */
902 *dst_status = DBSTATUS_S_OK;
903 bytes_to_copy = min(length + 1, dst_max_len);
904 if(dst)
906 if(bytes_to_copy >= sizeof(char))
908 WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b), dst, bytes_to_copy - 1, NULL, NULL);
909 *((char *)dst + bytes_to_copy - 1) = 0;
910 if(bytes_to_copy < length + 1)
911 *dst_status = DBSTATUS_S_TRUNCATED;
913 else
915 *dst_status = DBSTATUS_E_DATAOVERFLOW;
916 hr = DB_E_ERRORSOCCURRED;
919 SysFreeString(b);
920 return hr;
923 case DBTYPE_BYREF | DBTYPE_WSTR:
925 BSTR b;
926 WCHAR **d = dst;
927 DBLENGTH bstr_len;
928 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
929 src, &b, sizeof(BSTR), src_status, dst_status,
930 precision, scale, flags);
931 if(hr != S_OK) return hr;
933 bstr_len = SysStringLen(b) * sizeof(WCHAR);
934 *dst_len = bstr_len; /* Doesn't include size for '\0' */
936 *d = CoTaskMemAlloc(bstr_len + sizeof(WCHAR));
937 if(*d) memcpy(*d, b, bstr_len + sizeof(WCHAR));
938 else hr = E_OUTOFMEMORY;
939 SysFreeString(b);
940 return hr;
943 case DBTYPE_BYREF | DBTYPE_STR:
945 BSTR b;
946 char **d = dst;
947 DBLENGTH length;
948 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &length,
949 src, &b, sizeof(BSTR), src_status, dst_status,
950 precision, scale, flags);
951 if(hr != S_OK) return hr;
953 length = WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b) + 1, NULL, 0, NULL, NULL);
954 *dst_len = length - 1; /* Doesn't include size for '\0' */
955 *dst_status = DBSTATUS_S_OK;
956 *d = CoTaskMemAlloc(length);
958 if(*d)
959 WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b) + 1, *d, length, NULL, NULL);
960 else
961 hr = E_OUTOFMEMORY;
962 SysFreeString(b);
964 return hr;
967 case DBTYPE_VARIANT:
969 VARIANT *v = dst;
971 switch(src_type)
973 case DBTYPE_BOOL:
974 V_VT(v) = VT_BOOL;
975 V_BOOL(v) = *(VARIANT_BOOL*)src;
976 hr = S_OK;
977 break;
978 case DBTYPE_I2:
979 V_VT(v) = VT_I2;
980 V_I2(v) = *(signed short*)src;
981 hr = S_OK;
982 break;
983 case DBTYPE_I4:
984 V_VT(v) = VT_I4;
985 V_I4(v) = *(signed int*)src;
986 hr = S_OK;
987 break;
988 case DBTYPE_I8:
989 V_VT(v) = VT_DECIMAL;
990 hr = VarDecFromI8( *(LONGLONG*)src, &V_DECIMAL(v));
991 break;
992 case DBTYPE_R4:
993 V_VT(v) = VT_R4;
994 V_R4(v) = *(FLOAT*)src;
995 hr = S_OK;
996 break;
997 case DBTYPE_R8:
998 V_VT(v) = VT_R8;
999 V_R8(v) = *(double*)src;
1000 hr = S_OK;
1001 break;
1002 case DBTYPE_BSTR:
1004 BSTR s = *(WCHAR**)src;
1005 TRACE("%s\n", debugstr_w(s));
1006 V_VT(v) = VT_BSTR;
1007 V_BSTR(v) = SysAllocString(s);
1008 hr = V_BSTR(v) ? S_OK : E_OUTOFMEMORY;
1009 break;
1011 case DBTYPE_DATE:
1012 V_VT(v) = VT_DATE;
1013 V_DATE(v) = *(DATE*)src;
1014 hr = S_OK;
1015 break;
1016 case DBTYPE_DBDATE:
1018 SYSTEMTIME st;
1019 DBDATE *ts = src;
1021 V_VT(v) = VT_DATE;
1023 st.wYear = ts->year;
1024 st.wMonth = ts->month;
1025 st.wDay = ts->day;
1026 st.wHour = 0;
1027 st.wMinute = 0;
1028 st.wSecond = 0;
1029 st.wMilliseconds = 0;
1030 hr = (SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL);
1031 break;
1033 case DBTYPE_DBTIMESTAMP:
1035 SYSTEMTIME st;
1036 DBTIMESTAMP *ts = src;
1038 V_VT(v) = VT_DATE;
1040 st.wYear = ts->year;
1041 st.wMonth = ts->month;
1042 st.wDay = ts->day;
1043 st.wHour = ts->hour;
1044 st.wMinute = ts->minute;
1045 st.wSecond = ts->second;
1046 st.wMilliseconds = ts->fraction/1000000;
1047 hr = SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL;
1048 break;
1050 case DBTYPE_CY:
1051 V_VT(v) = VT_CY;
1052 V_CY(v) = *(CY*)src;
1053 hr = S_OK;
1054 break;
1055 case DBTYPE_BYTES:
1057 LONG i;
1058 SAFEARRAY *psa = NULL;
1059 SAFEARRAYBOUND rgsabound[1];
1060 unsigned char *p = src;
1062 rgsabound[0].lLbound = 0;
1063 rgsabound[0].cElements = src_len;
1065 psa = SafeArrayCreate(VT_UI1,1,rgsabound);
1066 for(i =0; i < src_len; i++,p++)
1068 hr = SafeArrayPutElement(psa, &i, p);
1069 if(FAILED(hr)) {
1070 SafeArrayDestroy (psa);
1071 return hr;
1075 V_VT(v) = VT_ARRAY|VT_UI1;
1076 V_ARRAY(v) = psa;
1077 hr = S_OK;
1078 break;
1080 default: FIXME("Unimplemented conversion %04x -> VARIANT\n", src_type); return E_NOTIMPL;
1082 break;
1084 case DBTYPE_BYTES:
1086 BYTE *d = dst;
1088 switch(src_type)
1090 case DBTYPE_BYTES:
1091 if( src_len > dst_max_len)
1092 *dst_status = DBSTATUS_S_TRUNCATED;
1093 else
1094 *dst_status = DBSTATUS_S_OK;
1096 *dst_len = src_len;
1097 memcpy(d, src, min(src_len, dst_max_len));
1099 return S_OK;
1100 case DBTYPE_VARIANT:
1102 switch(V_VT((VARIANT*)src))
1104 case VT_UI1 | VT_ARRAY:
1106 LONG l;
1107 BYTE *data = NULL;
1109 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1110 if(FAILED(hr))
1111 return hr;
1113 hr = SafeArrayAccessData(V_ARRAY((VARIANT*)src), (VOID**)&data);
1114 if(FAILED(hr))
1116 ERR("SafeArrayAccessData Failed = 0x%08lx\n", hr);
1117 return hr;
1120 *dst_len = l+1;
1121 *dst_status = DBSTATUS_S_OK;
1122 memcpy(d, data, *dst_len);
1124 SafeArrayUnaccessData(V_ARRAY((VARIANT*)src));
1125 return S_OK;
1127 break;
1128 default:
1129 FIXME("Unimplemented variant type %d -> BYTES\n", V_VT((VARIANT*)src));
1130 return E_NOTIMPL;
1133 break;
1134 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES\n", src_type); return E_NOTIMPL;
1136 break;
1138 case DBTYPE_BYTES | DBTYPE_BYREF:
1140 BYTE **d = dst;
1142 switch(src_type)
1144 case DBTYPE_BYTES:
1145 *d = CoTaskMemAlloc(src_len);
1146 if(*d) memcpy(*d, src, src_len);
1147 else hr = E_OUTOFMEMORY;
1149 *dst_len = src_len;
1150 *dst_status = DBSTATUS_S_OK;
1151 return S_OK;
1152 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES | DBTYPE_BYREF\n", src_type); return E_NOTIMPL;
1154 break;
1157 case DBTYPE_NUMERIC:
1158 FIXME("Unimplemented conversion %04x -> DBTYPE_NUMERIC\n", src_type);
1159 return E_NOTIMPL;
1161 default:
1162 FIXME("Unimplemented conversion %04x -> %04x\n", src_type, dst_type);
1163 return E_NOTIMPL;
1166 if(hr == DISP_E_OVERFLOW)
1168 *dst_status = DBSTATUS_E_DATAOVERFLOW;
1169 *dst_len = get_length(dst_type);
1170 hr = DB_E_ERRORSOCCURRED;
1172 else if(hr == S_OK)
1174 *dst_status = DBSTATUS_S_OK;
1175 *dst_len = get_length(dst_type);
1178 return hr;
1181 static inline WORD get_dbtype_class(DBTYPE type)
1183 switch(type)
1185 case DBTYPE_I2:
1186 case DBTYPE_R4:
1187 case DBTYPE_R8:
1188 case DBTYPE_I1:
1189 case DBTYPE_UI1:
1190 case DBTYPE_UI2:
1191 return DBTYPE_I2;
1193 case DBTYPE_I4:
1194 case DBTYPE_UI4:
1195 return DBTYPE_I4;
1197 case DBTYPE_I8:
1198 case DBTYPE_UI8:
1199 return DBTYPE_I8;
1201 case DBTYPE_BSTR:
1202 case DBTYPE_STR:
1203 case DBTYPE_WSTR:
1204 return DBTYPE_BSTR;
1206 case DBTYPE_DBDATE:
1207 case DBTYPE_DBTIME:
1208 case DBTYPE_DBTIMESTAMP:
1209 return DBTYPE_DBDATE;
1211 return type;
1214 /* Many src types will convert to this group of dst types */
1215 static inline BOOL common_class(WORD dst_class)
1217 switch(dst_class)
1219 case DBTYPE_EMPTY:
1220 case DBTYPE_NULL:
1221 case DBTYPE_I2:
1222 case DBTYPE_I4:
1223 case DBTYPE_BSTR:
1224 case DBTYPE_BOOL:
1225 case DBTYPE_VARIANT:
1226 case DBTYPE_I8:
1227 case DBTYPE_CY:
1228 case DBTYPE_DECIMAL:
1229 case DBTYPE_NUMERIC:
1230 return TRUE;
1232 return FALSE;
1235 static inline BOOL array_type(DBTYPE type)
1237 return (type >= DBTYPE_I2 && type <= DBTYPE_UI4);
1240 static HRESULT WINAPI convert_CanConvert(IDataConvert* iface,
1241 DBTYPE src_type, DBTYPE dst_type)
1243 convert *This = impl_from_IDataConvert(iface);
1244 DBTYPE src_base_type = src_type & 0x1ff;
1245 DBTYPE dst_base_type = dst_type & 0x1ff;
1246 WORD dst_class = get_dbtype_class(dst_base_type);
1248 TRACE("(%p)->(%d, %d)\n", This, src_type, dst_type);
1250 if(src_type & DBTYPE_VECTOR || dst_type & DBTYPE_VECTOR) return S_FALSE;
1252 if(src_type & DBTYPE_ARRAY)
1254 if(!array_type(src_base_type)) return S_FALSE;
1255 if(dst_type & DBTYPE_ARRAY)
1257 if(src_type == dst_type) return S_OK;
1258 return S_FALSE;
1260 if(dst_type == DBTYPE_VARIANT) return S_OK;
1261 return S_FALSE;
1264 if(dst_type & DBTYPE_ARRAY)
1266 if(!array_type(dst_base_type)) return S_FALSE;
1267 if(src_type == DBTYPE_IDISPATCH || src_type == DBTYPE_VARIANT) return S_OK;
1268 return S_FALSE;
1271 if(dst_type & DBTYPE_BYREF)
1272 if(dst_base_type != DBTYPE_BYTES && dst_base_type != DBTYPE_STR && dst_base_type != DBTYPE_WSTR)
1273 return S_FALSE;
1275 switch(get_dbtype_class(src_base_type))
1277 case DBTYPE_EMPTY:
1278 if(common_class(dst_class)) return S_OK;
1279 switch(dst_class)
1281 case DBTYPE_DATE:
1282 case DBTYPE_GUID:
1283 case DBTYPE_FILETIME:
1284 return S_OK;
1285 default:
1286 if(dst_base_type == DBTYPE_DBTIMESTAMP) return S_OK;
1287 return S_FALSE;
1290 case DBTYPE_NULL:
1291 switch(dst_base_type)
1293 case DBTYPE_NULL:
1294 case DBTYPE_VARIANT:
1295 case DBTYPE_FILETIME: return S_OK;
1296 default: return S_FALSE;
1299 case DBTYPE_I4:
1300 if(dst_base_type == DBTYPE_BYTES) return S_OK;
1301 /* fall through */
1302 case DBTYPE_I2:
1303 if(dst_base_type == DBTYPE_DATE) return S_OK;
1304 /* fall through */
1305 case DBTYPE_DECIMAL:
1306 if(common_class(dst_class)) return S_OK;
1307 if(dst_class == DBTYPE_DBDATE) return S_OK;
1308 return S_FALSE;
1310 case DBTYPE_BOOL:
1311 if(dst_base_type == DBTYPE_DATE) return S_OK;
1312 /* fall through */
1313 case DBTYPE_NUMERIC:
1314 case DBTYPE_CY:
1315 if(common_class(dst_class)) return S_OK;
1316 return S_FALSE;
1318 case DBTYPE_I8:
1319 if(common_class(dst_class)) return S_OK;
1320 switch(dst_base_type)
1322 case DBTYPE_BYTES:
1323 case DBTYPE_FILETIME: return S_OK;
1324 default: return S_FALSE;
1327 case DBTYPE_DATE:
1328 switch(dst_class)
1330 case DBTYPE_EMPTY:
1331 case DBTYPE_NULL:
1332 case DBTYPE_I2:
1333 case DBTYPE_I4:
1334 case DBTYPE_BSTR:
1335 case DBTYPE_BOOL:
1336 case DBTYPE_VARIANT:
1337 case DBTYPE_I8:
1338 case DBTYPE_DATE:
1339 case DBTYPE_DBDATE:
1340 case DBTYPE_FILETIME:
1341 return S_OK;
1342 default: return S_FALSE;
1345 case DBTYPE_IDISPATCH:
1346 case DBTYPE_VARIANT:
1347 switch(dst_base_type)
1349 case DBTYPE_IDISPATCH:
1350 case DBTYPE_ERROR:
1351 case DBTYPE_IUNKNOWN:
1352 return S_OK;
1354 /* fall through */
1355 case DBTYPE_BSTR:
1356 if(common_class(dst_class)) return S_OK;
1357 switch(dst_class)
1359 case DBTYPE_DATE:
1360 case DBTYPE_GUID:
1361 case DBTYPE_BYTES:
1362 case DBTYPE_DBDATE:
1363 case DBTYPE_FILETIME:
1364 return S_OK;
1365 default: return S_FALSE;
1368 case DBTYPE_ERROR:
1369 switch(dst_base_type)
1371 case DBTYPE_BSTR:
1372 case DBTYPE_ERROR:
1373 case DBTYPE_VARIANT:
1374 case DBTYPE_WSTR:
1375 return S_OK;
1376 default: return S_FALSE;
1379 case DBTYPE_IUNKNOWN:
1380 switch(dst_base_type)
1382 case DBTYPE_EMPTY:
1383 case DBTYPE_NULL:
1384 case DBTYPE_IDISPATCH:
1385 case DBTYPE_VARIANT:
1386 case DBTYPE_IUNKNOWN:
1387 return S_OK;
1388 default: return S_FALSE;
1391 case DBTYPE_BYTES:
1392 if(dst_class == DBTYPE_I4 || dst_class == DBTYPE_I8) return S_OK;
1393 /* fall through */
1394 case DBTYPE_GUID:
1395 switch(dst_class)
1397 case DBTYPE_EMPTY:
1398 case DBTYPE_NULL:
1399 case DBTYPE_BSTR:
1400 case DBTYPE_VARIANT:
1401 case DBTYPE_GUID:
1402 case DBTYPE_BYTES:
1403 return S_OK;
1404 default: return S_FALSE;
1407 case DBTYPE_FILETIME:
1408 if(dst_class == DBTYPE_I8) return S_OK;
1409 /* fall through */
1410 case DBTYPE_DBDATE:
1411 switch(dst_class)
1413 case DBTYPE_EMPTY:
1414 case DBTYPE_NULL:
1415 case DBTYPE_DATE:
1416 case DBTYPE_BSTR:
1417 case DBTYPE_VARIANT:
1418 case DBTYPE_DBDATE:
1419 case DBTYPE_FILETIME:
1420 return S_OK;
1421 default: return S_FALSE;
1425 return S_FALSE;
1428 static HRESULT WINAPI convert_GetConversionSize(IDataConvert* iface,
1429 DBTYPE src_type, DBTYPE dst_type,
1430 DBLENGTH *src_len, DBLENGTH *dst_len,
1431 void *src)
1433 convert *This = impl_from_IDataConvert(iface);
1434 HRESULT hr;
1436 TRACE("(%p)->(%d, %d, %p, %p, %p)\n", This, src_type, dst_type, src_len, dst_len, src);
1438 hr = IDataConvert_CanConvert(iface, src_type, dst_type);
1439 if (hr != S_OK)
1440 return DB_E_UNSUPPORTEDCONVERSION;
1442 if (!dst_len)
1443 return E_INVALIDARG;
1445 /* for some types we don't need to look into source data */
1446 if ((*dst_len = get_length(dst_type)))
1447 return S_OK;
1449 *dst_len = 110;
1451 if(src_type == DBTYPE_VARIANT && V_VT((VARIANT*)src) == VT_NULL)
1452 return S_OK;
1454 switch (dst_type)
1456 case DBTYPE_STR:
1457 switch (src_type)
1459 case DBTYPE_VARIANT:
1461 VARIANT v;
1463 VariantInit(&v);
1464 if ((hr = VariantChangeType(&v, src, 0, VT_BSTR)) == S_OK)
1466 *dst_len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), -1, NULL, 0, NULL, NULL);
1467 VariantClear(&v);
1469 else
1470 return hr;
1472 break;
1473 case DBTYPE_DATE:
1474 case DBTYPE_DECIMAL:
1475 case DBTYPE_EMPTY:
1476 case DBTYPE_I1:
1477 case DBTYPE_I2:
1478 case DBTYPE_UI2:
1479 case DBTYPE_I4:
1480 case DBTYPE_UI4:
1481 case DBTYPE_I8:
1482 case DBTYPE_UI8:
1483 case DBTYPE_R4:
1484 case DBTYPE_DBDATE:
1485 case DBTYPE_DBTIMESTAMP:
1486 case DBTYPE_DBTIME:
1487 break;
1488 default:
1489 FIXME("unimplemented for %04x -> DBTYPE_STR\n", src_type);
1490 return E_NOTIMPL;
1492 break;
1493 case DBTYPE_WSTR:
1494 switch (src_type)
1496 case DBTYPE_VARIANT:
1498 VARIANT v;
1500 VariantInit(&v);
1501 if ((hr = VariantChangeType(&v, src, 0, VT_BSTR)) == S_OK)
1503 *dst_len = (SysStringLen(V_BSTR(&v))+1) * sizeof(WCHAR);
1504 VariantClear(&v);
1507 break;
1508 case DBTYPE_STR:
1509 if(src_len)
1510 *dst_len = (*src_len + 1) * sizeof(WCHAR);
1511 else
1512 *dst_len = (strlen(src) + 1) * sizeof(WCHAR);
1513 break;
1514 case DBTYPE_WSTR:
1515 if(src_len)
1516 *dst_len = (*src_len) + sizeof(WCHAR);
1517 else
1518 *dst_len = (lstrlenW(src) + 1) * sizeof(WCHAR);
1519 break;
1520 case DBTYPE_DATE:
1521 case DBTYPE_DECIMAL:
1522 case DBTYPE_EMPTY:
1523 case DBTYPE_I1:
1524 case DBTYPE_I2:
1525 case DBTYPE_UI2:
1526 case DBTYPE_I4:
1527 case DBTYPE_UI4:
1528 case DBTYPE_I8:
1529 case DBTYPE_UI8:
1530 case DBTYPE_R4:
1531 case DBTYPE_DBDATE:
1532 case DBTYPE_DBTIMESTAMP:
1533 case DBTYPE_DBTIME:
1534 break;
1535 default:
1536 FIXME("unimplemented for %04x -> DBTYPE_WSTR\n", src_type);
1537 return E_NOTIMPL;
1539 break;
1540 case DBTYPE_BYTES:
1541 switch (src_type)
1543 case DBTYPE_VARIANT:
1544 if(V_VT((VARIANT*)src) == VT_BSTR)
1545 *dst_len = (SysStringLen(V_BSTR((VARIANT*)src))) / sizeof(WCHAR);
1546 else
1548 switch(V_VT((VARIANT*)src))
1550 case VT_UI1 | VT_ARRAY:
1552 LONG l;
1554 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1555 if(FAILED(hr))
1556 return hr;
1557 *dst_len = l+1;
1559 break;
1561 default:
1562 WARN("DBTYPE_VARIANT(%d)->DBTYPE_BYTES unimplemented\n", V_VT((VARIANT*)src));
1565 break;
1566 default:
1567 FIXME("unimplemented for %04x -> DBTYPE_BYTES\n", src_type);
1568 return E_NOTIMPL;
1570 break;
1571 default:
1572 FIXME("unimplemented for conversion %d->%d\n", src_type, dst_type);
1573 return E_NOTIMPL;
1576 return S_OK;
1579 static const struct IDataConvertVtbl convert_vtbl =
1581 convert_QueryInterface,
1582 convert_AddRef,
1583 convert_Release,
1584 convert_DataConvert,
1585 convert_CanConvert,
1586 convert_GetConversionSize
1589 static HRESULT WINAPI dcinfo_QueryInterface(IDCInfo* iface, REFIID riid, void **obj)
1591 convert *This = impl_from_IDCInfo(iface);
1593 return IDataConvert_QueryInterface(&This->IDataConvert_iface, riid, obj);
1596 static ULONG WINAPI dcinfo_AddRef(IDCInfo* iface)
1598 convert *This = impl_from_IDCInfo(iface);
1600 return IDataConvert_AddRef(&This->IDataConvert_iface);
1603 static ULONG WINAPI dcinfo_Release(IDCInfo* iface)
1605 convert *This = impl_from_IDCInfo(iface);
1607 return IDataConvert_Release(&This->IDataConvert_iface);
1610 static HRESULT WINAPI dcinfo_GetInfo(IDCInfo *iface, ULONG num, DCINFOTYPE types[], DCINFO **info_ptr)
1612 convert *This = impl_from_IDCInfo(iface);
1613 ULONG i;
1614 DCINFO *infos;
1616 TRACE("(%p)->(%ld, %p, %p)\n", This, num, types, info_ptr);
1618 *info_ptr = infos = CoTaskMemAlloc(num * sizeof(*infos));
1619 if(!infos) return E_OUTOFMEMORY;
1621 for(i = 0; i < num; i++)
1623 infos[i].eInfoType = types[i];
1624 VariantInit(&infos[i].vData);
1626 switch(types[i])
1628 case DCINFOTYPE_VERSION:
1629 V_VT(&infos[i].vData) = VT_UI4;
1630 V_UI4(&infos[i].vData) = This->version;
1631 break;
1635 return S_OK;
1638 static HRESULT WINAPI dcinfo_SetInfo(IDCInfo* iface, ULONG num, DCINFO info[])
1640 convert *This = impl_from_IDCInfo(iface);
1641 ULONG i;
1642 HRESULT hr = S_OK;
1644 TRACE("(%p)->(%ld, %p)\n", This, num, info);
1646 for(i = 0; i < num; i++)
1648 switch(info[i].eInfoType)
1650 case DCINFOTYPE_VERSION:
1651 if(V_VT(&info[i].vData) != VT_UI4)
1653 FIXME("VERSION with vt %x\n", V_VT(&info[i].vData));
1654 hr = DB_S_ERRORSOCCURRED;
1655 break;
1657 This->version = V_UI4(&info[i].vData);
1658 break;
1660 default:
1661 FIXME("Unhandled info type %ld (vt %x)\n", info[i].eInfoType, V_VT(&info[i].vData));
1664 return hr;
1667 static const struct IDCInfoVtbl dcinfo_vtbl =
1669 dcinfo_QueryInterface,
1670 dcinfo_AddRef,
1671 dcinfo_Release,
1672 dcinfo_GetInfo,
1673 dcinfo_SetInfo
1676 HRESULT create_oledb_convert(IUnknown *outer, void **obj)
1678 convert *This;
1680 TRACE("(%p, %p)\n", outer, obj);
1682 *obj = NULL;
1684 if(outer) return CLASS_E_NOAGGREGATION;
1686 This = heap_alloc(sizeof(*This));
1687 if(!This) return E_OUTOFMEMORY;
1689 This->IDataConvert_iface.lpVtbl = &convert_vtbl;
1690 This->IDCInfo_iface.lpVtbl = &dcinfo_vtbl;
1691 This->ref = 1;
1692 This->version = 0x110;
1694 *obj = &This->IDataConvert_iface;
1696 return S_OK;