oledb32: Use the correct property for initial catalog.
[wine.git] / dlls / oledb32 / datainit.c
blob87a05638ffe2e0216be713d85d932d9f72f1463e
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 struct dbprop_pair *pair;
522 int i = 0;
523 HRESULT hr;
525 *propset = CoTaskMemAlloc(sizeof(DBPROPSET));
526 if (!*propset)
527 return E_OUTOFMEMORY;
529 (*propset)->rgProperties = CoTaskMemAlloc(props->count*sizeof(DBPROP));
530 if (!(*propset)->rgProperties)
532 CoTaskMemFree(*propset);
533 *propset = NULL;
534 return E_OUTOFMEMORY;
537 (*propset)->cProperties = 0;
538 LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
540 const struct dbproperty *descr = get_known_dprop_descr(pair->name);
541 VARIANT dest, src;
543 if (!descr)
545 BSTR str;
546 int len;
548 /* provider specific property is always VT_BSTR */
549 len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1 /* for '=' */;
550 str = SysAllocStringLen(NULL, len);
551 lstrcpyW(str, pair->name);
552 lstrcatW(str, L"=");
553 lstrcatW(str, pair->value);
555 (*propset)->cProperties++;
556 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
557 (*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
558 (*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
559 (*propset)->rgProperties[i].dwStatus = 0;
560 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
561 V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
562 V_BSTR(&(*propset)->rgProperties[i].vValue) = str;
563 i++;
564 continue;
567 V_VT(&src) = VT_BSTR;
568 V_BSTR(&src) = pair->value;
570 VariantInit(&dest);
571 hr = VariantChangeType(&dest, &src, 0, descr->type);
572 if (FAILED(hr) && descr->convert_dbproperty)
573 hr = descr->convert_dbproperty(pair->value, &dest);
575 if (FAILED(hr))
577 ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
578 free_dbpropset(1, *propset);
579 *propset = NULL;
580 return hr;
583 (*propset)->cProperties++;
584 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
585 (*propset)->rgProperties[i].dwPropertyID = descr->id;
586 (*propset)->rgProperties[i].dwOptions = descr->options;
587 (*propset)->rgProperties[i].dwStatus = 0;
588 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
589 (*propset)->rgProperties[i].vValue = dest;
590 i++;
593 return S_OK;
596 /*** IDataInitialize methods ***/
597 static void datasource_release(BOOL created, IUnknown **datasource)
599 if (!created)
600 return;
602 IUnknown_Release(*datasource);
603 *datasource = NULL;
606 static WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
608 LPWSTR strlower, sublower, r;
609 strlower = CharLowerW(wcsdup(str));
610 sublower = CharLowerW(wcsdup(sub));
611 r = wcsstr(strlower, sublower);
612 if (r)
613 r = (LPWSTR)str + (r - strlower);
614 free(strlower);
615 free(sublower);
616 return r;
619 HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
621 static const WCHAR providerW[] = L"Provider=";
622 BOOL datasource_created = FALSE;
623 IDBProperties *dbprops;
624 DBPROPSET *propset;
625 WCHAR *prov = NULL;
626 CLSID provclsid;
627 HRESULT hr;
630 /* first get provider name */
631 provclsid = IID_NULL;
632 if (initstring && (prov = strstriW(initstring, providerW)))
634 WCHAR *start, *progid;
635 int len;
637 prov += ARRAY_SIZE(providerW)-1;
638 start = prov;
639 while (*prov && *prov != ';')
640 ++prov;
641 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
643 len = prov - start;
644 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
645 if (!progid) return E_OUTOFMEMORY;
647 memcpy(progid, start, len*sizeof(WCHAR));
648 progid[len] = 0;
650 hr = CLSIDFromProgID(progid, &provclsid);
651 CoTaskMemFree(progid);
652 if (FAILED(hr))
654 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
655 return hr;
658 else
660 hr = CLSIDFromProgID(L"MSDASQL", &provclsid);
661 if (FAILED(hr))
662 ERR("ODBC provider for OLE DB not registered\n");
665 /* check for provider mismatch if it was specified in init string */
666 if (*datasource && prov)
668 DBPROPIDSET propidset;
669 DWORD prop;
670 CLSID initprov;
671 ULONG count;
673 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
674 if (FAILED(hr))
676 WARN("provider doesn't support IDBProperties\n");
677 return hr;
680 prop = DBPROP_INIT_DATASOURCE;
681 propidset.rgPropertyIDs = &prop;
682 propidset.cPropertyIDs = 1;
683 propidset.guidPropertySet = DBPROPSET_DBINIT;
684 count = 0;
685 propset = NULL;
686 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
687 IDBProperties_Release(dbprops);
688 if (FAILED(hr))
690 WARN("GetProperties failed for datasource, 0x%08lx\n", hr);
691 return hr;
694 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
695 initprov = IID_NULL;
696 hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
697 free_dbpropset(count, propset);
699 if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
702 if (!*datasource)
704 if (!IsEqualIID(&provclsid, &IID_NULL))
705 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
707 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
708 hr = create_db_init(datasource);
710 datasource_created = *datasource != NULL;
713 /* now set properties */
714 if (initstring)
716 struct dbprops props;
718 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
719 if (FAILED(hr))
721 ERR("provider doesn't support IDBProperties\n");
722 datasource_release(datasource_created, datasource);
723 return hr;
726 hr = parse_init_string(initstring, &props);
727 if (FAILED(hr))
729 datasource_release(datasource_created, datasource);
730 return hr;
733 hr = get_dbpropset_from_proplist(&props, &propset);
734 free_dbprop_list(&props);
735 if (FAILED(hr))
737 datasource_release(datasource_created, datasource);
738 return hr;
741 hr = IDBProperties_SetProperties(dbprops, 1, propset);
742 IDBProperties_Release(dbprops);
743 free_dbpropset(1, propset);
744 if (FAILED(hr))
746 ERR("SetProperties failed, 0x%08lx\n", hr);
747 datasource_release(datasource_created, datasource);
748 return hr;
752 return hr;
755 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
756 LPWSTR initstring, REFIID riid, IUnknown **datasource)
758 datainit *This = impl_from_IDataInitialize(iface);
760 TRACE("(%p)->(%p 0x%lx %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
762 return get_data_source(outer, clsctx, initstring, riid, datasource);
765 /* returns character length of string representation */
766 static int get_propvalue_length(DBPROP *prop)
768 VARIANT str;
769 HRESULT hr;
771 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
773 VariantInit(&str);
774 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
775 if (hr == S_OK)
777 int len = SysStringLen(V_BSTR(&str));
778 VariantClear(&str);
779 return len;
782 return 0;
785 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
787 VARIANT *v = &prop->vValue;
788 VARIANT vstr;
789 HRESULT hr;
791 if (V_VT(v) == VT_BSTR)
793 lstrcatW(str, V_BSTR(v));
794 return;
797 VariantInit(&vstr);
798 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
799 if (hr == S_OK)
801 lstrcatW(str, V_BSTR(&vstr));
802 VariantClear(&vstr);
806 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
808 ULONG i;
810 for (i = 0; i < propinfoset->cPropertyInfos; i++)
811 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
812 return propinfoset->rgPropertyInfos[i].pwszDescription;
814 return NULL;
817 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
819 ULONG i;
821 for (i = 0; i < count; i++)
823 ULONG p;
825 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
826 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
828 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
830 CoTaskMemFree(propinfoset);
833 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
834 boolean include_pass, LPWSTR *init_string)
836 static const WCHAR providerW[] = L"Provider=";
837 datainit *This = impl_from_IDataInitialize(iface);
838 DBPROPINFOSET *propinfoset;
839 IDBProperties *props;
840 DBPROPIDSET propidset;
841 ULONG count, infocount;
842 WCHAR *progid, *desc;
843 DBPROPSET *propset;
844 IPersist *persist;
845 HRESULT hr;
846 CLSID clsid;
847 ULONG i, len;
849 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
851 /* IPersist support is mandatory for data sources */
852 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
853 if (FAILED(hr)) return hr;
855 memset(&clsid, 0, sizeof(clsid));
856 hr = IPersist_GetClassID(persist, &clsid);
857 IPersist_Release(persist);
858 if (FAILED(hr)) return hr;
860 progid = NULL;
861 ProgIDFromCLSID(&clsid, &progid);
862 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
864 /* now get initialization properties */
865 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
866 if (FAILED(hr))
868 WARN("IDBProperties not supported\n");
869 CoTaskMemFree(progid);
870 return hr;
873 propidset.rgPropertyIDs = NULL;
874 propidset.cPropertyIDs = 0;
875 propidset.guidPropertySet = DBPROPSET_DBINIT;
876 propset = NULL;
877 count = 0;
878 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
879 if (FAILED(hr))
881 WARN("failed to get data source properties, 0x%08lx\n", hr);
882 CoTaskMemFree(progid);
883 return hr;
886 infocount = 0;
887 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
888 IDBProperties_Release(props);
890 /* check if we need to skip password */
891 len = lstrlenW(progid) + lstrlenW(providerW) + 1; /* including ';' */
892 for (i = 0; i < count; i++)
894 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
895 if (descr)
897 /* include '=' and ';' */
898 len += lstrlenW(descr) + 2;
899 len += get_propvalue_length(&propset->rgProperties[i]);
902 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
903 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
904 include_pass = FALSE;
907 len *= sizeof(WCHAR);
908 *init_string = CoTaskMemAlloc(len);
909 *init_string[0] = 0;
911 /* provider name */
912 lstrcatW(*init_string, providerW);
913 lstrcatW(*init_string, progid);
914 lstrcatW(*init_string, L";");
915 CoTaskMemFree(progid);
917 for (i = 0; i < count; i++)
919 WCHAR *descr;
921 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
923 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
924 if (descr)
926 lstrcatW(*init_string, descr);
927 lstrcatW(*init_string, L"=");
928 write_propvalue_str(*init_string, &propset->rgProperties[i]);
929 lstrcatW(*init_string, L";");
933 free_dbpropset(count, propset);
934 free_dbpropinfoset(infocount, propinfoset);
935 CoTaskMemFree(desc);
937 if (!include_pass)
938 TRACE("%s\n", debugstr_w(*init_string));
939 return S_OK;
942 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
943 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
944 IUnknown **datasource)
946 datainit *This = impl_from_IDataInitialize(iface);
948 TRACE("(%p)->(%s %p 0x%08lx %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
949 debugstr_guid(riid), datasource);
951 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
954 static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
955 DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
957 datainit *This = impl_from_IDataInitialize(iface);
959 FIXME("(%p)->(%s %p %#lx %s %p %lu %p)\n", This, debugstr_guid(provider), outer, clsctx,
960 debugstr_w(reserved), server_info, cmq, results);
962 return E_NOTIMPL;
965 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
966 LPWSTR *ppwszInitializationString)
968 datainit *This = impl_from_IDataInitialize(iface);
970 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
972 return E_NOTIMPL;
975 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
976 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
978 datainit *This = impl_from_IDataInitialize(iface);
980 FIXME("(%p)->(%s %s %ld)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
982 return E_NOTIMPL;
986 static const struct IDataInitializeVtbl datainit_vtbl =
988 datainit_QueryInterface,
989 datainit_AddRef,
990 datainit_Release,
991 datainit_GetDataSource,
992 datainit_GetInitializationString,
993 datainit_CreateDBInstance,
994 datainit_CreateDBInstanceEx,
995 datainit_LoadStringFromStorage,
996 datainit_WriteStringToStorage
999 HRESULT create_data_init(IUnknown *outer, void **obj)
1001 datainit *This;
1003 TRACE("(%p)\n", obj);
1005 if(outer) return CLASS_E_NOAGGREGATION;
1007 *obj = NULL;
1009 This = malloc(sizeof(*This));
1010 if(!This) return E_OUTOFMEMORY;
1012 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
1013 This->ref = 1;
1015 *obj = &This->IDataInitialize_iface;
1017 return S_OK;