combase: Fix the trailing linefeed of a TRACE().
[wine/zf.git] / dlls / oledb32 / convert.c
blob2fc65c7403147a11df6f36ad98a5d5669d892aae
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, %ld, %p, %p, %p, %ld, %d, %p, %d, %d, %x)\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 signed int *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 static const WCHAR format[] = {
674 '{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
675 '%','0','2','X','%','0','2','X','-',
676 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
677 wsprintfW(szBuff, format,
678 id->Data1, id->Data2, id->Data3,
679 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
680 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
681 *d = SysAllocString(szBuff);
682 hr = *d ? S_OK : E_OUTOFMEMORY;
684 break;
685 case DBTYPE_BYTES:
687 *d = SysAllocStringLen(NULL, 2 * src_len);
688 if (*d == NULL)
689 hr = E_OUTOFMEMORY;
690 else
692 static const char hexchars[] = "0123456789ABCDEF";
693 WCHAR *s = *d;
694 unsigned char *p = src;
695 while (src_len > 0)
697 *s++ = hexchars[(*p >> 4) & 0x0F];
698 *s++ = hexchars[(*p) & 0x0F];
699 src_len--; p++;
701 hr = S_OK;
704 break;
705 case DBTYPE_DBTIMESTAMP:
707 WCHAR szBuff[64];
708 static const WCHAR format1[] = {
709 '%','0','4','d','-','%','0','2','d','-','%','0','2','d',' ','%','0','2','d',':','%','0','2','d',
710 ':','%','0','2','d', 0};
711 static const WCHAR format2[] = {
712 '%','0','4','d','-','%','0','2','d','-','%','0','2','d',' ','%','0','2','d',':','%','0','2','d',
713 ':','%','0','2','d','.','%','0','9','d', 0};
714 DBTIMESTAMP *ts = src;
716 if(ts->fraction == 0)
717 wsprintfW(szBuff, format1, ts->year, ts->month, ts->day, ts->hour, ts->minute, ts->second);
718 else
719 wsprintfW(szBuff, format2, ts->year, ts->month, ts->day, ts->hour, ts->minute, ts->second, ts->fraction );
720 *d = SysAllocString(szBuff);
721 hr = *d ? S_OK : E_OUTOFMEMORY;
722 break;
724 case DBTYPE_VARIANT:
725 VariantInit(&tmp);
726 if ((hr = VariantChangeType(&tmp, src, 0, VT_BSTR)) == S_OK)
727 *d = V_BSTR(&tmp);
728 break;
729 default: FIXME("Unimplemented conversion %04x -> BSTR\n", src_type); return E_NOTIMPL;
731 break;
734 case DBTYPE_UI1:
736 BYTE *d = dst;
737 switch(src_type)
739 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
740 case DBTYPE_I2: hr = VarUI1FromI2(*(signed short*)src, d); break;
741 case DBTYPE_I4: hr = VarUI1FromI4(*(signed int*)src, d); break;
742 case DBTYPE_R4: hr = VarUI1FromR4(*(FLOAT*)src, d); break;
743 case DBTYPE_R8: hr = VarUI1FromR8(*(double*)src, d); break;
744 case DBTYPE_CY: hr = VarUI1FromCy(*(CY*)src, d); break;
745 case DBTYPE_DATE: hr = VarUI1FromDate(*(DATE*)src, d); break;
746 case DBTYPE_BSTR: hr = VarUI1FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
747 case DBTYPE_BOOL: hr = VarUI1FromBool(*(VARIANT_BOOL*)src, d); break;
748 case DBTYPE_DECIMAL: hr = VarUI1FromDec(src, d); break;
749 case DBTYPE_I1: hr = VarUI1FromI1(*(signed char*)src, d); break;
750 case DBTYPE_UI1: *d = *(BYTE*)src; hr = S_OK; break;
751 case DBTYPE_UI2: hr = VarUI1FromUI2(*(WORD*)src, d); break;
752 case DBTYPE_UI4: hr = VarUI1FromUI4(*(DWORD*)src, d); break;
753 case DBTYPE_I8: hr = VarUI1FromI8(*(LONGLONG*)src, d); break;
754 case DBTYPE_UI8: hr = VarUI1FromUI8(*(ULONGLONG*)src, d); break;
755 default: FIXME("Unimplemented conversion %04x -> UI1\n", src_type); return E_NOTIMPL;
757 break;
759 case DBTYPE_UI2:
761 WORD *d = dst;
762 switch(src_type)
764 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
765 case DBTYPE_I2: hr = VarUI2FromI2(*(signed short*)src, d); break;
766 case DBTYPE_I4: hr = VarUI2FromI4(*(signed int*)src, d); break;
767 case DBTYPE_R4: hr = VarUI2FromR4(*(FLOAT*)src, d); break;
768 case DBTYPE_R8: hr = VarUI2FromR8(*(double*)src, d); break;
769 case DBTYPE_CY: hr = VarUI2FromCy(*(CY*)src, d); break;
770 case DBTYPE_DATE: hr = VarUI2FromDate(*(DATE*)src, d); break;
771 case DBTYPE_BSTR: hr = VarUI2FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
772 case DBTYPE_BOOL: hr = VarUI2FromBool(*(VARIANT_BOOL*)src, d); break;
773 case DBTYPE_DECIMAL: hr = VarUI2FromDec(src, d); break;
774 case DBTYPE_I1: hr = VarUI2FromI1(*(signed char*)src, d); break;
775 case DBTYPE_UI1: hr = VarUI2FromUI1(*(BYTE*)src, d); break;
776 case DBTYPE_UI2: *d = *(WORD*)src; hr = S_OK; break;
777 case DBTYPE_UI4: hr = VarUI2FromUI4(*(DWORD*)src, d); break;
778 case DBTYPE_I8: hr = VarUI2FromI8(*(LONGLONG*)src, d); break;
779 case DBTYPE_UI8: hr = VarUI2FromUI8(*(ULONGLONG*)src, d); break;
780 default: FIXME("Unimplemented conversion %04x -> UI2\n", src_type); return E_NOTIMPL;
782 break;
785 case DBTYPE_UI4:
787 DWORD *d = dst;
788 switch(src_type)
790 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
791 case DBTYPE_I2: hr = VarUI4FromI2(*(signed short*)src, d); break;
792 case DBTYPE_I4: hr = VarUI4FromI4(*(signed int*)src, d); break;
793 case DBTYPE_R4: hr = VarUI4FromR4(*(FLOAT*)src, d); break;
794 case DBTYPE_R8: hr = VarUI4FromR8(*(double*)src, d); break;
795 case DBTYPE_CY: hr = VarUI4FromCy(*(CY*)src, d); break;
796 case DBTYPE_DATE: hr = VarUI4FromDate(*(DATE*)src, d); break;
797 case DBTYPE_BSTR: hr = VarUI4FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
798 case DBTYPE_BOOL: hr = VarUI4FromBool(*(VARIANT_BOOL*)src, d); break;
799 case DBTYPE_DECIMAL: hr = VarUI4FromDec(src, d); break;
800 case DBTYPE_I1: hr = VarUI4FromI1(*(signed char*)src, d); break;
801 case DBTYPE_UI1: hr = VarUI4FromUI1(*(BYTE*)src, d); break;
802 case DBTYPE_UI2: hr = VarUI4FromUI2(*(WORD*)src, d); break;
803 case DBTYPE_UI4: *d = *(DWORD*)src; hr = S_OK; break;
804 case DBTYPE_I8: hr = VarUI4FromI8(*(LONGLONG*)src, d); break;
805 case DBTYPE_UI8: hr = VarUI4FromUI8(*(ULONGLONG*)src, d); break;
806 case DBTYPE_VARIANT:
807 VariantInit(&tmp);
808 if ((hr = VariantChangeType(&tmp, src, 0, VT_UI4)) == S_OK)
809 *d = V_UI4(&tmp);
810 break;
811 default: FIXME("Unimplemented conversion %04x -> UI4\n", src_type); return E_NOTIMPL;
813 break;
816 case DBTYPE_UI8:
818 ULONGLONG *d = dst;
819 switch(src_type)
821 case DBTYPE_EMPTY: *d = 0; hr = S_OK; break;
822 case DBTYPE_I2: hr = VarUI8FromI2(*(signed short*)src, d); break;
823 case DBTYPE_I4: {LONGLONG s = *(signed int*)src; hr = VarUI8FromI8(s, d); break;}
824 case DBTYPE_R4: hr = VarUI8FromR4(*(FLOAT*)src, d); break;
825 case DBTYPE_R8: hr = VarUI8FromR8(*(double*)src, d); break;
826 case DBTYPE_CY: hr = VarUI8FromCy(*(CY*)src, d); break;
827 case DBTYPE_DATE: hr = VarUI8FromDate(*(DATE*)src, d); break;
828 case DBTYPE_BSTR: hr = VarUI8FromStr(*(WCHAR**)src, LOCALE_USER_DEFAULT, 0, d); break;
829 case DBTYPE_BOOL: hr = VarUI8FromBool(*(VARIANT_BOOL*)src, d); break;
830 case DBTYPE_DECIMAL: hr = VarUI8FromDec(src, d); break;
831 case DBTYPE_I1: hr = VarUI8FromI1(*(signed char*)src, d); break;
832 case DBTYPE_UI1: hr = VarUI8FromUI1(*(BYTE*)src, d); break;
833 case DBTYPE_UI2: hr = VarUI8FromUI2(*(WORD*)src, d); break;
834 case DBTYPE_UI4: hr = VarUI8FromUI4(*(DWORD*)src, d); break;
835 case DBTYPE_I8: hr = VarUI8FromI8(*(LONGLONG*)src, d); break;
836 case DBTYPE_UI8: *d = *(ULONGLONG*)src; hr = S_OK; break;
837 default: FIXME("Unimplemented conversion %04x -> UI8\n", src_type); return E_NOTIMPL;
839 break;
842 case DBTYPE_FILETIME:
844 FILETIME *d = dst;
845 switch(src_type)
847 case DBTYPE_EMPTY: d->dwLowDateTime = d->dwHighDateTime = 0; hr = S_OK; break;
848 case DBTYPE_FILETIME: *d = *(FILETIME*)src; hr = S_OK; break;
849 default: FIXME("Unimplemented conversion %04x -> FILETIME\n", src_type); return E_NOTIMPL;
851 break;
854 case DBTYPE_GUID:
856 GUID *d = dst;
857 switch(src_type)
859 case DBTYPE_EMPTY: *d = GUID_NULL; hr = S_OK; break;
860 case DBTYPE_GUID: *d = *(GUID*)src; hr = S_OK; break;
861 default: FIXME("Unimplemented conversion %04x -> GUID\n", src_type); return E_NOTIMPL;
863 break;
866 case DBTYPE_WSTR:
868 BSTR b;
869 DBLENGTH bstr_len;
870 INT bytes_to_copy;
871 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
872 src, &b, sizeof(BSTR), src_status, dst_status,
873 precision, scale, flags);
874 if(hr != S_OK || *dst_status == DBSTATUS_S_ISNULL)
875 return hr;
876 bstr_len = SysStringLen(b);
877 *dst_len = bstr_len * sizeof(WCHAR); /* Doesn't include size for '\0' */
878 *dst_status = DBSTATUS_S_OK;
879 bytes_to_copy = min(*dst_len + sizeof(WCHAR), dst_max_len);
880 if(dst)
882 if(bytes_to_copy >= sizeof(WCHAR))
884 memcpy(dst, b, bytes_to_copy - sizeof(WCHAR));
885 *((WCHAR*)dst + bytes_to_copy / sizeof(WCHAR) - 1) = 0;
886 if(bytes_to_copy < *dst_len + sizeof(WCHAR))
887 *dst_status = DBSTATUS_S_TRUNCATED;
889 else
891 *dst_status = DBSTATUS_E_DATAOVERFLOW;
892 hr = DB_E_ERRORSOCCURRED;
895 SysFreeString(b);
896 return hr;
899 case DBTYPE_STR:
901 BSTR b;
902 DBLENGTH length;
903 INT bytes_to_copy;
904 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &length,
905 src, &b, sizeof(BSTR), src_status, dst_status,
906 precision, scale, flags);
907 if(hr != S_OK) return hr;
908 length = WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b), NULL, 0, NULL, NULL);
909 *dst_len = length; /* Doesn't include size for '\0' */
910 *dst_status = DBSTATUS_S_OK;
911 bytes_to_copy = min(length + 1, dst_max_len);
912 if(dst)
914 if(bytes_to_copy >= sizeof(char))
916 WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b), dst, bytes_to_copy - 1, NULL, NULL);
917 *((char *)dst + bytes_to_copy - 1) = 0;
918 if(bytes_to_copy < length + 1)
919 *dst_status = DBSTATUS_S_TRUNCATED;
921 else
923 *dst_status = DBSTATUS_E_DATAOVERFLOW;
924 hr = DB_E_ERRORSOCCURRED;
927 SysFreeString(b);
928 return hr;
931 case DBTYPE_BYREF | DBTYPE_WSTR:
933 BSTR b;
934 WCHAR **d = dst;
935 DBLENGTH bstr_len;
936 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &bstr_len,
937 src, &b, sizeof(BSTR), src_status, dst_status,
938 precision, scale, flags);
939 if(hr != S_OK) return hr;
941 bstr_len = SysStringLen(b) * sizeof(WCHAR);
942 *dst_len = bstr_len; /* Doesn't include size for '\0' */
944 *d = CoTaskMemAlloc(bstr_len + sizeof(WCHAR));
945 if(*d) memcpy(*d, b, bstr_len + sizeof(WCHAR));
946 else hr = E_OUTOFMEMORY;
947 SysFreeString(b);
948 return hr;
951 case DBTYPE_BYREF | DBTYPE_STR:
953 BSTR b;
954 char **d = dst;
955 DBLENGTH length;
956 hr = IDataConvert_DataConvert(iface, src_type, DBTYPE_BSTR, src_len, &length,
957 src, &b, sizeof(BSTR), src_status, dst_status,
958 precision, scale, flags);
959 if(hr != S_OK) return hr;
961 length = WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b) + 1, NULL, 0, NULL, NULL);
962 *dst_len = length - 1; /* Doesn't include size for '\0' */
963 *dst_status = DBSTATUS_S_OK;
964 *d = CoTaskMemAlloc(length);
966 if(*d)
967 WideCharToMultiByte(CP_ACP, 0, b, SysStringLen(b) + 1, *d, length, NULL, NULL);
968 else
969 hr = E_OUTOFMEMORY;
970 SysFreeString(b);
972 return hr;
975 case DBTYPE_VARIANT:
977 VARIANT *v = dst;
979 switch(src_type)
981 case DBTYPE_BOOL:
982 V_VT(v) = VT_BOOL;
983 V_BOOL(v) = *(VARIANT_BOOL*)src;
984 hr = S_OK;
985 break;
986 case DBTYPE_I2:
987 V_VT(v) = VT_I2;
988 V_I2(v) = *(signed short*)src;
989 hr = S_OK;
990 break;
991 case DBTYPE_I4:
992 V_VT(v) = VT_I4;
993 V_I4(v) = *(signed int*)src;
994 hr = S_OK;
995 break;
996 case DBTYPE_I8:
997 V_VT(v) = VT_DECIMAL;
998 hr = VarDecFromI8( *(LONGLONG*)src, &V_DECIMAL(v));
999 break;
1000 case DBTYPE_R4:
1001 V_VT(v) = VT_R4;
1002 V_R4(v) = *(FLOAT*)src;
1003 hr = S_OK;
1004 break;
1005 case DBTYPE_R8:
1006 V_VT(v) = VT_R8;
1007 V_R8(v) = *(double*)src;
1008 hr = S_OK;
1009 break;
1010 case DBTYPE_BSTR:
1012 BSTR s = *(WCHAR**)src;
1013 TRACE("%s\n", debugstr_w(s));
1014 V_VT(v) = VT_BSTR;
1015 V_BSTR(v) = SysAllocString(s);
1016 hr = V_BSTR(v) ? S_OK : E_OUTOFMEMORY;
1017 break;
1019 case DBTYPE_DATE:
1020 V_VT(v) = VT_DATE;
1021 V_DATE(v) = *(DATE*)src;
1022 hr = S_OK;
1023 break;
1024 case DBTYPE_DBDATE:
1026 SYSTEMTIME st;
1027 DBDATE *ts = src;
1029 V_VT(v) = VT_DATE;
1031 st.wYear = ts->year;
1032 st.wMonth = ts->month;
1033 st.wDay = ts->day;
1034 st.wHour = 0;
1035 st.wMinute = 0;
1036 st.wSecond = 0;
1037 st.wMilliseconds = 0;
1038 hr = (SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL);
1039 break;
1041 case DBTYPE_DBTIMESTAMP:
1043 SYSTEMTIME st;
1044 DBTIMESTAMP *ts = src;
1046 V_VT(v) = VT_DATE;
1048 st.wYear = ts->year;
1049 st.wMonth = ts->month;
1050 st.wDay = ts->day;
1051 st.wHour = ts->hour;
1052 st.wMinute = ts->minute;
1053 st.wSecond = ts->second;
1054 st.wMilliseconds = ts->fraction/1000000;
1055 hr = SystemTimeToVariantTime(&st, &V_DATE(v)) ? S_OK : E_FAIL;
1056 break;
1058 case DBTYPE_CY:
1059 V_VT(v) = VT_CY;
1060 V_CY(v) = *(CY*)src;
1061 hr = S_OK;
1062 break;
1063 case DBTYPE_BYTES:
1065 LONG i;
1066 SAFEARRAY *psa = NULL;
1067 SAFEARRAYBOUND rgsabound[1];
1068 unsigned char *p = src;
1070 rgsabound[0].lLbound = 0;
1071 rgsabound[0].cElements = src_len;
1073 psa = SafeArrayCreate(VT_UI1,1,rgsabound);
1074 for(i =0; i < src_len; i++,p++)
1076 hr = SafeArrayPutElement(psa, &i, p);
1077 if(FAILED(hr)) {
1078 SafeArrayDestroy (psa);
1079 return hr;
1083 V_VT(v) = VT_ARRAY|VT_UI1;
1084 V_ARRAY(v) = psa;
1085 hr = S_OK;
1086 break;
1088 default: FIXME("Unimplemented conversion %04x -> VARIANT\n", src_type); return E_NOTIMPL;
1090 break;
1092 case DBTYPE_BYTES:
1094 BYTE *d = dst;
1096 switch(src_type)
1098 case DBTYPE_BYTES:
1099 if( src_len > dst_max_len)
1100 *dst_status = DBSTATUS_S_TRUNCATED;
1101 else
1102 *dst_status = DBSTATUS_S_OK;
1104 *dst_len = src_len;
1105 memcpy(d, src, min(src_len, dst_max_len));
1107 return S_OK;
1108 case DBTYPE_VARIANT:
1110 switch(V_VT((VARIANT*)src))
1112 case VT_UI1 | VT_ARRAY:
1114 LONG l;
1115 BYTE *data = NULL;
1117 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1118 if(FAILED(hr))
1119 return hr;
1121 hr = SafeArrayAccessData(V_ARRAY((VARIANT*)src), (VOID**)&data);
1122 if(FAILED(hr))
1124 ERR("SafeArrayAccessData Failed = 0x%08x\n", hr);
1125 return hr;
1128 *dst_len = l+1;
1129 *dst_status = DBSTATUS_S_OK;
1130 memcpy(d, data, *dst_len);
1132 SafeArrayUnaccessData(V_ARRAY((VARIANT*)src));
1133 return S_OK;
1135 break;
1136 default:
1137 FIXME("Unimplemented variant type %d -> BYTES\n", V_VT((VARIANT*)src));
1138 return E_NOTIMPL;
1141 break;
1142 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES\n", src_type); return E_NOTIMPL;
1144 break;
1146 case DBTYPE_BYTES | DBTYPE_BYREF:
1148 BYTE **d = dst;
1150 switch(src_type)
1152 case DBTYPE_BYTES:
1153 *d = CoTaskMemAlloc(src_len);
1154 if(*d) memcpy(*d, src, src_len);
1155 else hr = E_OUTOFMEMORY;
1157 *dst_len = src_len;
1158 *dst_status = DBSTATUS_S_OK;
1159 return S_OK;
1160 default: FIXME("Unimplemented conversion %04x -> DBTYPE_BYTES | DBTYPE_BYREF\n", src_type); return E_NOTIMPL;
1162 break;
1165 case DBTYPE_NUMERIC:
1166 FIXME("Unimplemented conversion %04x -> DBTYPE_NUMERIC\n", src_type);
1167 return E_NOTIMPL;
1169 default:
1170 FIXME("Unimplemented conversion %04x -> %04x\n", src_type, dst_type);
1171 return E_NOTIMPL;
1174 if(hr == DISP_E_OVERFLOW)
1176 *dst_status = DBSTATUS_E_DATAOVERFLOW;
1177 *dst_len = get_length(dst_type);
1178 hr = DB_E_ERRORSOCCURRED;
1180 else if(hr == S_OK)
1182 *dst_status = DBSTATUS_S_OK;
1183 *dst_len = get_length(dst_type);
1186 return hr;
1189 static inline WORD get_dbtype_class(DBTYPE type)
1191 switch(type)
1193 case DBTYPE_I2:
1194 case DBTYPE_R4:
1195 case DBTYPE_R8:
1196 case DBTYPE_I1:
1197 case DBTYPE_UI1:
1198 case DBTYPE_UI2:
1199 return DBTYPE_I2;
1201 case DBTYPE_I4:
1202 case DBTYPE_UI4:
1203 return DBTYPE_I4;
1205 case DBTYPE_I8:
1206 case DBTYPE_UI8:
1207 return DBTYPE_I8;
1209 case DBTYPE_BSTR:
1210 case DBTYPE_STR:
1211 case DBTYPE_WSTR:
1212 return DBTYPE_BSTR;
1214 case DBTYPE_DBDATE:
1215 case DBTYPE_DBTIME:
1216 case DBTYPE_DBTIMESTAMP:
1217 return DBTYPE_DBDATE;
1219 return type;
1222 /* Many src types will convert to this group of dst types */
1223 static inline BOOL common_class(WORD dst_class)
1225 switch(dst_class)
1227 case DBTYPE_EMPTY:
1228 case DBTYPE_NULL:
1229 case DBTYPE_I2:
1230 case DBTYPE_I4:
1231 case DBTYPE_BSTR:
1232 case DBTYPE_BOOL:
1233 case DBTYPE_VARIANT:
1234 case DBTYPE_I8:
1235 case DBTYPE_CY:
1236 case DBTYPE_DECIMAL:
1237 case DBTYPE_NUMERIC:
1238 return TRUE;
1240 return FALSE;
1243 static inline BOOL array_type(DBTYPE type)
1245 return (type >= DBTYPE_I2 && type <= DBTYPE_UI4);
1248 static HRESULT WINAPI convert_CanConvert(IDataConvert* iface,
1249 DBTYPE src_type, DBTYPE dst_type)
1251 convert *This = impl_from_IDataConvert(iface);
1252 DBTYPE src_base_type = src_type & 0x1ff;
1253 DBTYPE dst_base_type = dst_type & 0x1ff;
1254 WORD dst_class = get_dbtype_class(dst_base_type);
1256 TRACE("(%p)->(%d, %d)\n", This, src_type, dst_type);
1258 if(src_type & DBTYPE_VECTOR || dst_type & DBTYPE_VECTOR) return S_FALSE;
1260 if(src_type & DBTYPE_ARRAY)
1262 if(!array_type(src_base_type)) return S_FALSE;
1263 if(dst_type & DBTYPE_ARRAY)
1265 if(src_type == dst_type) return S_OK;
1266 return S_FALSE;
1268 if(dst_type == DBTYPE_VARIANT) return S_OK;
1269 return S_FALSE;
1272 if(dst_type & DBTYPE_ARRAY)
1274 if(!array_type(dst_base_type)) return S_FALSE;
1275 if(src_type == DBTYPE_IDISPATCH || src_type == DBTYPE_VARIANT) return S_OK;
1276 return S_FALSE;
1279 if(dst_type & DBTYPE_BYREF)
1280 if(dst_base_type != DBTYPE_BYTES && dst_base_type != DBTYPE_STR && dst_base_type != DBTYPE_WSTR)
1281 return S_FALSE;
1283 switch(get_dbtype_class(src_base_type))
1285 case DBTYPE_EMPTY:
1286 if(common_class(dst_class)) return S_OK;
1287 switch(dst_class)
1289 case DBTYPE_DATE:
1290 case DBTYPE_GUID:
1291 case DBTYPE_FILETIME:
1292 return S_OK;
1293 default:
1294 if(dst_base_type == DBTYPE_DBTIMESTAMP) return S_OK;
1295 return S_FALSE;
1298 case DBTYPE_NULL:
1299 switch(dst_base_type)
1301 case DBTYPE_NULL:
1302 case DBTYPE_VARIANT:
1303 case DBTYPE_FILETIME: return S_OK;
1304 default: return S_FALSE;
1307 case DBTYPE_I4:
1308 if(dst_base_type == DBTYPE_BYTES) return S_OK;
1309 /* fall through */
1310 case DBTYPE_I2:
1311 if(dst_base_type == DBTYPE_DATE) return S_OK;
1312 /* fall through */
1313 case DBTYPE_DECIMAL:
1314 if(common_class(dst_class)) return S_OK;
1315 if(dst_class == DBTYPE_DBDATE) return S_OK;
1316 return S_FALSE;
1318 case DBTYPE_BOOL:
1319 if(dst_base_type == DBTYPE_DATE) return S_OK;
1320 /* fall through */
1321 case DBTYPE_NUMERIC:
1322 case DBTYPE_CY:
1323 if(common_class(dst_class)) return S_OK;
1324 return S_FALSE;
1326 case DBTYPE_I8:
1327 if(common_class(dst_class)) return S_OK;
1328 switch(dst_base_type)
1330 case DBTYPE_BYTES:
1331 case DBTYPE_FILETIME: return S_OK;
1332 default: return S_FALSE;
1335 case DBTYPE_DATE:
1336 switch(dst_class)
1338 case DBTYPE_EMPTY:
1339 case DBTYPE_NULL:
1340 case DBTYPE_I2:
1341 case DBTYPE_I4:
1342 case DBTYPE_BSTR:
1343 case DBTYPE_BOOL:
1344 case DBTYPE_VARIANT:
1345 case DBTYPE_I8:
1346 case DBTYPE_DATE:
1347 case DBTYPE_DBDATE:
1348 case DBTYPE_FILETIME:
1349 return S_OK;
1350 default: return S_FALSE;
1353 case DBTYPE_IDISPATCH:
1354 case DBTYPE_VARIANT:
1355 switch(dst_base_type)
1357 case DBTYPE_IDISPATCH:
1358 case DBTYPE_ERROR:
1359 case DBTYPE_IUNKNOWN:
1360 return S_OK;
1362 /* fall through */
1363 case DBTYPE_BSTR:
1364 if(common_class(dst_class)) return S_OK;
1365 switch(dst_class)
1367 case DBTYPE_DATE:
1368 case DBTYPE_GUID:
1369 case DBTYPE_BYTES:
1370 case DBTYPE_DBDATE:
1371 case DBTYPE_FILETIME:
1372 return S_OK;
1373 default: return S_FALSE;
1376 case DBTYPE_ERROR:
1377 switch(dst_base_type)
1379 case DBTYPE_BSTR:
1380 case DBTYPE_ERROR:
1381 case DBTYPE_VARIANT:
1382 case DBTYPE_WSTR:
1383 return S_OK;
1384 default: return S_FALSE;
1387 case DBTYPE_IUNKNOWN:
1388 switch(dst_base_type)
1390 case DBTYPE_EMPTY:
1391 case DBTYPE_NULL:
1392 case DBTYPE_IDISPATCH:
1393 case DBTYPE_VARIANT:
1394 case DBTYPE_IUNKNOWN:
1395 return S_OK;
1396 default: return S_FALSE;
1399 case DBTYPE_BYTES:
1400 if(dst_class == DBTYPE_I4 || dst_class == DBTYPE_I8) return S_OK;
1401 /* fall through */
1402 case DBTYPE_GUID:
1403 switch(dst_class)
1405 case DBTYPE_EMPTY:
1406 case DBTYPE_NULL:
1407 case DBTYPE_BSTR:
1408 case DBTYPE_VARIANT:
1409 case DBTYPE_GUID:
1410 case DBTYPE_BYTES:
1411 return S_OK;
1412 default: return S_FALSE;
1415 case DBTYPE_FILETIME:
1416 if(dst_class == DBTYPE_I8) return S_OK;
1417 /* fall through */
1418 case DBTYPE_DBDATE:
1419 switch(dst_class)
1421 case DBTYPE_EMPTY:
1422 case DBTYPE_NULL:
1423 case DBTYPE_DATE:
1424 case DBTYPE_BSTR:
1425 case DBTYPE_VARIANT:
1426 case DBTYPE_DBDATE:
1427 case DBTYPE_FILETIME:
1428 return S_OK;
1429 default: return S_FALSE;
1433 return S_FALSE;
1436 static HRESULT WINAPI convert_GetConversionSize(IDataConvert* iface,
1437 DBTYPE src_type, DBTYPE dst_type,
1438 DBLENGTH *src_len, DBLENGTH *dst_len,
1439 void *src)
1441 convert *This = impl_from_IDataConvert(iface);
1442 HRESULT hr;
1444 TRACE("(%p)->(%d, %d, %p, %p, %p)\n", This, src_type, dst_type, src_len, dst_len, src);
1446 hr = IDataConvert_CanConvert(iface, src_type, dst_type);
1447 if (hr != S_OK)
1448 return DB_E_UNSUPPORTEDCONVERSION;
1450 if (!dst_len)
1451 return E_INVALIDARG;
1453 /* for some types we don't need to look into source data */
1454 if ((*dst_len = get_length(dst_type)))
1455 return S_OK;
1457 *dst_len = 110;
1459 if(src_type == DBTYPE_VARIANT && V_VT((VARIANT*)src) == VT_NULL)
1460 return S_OK;
1462 switch (dst_type)
1464 case DBTYPE_STR:
1465 switch (src_type)
1467 case DBTYPE_VARIANT:
1469 VARIANT v;
1471 VariantInit(&v);
1472 if ((hr = VariantChangeType(&v, src, 0, VT_BSTR)) == S_OK)
1474 *dst_len = WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), -1, NULL, 0, NULL, NULL);
1475 VariantClear(&v);
1477 else
1478 return hr;
1480 break;
1481 case DBTYPE_DATE:
1482 case DBTYPE_DECIMAL:
1483 case DBTYPE_EMPTY:
1484 case DBTYPE_I1:
1485 case DBTYPE_I2:
1486 case DBTYPE_UI2:
1487 case DBTYPE_I4:
1488 case DBTYPE_UI4:
1489 case DBTYPE_I8:
1490 case DBTYPE_UI8:
1491 case DBTYPE_R4:
1492 case DBTYPE_DBDATE:
1493 case DBTYPE_DBTIMESTAMP:
1494 case DBTYPE_DBTIME:
1495 break;
1496 default:
1497 FIXME("unimplemented for %04x -> DBTYPE_STR\n", src_type);
1498 return E_NOTIMPL;
1500 break;
1501 case DBTYPE_WSTR:
1502 switch (src_type)
1504 case DBTYPE_VARIANT:
1506 VARIANT v;
1508 VariantInit(&v);
1509 if ((hr = VariantChangeType(&v, src, 0, VT_BSTR)) == S_OK)
1511 *dst_len = (SysStringLen(V_BSTR(&v))+1) * sizeof(WCHAR);
1512 VariantClear(&v);
1515 break;
1516 case DBTYPE_STR:
1517 if(src_len)
1518 *dst_len = (*src_len + 1) * sizeof(WCHAR);
1519 else
1520 *dst_len = (strlen(src) + 1) * sizeof(WCHAR);
1521 break;
1522 case DBTYPE_WSTR:
1523 if(src_len)
1524 *dst_len = (*src_len) + sizeof(WCHAR);
1525 else
1526 *dst_len = (lstrlenW(src) + 1) * sizeof(WCHAR);
1527 break;
1528 case DBTYPE_DATE:
1529 case DBTYPE_DECIMAL:
1530 case DBTYPE_EMPTY:
1531 case DBTYPE_I1:
1532 case DBTYPE_I2:
1533 case DBTYPE_UI2:
1534 case DBTYPE_I4:
1535 case DBTYPE_UI4:
1536 case DBTYPE_I8:
1537 case DBTYPE_UI8:
1538 case DBTYPE_R4:
1539 case DBTYPE_DBDATE:
1540 case DBTYPE_DBTIMESTAMP:
1541 case DBTYPE_DBTIME:
1542 break;
1543 default:
1544 FIXME("unimplemented for %04x -> DBTYPE_WSTR\n", src_type);
1545 return E_NOTIMPL;
1547 break;
1548 case DBTYPE_BYTES:
1549 switch (src_type)
1551 case DBTYPE_VARIANT:
1552 if(V_VT((VARIANT*)src) == VT_BSTR)
1553 *dst_len = (SysStringLen(V_BSTR((VARIANT*)src))) / sizeof(WCHAR);
1554 else
1556 switch(V_VT((VARIANT*)src))
1558 case VT_UI1 | VT_ARRAY:
1560 LONG l;
1562 hr = SafeArrayGetUBound(V_ARRAY((VARIANT*)src), 1, &l);
1563 if(FAILED(hr))
1564 return hr;
1565 *dst_len = l+1;
1567 break;
1569 default:
1570 WARN("DBTYPE_VARIANT(%d)->DBTYPE_BYTES unimplemented\n", V_VT((VARIANT*)src));
1573 break;
1574 default:
1575 FIXME("unimplemented for %04x -> DBTYPE_BYTES\n", src_type);
1576 return E_NOTIMPL;
1578 break;
1579 default:
1580 FIXME("unimplemented for conversion %d->%d\n", src_type, dst_type);
1581 return E_NOTIMPL;
1584 return S_OK;
1587 static const struct IDataConvertVtbl convert_vtbl =
1589 convert_QueryInterface,
1590 convert_AddRef,
1591 convert_Release,
1592 convert_DataConvert,
1593 convert_CanConvert,
1594 convert_GetConversionSize
1597 static HRESULT WINAPI dcinfo_QueryInterface(IDCInfo* iface, REFIID riid, void **obj)
1599 convert *This = impl_from_IDCInfo(iface);
1601 return IDataConvert_QueryInterface(&This->IDataConvert_iface, riid, obj);
1604 static ULONG WINAPI dcinfo_AddRef(IDCInfo* iface)
1606 convert *This = impl_from_IDCInfo(iface);
1608 return IDataConvert_AddRef(&This->IDataConvert_iface);
1611 static ULONG WINAPI dcinfo_Release(IDCInfo* iface)
1613 convert *This = impl_from_IDCInfo(iface);
1615 return IDataConvert_Release(&This->IDataConvert_iface);
1618 static HRESULT WINAPI dcinfo_GetInfo(IDCInfo *iface, ULONG num, DCINFOTYPE types[], DCINFO **info_ptr)
1620 convert *This = impl_from_IDCInfo(iface);
1621 ULONG i;
1622 DCINFO *infos;
1624 TRACE("(%p)->(%d, %p, %p)\n", This, num, types, info_ptr);
1626 *info_ptr = infos = CoTaskMemAlloc(num * sizeof(*infos));
1627 if(!infos) return E_OUTOFMEMORY;
1629 for(i = 0; i < num; i++)
1631 infos[i].eInfoType = types[i];
1632 VariantInit(&infos[i].vData);
1634 switch(types[i])
1636 case DCINFOTYPE_VERSION:
1637 V_VT(&infos[i].vData) = VT_UI4;
1638 V_UI4(&infos[i].vData) = This->version;
1639 break;
1643 return S_OK;
1646 static HRESULT WINAPI dcinfo_SetInfo(IDCInfo* iface, ULONG num, DCINFO info[])
1648 convert *This = impl_from_IDCInfo(iface);
1649 ULONG i;
1650 HRESULT hr = S_OK;
1652 TRACE("(%p)->(%d, %p)\n", This, num, info);
1654 for(i = 0; i < num; i++)
1656 switch(info[i].eInfoType)
1658 case DCINFOTYPE_VERSION:
1659 if(V_VT(&info[i].vData) != VT_UI4)
1661 FIXME("VERSION with vt %x\n", V_VT(&info[i].vData));
1662 hr = DB_S_ERRORSOCCURRED;
1663 break;
1665 This->version = V_UI4(&info[i].vData);
1666 break;
1668 default:
1669 FIXME("Unhandled info type %d (vt %x)\n", info[i].eInfoType, V_VT(&info[i].vData));
1672 return hr;
1675 static const struct IDCInfoVtbl dcinfo_vtbl =
1677 dcinfo_QueryInterface,
1678 dcinfo_AddRef,
1679 dcinfo_Release,
1680 dcinfo_GetInfo,
1681 dcinfo_SetInfo
1684 HRESULT create_oledb_convert(IUnknown *outer, void **obj)
1686 convert *This;
1688 TRACE("(%p, %p)\n", outer, obj);
1690 *obj = NULL;
1692 if(outer) return CLASS_E_NOAGGREGATION;
1694 This = heap_alloc(sizeof(*This));
1695 if(!This) return E_OUTOFMEMORY;
1697 This->IDataConvert_iface.lpVtbl = &convert_vtbl;
1698 This->IDCInfo_iface.lpVtbl = &dcinfo_vtbl;
1699 This->ref = 1;
1700 This->version = 0x110;
1702 *obj = &This->IDataConvert_iface;
1704 return S_OK;