user32: Fix error handling in EndDeferWindowPos.
[wine.git] / dlls / oledb32 / datainit.c
blob7ed4099b0dd6b491886aaa3c07e8d31db64acde3
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;
294 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};
295 static const WCHAR bindW[] = {'B','i','n','d',' ','F','l','a','g','s',0};
296 static const WCHAR cacheW[] = {'C','a','c','h','e',' ','A','u','t','h','e','n','i','c','a','t','i','o','n',0};
297 static const WCHAR conn_timeout[] = {'C','o','n','n','e','c','t',' ','T','i','m','e','o','u','t',0};
298 static const WCHAR datasourceW[] = {'D','a','t','a',' ','S','o','u','r','c','e',0};
299 static const WCHAR encryptW[] = {'E','n','c','r','y','p','t',' ','P','a','s','s','w','o','r','d',0};
300 static const WCHAR extendedW[] = {'E','x','t','e','n','d','e','d',' ','P','r','o','p','e','r','t','i','e','s',0};
301 static const WCHAR gen_timeout[] = {'G','e','n','e','r','a','l',' ','T','i','m','e','o','u','t',0};
302 static const WCHAR impersonW[] = {'I','m','p','e','r','s','o','n','a','t','i','o','n',' ','L','e','v','e','l',0};
303 static const WCHAR initcatW[] = {'I','n','i','t','i','a','l',' ','C','a','t','a','l','o','g',0};
304 static const WCHAR integratedW[] = {'I','n','t','e','g','r','a','t','e','d',' ','S','e','c','u','r','i','t','y',0};
305 static const WCHAR localeIDW[] = {'L','o','c','a','l','e',' ','I','d','e','n','t','i','f','i','e','r',0};
306 static const WCHAR locationW[] = {'L','o','c','a','t','i','o','n',0};
307 static const WCHAR lockownerW[] = {'L','o','c','k',' ','O','w','n','e','r',0};
308 static const WCHAR maskpassW[] = {'M','a','s','k',' ','P','a','s','s','w','o','r','d',0};
309 static const WCHAR modeW[] = {'M','o','d','e',0};
310 static const WCHAR oledbservW[] = {'O','L','E',' ','D','B',' ','S','e','r','v','i','c','i','e','s',0};
311 static const WCHAR passwordW[] = {'P','a','s','s','w','o','r','d',0};
312 static const WCHAR persistW[] = {'P','e','r','s','i','s','t',' ','S','e','c','u','r','i','t','y',' ','I','n','f','o',0};
313 static const WCHAR persistEncW[] = {'P','e','r','s','i','s','t',' ','E','n','c','r','y','p','t','e','d',0};
314 static const WCHAR promptW[] = {'P','r','o','m','p','t',0};
315 static const WCHAR protectW[] = {'P','r','o','t','e','c','t','i','o','n',' ','l','e','v','e','l',0};
316 static const WCHAR useridW[] = {'U','s','e','r',' ','I','D',0};
317 static const WCHAR winhandleW[] = {'W','i','n','d','o','w',' ','H','a','n','d','l','e',0};
319 static const struct dbproperty dbproperties[] = {
320 { asyncW, DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
321 { bindW, DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
322 { cacheW, DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
323 { conn_timeout,DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
324 { datasourceW, DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
325 { extendedW, DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
326 { encryptW, DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
327 { gen_timeout, DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
328 { impersonW, DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
329 { initcatW, DBPROP_CATALOGLOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
330 { integratedW, DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
331 { localeIDW, DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
332 { locationW, DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
333 { lockownerW, DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
334 { maskpassW, DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
335 { modeW, DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4 },
336 { oledbservW, DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
337 { passwordW, DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
338 { persistEncW, DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
339 { persistW, DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
340 { promptW, DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
341 { protectW, DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
342 { useridW, DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
343 #ifndef _WIN64
344 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
345 #else
346 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
347 #endif
350 struct dbprop_pair
352 struct list entry;
353 BSTR name;
354 BSTR value;
357 struct dbprops
359 struct list props;
360 unsigned int count;
363 /* name/value strings are reused, so they shouldn't be freed after this call */
364 static HRESULT add_dbprop_to_list(struct dbprops *props, BSTR name, BSTR value)
366 struct dbprop_pair *pair;
368 pair = heap_alloc(sizeof(*pair));
369 if (!pair)
370 return E_OUTOFMEMORY;
372 pair->name = name;
373 pair->value = value;
374 list_add_tail(&props->props, &pair->entry);
375 props->count++;
376 return S_OK;
379 static void free_dbprop_list(struct dbprops *props)
381 struct dbprop_pair *p, *p2;
382 LIST_FOR_EACH_ENTRY_SAFE(p, p2, &props->props, struct dbprop_pair, entry)
384 list_remove(&p->entry);
385 SysFreeString(p->name);
386 SysFreeString(p->value);
387 heap_free(p);
391 static HRESULT parse_init_string(const WCHAR *initstring, struct dbprops *props)
393 const WCHAR *start;
394 WCHAR *eq;
395 HRESULT hr = S_OK;
397 list_init(&props->props);
398 props->count = 0;
400 start = initstring;
401 while (start && (eq = strchrW(start, '=')))
403 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r',0};
404 WCHAR *delim, quote;
405 BSTR value, name;
407 name = SysAllocStringLen(start, eq - start);
408 /* skip equal sign to get value */
409 eq++;
411 quote = (*eq == '"' || *eq == '\'') ? *eq : 0;
412 if (quote)
414 /* for quoted value string, skip opening mark, look for terminating one */
415 eq++;
416 delim = strchrW(eq, quote);
418 else
419 delim = strchrW(eq, ';');
421 value = SysAllocStringLen(eq, delim ? delim - eq : -1);
423 /* skip semicolon if present */
424 if (delim)
426 if (*delim == quote)
427 delim++;
428 if (*delim == ';')
429 delim++;
431 start = delim;
433 if (!strcmpiW(name, providerW))
435 SysFreeString(name);
436 SysFreeString(value);
437 continue;
440 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
442 hr = add_dbprop_to_list(props, name, value);
443 if (FAILED(hr))
445 SysFreeString(name);
446 SysFreeString(value);
447 break;
451 if (FAILED(hr))
452 free_dbprop_list(props);
454 return hr;
457 static const struct dbproperty *get_known_dprop_descr(BSTR name)
459 int min, max, n;
461 min = 0;
462 max = sizeof(dbproperties)/sizeof(struct dbproperty) - 1;
464 while (min <= max)
466 int r;
468 n = (min+max)/2;
470 r = strcmpiW(dbproperties[n].name, name);
471 if (!r)
472 break;
474 if (r < 0)
475 min = n+1;
476 else
477 max = n-1;
480 return (min <= max) ? &dbproperties[n] : NULL;
483 static HRESULT get_dbpropset_from_proplist(struct dbprops *props, DBPROPSET **propset)
485 struct dbprop_pair *pair;
486 int i = 0;
487 HRESULT hr;
489 *propset = CoTaskMemAlloc(sizeof(DBPROPSET));
490 if (!*propset)
491 return E_OUTOFMEMORY;
493 (*propset)->rgProperties = CoTaskMemAlloc(props->count*sizeof(DBPROP));
494 if (!(*propset)->rgProperties)
496 CoTaskMemFree(*propset);
497 *propset = NULL;
498 return E_OUTOFMEMORY;
501 (*propset)->cProperties = 0;
502 LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
504 const struct dbproperty *descr = get_known_dprop_descr(pair->name);
505 VARIANT dest, src;
507 if (!descr)
509 static const WCHAR eqW[] = {'=',0};
510 BSTR str;
511 int len;
513 /* provider specific property is always VT_BSTR */
514 len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1 /* for '=' */;
515 str = SysAllocStringLen(NULL, len);
516 strcpyW(str, pair->name);
517 strcatW(str, eqW);
518 strcatW(str, pair->value);
520 (*propset)->cProperties++;
521 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
522 (*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
523 (*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
524 (*propset)->rgProperties[i].dwStatus = 0;
525 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
526 V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
527 V_BSTR(&(*propset)->rgProperties[i].vValue) = str;
528 i++;
529 continue;
532 V_VT(&src) = VT_BSTR;
533 V_BSTR(&src) = pair->value;
535 VariantInit(&dest);
536 hr = VariantChangeType(&dest, &src, 0, descr->type);
537 if (FAILED(hr))
539 ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
540 free_dbpropset(1, *propset);
541 *propset = NULL;
542 return hr;
545 (*propset)->cProperties++;
546 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
547 (*propset)->rgProperties[i].dwPropertyID = descr->id;
548 (*propset)->rgProperties[i].dwOptions = descr->options;
549 (*propset)->rgProperties[i].dwStatus = 0;
550 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
551 (*propset)->rgProperties[i].vValue = dest;
552 i++;
555 return S_OK;
558 /*** IDataInitialize methods ***/
559 static void datasource_release(BOOL created, IUnknown **datasource)
561 if (!created)
562 return;
564 IUnknown_Release(*datasource);
565 *datasource = NULL;
568 static inline WCHAR *strdupW(const WCHAR *src)
570 WCHAR *dest;
571 if (!src) return NULL;
572 dest = heap_alloc((strlenW(src)+1)*sizeof(WCHAR));
573 if (dest)
574 strcpyW(dest, src);
575 return dest;
578 static WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
580 LPWSTR strlower, sublower, r;
581 strlower = CharLowerW(strdupW(str));
582 sublower = CharLowerW(strdupW(sub));
583 r = strstrW(strlower, sublower);
584 if (r)
585 r = (LPWSTR)str + (r - strlower);
586 heap_free(strlower);
587 heap_free(sublower);
588 return r;
591 HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
593 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r','=',0};
594 static const WCHAR msdasqlW[] = {'M','S','D','A','S','Q','L',0};
595 BOOL datasource_created = FALSE;
596 IDBProperties *dbprops;
597 DBPROPSET *propset;
598 WCHAR *prov = NULL;
599 CLSID provclsid;
600 HRESULT hr;
603 /* first get provider name */
604 provclsid = IID_NULL;
605 if (initstring && (prov = strstriW(initstring, providerW)))
607 WCHAR *start, *progid;
608 int len;
610 prov += sizeof(providerW)/sizeof(WCHAR)-1;
611 start = prov;
612 while (*prov && *prov != ';')
613 ++prov;
614 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
616 len = prov - start;
617 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
618 if (!progid) return E_OUTOFMEMORY;
620 memcpy(progid, start, len*sizeof(WCHAR));
621 progid[len] = 0;
623 hr = CLSIDFromProgID(progid, &provclsid);
624 CoTaskMemFree(progid);
625 if (FAILED(hr))
627 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
628 return hr;
631 else
633 hr = CLSIDFromProgID(msdasqlW, &provclsid);
634 if (FAILED(hr))
635 ERR("ODBC provider for OLE DB not registered\n");
638 /* check for provider mismatch if it was specified in init string */
639 if (*datasource && prov)
641 DBPROPIDSET propidset;
642 enum DBPROPENUM prop;
643 CLSID initprov;
644 ULONG count;
646 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
647 if (FAILED(hr))
649 WARN("provider doesn't support IDBProperties\n");
650 return hr;
653 prop = DBPROP_INIT_DATASOURCE;
654 propidset.rgPropertyIDs = &prop;
655 propidset.cPropertyIDs = 1;
656 propidset.guidPropertySet = DBPROPSET_DBINIT;
657 count = 0;
658 propset = NULL;
659 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
660 IDBProperties_Release(dbprops);
661 if (FAILED(hr))
663 WARN("GetProperties failed for datasource, 0x%08x\n", hr);
664 return hr;
667 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
668 initprov = IID_NULL;
669 hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
670 free_dbpropset(count, propset);
672 if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
675 if (!*datasource)
677 if (!IsEqualIID(&provclsid, &IID_NULL))
678 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
680 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
681 hr = create_db_init(datasource);
683 datasource_created = *datasource != NULL;
686 /* now set properties */
687 if (initstring)
689 struct dbprops props;
691 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
692 if (FAILED(hr))
694 ERR("provider doesn't support IDBProperties\n");
695 datasource_release(datasource_created, datasource);
696 return hr;
699 hr = parse_init_string(initstring, &props);
700 if (FAILED(hr))
702 datasource_release(datasource_created, datasource);
703 return hr;
706 hr = get_dbpropset_from_proplist(&props, &propset);
707 free_dbprop_list(&props);
708 if (FAILED(hr))
710 datasource_release(datasource_created, datasource);
711 return hr;
714 hr = IDBProperties_SetProperties(dbprops, 1, propset);
715 IDBProperties_Release(dbprops);
716 free_dbpropset(1, propset);
717 if (FAILED(hr))
719 ERR("SetProperties failed, 0x%08x\n", hr);
720 datasource_release(datasource_created, datasource);
721 return hr;
725 return hr;
728 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
729 LPWSTR initstring, REFIID riid, IUnknown **datasource)
731 datainit *This = impl_from_IDataInitialize(iface);
733 TRACE("(%p)->(%p 0x%x %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
735 return get_data_source(outer, clsctx, initstring, riid, datasource);
738 /* returns character length of string representation */
739 static int get_propvalue_length(DBPROP *prop)
741 VARIANT str;
742 HRESULT hr;
744 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
746 VariantInit(&str);
747 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
748 if (hr == S_OK)
750 int len = SysStringLen(V_BSTR(&str));
751 VariantClear(&str);
752 return len;
755 return 0;
758 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
760 VARIANT *v = &prop->vValue;
761 VARIANT vstr;
762 HRESULT hr;
764 if (V_VT(v) == VT_BSTR)
766 strcatW(str, V_BSTR(v));
767 return;
770 VariantInit(&vstr);
771 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
772 if (hr == S_OK)
774 strcatW(str, V_BSTR(&vstr));
775 VariantClear(&vstr);
779 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
781 ULONG i;
783 for (i = 0; i < propinfoset->cPropertyInfos; i++)
784 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
785 return propinfoset->rgPropertyInfos[i].pwszDescription;
787 return NULL;
790 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
792 ULONG i;
794 for (i = 0; i < count; i++)
796 ULONG p;
798 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
799 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
801 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
803 CoTaskMemFree(propinfoset);
806 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
807 boolean include_pass, LPWSTR *init_string)
809 static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
810 static const WCHAR colW[] = {';',0};
811 datainit *This = impl_from_IDataInitialize(iface);
812 DBPROPINFOSET *propinfoset;
813 IDBProperties *props;
814 DBPROPIDSET propidset;
815 ULONG count, infocount;
816 WCHAR *progid, *desc;
817 DBPROPSET *propset;
818 IPersist *persist;
819 HRESULT hr;
820 CLSID clsid;
821 ULONG i, len;
823 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
825 /* IPersist support is mandatory for data sources */
826 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
827 if (FAILED(hr)) return hr;
829 memset(&clsid, 0, sizeof(clsid));
830 hr = IPersist_GetClassID(persist, &clsid);
831 IPersist_Release(persist);
832 if (FAILED(hr)) return hr;
834 progid = NULL;
835 ProgIDFromCLSID(&clsid, &progid);
836 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
838 /* now get initialization properties */
839 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
840 if (FAILED(hr))
842 WARN("IDBProperties not supported\n");
843 CoTaskMemFree(progid);
844 return hr;
847 propidset.rgPropertyIDs = NULL;
848 propidset.cPropertyIDs = 0;
849 propidset.guidPropertySet = DBPROPSET_DBINIT;
850 propset = NULL;
851 count = 0;
852 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
853 if (FAILED(hr))
855 WARN("failed to get data source properties, 0x%08x\n", hr);
856 CoTaskMemFree(progid);
857 return hr;
860 infocount = 0;
861 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
862 IDBProperties_Release(props);
864 /* check if we need to skip password */
865 len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
866 for (i = 0; i < count; i++)
868 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
869 if (descr)
871 /* include '=' and ';' */
872 len += strlenW(descr) + 2;
873 len += get_propvalue_length(&propset->rgProperties[i]);
876 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
877 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
878 include_pass = FALSE;
881 len *= sizeof(WCHAR);
882 *init_string = CoTaskMemAlloc(len);
883 *init_string[0] = 0;
885 /* provider name */
886 strcatW(*init_string, provW);
887 strcatW(*init_string, progid);
888 strcatW(*init_string, colW);
889 CoTaskMemFree(progid);
891 for (i = 0; i < count; i++)
893 WCHAR *descr;
895 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
897 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
898 if (descr)
900 static const WCHAR eqW[] = {'=',0};
901 strcatW(*init_string, descr);
902 strcatW(*init_string, eqW);
903 write_propvalue_str(*init_string, &propset->rgProperties[i]);
904 strcatW(*init_string, colW);
908 free_dbpropset(count, propset);
909 free_dbpropinfoset(infocount, propinfoset);
910 CoTaskMemFree(desc);
912 if (!include_pass)
913 TRACE("%s\n", debugstr_w(*init_string));
914 return S_OK;
917 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
918 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
919 IUnknown **datasource)
921 datainit *This = impl_from_IDataInitialize(iface);
923 TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
924 debugstr_guid(riid), datasource);
926 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
929 static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
930 DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
932 datainit *This = impl_from_IDataInitialize(iface);
934 FIXME("(%p)->(%s %p %#x %s %p %u %p)\n", This, debugstr_guid(provider), outer, clsctx,
935 debugstr_w(reserved), server_info, cmq, results);
937 return E_NOTIMPL;
940 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
941 LPWSTR *ppwszInitializationString)
943 datainit *This = impl_from_IDataInitialize(iface);
945 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
947 return E_NOTIMPL;
950 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
951 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
953 datainit *This = impl_from_IDataInitialize(iface);
955 FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
957 return E_NOTIMPL;
961 static const struct IDataInitializeVtbl datainit_vtbl =
963 datainit_QueryInterface,
964 datainit_AddRef,
965 datainit_Release,
966 datainit_GetDataSource,
967 datainit_GetInitializationString,
968 datainit_CreateDBInstance,
969 datainit_CreateDBInstanceEx,
970 datainit_LoadStringFromStorage,
971 datainit_WriteStringToStorage
974 HRESULT create_data_init(IUnknown *outer, void **obj)
976 datainit *This;
978 TRACE("(%p)\n", obj);
980 if(outer) return CLASS_E_NOAGGREGATION;
982 *obj = NULL;
984 This = heap_alloc(sizeof(*This));
985 if(!This) return E_OUTOFMEMORY;
987 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
988 This->ref = 1;
990 *obj = &This->IDataInitialize_iface;
992 return S_OK;