oledb32: Put provider initialization properties in a single DBPROP_INIT_PROVIDERSTRING.
[wine.git] / dlls / oledb32 / datainit.c
blob325de558584257b5b4ca9bbd78006822ac76adf5
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"
34 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
36 DEFINE_GUID(DB_NULLGUID, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
37 DEFINE_GUID(DBGUID_SESSION, 0xc8b522f5, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
38 DEFINE_GUID(DBGUID_ROWSET, 0xc8b522f6, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
39 DEFINE_GUID(DBGUID_ROW, 0xc8b522f7, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
40 DEFINE_GUID(DBGUID_STREAM, 0xc8b522f9, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
42 DEFINE_GUID(DBPROPSET_COLUMN, 0xc8b522b9, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
43 DEFINE_GUID(DBPROPSET_DATASOURCE, 0xc8b522ba, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
44 DEFINE_GUID(DBPROPSET_DATASOURCEINFO, 0xc8b522bb, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
45 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
46 DEFINE_GUID(DBPROPSET_INDEX, 0xc8b522bd, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
47 DEFINE_GUID(DBPROPSET_ROWSET, 0xc8b522be, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
48 DEFINE_GUID(DBPROPSET_TABLE, 0xc8b522bf, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
49 DEFINE_GUID(DBPROPSET_DATASOURCEALL, 0xc8b522c0, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
50 DEFINE_GUID(DBPROPSET_DATASOURCEINFOALL, 0xc8b522c1, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
51 DEFINE_GUID(DBPROPSET_ROWSETALL, 0xc8b522c2, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
52 DEFINE_GUID(DBPROPSET_SESSION, 0xc8b522c6, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
53 DEFINE_GUID(DBPROPSET_SESSIONALL, 0xc8b522c7, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
54 DEFINE_GUID(DBPROPSET_DBINITALL, 0xc8b522ca, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
55 DEFINE_GUID(DBPROPSET_PROPERTIESINERROR, 0xc8b522d4, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
57 typedef struct datainit
59 IDataInitialize IDataInitialize_iface;
61 LONG ref;
62 } datainit;
64 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
66 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
69 typedef struct
71 IDBInitialize IDBInitialize_iface;
72 IDBProperties IDBProperties_iface;
74 LONG ref;
75 } dbinit;
77 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
79 return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
82 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
84 return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
87 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
89 dbinit *This = impl_from_IDBProperties(iface);
91 return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
94 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
96 dbinit *This = impl_from_IDBProperties(iface);
98 return IDBInitialize_AddRef(&This->IDBInitialize_iface);
101 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
103 dbinit *This = impl_from_IDBProperties(iface);
105 return IDBInitialize_Release(&This->IDBInitialize_iface);
108 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
109 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
111 dbinit *This = impl_from_IDBProperties(iface);
113 FIXME("(%p)->(%ld %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
115 return E_NOTIMPL;
118 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
119 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
120 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
122 dbinit *This = impl_from_IDBProperties(iface);
124 FIXME("(%p)->(%ld %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
125 prgPropertyInfoSets, ppDescBuffer);
127 return E_NOTIMPL;
130 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
131 DBPROPSET rgPropertySets[])
133 dbinit *This = impl_from_IDBProperties(iface);
135 FIXME("(%p)->(%ld %p)\n", This, cPropertySets, rgPropertySets);
137 return E_NOTIMPL;
140 static const struct IDBPropertiesVtbl dbprops_vtbl =
142 dbprops_QueryInterface,
143 dbprops_AddRef,
144 dbprops_Release,
145 dbprops_GetProperties,
146 dbprops_GetPropertyInfo,
147 dbprops_SetProperties
150 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
152 dbinit *This = impl_from_IDBInitialize(iface);
153 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
155 *obj = NULL;
157 if(IsEqualIID(riid, &IID_IUnknown) ||
158 IsEqualIID(riid, &IID_IDBInitialize))
160 *obj = iface;
162 else if(IsEqualIID(riid, &IID_IDBProperties))
164 *obj = &This->IDBProperties_iface;
166 else
168 FIXME("interface %s not implemented\n", debugstr_guid(riid));
169 return E_NOINTERFACE;
172 IDBInitialize_AddRef(iface);
173 return S_OK;
176 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
178 dbinit *This = impl_from_IDBInitialize(iface);
179 TRACE("(%p)\n", This);
181 return InterlockedIncrement(&This->ref);
184 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
186 dbinit *This = impl_from_IDBInitialize(iface);
187 LONG ref;
189 TRACE("(%p)\n", This);
191 ref = InterlockedDecrement(&This->ref);
192 if(ref == 0)
193 free(This);
195 return ref;
198 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
200 dbinit *This = impl_from_IDBInitialize(iface);
202 FIXME("(%p) stub\n", This);
204 return S_OK;
207 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
209 dbinit *This = impl_from_IDBInitialize(iface);
211 FIXME("(%p) stub\n", This);
213 return S_OK;
216 static const IDBInitializeVtbl dbinit_vtbl =
218 dbinit_QueryInterface,
219 dbinit_AddRef,
220 dbinit_Release,
221 dbinit_Initialize,
222 dbinit_Uninitialize
225 static HRESULT create_db_init(IUnknown **obj)
227 dbinit *This;
229 TRACE("()\n");
231 *obj = NULL;
233 This = malloc(sizeof(*This));
234 if(!This) return E_OUTOFMEMORY;
236 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
237 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
238 This->ref = 1;
240 *obj = (IUnknown*)&This->IDBInitialize_iface;
242 return S_OK;
245 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
247 datainit *This = impl_from_IDataInitialize(iface);
248 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
250 *obj = NULL;
252 if(IsEqualIID(riid, &IID_IUnknown) ||
253 IsEqualIID(riid, &IID_IDataInitialize))
255 *obj = &This->IDataInitialize_iface;
257 else
259 FIXME("interface %s not implemented\n", debugstr_guid(riid));
260 return E_NOINTERFACE;
263 IDataInitialize_AddRef(iface);
264 return S_OK;
267 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
269 datainit *This = impl_from_IDataInitialize(iface);
270 TRACE("(%p)\n", This);
272 return InterlockedIncrement(&This->ref);
275 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
277 datainit *This = impl_from_IDataInitialize(iface);
278 LONG ref;
280 TRACE("(%p)\n", This);
282 ref = InterlockedDecrement(&This->ref);
283 if(ref == 0)
284 free(This);
286 return ref;
289 static void free_dbpropset(ULONG count, DBPROPSET *propset)
291 ULONG i;
293 for (i = 0; i < count; i++)
295 ULONG p;
297 for (p = 0; p < propset[i].cProperties; p++)
298 VariantClear(&propset[i].rgProperties[p].vValue);
300 CoTaskMemFree(propset[i].rgProperties);
302 CoTaskMemFree(propset);
305 struct dbproperty {
306 const WCHAR *name;
307 DBPROPID id;
308 DBPROPOPTIONS options;
309 VARTYPE type;
310 HRESULT (*convert_dbproperty)(const WCHAR *src, VARIANT *dest);
313 struct mode_propval
315 const WCHAR *name;
316 DWORD value;
319 static int __cdecl dbmodeprop_compare(const void *a, const void *b)
321 const WCHAR *src = a;
322 const struct mode_propval *propval = b;
323 return wcsicmp(src, propval->name);
326 static HRESULT convert_dbproperty_mode(const WCHAR *src, VARIANT *dest)
328 static const struct mode_propval mode_propvals[] =
330 { L"Read", DB_MODE_READ },
331 { L"ReadWrite", DB_MODE_READWRITE },
332 { L"Share Deny None", DB_MODE_SHARE_DENY_NONE },
333 { L"Share Deny Read", DB_MODE_SHARE_DENY_READ },
334 { L"Share Deny Write", DB_MODE_SHARE_DENY_WRITE },
335 { L"Share Exclusive", DB_MODE_SHARE_EXCLUSIVE },
336 { L"Write", DB_MODE_WRITE },
338 struct mode_propval *prop;
340 if ((prop = bsearch(src, mode_propvals, ARRAY_SIZE(mode_propvals),
341 sizeof(struct mode_propval), dbmodeprop_compare)))
343 V_VT(dest) = VT_I4;
344 V_I4(dest) = prop->value;
345 TRACE("%s = %#lx\n", debugstr_w(src), prop->value);
346 return S_OK;
349 return E_FAIL;
352 static const struct dbproperty dbproperties[] =
354 { L"Asynchronous Processing", DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
355 { L"Bind Flags", DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
356 { L"Cache Authentication", DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
357 { L"Connect Timeout", DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
358 { L"Data Source", DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
359 { L"Extended Properties", DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
360 { L"Encrypt Password", DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
361 { L"General Timeout", DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
362 { L"Impersonation Level", DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
363 { L"Initial Catalog", DBPROP_INIT_CATALOG, DBPROPOPTIONS_REQUIRED, VT_BSTR },
364 { L"Integrated Security", DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
365 { L"Locale Identifier", DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
366 { L"Location", DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
367 { L"Lock Owner", DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
368 { L"Mask Password", DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
369 { L"Mode", DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4, convert_dbproperty_mode },
370 { L"OLE DB Services", DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
371 { L"Password", DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
372 { L"Persist Encrypted", DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
373 { L"Persist Security Info", DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
374 { L"Prompt", DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
375 { L"Protection level", DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
376 { L"User ID", DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
377 #ifndef _WIN64
378 { L"Window Handle", DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
379 #else
380 { L"Window Handle", DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
381 #endif
384 struct dbprop_pair
386 struct list entry;
387 BSTR name;
388 BSTR value;
391 struct dbprops
393 struct list props;
394 unsigned int count;
397 /* name/value strings are reused, so they shouldn't be freed after this call */
398 static HRESULT add_dbprop_to_list(struct dbprops *props, BSTR name, BSTR value)
400 struct dbprop_pair *pair;
402 pair = malloc(sizeof(*pair));
403 if (!pair)
404 return E_OUTOFMEMORY;
406 pair->name = name;
407 pair->value = value;
408 list_add_tail(&props->props, &pair->entry);
409 props->count++;
410 return S_OK;
413 static void free_dbprop_list(struct dbprops *props)
415 struct dbprop_pair *p, *p2;
416 LIST_FOR_EACH_ENTRY_SAFE(p, p2, &props->props, struct dbprop_pair, entry)
418 list_remove(&p->entry);
419 SysFreeString(p->name);
420 SysFreeString(p->value);
421 free(p);
425 static HRESULT parse_init_string(const WCHAR *initstring, struct dbprops *props)
427 const WCHAR *start;
428 WCHAR *eq;
429 HRESULT hr = S_OK;
431 list_init(&props->props);
432 props->count = 0;
434 start = initstring;
435 while (start && (eq = wcschr(start, '=')))
437 WCHAR *delim, quote;
438 BSTR value, name;
440 name = SysAllocStringLen(start, eq - start);
441 /* skip equal sign to get value */
442 eq++;
444 quote = (*eq == '"' || *eq == '\'') ? *eq : 0;
445 if (quote)
447 /* for quoted value string, skip opening mark, look for terminating one */
448 eq++;
449 delim = wcschr(eq, quote);
451 else
452 delim = wcschr(eq, ';');
454 if (delim)
455 value = SysAllocStringLen(eq, delim - eq);
456 else
457 value = SysAllocString(eq);
459 /* skip semicolon if present */
460 if (delim)
462 if (*delim == quote)
463 delim++;
464 if (*delim == ';')
465 delim++;
467 start = delim;
469 if (!wcsicmp(name, L"Provider"))
471 SysFreeString(name);
472 SysFreeString(value);
473 continue;
476 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
478 hr = add_dbprop_to_list(props, name, value);
479 if (FAILED(hr))
481 SysFreeString(name);
482 SysFreeString(value);
483 break;
487 if (FAILED(hr))
488 free_dbprop_list(props);
490 return hr;
493 static const struct dbproperty *get_known_dprop_descr(BSTR name)
495 int min, max, n;
497 min = 0;
498 max = ARRAY_SIZE(dbproperties) - 1;
500 while (min <= max)
502 int r;
504 n = (min+max)/2;
506 r = wcsicmp(dbproperties[n].name, name);
507 if (!r)
508 break;
510 if (r < 0)
511 min = n+1;
512 else
513 max = n-1;
516 return (min <= max) ? &dbproperties[n] : NULL;
519 static HRESULT get_dbpropset_from_proplist(struct dbprops *props, DBPROPSET **propset)
521 BSTR provider_string = NULL;
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 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
540 LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
542 const struct dbproperty *descr = get_known_dprop_descr(pair->name);
543 VARIANT dest, src;
545 if (!descr)
547 int len;
549 len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1; /* for '=' */
550 if (!provider_string)
552 provider_string = SysAllocStringLen(NULL, len);
554 else
556 BSTR old_string = provider_string;
558 len += SysStringLen(provider_string) + 1; /* for ';' separator */
559 provider_string = SysAllocStringLen(NULL, len);
560 lstrcpyW(provider_string, old_string);
561 lstrcatW(provider_string, L";");
562 SysFreeString(old_string);
564 lstrcatW(provider_string, pair->name);
565 lstrcatW(provider_string, L"=");
566 lstrcatW(provider_string, pair->value);
567 continue;
570 V_VT(&src) = VT_BSTR;
571 V_BSTR(&src) = pair->value;
573 VariantInit(&dest);
574 hr = VariantChangeType(&dest, &src, 0, descr->type);
575 if (FAILED(hr) && descr->convert_dbproperty)
576 hr = descr->convert_dbproperty(pair->value, &dest);
578 if (FAILED(hr))
580 ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
581 free_dbpropset(1, *propset);
582 *propset = NULL;
583 return hr;
586 (*propset)->cProperties++;
587 (*propset)->rgProperties[i].dwPropertyID = descr->id;
588 (*propset)->rgProperties[i].dwOptions = descr->options;
589 (*propset)->rgProperties[i].dwStatus = 0;
590 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
591 (*propset)->rgProperties[i].vValue = dest;
592 i++;
595 /* Provider specific property is always VT_BSTR */
596 /* DBPROP_INIT_PROVIDERSTRING should be specified only once. Otherwise, it will be considered as
597 * provider-specific rather than an initialization property */
598 if (provider_string)
600 (*propset)->cProperties++;
601 (*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
602 (*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
603 (*propset)->rgProperties[i].dwStatus = 0;
604 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
605 V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
606 V_BSTR(&(*propset)->rgProperties[i].vValue) = provider_string;
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 WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
624 LPWSTR strlower, sublower, r;
625 strlower = CharLowerW(wcsdup(str));
626 sublower = CharLowerW(wcsdup(sub));
627 r = wcsstr(strlower, sublower);
628 if (r)
629 r = (LPWSTR)str + (r - strlower);
630 free(strlower);
631 free(sublower);
632 return r;
635 HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
637 static const WCHAR providerW[] = L"Provider=";
638 BOOL datasource_created = FALSE;
639 IDBProperties *dbprops;
640 DBPROPSET *propset;
641 WCHAR *prov = NULL;
642 CLSID provclsid;
643 HRESULT hr;
646 /* first get provider name */
647 provclsid = IID_NULL;
648 if (initstring && (prov = strstriW(initstring, providerW)))
650 WCHAR *start, *progid;
651 int len;
653 prov += ARRAY_SIZE(providerW)-1;
654 start = prov;
655 while (*prov && *prov != ';')
656 ++prov;
657 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
659 len = prov - start;
660 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
661 if (!progid) return E_OUTOFMEMORY;
663 memcpy(progid, start, len*sizeof(WCHAR));
664 progid[len] = 0;
666 hr = CLSIDFromProgID(progid, &provclsid);
667 CoTaskMemFree(progid);
668 if (FAILED(hr))
670 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
671 return hr;
674 else
676 hr = CLSIDFromProgID(L"MSDASQL", &provclsid);
677 if (FAILED(hr))
678 ERR("ODBC provider for OLE DB not registered\n");
681 /* check for provider mismatch if it was specified in init string */
682 if (*datasource && prov)
684 DBPROPIDSET propidset;
685 DWORD prop;
686 CLSID initprov;
687 ULONG count;
689 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
690 if (FAILED(hr))
692 WARN("provider doesn't support IDBProperties\n");
693 return hr;
696 prop = DBPROP_INIT_DATASOURCE;
697 propidset.rgPropertyIDs = &prop;
698 propidset.cPropertyIDs = 1;
699 propidset.guidPropertySet = DBPROPSET_DBINIT;
700 count = 0;
701 propset = NULL;
702 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
703 IDBProperties_Release(dbprops);
704 if (FAILED(hr))
706 WARN("GetProperties failed for datasource, 0x%08lx\n", hr);
707 return hr;
710 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
711 initprov = IID_NULL;
712 hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
713 free_dbpropset(count, propset);
715 if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
718 if (!*datasource)
720 if (!IsEqualIID(&provclsid, &IID_NULL))
721 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
723 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
724 hr = create_db_init(datasource);
726 datasource_created = *datasource != NULL;
729 /* now set properties */
730 if (initstring)
732 struct dbprops props;
734 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
735 if (FAILED(hr))
737 ERR("provider doesn't support IDBProperties\n");
738 datasource_release(datasource_created, datasource);
739 return hr;
742 hr = parse_init_string(initstring, &props);
743 if (FAILED(hr))
745 datasource_release(datasource_created, datasource);
746 return hr;
749 hr = get_dbpropset_from_proplist(&props, &propset);
750 free_dbprop_list(&props);
751 if (FAILED(hr))
753 datasource_release(datasource_created, datasource);
754 return hr;
757 hr = IDBProperties_SetProperties(dbprops, 1, propset);
758 IDBProperties_Release(dbprops);
759 free_dbpropset(1, propset);
760 if (FAILED(hr))
762 ERR("SetProperties failed, 0x%08lx\n", hr);
763 datasource_release(datasource_created, datasource);
764 return hr;
768 return hr;
771 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
772 LPWSTR initstring, REFIID riid, IUnknown **datasource)
774 datainit *This = impl_from_IDataInitialize(iface);
776 TRACE("(%p)->(%p 0x%lx %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
778 return get_data_source(outer, clsctx, initstring, riid, datasource);
781 /* returns character length of string representation */
782 static int get_propvalue_length(DBPROP *prop)
784 VARIANT str;
785 HRESULT hr;
787 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
789 VariantInit(&str);
790 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
791 if (hr == S_OK)
793 int len = SysStringLen(V_BSTR(&str));
794 VariantClear(&str);
795 return len;
798 return 0;
801 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
803 VARIANT *v = &prop->vValue;
804 VARIANT vstr;
805 HRESULT hr;
807 if (V_VT(v) == VT_BSTR)
809 lstrcatW(str, V_BSTR(v));
810 return;
813 VariantInit(&vstr);
814 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
815 if (hr == S_OK)
817 lstrcatW(str, V_BSTR(&vstr));
818 VariantClear(&vstr);
822 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
824 ULONG i;
826 for (i = 0; i < propinfoset->cPropertyInfos; i++)
827 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
828 return propinfoset->rgPropertyInfos[i].pwszDescription;
830 return NULL;
833 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
835 ULONG i;
837 for (i = 0; i < count; i++)
839 ULONG p;
841 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
842 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
844 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
846 CoTaskMemFree(propinfoset);
849 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
850 boolean include_pass, LPWSTR *init_string)
852 static const WCHAR providerW[] = L"Provider=";
853 datainit *This = impl_from_IDataInitialize(iface);
854 DBPROPINFOSET *propinfoset;
855 IDBProperties *props;
856 DBPROPIDSET propidset;
857 ULONG count, infocount;
858 WCHAR *progid, *desc;
859 DBPROPSET *propset;
860 IPersist *persist;
861 HRESULT hr;
862 CLSID clsid;
863 ULONG i, len;
865 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
867 /* IPersist support is mandatory for data sources */
868 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
869 if (FAILED(hr)) return hr;
871 memset(&clsid, 0, sizeof(clsid));
872 hr = IPersist_GetClassID(persist, &clsid);
873 IPersist_Release(persist);
874 if (FAILED(hr)) return hr;
876 progid = NULL;
877 ProgIDFromCLSID(&clsid, &progid);
878 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
880 /* now get initialization properties */
881 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
882 if (FAILED(hr))
884 WARN("IDBProperties not supported\n");
885 CoTaskMemFree(progid);
886 return hr;
889 propidset.rgPropertyIDs = NULL;
890 propidset.cPropertyIDs = 0;
891 propidset.guidPropertySet = DBPROPSET_DBINIT;
892 propset = NULL;
893 count = 0;
894 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
895 if (FAILED(hr))
897 WARN("failed to get data source properties, 0x%08lx\n", hr);
898 CoTaskMemFree(progid);
899 return hr;
902 infocount = 0;
903 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
904 IDBProperties_Release(props);
906 /* check if we need to skip password */
907 len = lstrlenW(progid) + lstrlenW(providerW) + 1; /* including ';' */
908 for (i = 0; i < count; i++)
910 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
911 if (descr)
913 /* include '=' and ';' */
914 len += lstrlenW(descr) + 2;
915 len += get_propvalue_length(&propset->rgProperties[i]);
918 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
919 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
920 include_pass = FALSE;
923 len *= sizeof(WCHAR);
924 *init_string = CoTaskMemAlloc(len);
925 *init_string[0] = 0;
927 /* provider name */
928 lstrcatW(*init_string, providerW);
929 lstrcatW(*init_string, progid);
930 lstrcatW(*init_string, L";");
931 CoTaskMemFree(progid);
933 for (i = 0; i < count; i++)
935 WCHAR *descr;
937 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
939 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
940 if (descr)
942 lstrcatW(*init_string, descr);
943 lstrcatW(*init_string, L"=");
944 write_propvalue_str(*init_string, &propset->rgProperties[i]);
945 lstrcatW(*init_string, L";");
949 free_dbpropset(count, propset);
950 free_dbpropinfoset(infocount, propinfoset);
951 CoTaskMemFree(desc);
953 if (!include_pass)
954 TRACE("%s\n", debugstr_w(*init_string));
955 return S_OK;
958 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
959 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
960 IUnknown **datasource)
962 datainit *This = impl_from_IDataInitialize(iface);
964 TRACE("(%p)->(%s %p 0x%08lx %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
965 debugstr_guid(riid), datasource);
967 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
970 static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
971 DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
973 datainit *This = impl_from_IDataInitialize(iface);
975 FIXME("(%p)->(%s %p %#lx %s %p %lu %p)\n", This, debugstr_guid(provider), outer, clsctx,
976 debugstr_w(reserved), server_info, cmq, results);
978 return E_NOTIMPL;
981 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
982 LPWSTR *ppwszInitializationString)
984 datainit *This = impl_from_IDataInitialize(iface);
986 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
988 return E_NOTIMPL;
991 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
992 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
994 datainit *This = impl_from_IDataInitialize(iface);
996 FIXME("(%p)->(%s %s %ld)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
998 return E_NOTIMPL;
1002 static const struct IDataInitializeVtbl datainit_vtbl =
1004 datainit_QueryInterface,
1005 datainit_AddRef,
1006 datainit_Release,
1007 datainit_GetDataSource,
1008 datainit_GetInitializationString,
1009 datainit_CreateDBInstance,
1010 datainit_CreateDBInstanceEx,
1011 datainit_LoadStringFromStorage,
1012 datainit_WriteStringToStorage
1015 HRESULT create_data_init(IUnknown *outer, void **obj)
1017 datainit *This;
1019 TRACE("(%p)\n", obj);
1021 if(outer) return CLASS_E_NOAGGREGATION;
1023 *obj = NULL;
1025 This = malloc(sizeof(*This));
1026 if(!This) return E_OUTOFMEMORY;
1028 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
1029 This->ref = 1;
1031 *obj = &This->IDataInitialize_iface;
1033 return S_OK;