winex11: Implement vkEnumerateInstanceExtensionProperties.
[wine.git] / dlls / oledb32 / datainit.c
blob7d78e9fca2bffa6d79fdf94dc36c1ed12e525186
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"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
38 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
40 typedef struct datainit
42 IDataInitialize IDataInitialize_iface;
44 LONG ref;
45 } datainit;
47 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
49 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
52 typedef struct
54 IDBInitialize IDBInitialize_iface;
55 IDBProperties IDBProperties_iface;
57 LONG ref;
58 } dbinit;
60 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
62 return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
65 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
67 return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
70 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
72 dbinit *This = impl_from_IDBProperties(iface);
74 return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
77 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
79 dbinit *This = impl_from_IDBProperties(iface);
81 return IDBInitialize_AddRef(&This->IDBInitialize_iface);
84 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
86 dbinit *This = impl_from_IDBProperties(iface);
88 return IDBInitialize_Release(&This->IDBInitialize_iface);
91 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
92 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
94 dbinit *This = impl_from_IDBProperties(iface);
96 FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
98 return E_NOTIMPL;
101 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
102 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
103 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
105 dbinit *This = impl_from_IDBProperties(iface);
107 FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
108 prgPropertyInfoSets, ppDescBuffer);
110 return E_NOTIMPL;
113 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
114 DBPROPSET rgPropertySets[])
116 dbinit *This = impl_from_IDBProperties(iface);
118 FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
120 return E_NOTIMPL;
123 static const struct IDBPropertiesVtbl dbprops_vtbl =
125 dbprops_QueryInterface,
126 dbprops_AddRef,
127 dbprops_Release,
128 dbprops_GetProperties,
129 dbprops_GetPropertyInfo,
130 dbprops_SetProperties
133 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
135 dbinit *This = impl_from_IDBInitialize(iface);
136 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
138 *obj = NULL;
140 if(IsEqualIID(riid, &IID_IUnknown) ||
141 IsEqualIID(riid, &IID_IDBInitialize))
143 *obj = iface;
145 else if(IsEqualIID(riid, &IID_IDBProperties))
147 *obj = &This->IDBProperties_iface;
149 else
151 FIXME("interface %s not implemented\n", debugstr_guid(riid));
152 return E_NOINTERFACE;
155 IDBInitialize_AddRef(iface);
156 return S_OK;
159 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
161 dbinit *This = impl_from_IDBInitialize(iface);
162 TRACE("(%p)\n", This);
164 return InterlockedIncrement(&This->ref);
167 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
169 dbinit *This = impl_from_IDBInitialize(iface);
170 LONG ref;
172 TRACE("(%p)\n", This);
174 ref = InterlockedDecrement(&This->ref);
175 if(ref == 0)
176 heap_free(This);
178 return ref;
181 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
183 dbinit *This = impl_from_IDBInitialize(iface);
185 FIXME("(%p) stub\n", This);
187 return S_OK;
190 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
192 dbinit *This = impl_from_IDBInitialize(iface);
194 FIXME("(%p) stub\n", This);
196 return S_OK;
199 static const IDBInitializeVtbl dbinit_vtbl =
201 dbinit_QueryInterface,
202 dbinit_AddRef,
203 dbinit_Release,
204 dbinit_Initialize,
205 dbinit_Uninitialize
208 static HRESULT create_db_init(IUnknown **obj)
210 dbinit *This;
212 TRACE("()\n");
214 *obj = NULL;
216 This = heap_alloc(sizeof(*This));
217 if(!This) return E_OUTOFMEMORY;
219 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
220 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
221 This->ref = 1;
223 *obj = (IUnknown*)&This->IDBInitialize_iface;
225 return S_OK;
228 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
230 datainit *This = impl_from_IDataInitialize(iface);
231 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
233 *obj = NULL;
235 if(IsEqualIID(riid, &IID_IUnknown) ||
236 IsEqualIID(riid, &IID_IDataInitialize))
238 *obj = &This->IDataInitialize_iface;
240 else
242 FIXME("interface %s not implemented\n", debugstr_guid(riid));
243 return E_NOINTERFACE;
246 IDataInitialize_AddRef(iface);
247 return S_OK;
250 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
252 datainit *This = impl_from_IDataInitialize(iface);
253 TRACE("(%p)\n", This);
255 return InterlockedIncrement(&This->ref);
258 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
260 datainit *This = impl_from_IDataInitialize(iface);
261 LONG ref;
263 TRACE("(%p)\n", This);
265 ref = InterlockedDecrement(&This->ref);
266 if(ref == 0)
267 heap_free(This);
269 return ref;
272 static void free_dbpropset(ULONG count, DBPROPSET *propset)
274 ULONG i;
276 for (i = 0; i < count; i++)
278 ULONG p;
280 for (p = 0; p < propset[i].cProperties; p++)
281 VariantClear(&propset[i].rgProperties[p].vValue);
283 CoTaskMemFree(propset[i].rgProperties);
285 CoTaskMemFree(propset);
288 struct dbproperty {
289 const WCHAR *name;
290 DBPROPID id;
291 DBPROPOPTIONS options;
292 VARTYPE type;
293 HRESULT (*convert_dbproperty)(const WCHAR *src, VARIANT *dest);
296 struct mode_propval
298 const WCHAR *name;
299 DWORD value;
302 static int dbmodeprop_compare(const void *a, const void *b)
304 const WCHAR *src = a;
305 const struct mode_propval *propval = b;
306 return strcmpiW(src, propval->name);
309 static HRESULT convert_dbproperty_mode(const WCHAR *src, VARIANT *dest)
311 static const WCHAR readW[] = {'R','e','a','d',0};
312 static const WCHAR readwriteW[] = {'R','e','a','d','W','r','i','t','e',0};
313 static const WCHAR sharedenynoneW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','N','o','n','e',0};
314 static const WCHAR sharedenyreadW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','R','e','a','d',0};
315 static const WCHAR sharedenywriteW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','W','r','i','t','e',0};
316 static const WCHAR shareexclusiveW[] = {'S','h','a','r','e',' ','E','x','c','l','u','s','i','v','e',0};
317 static const WCHAR writeW[] = {'W','r','i','t','e',0};
319 struct mode_propval mode_propvals[] =
321 { readW, DB_MODE_READ },
322 { readwriteW, DB_MODE_READWRITE },
323 { sharedenynoneW, DB_MODE_SHARE_DENY_NONE },
324 { sharedenyreadW, DB_MODE_SHARE_DENY_READ },
325 { sharedenywriteW, DB_MODE_SHARE_DENY_WRITE },
326 { shareexclusiveW, DB_MODE_SHARE_EXCLUSIVE },
327 { writeW, DB_MODE_WRITE },
329 struct mode_propval *prop;
331 if ((prop = bsearch(src, mode_propvals, sizeof(mode_propvals) / sizeof(*mode_propvals),
332 sizeof(struct mode_propval), dbmodeprop_compare)))
334 V_VT(dest) = VT_I4;
335 V_I4(dest) = prop->value;
336 TRACE("%s = %#x\n", debugstr_w(src), prop->value);
337 return S_OK;
340 return E_FAIL;
343 static const WCHAR asyncW[] = {'A','s','y','n','c','h','r','o','n','o','u','s',' ','P','r','o','c','e','s','s','i','n','g',0};
344 static const WCHAR bindW[] = {'B','i','n','d',' ','F','l','a','g','s',0};
345 static const WCHAR cacheW[] = {'C','a','c','h','e',' ','A','u','t','h','e','n','i','c','a','t','i','o','n',0};
346 static const WCHAR conn_timeout[] = {'C','o','n','n','e','c','t',' ','T','i','m','e','o','u','t',0};
347 static const WCHAR datasourceW[] = {'D','a','t','a',' ','S','o','u','r','c','e',0};
348 static const WCHAR encryptW[] = {'E','n','c','r','y','p','t',' ','P','a','s','s','w','o','r','d',0};
349 static const WCHAR extendedW[] = {'E','x','t','e','n','d','e','d',' ','P','r','o','p','e','r','t','i','e','s',0};
350 static const WCHAR gen_timeout[] = {'G','e','n','e','r','a','l',' ','T','i','m','e','o','u','t',0};
351 static const WCHAR impersonW[] = {'I','m','p','e','r','s','o','n','a','t','i','o','n',' ','L','e','v','e','l',0};
352 static const WCHAR initcatW[] = {'I','n','i','t','i','a','l',' ','C','a','t','a','l','o','g',0};
353 static const WCHAR integratedW[] = {'I','n','t','e','g','r','a','t','e','d',' ','S','e','c','u','r','i','t','y',0};
354 static const WCHAR localeIDW[] = {'L','o','c','a','l','e',' ','I','d','e','n','t','i','f','i','e','r',0};
355 static const WCHAR locationW[] = {'L','o','c','a','t','i','o','n',0};
356 static const WCHAR lockownerW[] = {'L','o','c','k',' ','O','w','n','e','r',0};
357 static const WCHAR maskpassW[] = {'M','a','s','k',' ','P','a','s','s','w','o','r','d',0};
358 static const WCHAR modeW[] = {'M','o','d','e',0};
359 static const WCHAR oledbservW[] = {'O','L','E',' ','D','B',' ','S','e','r','v','i','c','i','e','s',0};
360 static const WCHAR passwordW[] = {'P','a','s','s','w','o','r','d',0};
361 static const WCHAR persistW[] = {'P','e','r','s','i','s','t',' ','S','e','c','u','r','i','t','y',' ','I','n','f','o',0};
362 static const WCHAR persistEncW[] = {'P','e','r','s','i','s','t',' ','E','n','c','r','y','p','t','e','d',0};
363 static const WCHAR promptW[] = {'P','r','o','m','p','t',0};
364 static const WCHAR protectW[] = {'P','r','o','t','e','c','t','i','o','n',' ','l','e','v','e','l',0};
365 static const WCHAR useridW[] = {'U','s','e','r',' ','I','D',0};
366 static const WCHAR winhandleW[] = {'W','i','n','d','o','w',' ','H','a','n','d','l','e',0};
368 static const struct dbproperty dbproperties[] = {
369 { asyncW, DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
370 { bindW, DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
371 { cacheW, DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
372 { conn_timeout,DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
373 { datasourceW, DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
374 { extendedW, DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
375 { encryptW, DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
376 { gen_timeout, DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
377 { impersonW, DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
378 { initcatW, DBPROP_CATALOGLOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
379 { integratedW, DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
380 { localeIDW, DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
381 { locationW, DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
382 { lockownerW, DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
383 { maskpassW, DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
384 { modeW, DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4, convert_dbproperty_mode },
385 { oledbservW, DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
386 { passwordW, DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
387 { persistEncW, DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
388 { persistW, DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
389 { promptW, DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
390 { protectW, DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
391 { useridW, DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
392 #ifndef _WIN64
393 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
394 #else
395 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
396 #endif
399 struct dbprop_pair
401 struct list entry;
402 BSTR name;
403 BSTR value;
406 struct dbprops
408 struct list props;
409 unsigned int count;
412 /* name/value strings are reused, so they shouldn't be freed after this call */
413 static HRESULT add_dbprop_to_list(struct dbprops *props, BSTR name, BSTR value)
415 struct dbprop_pair *pair;
417 pair = heap_alloc(sizeof(*pair));
418 if (!pair)
419 return E_OUTOFMEMORY;
421 pair->name = name;
422 pair->value = value;
423 list_add_tail(&props->props, &pair->entry);
424 props->count++;
425 return S_OK;
428 static void free_dbprop_list(struct dbprops *props)
430 struct dbprop_pair *p, *p2;
431 LIST_FOR_EACH_ENTRY_SAFE(p, p2, &props->props, struct dbprop_pair, entry)
433 list_remove(&p->entry);
434 SysFreeString(p->name);
435 SysFreeString(p->value);
436 heap_free(p);
440 static HRESULT parse_init_string(const WCHAR *initstring, struct dbprops *props)
442 const WCHAR *start;
443 WCHAR *eq;
444 HRESULT hr = S_OK;
446 list_init(&props->props);
447 props->count = 0;
449 start = initstring;
450 while (start && (eq = strchrW(start, '=')))
452 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r',0};
453 WCHAR *delim, quote;
454 BSTR value, name;
456 name = SysAllocStringLen(start, eq - start);
457 /* skip equal sign to get value */
458 eq++;
460 quote = (*eq == '"' || *eq == '\'') ? *eq : 0;
461 if (quote)
463 /* for quoted value string, skip opening mark, look for terminating one */
464 eq++;
465 delim = strchrW(eq, quote);
467 else
468 delim = strchrW(eq, ';');
470 if (delim)
471 value = SysAllocStringLen(eq, delim - eq);
472 else
473 value = SysAllocString(eq);
475 /* skip semicolon if present */
476 if (delim)
478 if (*delim == quote)
479 delim++;
480 if (*delim == ';')
481 delim++;
483 start = delim;
485 if (!strcmpiW(name, providerW))
487 SysFreeString(name);
488 SysFreeString(value);
489 continue;
492 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
494 hr = add_dbprop_to_list(props, name, value);
495 if (FAILED(hr))
497 SysFreeString(name);
498 SysFreeString(value);
499 break;
503 if (FAILED(hr))
504 free_dbprop_list(props);
506 return hr;
509 static const struct dbproperty *get_known_dprop_descr(BSTR name)
511 int min, max, n;
513 min = 0;
514 max = sizeof(dbproperties)/sizeof(struct dbproperty) - 1;
516 while (min <= max)
518 int r;
520 n = (min+max)/2;
522 r = strcmpiW(dbproperties[n].name, name);
523 if (!r)
524 break;
526 if (r < 0)
527 min = n+1;
528 else
529 max = n-1;
532 return (min <= max) ? &dbproperties[n] : NULL;
535 static HRESULT get_dbpropset_from_proplist(struct dbprops *props, DBPROPSET **propset)
537 struct dbprop_pair *pair;
538 int i = 0;
539 HRESULT hr;
541 *propset = CoTaskMemAlloc(sizeof(DBPROPSET));
542 if (!*propset)
543 return E_OUTOFMEMORY;
545 (*propset)->rgProperties = CoTaskMemAlloc(props->count*sizeof(DBPROP));
546 if (!(*propset)->rgProperties)
548 CoTaskMemFree(*propset);
549 *propset = NULL;
550 return E_OUTOFMEMORY;
553 (*propset)->cProperties = 0;
554 LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
556 const struct dbproperty *descr = get_known_dprop_descr(pair->name);
557 VARIANT dest, src;
559 if (!descr)
561 static const WCHAR eqW[] = {'=',0};
562 BSTR str;
563 int len;
565 /* provider specific property is always VT_BSTR */
566 len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1 /* for '=' */;
567 str = SysAllocStringLen(NULL, len);
568 strcpyW(str, pair->name);
569 strcatW(str, eqW);
570 strcatW(str, pair->value);
572 (*propset)->cProperties++;
573 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
574 (*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
575 (*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
576 (*propset)->rgProperties[i].dwStatus = 0;
577 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
578 V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
579 V_BSTR(&(*propset)->rgProperties[i].vValue) = str;
580 i++;
581 continue;
584 V_VT(&src) = VT_BSTR;
585 V_BSTR(&src) = pair->value;
587 VariantInit(&dest);
588 hr = VariantChangeType(&dest, &src, 0, descr->type);
589 if (FAILED(hr) && descr->convert_dbproperty)
590 hr = descr->convert_dbproperty(pair->value, &dest);
592 if (FAILED(hr))
594 ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
595 free_dbpropset(1, *propset);
596 *propset = NULL;
597 return hr;
600 (*propset)->cProperties++;
601 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
602 (*propset)->rgProperties[i].dwPropertyID = descr->id;
603 (*propset)->rgProperties[i].dwOptions = descr->options;
604 (*propset)->rgProperties[i].dwStatus = 0;
605 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
606 (*propset)->rgProperties[i].vValue = dest;
607 i++;
610 return S_OK;
613 /*** IDataInitialize methods ***/
614 static void datasource_release(BOOL created, IUnknown **datasource)
616 if (!created)
617 return;
619 IUnknown_Release(*datasource);
620 *datasource = NULL;
623 static inline WCHAR *strdupW(const WCHAR *src)
625 WCHAR *dest;
626 if (!src) return NULL;
627 dest = heap_alloc((strlenW(src)+1)*sizeof(WCHAR));
628 if (dest)
629 strcpyW(dest, src);
630 return dest;
633 static WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
635 LPWSTR strlower, sublower, r;
636 strlower = CharLowerW(strdupW(str));
637 sublower = CharLowerW(strdupW(sub));
638 r = strstrW(strlower, sublower);
639 if (r)
640 r = (LPWSTR)str + (r - strlower);
641 heap_free(strlower);
642 heap_free(sublower);
643 return r;
646 HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
648 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r','=',0};
649 static const WCHAR msdasqlW[] = {'M','S','D','A','S','Q','L',0};
650 BOOL datasource_created = FALSE;
651 IDBProperties *dbprops;
652 DBPROPSET *propset;
653 WCHAR *prov = NULL;
654 CLSID provclsid;
655 HRESULT hr;
658 /* first get provider name */
659 provclsid = IID_NULL;
660 if (initstring && (prov = strstriW(initstring, providerW)))
662 WCHAR *start, *progid;
663 int len;
665 prov += sizeof(providerW)/sizeof(WCHAR)-1;
666 start = prov;
667 while (*prov && *prov != ';')
668 ++prov;
669 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
671 len = prov - start;
672 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
673 if (!progid) return E_OUTOFMEMORY;
675 memcpy(progid, start, len*sizeof(WCHAR));
676 progid[len] = 0;
678 hr = CLSIDFromProgID(progid, &provclsid);
679 CoTaskMemFree(progid);
680 if (FAILED(hr))
682 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
683 return hr;
686 else
688 hr = CLSIDFromProgID(msdasqlW, &provclsid);
689 if (FAILED(hr))
690 ERR("ODBC provider for OLE DB not registered\n");
693 /* check for provider mismatch if it was specified in init string */
694 if (*datasource && prov)
696 DBPROPIDSET propidset;
697 enum DBPROPENUM prop;
698 CLSID initprov;
699 ULONG count;
701 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
702 if (FAILED(hr))
704 WARN("provider doesn't support IDBProperties\n");
705 return hr;
708 prop = DBPROP_INIT_DATASOURCE;
709 propidset.rgPropertyIDs = &prop;
710 propidset.cPropertyIDs = 1;
711 propidset.guidPropertySet = DBPROPSET_DBINIT;
712 count = 0;
713 propset = NULL;
714 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
715 IDBProperties_Release(dbprops);
716 if (FAILED(hr))
718 WARN("GetProperties failed for datasource, 0x%08x\n", hr);
719 return hr;
722 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
723 initprov = IID_NULL;
724 hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
725 free_dbpropset(count, propset);
727 if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
730 if (!*datasource)
732 if (!IsEqualIID(&provclsid, &IID_NULL))
733 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
735 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
736 hr = create_db_init(datasource);
738 datasource_created = *datasource != NULL;
741 /* now set properties */
742 if (initstring)
744 struct dbprops props;
746 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
747 if (FAILED(hr))
749 ERR("provider doesn't support IDBProperties\n");
750 datasource_release(datasource_created, datasource);
751 return hr;
754 hr = parse_init_string(initstring, &props);
755 if (FAILED(hr))
757 datasource_release(datasource_created, datasource);
758 return hr;
761 hr = get_dbpropset_from_proplist(&props, &propset);
762 free_dbprop_list(&props);
763 if (FAILED(hr))
765 datasource_release(datasource_created, datasource);
766 return hr;
769 hr = IDBProperties_SetProperties(dbprops, 1, propset);
770 IDBProperties_Release(dbprops);
771 free_dbpropset(1, propset);
772 if (FAILED(hr))
774 ERR("SetProperties failed, 0x%08x\n", hr);
775 datasource_release(datasource_created, datasource);
776 return hr;
780 return hr;
783 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
784 LPWSTR initstring, REFIID riid, IUnknown **datasource)
786 datainit *This = impl_from_IDataInitialize(iface);
788 TRACE("(%p)->(%p 0x%x %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
790 return get_data_source(outer, clsctx, initstring, riid, datasource);
793 /* returns character length of string representation */
794 static int get_propvalue_length(DBPROP *prop)
796 VARIANT str;
797 HRESULT hr;
799 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
801 VariantInit(&str);
802 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
803 if (hr == S_OK)
805 int len = SysStringLen(V_BSTR(&str));
806 VariantClear(&str);
807 return len;
810 return 0;
813 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
815 VARIANT *v = &prop->vValue;
816 VARIANT vstr;
817 HRESULT hr;
819 if (V_VT(v) == VT_BSTR)
821 strcatW(str, V_BSTR(v));
822 return;
825 VariantInit(&vstr);
826 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
827 if (hr == S_OK)
829 strcatW(str, V_BSTR(&vstr));
830 VariantClear(&vstr);
834 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
836 ULONG i;
838 for (i = 0; i < propinfoset->cPropertyInfos; i++)
839 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
840 return propinfoset->rgPropertyInfos[i].pwszDescription;
842 return NULL;
845 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
847 ULONG i;
849 for (i = 0; i < count; i++)
851 ULONG p;
853 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
854 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
856 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
858 CoTaskMemFree(propinfoset);
861 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
862 boolean include_pass, LPWSTR *init_string)
864 static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
865 static const WCHAR colW[] = {';',0};
866 datainit *This = impl_from_IDataInitialize(iface);
867 DBPROPINFOSET *propinfoset;
868 IDBProperties *props;
869 DBPROPIDSET propidset;
870 ULONG count, infocount;
871 WCHAR *progid, *desc;
872 DBPROPSET *propset;
873 IPersist *persist;
874 HRESULT hr;
875 CLSID clsid;
876 ULONG i, len;
878 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
880 /* IPersist support is mandatory for data sources */
881 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
882 if (FAILED(hr)) return hr;
884 memset(&clsid, 0, sizeof(clsid));
885 hr = IPersist_GetClassID(persist, &clsid);
886 IPersist_Release(persist);
887 if (FAILED(hr)) return hr;
889 progid = NULL;
890 ProgIDFromCLSID(&clsid, &progid);
891 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
893 /* now get initialization properties */
894 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
895 if (FAILED(hr))
897 WARN("IDBProperties not supported\n");
898 CoTaskMemFree(progid);
899 return hr;
902 propidset.rgPropertyIDs = NULL;
903 propidset.cPropertyIDs = 0;
904 propidset.guidPropertySet = DBPROPSET_DBINIT;
905 propset = NULL;
906 count = 0;
907 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
908 if (FAILED(hr))
910 WARN("failed to get data source properties, 0x%08x\n", hr);
911 CoTaskMemFree(progid);
912 return hr;
915 infocount = 0;
916 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
917 IDBProperties_Release(props);
919 /* check if we need to skip password */
920 len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
921 for (i = 0; i < count; i++)
923 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
924 if (descr)
926 /* include '=' and ';' */
927 len += strlenW(descr) + 2;
928 len += get_propvalue_length(&propset->rgProperties[i]);
931 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
932 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
933 include_pass = FALSE;
936 len *= sizeof(WCHAR);
937 *init_string = CoTaskMemAlloc(len);
938 *init_string[0] = 0;
940 /* provider name */
941 strcatW(*init_string, provW);
942 strcatW(*init_string, progid);
943 strcatW(*init_string, colW);
944 CoTaskMemFree(progid);
946 for (i = 0; i < count; i++)
948 WCHAR *descr;
950 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
952 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
953 if (descr)
955 static const WCHAR eqW[] = {'=',0};
956 strcatW(*init_string, descr);
957 strcatW(*init_string, eqW);
958 write_propvalue_str(*init_string, &propset->rgProperties[i]);
959 strcatW(*init_string, colW);
963 free_dbpropset(count, propset);
964 free_dbpropinfoset(infocount, propinfoset);
965 CoTaskMemFree(desc);
967 if (!include_pass)
968 TRACE("%s\n", debugstr_w(*init_string));
969 return S_OK;
972 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
973 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
974 IUnknown **datasource)
976 datainit *This = impl_from_IDataInitialize(iface);
978 TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
979 debugstr_guid(riid), datasource);
981 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
984 static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
985 DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
987 datainit *This = impl_from_IDataInitialize(iface);
989 FIXME("(%p)->(%s %p %#x %s %p %u %p)\n", This, debugstr_guid(provider), outer, clsctx,
990 debugstr_w(reserved), server_info, cmq, results);
992 return E_NOTIMPL;
995 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
996 LPWSTR *ppwszInitializationString)
998 datainit *This = impl_from_IDataInitialize(iface);
1000 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
1002 return E_NOTIMPL;
1005 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
1006 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
1008 datainit *This = impl_from_IDataInitialize(iface);
1010 FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
1012 return E_NOTIMPL;
1016 static const struct IDataInitializeVtbl datainit_vtbl =
1018 datainit_QueryInterface,
1019 datainit_AddRef,
1020 datainit_Release,
1021 datainit_GetDataSource,
1022 datainit_GetInitializationString,
1023 datainit_CreateDBInstance,
1024 datainit_CreateDBInstanceEx,
1025 datainit_LoadStringFromStorage,
1026 datainit_WriteStringToStorage
1029 HRESULT create_data_init(IUnknown *outer, void **obj)
1031 datainit *This;
1033 TRACE("(%p)\n", obj);
1035 if(outer) return CLASS_E_NOAGGREGATION;
1037 *obj = NULL;
1039 This = heap_alloc(sizeof(*This));
1040 if(!This) return E_OUTOFMEMORY;
1042 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
1043 This->ref = 1;
1045 *obj = &This->IDataInitialize_iface;
1047 return S_OK;