explorerframe/tests: Use CRT memory allocation functions.
[wine.git] / dlls / oledb32 / datainit.c
blob0438375202b927555709b34d293d25d2b7e75bfc
1 /*
2 * Copyright (C) 2012 Alistair Leslie-Hughes
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 #include <stdarg.h>
20 #define COBJMACROS
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winnls.h"
25 #include "ole2.h"
26 #include "msdasc.h"
27 #include "oledberr.h"
28 #include "initguid.h"
29 #include "oledb_private.h"
31 #include "wine/debug.h"
32 #include "wine/heap.h"
33 #include "wine/list.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
37 DEFINE_GUID(DB_NULLGUID, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
38 DEFINE_GUID(DBGUID_SESSION, 0xc8b522f5, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
39 DEFINE_GUID(DBGUID_ROWSET, 0xc8b522f6, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
40 DEFINE_GUID(DBGUID_ROW, 0xc8b522f7, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
41 DEFINE_GUID(DBGUID_STREAM, 0xc8b522f9, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
43 DEFINE_GUID(DBPROPSET_COLUMN, 0xc8b522b9, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
44 DEFINE_GUID(DBPROPSET_DATASOURCE, 0xc8b522ba, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
45 DEFINE_GUID(DBPROPSET_DATASOURCEINFO, 0xc8b522bb, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
46 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
47 DEFINE_GUID(DBPROPSET_INDEX, 0xc8b522bd, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
48 DEFINE_GUID(DBPROPSET_ROWSET, 0xc8b522be, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
49 DEFINE_GUID(DBPROPSET_TABLE, 0xc8b522bf, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
50 DEFINE_GUID(DBPROPSET_DATASOURCEALL, 0xc8b522c0, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
51 DEFINE_GUID(DBPROPSET_DATASOURCEINFOALL, 0xc8b522c1, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
52 DEFINE_GUID(DBPROPSET_ROWSETALL, 0xc8b522c2, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
53 DEFINE_GUID(DBPROPSET_SESSION, 0xc8b522c6, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
54 DEFINE_GUID(DBPROPSET_SESSIONALL, 0xc8b522c7, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
55 DEFINE_GUID(DBPROPSET_DBINITALL, 0xc8b522ca, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
56 DEFINE_GUID(DBPROPSET_PROPERTIESINERROR, 0xc8b522d4, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
58 typedef struct datainit
60 IDataInitialize IDataInitialize_iface;
62 LONG ref;
63 } datainit;
65 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
67 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
70 typedef struct
72 IDBInitialize IDBInitialize_iface;
73 IDBProperties IDBProperties_iface;
75 LONG ref;
76 } dbinit;
78 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
80 return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
83 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
85 return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
88 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
90 dbinit *This = impl_from_IDBProperties(iface);
92 return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
95 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
97 dbinit *This = impl_from_IDBProperties(iface);
99 return IDBInitialize_AddRef(&This->IDBInitialize_iface);
102 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
104 dbinit *This = impl_from_IDBProperties(iface);
106 return IDBInitialize_Release(&This->IDBInitialize_iface);
109 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
110 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
112 dbinit *This = impl_from_IDBProperties(iface);
114 FIXME("(%p)->(%ld %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
116 return E_NOTIMPL;
119 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
120 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
121 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
123 dbinit *This = impl_from_IDBProperties(iface);
125 FIXME("(%p)->(%ld %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
126 prgPropertyInfoSets, ppDescBuffer);
128 return E_NOTIMPL;
131 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
132 DBPROPSET rgPropertySets[])
134 dbinit *This = impl_from_IDBProperties(iface);
136 FIXME("(%p)->(%ld %p)\n", This, cPropertySets, rgPropertySets);
138 return E_NOTIMPL;
141 static const struct IDBPropertiesVtbl dbprops_vtbl =
143 dbprops_QueryInterface,
144 dbprops_AddRef,
145 dbprops_Release,
146 dbprops_GetProperties,
147 dbprops_GetPropertyInfo,
148 dbprops_SetProperties
151 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
153 dbinit *This = impl_from_IDBInitialize(iface);
154 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
156 *obj = NULL;
158 if(IsEqualIID(riid, &IID_IUnknown) ||
159 IsEqualIID(riid, &IID_IDBInitialize))
161 *obj = iface;
163 else if(IsEqualIID(riid, &IID_IDBProperties))
165 *obj = &This->IDBProperties_iface;
167 else
169 FIXME("interface %s not implemented\n", debugstr_guid(riid));
170 return E_NOINTERFACE;
173 IDBInitialize_AddRef(iface);
174 return S_OK;
177 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
179 dbinit *This = impl_from_IDBInitialize(iface);
180 TRACE("(%p)\n", This);
182 return InterlockedIncrement(&This->ref);
185 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
187 dbinit *This = impl_from_IDBInitialize(iface);
188 LONG ref;
190 TRACE("(%p)\n", This);
192 ref = InterlockedDecrement(&This->ref);
193 if(ref == 0)
194 heap_free(This);
196 return ref;
199 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
201 dbinit *This = impl_from_IDBInitialize(iface);
203 FIXME("(%p) stub\n", This);
205 return S_OK;
208 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
210 dbinit *This = impl_from_IDBInitialize(iface);
212 FIXME("(%p) stub\n", This);
214 return S_OK;
217 static const IDBInitializeVtbl dbinit_vtbl =
219 dbinit_QueryInterface,
220 dbinit_AddRef,
221 dbinit_Release,
222 dbinit_Initialize,
223 dbinit_Uninitialize
226 static HRESULT create_db_init(IUnknown **obj)
228 dbinit *This;
230 TRACE("()\n");
232 *obj = NULL;
234 This = heap_alloc(sizeof(*This));
235 if(!This) return E_OUTOFMEMORY;
237 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
238 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
239 This->ref = 1;
241 *obj = (IUnknown*)&This->IDBInitialize_iface;
243 return S_OK;
246 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
248 datainit *This = impl_from_IDataInitialize(iface);
249 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
251 *obj = NULL;
253 if(IsEqualIID(riid, &IID_IUnknown) ||
254 IsEqualIID(riid, &IID_IDataInitialize))
256 *obj = &This->IDataInitialize_iface;
258 else
260 FIXME("interface %s not implemented\n", debugstr_guid(riid));
261 return E_NOINTERFACE;
264 IDataInitialize_AddRef(iface);
265 return S_OK;
268 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
270 datainit *This = impl_from_IDataInitialize(iface);
271 TRACE("(%p)\n", This);
273 return InterlockedIncrement(&This->ref);
276 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
278 datainit *This = impl_from_IDataInitialize(iface);
279 LONG ref;
281 TRACE("(%p)\n", This);
283 ref = InterlockedDecrement(&This->ref);
284 if(ref == 0)
285 heap_free(This);
287 return ref;
290 static void free_dbpropset(ULONG count, DBPROPSET *propset)
292 ULONG i;
294 for (i = 0; i < count; i++)
296 ULONG p;
298 for (p = 0; p < propset[i].cProperties; p++)
299 VariantClear(&propset[i].rgProperties[p].vValue);
301 CoTaskMemFree(propset[i].rgProperties);
303 CoTaskMemFree(propset);
306 struct dbproperty {
307 const WCHAR *name;
308 DBPROPID id;
309 DBPROPOPTIONS options;
310 VARTYPE type;
311 HRESULT (*convert_dbproperty)(const WCHAR *src, VARIANT *dest);
314 struct mode_propval
316 const WCHAR *name;
317 DWORD value;
320 static int __cdecl dbmodeprop_compare(const void *a, const void *b)
322 const WCHAR *src = a;
323 const struct mode_propval *propval = b;
324 return wcsicmp(src, propval->name);
327 static HRESULT convert_dbproperty_mode(const WCHAR *src, VARIANT *dest)
329 static const struct mode_propval mode_propvals[] =
331 { L"Read", DB_MODE_READ },
332 { L"ReadWrite", DB_MODE_READWRITE },
333 { L"Share Deny None", DB_MODE_SHARE_DENY_NONE },
334 { L"Share Deny Read", DB_MODE_SHARE_DENY_READ },
335 { L"Share Deny Write", DB_MODE_SHARE_DENY_WRITE },
336 { L"Share Exclusive", DB_MODE_SHARE_EXCLUSIVE },
337 { L"Write", DB_MODE_WRITE },
339 struct mode_propval *prop;
341 if ((prop = bsearch(src, mode_propvals, ARRAY_SIZE(mode_propvals),
342 sizeof(struct mode_propval), dbmodeprop_compare)))
344 V_VT(dest) = VT_I4;
345 V_I4(dest) = prop->value;
346 TRACE("%s = %#lx\n", debugstr_w(src), prop->value);
347 return S_OK;
350 return E_FAIL;
353 static const struct dbproperty dbproperties[] =
355 { L"Asynchronous Processing", DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
356 { L"Bind Flags", DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
357 { L"Cache Authentication", DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
358 { L"Connect Timeout", DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
359 { L"Data Source", DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
360 { L"Extended Properties", DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
361 { L"Encrypt Password", DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
362 { L"General Timeout", DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
363 { L"Impersonation Level", DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
364 { L"Initial Catalog", DBPROP_CATALOGLOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
365 { L"Integrated Security", DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
366 { L"Locale Identifier", DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
367 { L"Location", DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
368 { L"Lock Owner", DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
369 { L"Mask Password", DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
370 { L"Mode", DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4, convert_dbproperty_mode },
371 { L"OLE DB Services", DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
372 { L"Password", DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
373 { L"Persist Encrypted", DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
374 { L"Persist Security Info", DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
375 { L"Prompt", DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
376 { L"Protection level", DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
377 { L"User ID", DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
378 #ifndef _WIN64
379 { L"Window Handle", DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
380 #else
381 { L"Window Handle", DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
382 #endif
385 struct dbprop_pair
387 struct list entry;
388 BSTR name;
389 BSTR value;
392 struct dbprops
394 struct list props;
395 unsigned int count;
398 /* name/value strings are reused, so they shouldn't be freed after this call */
399 static HRESULT add_dbprop_to_list(struct dbprops *props, BSTR name, BSTR value)
401 struct dbprop_pair *pair;
403 pair = heap_alloc(sizeof(*pair));
404 if (!pair)
405 return E_OUTOFMEMORY;
407 pair->name = name;
408 pair->value = value;
409 list_add_tail(&props->props, &pair->entry);
410 props->count++;
411 return S_OK;
414 static void free_dbprop_list(struct dbprops *props)
416 struct dbprop_pair *p, *p2;
417 LIST_FOR_EACH_ENTRY_SAFE(p, p2, &props->props, struct dbprop_pair, entry)
419 list_remove(&p->entry);
420 SysFreeString(p->name);
421 SysFreeString(p->value);
422 heap_free(p);
426 static HRESULT parse_init_string(const WCHAR *initstring, struct dbprops *props)
428 const WCHAR *start;
429 WCHAR *eq;
430 HRESULT hr = S_OK;
432 list_init(&props->props);
433 props->count = 0;
435 start = initstring;
436 while (start && (eq = wcschr(start, '=')))
438 WCHAR *delim, quote;
439 BSTR value, name;
441 name = SysAllocStringLen(start, eq - start);
442 /* skip equal sign to get value */
443 eq++;
445 quote = (*eq == '"' || *eq == '\'') ? *eq : 0;
446 if (quote)
448 /* for quoted value string, skip opening mark, look for terminating one */
449 eq++;
450 delim = wcschr(eq, quote);
452 else
453 delim = wcschr(eq, ';');
455 if (delim)
456 value = SysAllocStringLen(eq, delim - eq);
457 else
458 value = SysAllocString(eq);
460 /* skip semicolon if present */
461 if (delim)
463 if (*delim == quote)
464 delim++;
465 if (*delim == ';')
466 delim++;
468 start = delim;
470 if (!wcsicmp(name, L"Provider"))
472 SysFreeString(name);
473 SysFreeString(value);
474 continue;
477 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
479 hr = add_dbprop_to_list(props, name, value);
480 if (FAILED(hr))
482 SysFreeString(name);
483 SysFreeString(value);
484 break;
488 if (FAILED(hr))
489 free_dbprop_list(props);
491 return hr;
494 static const struct dbproperty *get_known_dprop_descr(BSTR name)
496 int min, max, n;
498 min = 0;
499 max = ARRAY_SIZE(dbproperties) - 1;
501 while (min <= max)
503 int r;
505 n = (min+max)/2;
507 r = wcsicmp(dbproperties[n].name, name);
508 if (!r)
509 break;
511 if (r < 0)
512 min = n+1;
513 else
514 max = n-1;
517 return (min <= max) ? &dbproperties[n] : NULL;
520 static HRESULT get_dbpropset_from_proplist(struct dbprops *props, DBPROPSET **propset)
522 struct dbprop_pair *pair;
523 int i = 0;
524 HRESULT hr;
526 *propset = CoTaskMemAlloc(sizeof(DBPROPSET));
527 if (!*propset)
528 return E_OUTOFMEMORY;
530 (*propset)->rgProperties = CoTaskMemAlloc(props->count*sizeof(DBPROP));
531 if (!(*propset)->rgProperties)
533 CoTaskMemFree(*propset);
534 *propset = NULL;
535 return E_OUTOFMEMORY;
538 (*propset)->cProperties = 0;
539 LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
541 const struct dbproperty *descr = get_known_dprop_descr(pair->name);
542 VARIANT dest, src;
544 if (!descr)
546 BSTR str;
547 int len;
549 /* provider specific property is always VT_BSTR */
550 len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1 /* for '=' */;
551 str = SysAllocStringLen(NULL, len);
552 lstrcpyW(str, pair->name);
553 lstrcatW(str, L"=");
554 lstrcatW(str, pair->value);
556 (*propset)->cProperties++;
557 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
558 (*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
559 (*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
560 (*propset)->rgProperties[i].dwStatus = 0;
561 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
562 V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
563 V_BSTR(&(*propset)->rgProperties[i].vValue) = str;
564 i++;
565 continue;
568 V_VT(&src) = VT_BSTR;
569 V_BSTR(&src) = pair->value;
571 VariantInit(&dest);
572 hr = VariantChangeType(&dest, &src, 0, descr->type);
573 if (FAILED(hr) && descr->convert_dbproperty)
574 hr = descr->convert_dbproperty(pair->value, &dest);
576 if (FAILED(hr))
578 ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
579 free_dbpropset(1, *propset);
580 *propset = NULL;
581 return hr;
584 (*propset)->cProperties++;
585 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
586 (*propset)->rgProperties[i].dwPropertyID = descr->id;
587 (*propset)->rgProperties[i].dwOptions = descr->options;
588 (*propset)->rgProperties[i].dwStatus = 0;
589 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
590 (*propset)->rgProperties[i].vValue = dest;
591 i++;
594 return S_OK;
597 /*** IDataInitialize methods ***/
598 static void datasource_release(BOOL created, IUnknown **datasource)
600 if (!created)
601 return;
603 IUnknown_Release(*datasource);
604 *datasource = NULL;
607 static inline WCHAR *strdupW(const WCHAR *src)
609 WCHAR *dest;
610 if (!src) return NULL;
611 dest = heap_alloc((lstrlenW(src)+1)*sizeof(WCHAR));
612 if (dest)
613 lstrcpyW(dest, src);
614 return dest;
617 static WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
619 LPWSTR strlower, sublower, r;
620 strlower = CharLowerW(strdupW(str));
621 sublower = CharLowerW(strdupW(sub));
622 r = wcsstr(strlower, sublower);
623 if (r)
624 r = (LPWSTR)str + (r - strlower);
625 heap_free(strlower);
626 heap_free(sublower);
627 return r;
630 HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
632 static const WCHAR providerW[] = L"Provider=";
633 BOOL datasource_created = FALSE;
634 IDBProperties *dbprops;
635 DBPROPSET *propset;
636 WCHAR *prov = NULL;
637 CLSID provclsid;
638 HRESULT hr;
641 /* first get provider name */
642 provclsid = IID_NULL;
643 if (initstring && (prov = strstriW(initstring, providerW)))
645 WCHAR *start, *progid;
646 int len;
648 prov += ARRAY_SIZE(providerW)-1;
649 start = prov;
650 while (*prov && *prov != ';')
651 ++prov;
652 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
654 len = prov - start;
655 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
656 if (!progid) return E_OUTOFMEMORY;
658 memcpy(progid, start, len*sizeof(WCHAR));
659 progid[len] = 0;
661 hr = CLSIDFromProgID(progid, &provclsid);
662 CoTaskMemFree(progid);
663 if (FAILED(hr))
665 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
666 return hr;
669 else
671 hr = CLSIDFromProgID(L"MSDASQL", &provclsid);
672 if (FAILED(hr))
673 ERR("ODBC provider for OLE DB not registered\n");
676 /* check for provider mismatch if it was specified in init string */
677 if (*datasource && prov)
679 DBPROPIDSET propidset;
680 DWORD prop;
681 CLSID initprov;
682 ULONG count;
684 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
685 if (FAILED(hr))
687 WARN("provider doesn't support IDBProperties\n");
688 return hr;
691 prop = DBPROP_INIT_DATASOURCE;
692 propidset.rgPropertyIDs = &prop;
693 propidset.cPropertyIDs = 1;
694 propidset.guidPropertySet = DBPROPSET_DBINIT;
695 count = 0;
696 propset = NULL;
697 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
698 IDBProperties_Release(dbprops);
699 if (FAILED(hr))
701 WARN("GetProperties failed for datasource, 0x%08lx\n", hr);
702 return hr;
705 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
706 initprov = IID_NULL;
707 hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
708 free_dbpropset(count, propset);
710 if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
713 if (!*datasource)
715 if (!IsEqualIID(&provclsid, &IID_NULL))
716 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
718 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
719 hr = create_db_init(datasource);
721 datasource_created = *datasource != NULL;
724 /* now set properties */
725 if (initstring)
727 struct dbprops props;
729 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
730 if (FAILED(hr))
732 ERR("provider doesn't support IDBProperties\n");
733 datasource_release(datasource_created, datasource);
734 return hr;
737 hr = parse_init_string(initstring, &props);
738 if (FAILED(hr))
740 datasource_release(datasource_created, datasource);
741 return hr;
744 hr = get_dbpropset_from_proplist(&props, &propset);
745 free_dbprop_list(&props);
746 if (FAILED(hr))
748 datasource_release(datasource_created, datasource);
749 return hr;
752 hr = IDBProperties_SetProperties(dbprops, 1, propset);
753 IDBProperties_Release(dbprops);
754 free_dbpropset(1, propset);
755 if (FAILED(hr))
757 ERR("SetProperties failed, 0x%08lx\n", hr);
758 datasource_release(datasource_created, datasource);
759 return hr;
763 return hr;
766 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
767 LPWSTR initstring, REFIID riid, IUnknown **datasource)
769 datainit *This = impl_from_IDataInitialize(iface);
771 TRACE("(%p)->(%p 0x%lx %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
773 return get_data_source(outer, clsctx, initstring, riid, datasource);
776 /* returns character length of string representation */
777 static int get_propvalue_length(DBPROP *prop)
779 VARIANT str;
780 HRESULT hr;
782 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
784 VariantInit(&str);
785 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
786 if (hr == S_OK)
788 int len = SysStringLen(V_BSTR(&str));
789 VariantClear(&str);
790 return len;
793 return 0;
796 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
798 VARIANT *v = &prop->vValue;
799 VARIANT vstr;
800 HRESULT hr;
802 if (V_VT(v) == VT_BSTR)
804 lstrcatW(str, V_BSTR(v));
805 return;
808 VariantInit(&vstr);
809 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
810 if (hr == S_OK)
812 lstrcatW(str, V_BSTR(&vstr));
813 VariantClear(&vstr);
817 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
819 ULONG i;
821 for (i = 0; i < propinfoset->cPropertyInfos; i++)
822 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
823 return propinfoset->rgPropertyInfos[i].pwszDescription;
825 return NULL;
828 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
830 ULONG i;
832 for (i = 0; i < count; i++)
834 ULONG p;
836 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
837 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
839 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
841 CoTaskMemFree(propinfoset);
844 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
845 boolean include_pass, LPWSTR *init_string)
847 static const WCHAR providerW[] = L"Provider=";
848 datainit *This = impl_from_IDataInitialize(iface);
849 DBPROPINFOSET *propinfoset;
850 IDBProperties *props;
851 DBPROPIDSET propidset;
852 ULONG count, infocount;
853 WCHAR *progid, *desc;
854 DBPROPSET *propset;
855 IPersist *persist;
856 HRESULT hr;
857 CLSID clsid;
858 ULONG i, len;
860 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
862 /* IPersist support is mandatory for data sources */
863 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
864 if (FAILED(hr)) return hr;
866 memset(&clsid, 0, sizeof(clsid));
867 hr = IPersist_GetClassID(persist, &clsid);
868 IPersist_Release(persist);
869 if (FAILED(hr)) return hr;
871 progid = NULL;
872 ProgIDFromCLSID(&clsid, &progid);
873 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
875 /* now get initialization properties */
876 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
877 if (FAILED(hr))
879 WARN("IDBProperties not supported\n");
880 CoTaskMemFree(progid);
881 return hr;
884 propidset.rgPropertyIDs = NULL;
885 propidset.cPropertyIDs = 0;
886 propidset.guidPropertySet = DBPROPSET_DBINIT;
887 propset = NULL;
888 count = 0;
889 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
890 if (FAILED(hr))
892 WARN("failed to get data source properties, 0x%08lx\n", hr);
893 CoTaskMemFree(progid);
894 return hr;
897 infocount = 0;
898 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
899 IDBProperties_Release(props);
901 /* check if we need to skip password */
902 len = lstrlenW(progid) + lstrlenW(providerW) + 1; /* including ';' */
903 for (i = 0; i < count; i++)
905 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
906 if (descr)
908 /* include '=' and ';' */
909 len += lstrlenW(descr) + 2;
910 len += get_propvalue_length(&propset->rgProperties[i]);
913 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
914 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
915 include_pass = FALSE;
918 len *= sizeof(WCHAR);
919 *init_string = CoTaskMemAlloc(len);
920 *init_string[0] = 0;
922 /* provider name */
923 lstrcatW(*init_string, providerW);
924 lstrcatW(*init_string, progid);
925 lstrcatW(*init_string, L";");
926 CoTaskMemFree(progid);
928 for (i = 0; i < count; i++)
930 WCHAR *descr;
932 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
934 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
935 if (descr)
937 lstrcatW(*init_string, descr);
938 lstrcatW(*init_string, L"=");
939 write_propvalue_str(*init_string, &propset->rgProperties[i]);
940 lstrcatW(*init_string, L";");
944 free_dbpropset(count, propset);
945 free_dbpropinfoset(infocount, propinfoset);
946 CoTaskMemFree(desc);
948 if (!include_pass)
949 TRACE("%s\n", debugstr_w(*init_string));
950 return S_OK;
953 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
954 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
955 IUnknown **datasource)
957 datainit *This = impl_from_IDataInitialize(iface);
959 TRACE("(%p)->(%s %p 0x%08lx %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
960 debugstr_guid(riid), datasource);
962 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
965 static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
966 DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
968 datainit *This = impl_from_IDataInitialize(iface);
970 FIXME("(%p)->(%s %p %#lx %s %p %lu %p)\n", This, debugstr_guid(provider), outer, clsctx,
971 debugstr_w(reserved), server_info, cmq, results);
973 return E_NOTIMPL;
976 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
977 LPWSTR *ppwszInitializationString)
979 datainit *This = impl_from_IDataInitialize(iface);
981 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
983 return E_NOTIMPL;
986 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
987 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
989 datainit *This = impl_from_IDataInitialize(iface);
991 FIXME("(%p)->(%s %s %ld)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
993 return E_NOTIMPL;
997 static const struct IDataInitializeVtbl datainit_vtbl =
999 datainit_QueryInterface,
1000 datainit_AddRef,
1001 datainit_Release,
1002 datainit_GetDataSource,
1003 datainit_GetInitializationString,
1004 datainit_CreateDBInstance,
1005 datainit_CreateDBInstanceEx,
1006 datainit_LoadStringFromStorage,
1007 datainit_WriteStringToStorage
1010 HRESULT create_data_init(IUnknown *outer, void **obj)
1012 datainit *This;
1014 TRACE("(%p)\n", obj);
1016 if(outer) return CLASS_E_NOAGGREGATION;
1018 *obj = NULL;
1020 This = heap_alloc(sizeof(*This));
1021 if(!This) return E_OUTOFMEMORY;
1023 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
1024 This->ref = 1;
1026 *obj = &This->IDataInitialize_iface;
1028 return S_OK;