server: Add a platform-specific entry point to initialize registers of a new thread.
[wine.git] / dlls / oledb32 / datainit.c
blob650c2207415a3aef03124c5ccab5661b50a0c69f
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/list.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
37 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
39 typedef struct datainit
41 IDataInitialize IDataInitialize_iface;
43 LONG ref;
44 } datainit;
46 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
48 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
51 typedef struct
53 IDBInitialize IDBInitialize_iface;
54 IDBProperties IDBProperties_iface;
56 LONG ref;
57 } dbinit;
59 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
61 return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
64 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
66 return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
69 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
71 dbinit *This = impl_from_IDBProperties(iface);
73 return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
76 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
78 dbinit *This = impl_from_IDBProperties(iface);
80 return IDBInitialize_AddRef(&This->IDBInitialize_iface);
83 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
85 dbinit *This = impl_from_IDBProperties(iface);
87 return IDBInitialize_Release(&This->IDBInitialize_iface);
90 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
91 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
93 dbinit *This = impl_from_IDBProperties(iface);
95 FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
97 return E_NOTIMPL;
100 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
101 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
102 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
104 dbinit *This = impl_from_IDBProperties(iface);
106 FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
107 prgPropertyInfoSets, ppDescBuffer);
109 return E_NOTIMPL;
112 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
113 DBPROPSET rgPropertySets[])
115 dbinit *This = impl_from_IDBProperties(iface);
117 FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
119 return E_NOTIMPL;
122 static const struct IDBPropertiesVtbl dbprops_vtbl =
124 dbprops_QueryInterface,
125 dbprops_AddRef,
126 dbprops_Release,
127 dbprops_GetProperties,
128 dbprops_GetPropertyInfo,
129 dbprops_SetProperties
132 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
134 dbinit *This = impl_from_IDBInitialize(iface);
135 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
137 *obj = NULL;
139 if(IsEqualIID(riid, &IID_IUnknown) ||
140 IsEqualIID(riid, &IID_IDBInitialize))
142 *obj = iface;
144 else if(IsEqualIID(riid, &IID_IDBProperties))
146 *obj = &This->IDBProperties_iface;
148 else
150 FIXME("interface %s not implemented\n", debugstr_guid(riid));
151 return E_NOINTERFACE;
154 IDBInitialize_AddRef(iface);
155 return S_OK;
158 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
160 dbinit *This = impl_from_IDBInitialize(iface);
161 TRACE("(%p)\n", This);
163 return InterlockedIncrement(&This->ref);
166 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
168 dbinit *This = impl_from_IDBInitialize(iface);
169 LONG ref;
171 TRACE("(%p)\n", This);
173 ref = InterlockedDecrement(&This->ref);
174 if(ref == 0)
175 heap_free(This);
177 return ref;
180 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
182 dbinit *This = impl_from_IDBInitialize(iface);
184 FIXME("(%p) stub\n", This);
186 return S_OK;
189 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
191 dbinit *This = impl_from_IDBInitialize(iface);
193 FIXME("(%p) stub\n", This);
195 return S_OK;
198 static const IDBInitializeVtbl dbinit_vtbl =
200 dbinit_QueryInterface,
201 dbinit_AddRef,
202 dbinit_Release,
203 dbinit_Initialize,
204 dbinit_Uninitialize
207 static HRESULT create_db_init(IUnknown **obj)
209 dbinit *This;
211 TRACE("()\n");
213 *obj = NULL;
215 This = heap_alloc(sizeof(*This));
216 if(!This) return E_OUTOFMEMORY;
218 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
219 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
220 This->ref = 1;
222 *obj = (IUnknown*)&This->IDBInitialize_iface;
224 return S_OK;
227 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
229 datainit *This = impl_from_IDataInitialize(iface);
230 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
232 *obj = NULL;
234 if(IsEqualIID(riid, &IID_IUnknown) ||
235 IsEqualIID(riid, &IID_IDataInitialize))
237 *obj = &This->IDataInitialize_iface;
239 else
241 FIXME("interface %s not implemented\n", debugstr_guid(riid));
242 return E_NOINTERFACE;
245 IDataInitialize_AddRef(iface);
246 return S_OK;
249 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
251 datainit *This = impl_from_IDataInitialize(iface);
252 TRACE("(%p)\n", This);
254 return InterlockedIncrement(&This->ref);
257 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
259 datainit *This = impl_from_IDataInitialize(iface);
260 LONG ref;
262 TRACE("(%p)\n", This);
264 ref = InterlockedDecrement(&This->ref);
265 if(ref == 0)
266 heap_free(This);
268 return ref;
271 static void free_dbpropset(ULONG count, DBPROPSET *propset)
273 ULONG i;
275 for (i = 0; i < count; i++)
277 ULONG p;
279 for (p = 0; p < propset[i].cProperties; p++)
280 VariantClear(&propset[i].rgProperties[p].vValue);
282 CoTaskMemFree(propset[i].rgProperties);
284 CoTaskMemFree(propset);
287 struct dbproperty {
288 const WCHAR *name;
289 DBPROPID id;
290 DBPROPOPTIONS options;
291 VARTYPE type;
292 HRESULT (*convert_dbproperty)(const WCHAR *src, VARIANT *dest);
295 struct mode_propval
297 const WCHAR *name;
298 DWORD value;
301 static int dbmodeprop_compare(const void *a, const void *b)
303 const WCHAR *src = a;
304 const struct mode_propval *propval = b;
305 return strcmpiW(src, propval->name);
308 static HRESULT convert_dbproperty_mode(const WCHAR *src, VARIANT *dest)
310 static const WCHAR readW[] = {'R','e','a','d',0};
311 static const WCHAR readwriteW[] = {'R','e','a','d','W','r','i','t','e',0};
312 static const WCHAR sharedenynoneW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','N','o','n','e',0};
313 static const WCHAR sharedenyreadW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','R','e','a','d',0};
314 static const WCHAR sharedenywriteW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','W','r','i','t','e',0};
315 static const WCHAR shareexclusiveW[] = {'S','h','a','r','e',' ','E','x','c','l','u','s','i','v','e',0};
316 static const WCHAR writeW[] = {'W','r','i','t','e',0};
318 struct mode_propval mode_propvals[] =
320 { readW, DB_MODE_READ },
321 { readwriteW, DB_MODE_READWRITE },
322 { sharedenynoneW, DB_MODE_SHARE_DENY_NONE },
323 { sharedenyreadW, DB_MODE_SHARE_DENY_READ },
324 { sharedenywriteW, DB_MODE_SHARE_DENY_WRITE },
325 { shareexclusiveW, DB_MODE_SHARE_EXCLUSIVE },
326 { writeW, DB_MODE_WRITE },
328 struct mode_propval *prop;
330 if ((prop = bsearch(src, mode_propvals, sizeof(mode_propvals) / sizeof(*mode_propvals),
331 sizeof(struct mode_propval), dbmodeprop_compare)))
333 V_VT(dest) = VT_I4;
334 V_I4(dest) = prop->value;
335 TRACE("%s = %#x\n", debugstr_w(src), prop->value);
336 return S_OK;
339 return E_FAIL;
342 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};
343 static const WCHAR bindW[] = {'B','i','n','d',' ','F','l','a','g','s',0};
344 static const WCHAR cacheW[] = {'C','a','c','h','e',' ','A','u','t','h','e','n','i','c','a','t','i','o','n',0};
345 static const WCHAR conn_timeout[] = {'C','o','n','n','e','c','t',' ','T','i','m','e','o','u','t',0};
346 static const WCHAR datasourceW[] = {'D','a','t','a',' ','S','o','u','r','c','e',0};
347 static const WCHAR encryptW[] = {'E','n','c','r','y','p','t',' ','P','a','s','s','w','o','r','d',0};
348 static const WCHAR extendedW[] = {'E','x','t','e','n','d','e','d',' ','P','r','o','p','e','r','t','i','e','s',0};
349 static const WCHAR gen_timeout[] = {'G','e','n','e','r','a','l',' ','T','i','m','e','o','u','t',0};
350 static const WCHAR impersonW[] = {'I','m','p','e','r','s','o','n','a','t','i','o','n',' ','L','e','v','e','l',0};
351 static const WCHAR initcatW[] = {'I','n','i','t','i','a','l',' ','C','a','t','a','l','o','g',0};
352 static const WCHAR integratedW[] = {'I','n','t','e','g','r','a','t','e','d',' ','S','e','c','u','r','i','t','y',0};
353 static const WCHAR localeIDW[] = {'L','o','c','a','l','e',' ','I','d','e','n','t','i','f','i','e','r',0};
354 static const WCHAR locationW[] = {'L','o','c','a','t','i','o','n',0};
355 static const WCHAR lockownerW[] = {'L','o','c','k',' ','O','w','n','e','r',0};
356 static const WCHAR maskpassW[] = {'M','a','s','k',' ','P','a','s','s','w','o','r','d',0};
357 static const WCHAR modeW[] = {'M','o','d','e',0};
358 static const WCHAR oledbservW[] = {'O','L','E',' ','D','B',' ','S','e','r','v','i','c','i','e','s',0};
359 static const WCHAR passwordW[] = {'P','a','s','s','w','o','r','d',0};
360 static const WCHAR persistW[] = {'P','e','r','s','i','s','t',' ','S','e','c','u','r','i','t','y',' ','I','n','f','o',0};
361 static const WCHAR persistEncW[] = {'P','e','r','s','i','s','t',' ','E','n','c','r','y','p','t','e','d',0};
362 static const WCHAR promptW[] = {'P','r','o','m','p','t',0};
363 static const WCHAR protectW[] = {'P','r','o','t','e','c','t','i','o','n',' ','l','e','v','e','l',0};
364 static const WCHAR useridW[] = {'U','s','e','r',' ','I','D',0};
365 static const WCHAR winhandleW[] = {'W','i','n','d','o','w',' ','H','a','n','d','l','e',0};
367 static const struct dbproperty dbproperties[] = {
368 { asyncW, DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
369 { bindW, DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
370 { cacheW, DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
371 { conn_timeout,DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
372 { datasourceW, DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
373 { extendedW, DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
374 { encryptW, DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
375 { gen_timeout, DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
376 { impersonW, DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
377 { initcatW, DBPROP_CATALOGLOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
378 { integratedW, DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
379 { localeIDW, DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
380 { locationW, DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
381 { lockownerW, DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
382 { maskpassW, DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
383 { modeW, DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4, convert_dbproperty_mode },
384 { oledbservW, DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
385 { passwordW, DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
386 { persistEncW, DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
387 { persistW, DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
388 { promptW, DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
389 { protectW, DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
390 { useridW, DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
391 #ifndef _WIN64
392 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
393 #else
394 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
395 #endif
398 struct dbprop_pair
400 struct list entry;
401 BSTR name;
402 BSTR value;
405 struct dbprops
407 struct list props;
408 unsigned int count;
411 /* name/value strings are reused, so they shouldn't be freed after this call */
412 static HRESULT add_dbprop_to_list(struct dbprops *props, BSTR name, BSTR value)
414 struct dbprop_pair *pair;
416 pair = heap_alloc(sizeof(*pair));
417 if (!pair)
418 return E_OUTOFMEMORY;
420 pair->name = name;
421 pair->value = value;
422 list_add_tail(&props->props, &pair->entry);
423 props->count++;
424 return S_OK;
427 static void free_dbprop_list(struct dbprops *props)
429 struct dbprop_pair *p, *p2;
430 LIST_FOR_EACH_ENTRY_SAFE(p, p2, &props->props, struct dbprop_pair, entry)
432 list_remove(&p->entry);
433 SysFreeString(p->name);
434 SysFreeString(p->value);
435 heap_free(p);
439 static HRESULT parse_init_string(const WCHAR *initstring, struct dbprops *props)
441 const WCHAR *start;
442 WCHAR *eq;
443 HRESULT hr = S_OK;
445 list_init(&props->props);
446 props->count = 0;
448 start = initstring;
449 while (start && (eq = strchrW(start, '=')))
451 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r',0};
452 WCHAR *delim, quote;
453 BSTR value, name;
455 name = SysAllocStringLen(start, eq - start);
456 /* skip equal sign to get value */
457 eq++;
459 quote = (*eq == '"' || *eq == '\'') ? *eq : 0;
460 if (quote)
462 /* for quoted value string, skip opening mark, look for terminating one */
463 eq++;
464 delim = strchrW(eq, quote);
466 else
467 delim = strchrW(eq, ';');
469 if (delim)
470 value = SysAllocStringLen(eq, delim - eq);
471 else
472 value = SysAllocString(eq);
474 /* skip semicolon if present */
475 if (delim)
477 if (*delim == quote)
478 delim++;
479 if (*delim == ';')
480 delim++;
482 start = delim;
484 if (!strcmpiW(name, providerW))
486 SysFreeString(name);
487 SysFreeString(value);
488 continue;
491 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
493 hr = add_dbprop_to_list(props, name, value);
494 if (FAILED(hr))
496 SysFreeString(name);
497 SysFreeString(value);
498 break;
502 if (FAILED(hr))
503 free_dbprop_list(props);
505 return hr;
508 static const struct dbproperty *get_known_dprop_descr(BSTR name)
510 int min, max, n;
512 min = 0;
513 max = sizeof(dbproperties)/sizeof(struct dbproperty) - 1;
515 while (min <= max)
517 int r;
519 n = (min+max)/2;
521 r = strcmpiW(dbproperties[n].name, name);
522 if (!r)
523 break;
525 if (r < 0)
526 min = n+1;
527 else
528 max = n-1;
531 return (min <= max) ? &dbproperties[n] : NULL;
534 static HRESULT get_dbpropset_from_proplist(struct dbprops *props, DBPROPSET **propset)
536 struct dbprop_pair *pair;
537 int i = 0;
538 HRESULT hr;
540 *propset = CoTaskMemAlloc(sizeof(DBPROPSET));
541 if (!*propset)
542 return E_OUTOFMEMORY;
544 (*propset)->rgProperties = CoTaskMemAlloc(props->count*sizeof(DBPROP));
545 if (!(*propset)->rgProperties)
547 CoTaskMemFree(*propset);
548 *propset = NULL;
549 return E_OUTOFMEMORY;
552 (*propset)->cProperties = 0;
553 LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
555 const struct dbproperty *descr = get_known_dprop_descr(pair->name);
556 VARIANT dest, src;
558 if (!descr)
560 static const WCHAR eqW[] = {'=',0};
561 BSTR str;
562 int len;
564 /* provider specific property is always VT_BSTR */
565 len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1 /* for '=' */;
566 str = SysAllocStringLen(NULL, len);
567 strcpyW(str, pair->name);
568 strcatW(str, eqW);
569 strcatW(str, pair->value);
571 (*propset)->cProperties++;
572 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
573 (*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
574 (*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
575 (*propset)->rgProperties[i].dwStatus = 0;
576 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
577 V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
578 V_BSTR(&(*propset)->rgProperties[i].vValue) = str;
579 i++;
580 continue;
583 V_VT(&src) = VT_BSTR;
584 V_BSTR(&src) = pair->value;
586 VariantInit(&dest);
587 hr = VariantChangeType(&dest, &src, 0, descr->type);
588 if (FAILED(hr) && descr->convert_dbproperty)
589 hr = descr->convert_dbproperty(pair->value, &dest);
591 if (FAILED(hr))
593 ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
594 free_dbpropset(1, *propset);
595 *propset = NULL;
596 return hr;
599 (*propset)->cProperties++;
600 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
601 (*propset)->rgProperties[i].dwPropertyID = descr->id;
602 (*propset)->rgProperties[i].dwOptions = descr->options;
603 (*propset)->rgProperties[i].dwStatus = 0;
604 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
605 (*propset)->rgProperties[i].vValue = dest;
606 i++;
609 return S_OK;
612 /*** IDataInitialize methods ***/
613 static void datasource_release(BOOL created, IUnknown **datasource)
615 if (!created)
616 return;
618 IUnknown_Release(*datasource);
619 *datasource = NULL;
622 static inline WCHAR *strdupW(const WCHAR *src)
624 WCHAR *dest;
625 if (!src) return NULL;
626 dest = heap_alloc((strlenW(src)+1)*sizeof(WCHAR));
627 if (dest)
628 strcpyW(dest, src);
629 return dest;
632 static WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
634 LPWSTR strlower, sublower, r;
635 strlower = CharLowerW(strdupW(str));
636 sublower = CharLowerW(strdupW(sub));
637 r = strstrW(strlower, sublower);
638 if (r)
639 r = (LPWSTR)str + (r - strlower);
640 heap_free(strlower);
641 heap_free(sublower);
642 return r;
645 HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
647 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r','=',0};
648 static const WCHAR msdasqlW[] = {'M','S','D','A','S','Q','L',0};
649 BOOL datasource_created = FALSE;
650 IDBProperties *dbprops;
651 DBPROPSET *propset;
652 WCHAR *prov = NULL;
653 CLSID provclsid;
654 HRESULT hr;
657 /* first get provider name */
658 provclsid = IID_NULL;
659 if (initstring && (prov = strstriW(initstring, providerW)))
661 WCHAR *start, *progid;
662 int len;
664 prov += sizeof(providerW)/sizeof(WCHAR)-1;
665 start = prov;
666 while (*prov && *prov != ';')
667 ++prov;
668 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
670 len = prov - start;
671 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
672 if (!progid) return E_OUTOFMEMORY;
674 memcpy(progid, start, len*sizeof(WCHAR));
675 progid[len] = 0;
677 hr = CLSIDFromProgID(progid, &provclsid);
678 CoTaskMemFree(progid);
679 if (FAILED(hr))
681 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
682 return hr;
685 else
687 hr = CLSIDFromProgID(msdasqlW, &provclsid);
688 if (FAILED(hr))
689 ERR("ODBC provider for OLE DB not registered\n");
692 /* check for provider mismatch if it was specified in init string */
693 if (*datasource && prov)
695 DBPROPIDSET propidset;
696 enum DBPROPENUM prop;
697 CLSID initprov;
698 ULONG count;
700 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
701 if (FAILED(hr))
703 WARN("provider doesn't support IDBProperties\n");
704 return hr;
707 prop = DBPROP_INIT_DATASOURCE;
708 propidset.rgPropertyIDs = &prop;
709 propidset.cPropertyIDs = 1;
710 propidset.guidPropertySet = DBPROPSET_DBINIT;
711 count = 0;
712 propset = NULL;
713 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
714 IDBProperties_Release(dbprops);
715 if (FAILED(hr))
717 WARN("GetProperties failed for datasource, 0x%08x\n", hr);
718 return hr;
721 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
722 initprov = IID_NULL;
723 hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
724 free_dbpropset(count, propset);
726 if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
729 if (!*datasource)
731 if (!IsEqualIID(&provclsid, &IID_NULL))
732 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
734 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
735 hr = create_db_init(datasource);
737 datasource_created = *datasource != NULL;
740 /* now set properties */
741 if (initstring)
743 struct dbprops props;
745 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
746 if (FAILED(hr))
748 ERR("provider doesn't support IDBProperties\n");
749 datasource_release(datasource_created, datasource);
750 return hr;
753 hr = parse_init_string(initstring, &props);
754 if (FAILED(hr))
756 datasource_release(datasource_created, datasource);
757 return hr;
760 hr = get_dbpropset_from_proplist(&props, &propset);
761 free_dbprop_list(&props);
762 if (FAILED(hr))
764 datasource_release(datasource_created, datasource);
765 return hr;
768 hr = IDBProperties_SetProperties(dbprops, 1, propset);
769 IDBProperties_Release(dbprops);
770 free_dbpropset(1, propset);
771 if (FAILED(hr))
773 ERR("SetProperties failed, 0x%08x\n", hr);
774 datasource_release(datasource_created, datasource);
775 return hr;
779 return hr;
782 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
783 LPWSTR initstring, REFIID riid, IUnknown **datasource)
785 datainit *This = impl_from_IDataInitialize(iface);
787 TRACE("(%p)->(%p 0x%x %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
789 return get_data_source(outer, clsctx, initstring, riid, datasource);
792 /* returns character length of string representation */
793 static int get_propvalue_length(DBPROP *prop)
795 VARIANT str;
796 HRESULT hr;
798 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
800 VariantInit(&str);
801 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
802 if (hr == S_OK)
804 int len = SysStringLen(V_BSTR(&str));
805 VariantClear(&str);
806 return len;
809 return 0;
812 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
814 VARIANT *v = &prop->vValue;
815 VARIANT vstr;
816 HRESULT hr;
818 if (V_VT(v) == VT_BSTR)
820 strcatW(str, V_BSTR(v));
821 return;
824 VariantInit(&vstr);
825 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
826 if (hr == S_OK)
828 strcatW(str, V_BSTR(&vstr));
829 VariantClear(&vstr);
833 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
835 ULONG i;
837 for (i = 0; i < propinfoset->cPropertyInfos; i++)
838 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
839 return propinfoset->rgPropertyInfos[i].pwszDescription;
841 return NULL;
844 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
846 ULONG i;
848 for (i = 0; i < count; i++)
850 ULONG p;
852 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
853 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
855 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
857 CoTaskMemFree(propinfoset);
860 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
861 boolean include_pass, LPWSTR *init_string)
863 static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
864 static const WCHAR colW[] = {';',0};
865 datainit *This = impl_from_IDataInitialize(iface);
866 DBPROPINFOSET *propinfoset;
867 IDBProperties *props;
868 DBPROPIDSET propidset;
869 ULONG count, infocount;
870 WCHAR *progid, *desc;
871 DBPROPSET *propset;
872 IPersist *persist;
873 HRESULT hr;
874 CLSID clsid;
875 ULONG i, len;
877 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
879 /* IPersist support is mandatory for data sources */
880 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
881 if (FAILED(hr)) return hr;
883 memset(&clsid, 0, sizeof(clsid));
884 hr = IPersist_GetClassID(persist, &clsid);
885 IPersist_Release(persist);
886 if (FAILED(hr)) return hr;
888 progid = NULL;
889 ProgIDFromCLSID(&clsid, &progid);
890 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
892 /* now get initialization properties */
893 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
894 if (FAILED(hr))
896 WARN("IDBProperties not supported\n");
897 CoTaskMemFree(progid);
898 return hr;
901 propidset.rgPropertyIDs = NULL;
902 propidset.cPropertyIDs = 0;
903 propidset.guidPropertySet = DBPROPSET_DBINIT;
904 propset = NULL;
905 count = 0;
906 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
907 if (FAILED(hr))
909 WARN("failed to get data source properties, 0x%08x\n", hr);
910 CoTaskMemFree(progid);
911 return hr;
914 infocount = 0;
915 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
916 IDBProperties_Release(props);
918 /* check if we need to skip password */
919 len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
920 for (i = 0; i < count; i++)
922 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
923 if (descr)
925 /* include '=' and ';' */
926 len += strlenW(descr) + 2;
927 len += get_propvalue_length(&propset->rgProperties[i]);
930 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
931 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
932 include_pass = FALSE;
935 len *= sizeof(WCHAR);
936 *init_string = CoTaskMemAlloc(len);
937 *init_string[0] = 0;
939 /* provider name */
940 strcatW(*init_string, provW);
941 strcatW(*init_string, progid);
942 strcatW(*init_string, colW);
943 CoTaskMemFree(progid);
945 for (i = 0; i < count; i++)
947 WCHAR *descr;
949 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
951 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
952 if (descr)
954 static const WCHAR eqW[] = {'=',0};
955 strcatW(*init_string, descr);
956 strcatW(*init_string, eqW);
957 write_propvalue_str(*init_string, &propset->rgProperties[i]);
958 strcatW(*init_string, colW);
962 free_dbpropset(count, propset);
963 free_dbpropinfoset(infocount, propinfoset);
964 CoTaskMemFree(desc);
966 if (!include_pass)
967 TRACE("%s\n", debugstr_w(*init_string));
968 return S_OK;
971 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
972 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
973 IUnknown **datasource)
975 datainit *This = impl_from_IDataInitialize(iface);
977 TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
978 debugstr_guid(riid), datasource);
980 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
983 static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
984 DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
986 datainit *This = impl_from_IDataInitialize(iface);
988 FIXME("(%p)->(%s %p %#x %s %p %u %p)\n", This, debugstr_guid(provider), outer, clsctx,
989 debugstr_w(reserved), server_info, cmq, results);
991 return E_NOTIMPL;
994 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
995 LPWSTR *ppwszInitializationString)
997 datainit *This = impl_from_IDataInitialize(iface);
999 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
1001 return E_NOTIMPL;
1004 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
1005 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
1007 datainit *This = impl_from_IDataInitialize(iface);
1009 FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
1011 return E_NOTIMPL;
1015 static const struct IDataInitializeVtbl datainit_vtbl =
1017 datainit_QueryInterface,
1018 datainit_AddRef,
1019 datainit_Release,
1020 datainit_GetDataSource,
1021 datainit_GetInitializationString,
1022 datainit_CreateDBInstance,
1023 datainit_CreateDBInstanceEx,
1024 datainit_LoadStringFromStorage,
1025 datainit_WriteStringToStorage
1028 HRESULT create_data_init(IUnknown *outer, void **obj)
1030 datainit *This;
1032 TRACE("(%p)\n", obj);
1034 if(outer) return CLASS_E_NOAGGREGATION;
1036 *obj = NULL;
1038 This = heap_alloc(sizeof(*This));
1039 if(!This) return E_OUTOFMEMORY;
1041 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
1042 This->ref = 1;
1044 *obj = &This->IDataInitialize_iface;
1046 return S_OK;