wined3d: Introduce a wined3d_state_get_ffp_texture() helper.
[wine.git] / dlls / oledb32 / datainit.c
blob3694ac67fec643ec4528806c95d59ded4cb7b386
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(DBGUID_SESSION, 0xc8b522f5, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
37 DEFINE_GUID(DBGUID_ROWSET, 0xc8b522f6, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
38 DEFINE_GUID(DBGUID_ROW, 0xc8b522f7, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
39 DEFINE_GUID(DBGUID_STREAM, 0xc8b522f9, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
41 DEFINE_GUID(DBPROPSET_COLUMN, 0xc8b522b9, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
42 DEFINE_GUID(DBPROPSET_DATASOURCE, 0xc8b522ba, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
43 DEFINE_GUID(DBPROPSET_DATASOURCEINFO, 0xc8b522bb, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
44 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
45 DEFINE_GUID(DBPROPSET_INDEX, 0xc8b522bd, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
46 DEFINE_GUID(DBPROPSET_ROWSET, 0xc8b522be, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
47 DEFINE_GUID(DBPROPSET_TABLE, 0xc8b522bf, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
48 DEFINE_GUID(DBPROPSET_DATASOURCEALL, 0xc8b522c0, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
49 DEFINE_GUID(DBPROPSET_DATASOURCEINFOALL, 0xc8b522c1, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
50 DEFINE_GUID(DBPROPSET_ROWSETALL, 0xc8b522c2, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
51 DEFINE_GUID(DBPROPSET_SESSION, 0xc8b522c6, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
52 DEFINE_GUID(DBPROPSET_SESSIONALL, 0xc8b522c7, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
53 DEFINE_GUID(DBPROPSET_DBINITALL, 0xc8b522ca, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
54 DEFINE_GUID(DBPROPSET_PROPERTIESINERROR, 0xc8b522d4, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
56 typedef struct datainit
58 IDataInitialize IDataInitialize_iface;
60 LONG ref;
61 } datainit;
63 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
65 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
68 typedef struct
70 IDBInitialize IDBInitialize_iface;
71 IDBProperties IDBProperties_iface;
73 LONG ref;
74 } dbinit;
76 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
78 return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
81 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
83 return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
86 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
88 dbinit *This = impl_from_IDBProperties(iface);
90 return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
93 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
95 dbinit *This = impl_from_IDBProperties(iface);
97 return IDBInitialize_AddRef(&This->IDBInitialize_iface);
100 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
102 dbinit *This = impl_from_IDBProperties(iface);
104 return IDBInitialize_Release(&This->IDBInitialize_iface);
107 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
108 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
110 dbinit *This = impl_from_IDBProperties(iface);
112 FIXME("(%p)->(%ld %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
114 return E_NOTIMPL;
117 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
118 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
119 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
121 dbinit *This = impl_from_IDBProperties(iface);
123 FIXME("(%p)->(%ld %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
124 prgPropertyInfoSets, ppDescBuffer);
126 return E_NOTIMPL;
129 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
130 DBPROPSET rgPropertySets[])
132 dbinit *This = impl_from_IDBProperties(iface);
134 FIXME("(%p)->(%ld %p)\n", This, cPropertySets, rgPropertySets);
136 return E_NOTIMPL;
139 static const struct IDBPropertiesVtbl dbprops_vtbl =
141 dbprops_QueryInterface,
142 dbprops_AddRef,
143 dbprops_Release,
144 dbprops_GetProperties,
145 dbprops_GetPropertyInfo,
146 dbprops_SetProperties
149 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
151 dbinit *This = impl_from_IDBInitialize(iface);
152 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
154 *obj = NULL;
156 if(IsEqualIID(riid, &IID_IUnknown) ||
157 IsEqualIID(riid, &IID_IDBInitialize))
159 *obj = iface;
161 else if(IsEqualIID(riid, &IID_IDBProperties))
163 *obj = &This->IDBProperties_iface;
165 else
167 FIXME("interface %s not implemented\n", debugstr_guid(riid));
168 return E_NOINTERFACE;
171 IDBInitialize_AddRef(iface);
172 return S_OK;
175 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
177 dbinit *This = impl_from_IDBInitialize(iface);
178 TRACE("(%p)\n", This);
180 return InterlockedIncrement(&This->ref);
183 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
185 dbinit *This = impl_from_IDBInitialize(iface);
186 LONG ref;
188 TRACE("(%p)\n", This);
190 ref = InterlockedDecrement(&This->ref);
191 if(ref == 0)
192 free(This);
194 return ref;
197 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
199 dbinit *This = impl_from_IDBInitialize(iface);
201 FIXME("(%p) stub\n", This);
203 return S_OK;
206 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
208 dbinit *This = impl_from_IDBInitialize(iface);
210 FIXME("(%p) stub\n", This);
212 return S_OK;
215 static const IDBInitializeVtbl dbinit_vtbl =
217 dbinit_QueryInterface,
218 dbinit_AddRef,
219 dbinit_Release,
220 dbinit_Initialize,
221 dbinit_Uninitialize
224 static HRESULT create_db_init(IUnknown **obj)
226 dbinit *This;
228 TRACE("()\n");
230 *obj = NULL;
232 This = malloc(sizeof(*This));
233 if(!This) return E_OUTOFMEMORY;
235 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
236 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
237 This->ref = 1;
239 *obj = (IUnknown*)&This->IDBInitialize_iface;
241 return S_OK;
244 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
246 datainit *This = impl_from_IDataInitialize(iface);
247 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
249 *obj = NULL;
251 if(IsEqualIID(riid, &IID_IUnknown) ||
252 IsEqualIID(riid, &IID_IDataInitialize))
254 *obj = &This->IDataInitialize_iface;
256 else
258 FIXME("interface %s not implemented\n", debugstr_guid(riid));
259 return E_NOINTERFACE;
262 IDataInitialize_AddRef(iface);
263 return S_OK;
266 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
268 datainit *This = impl_from_IDataInitialize(iface);
269 TRACE("(%p)\n", This);
271 return InterlockedIncrement(&This->ref);
274 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
276 datainit *This = impl_from_IDataInitialize(iface);
277 LONG ref;
279 TRACE("(%p)\n", This);
281 ref = InterlockedDecrement(&This->ref);
282 if(ref == 0)
283 free(This);
285 return ref;
288 static void free_dbpropset(ULONG count, DBPROPSET *propset)
290 ULONG i;
292 for (i = 0; i < count; i++)
294 ULONG p;
296 for (p = 0; p < propset[i].cProperties; p++)
297 VariantClear(&propset[i].rgProperties[p].vValue);
299 CoTaskMemFree(propset[i].rgProperties);
301 CoTaskMemFree(propset);
304 struct dbproperty {
305 const WCHAR *name;
306 DBPROPID id;
307 DBPROPOPTIONS options;
308 VARTYPE type;
309 HRESULT (*convert_dbproperty)(const WCHAR *src, VARIANT *dest);
312 struct mode_propval
314 const WCHAR *name;
315 DWORD value;
318 static int __cdecl dbmodeprop_compare(const void *a, const void *b)
320 const WCHAR *src = a;
321 const struct mode_propval *propval = b;
322 return wcsicmp(src, propval->name);
325 static HRESULT convert_dbproperty_mode(const WCHAR *src, VARIANT *dest)
327 static const struct mode_propval mode_propvals[] =
329 { L"Read", DB_MODE_READ },
330 { L"ReadWrite", DB_MODE_READWRITE },
331 { L"Share Deny None", DB_MODE_SHARE_DENY_NONE },
332 { L"Share Deny Read", DB_MODE_SHARE_DENY_READ },
333 { L"Share Deny Write", DB_MODE_SHARE_DENY_WRITE },
334 { L"Share Exclusive", DB_MODE_SHARE_EXCLUSIVE },
335 { L"Write", DB_MODE_WRITE },
337 struct mode_propval *prop;
339 if ((prop = bsearch(src, mode_propvals, ARRAY_SIZE(mode_propvals),
340 sizeof(struct mode_propval), dbmodeprop_compare)))
342 V_VT(dest) = VT_I4;
343 V_I4(dest) = prop->value;
344 TRACE("%s = %#lx\n", debugstr_w(src), prop->value);
345 return S_OK;
348 return E_FAIL;
351 static const struct dbproperty dbproperties[] =
353 { L"Asynchronous Processing", DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
354 { L"Bind Flags", DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
355 { L"Cache Authentication", DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
356 { L"Connect Timeout", DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
357 { L"Data Source", DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
358 { L"Extended Properties", DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
359 { L"Encrypt Password", DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
360 { L"General Timeout", DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
361 { L"Impersonation Level", DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
362 { L"Initial Catalog", DBPROP_INIT_CATALOG, DBPROPOPTIONS_REQUIRED, VT_BSTR },
363 { L"Integrated Security", DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
364 { L"Locale Identifier", DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
365 { L"Location", DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
366 { L"Lock Owner", DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
367 { L"Mask Password", DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
368 { L"Mode", DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4, convert_dbproperty_mode },
369 { L"OLE DB Services", DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
370 { L"Password", DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
371 { L"Persist Encrypted", DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
372 { L"Persist Security Info", DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
373 { L"Prompt", DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
374 { L"Protection level", DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
375 { L"User ID", DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
376 #ifndef _WIN64
377 { L"Window Handle", DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
378 #else
379 { L"Window Handle", DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
380 #endif
383 struct dbprop_pair
385 struct list entry;
386 BSTR name;
387 BSTR value;
390 struct dbprops
392 struct list props;
393 unsigned int count;
396 /* name/value strings are reused, so they shouldn't be freed after this call */
397 static HRESULT add_dbprop_to_list(struct dbprops *props, BSTR name, BSTR value)
399 struct dbprop_pair *pair;
401 pair = malloc(sizeof(*pair));
402 if (!pair)
403 return E_OUTOFMEMORY;
405 pair->name = name;
406 pair->value = value;
407 list_add_tail(&props->props, &pair->entry);
408 props->count++;
409 return S_OK;
412 static void free_dbprop_list(struct dbprops *props)
414 struct dbprop_pair *p, *p2;
415 LIST_FOR_EACH_ENTRY_SAFE(p, p2, &props->props, struct dbprop_pair, entry)
417 list_remove(&p->entry);
418 SysFreeString(p->name);
419 SysFreeString(p->value);
420 free(p);
424 static HRESULT parse_init_string(const WCHAR *initstring, struct dbprops *props)
426 const WCHAR *start;
427 WCHAR *eq;
428 HRESULT hr = S_OK;
430 list_init(&props->props);
431 props->count = 0;
433 start = initstring;
434 while (start && (eq = wcschr(start, '=')))
436 WCHAR *delim, quote;
437 BSTR value, name;
439 name = SysAllocStringLen(start, eq - start);
440 /* skip equal sign to get value */
441 eq++;
443 quote = (*eq == '"' || *eq == '\'') ? *eq : 0;
444 if (quote)
446 /* for quoted value string, skip opening mark, look for terminating one */
447 eq++;
448 delim = wcschr(eq, quote);
450 else
451 delim = wcschr(eq, ';');
453 if (delim)
454 value = SysAllocStringLen(eq, delim - eq);
455 else
456 value = SysAllocString(eq);
458 /* skip semicolon if present */
459 if (delim)
461 if (*delim == quote)
462 delim++;
463 if (*delim == ';')
464 delim++;
466 start = delim;
468 if (!wcsicmp(name, L"Provider"))
470 SysFreeString(name);
471 SysFreeString(value);
472 continue;
475 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
477 hr = add_dbprop_to_list(props, name, value);
478 if (FAILED(hr))
480 SysFreeString(name);
481 SysFreeString(value);
482 break;
486 if (FAILED(hr))
487 free_dbprop_list(props);
489 return hr;
492 static const struct dbproperty *get_known_dprop_descr(BSTR name)
494 int min, max, n;
496 min = 0;
497 max = ARRAY_SIZE(dbproperties) - 1;
499 while (min <= max)
501 int r;
503 n = (min+max)/2;
505 r = wcsicmp(dbproperties[n].name, name);
506 if (!r)
507 break;
509 if (r < 0)
510 min = n+1;
511 else
512 max = n-1;
515 return (min <= max) ? &dbproperties[n] : NULL;
518 static HRESULT get_dbpropset_from_proplist(struct dbprops *props, DBPROPSET **propset)
520 BSTR provider_string = NULL;
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 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
539 LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
541 const struct dbproperty *descr = get_known_dprop_descr(pair->name);
542 VARIANT dest, src;
544 if (!descr)
546 int len;
548 len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1; /* for '=' */
549 if (!provider_string)
551 provider_string = SysAllocStringLen(NULL, len);
553 else
555 BSTR old_string = provider_string;
557 len += SysStringLen(provider_string) + 1; /* for ';' separator */
558 provider_string = SysAllocStringLen(NULL, len);
559 lstrcpyW(provider_string, old_string);
560 lstrcatW(provider_string, L";");
561 SysFreeString(old_string);
563 lstrcatW(provider_string, pair->name);
564 lstrcatW(provider_string, L"=");
565 lstrcatW(provider_string, pair->value);
566 continue;
569 V_VT(&src) = VT_BSTR;
570 V_BSTR(&src) = pair->value;
572 VariantInit(&dest);
573 hr = VariantChangeType(&dest, &src, 0, descr->type);
574 if (FAILED(hr) && descr->convert_dbproperty)
575 hr = descr->convert_dbproperty(pair->value, &dest);
577 if (FAILED(hr))
579 ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
580 free_dbpropset(1, *propset);
581 *propset = NULL;
582 return hr;
585 (*propset)->cProperties++;
586 (*propset)->rgProperties[i].dwPropertyID = descr->id;
587 (*propset)->rgProperties[i].dwOptions = descr->options;
588 (*propset)->rgProperties[i].dwStatus = 0;
589 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
590 (*propset)->rgProperties[i].vValue = dest;
591 i++;
594 /* Provider specific property is always VT_BSTR */
595 /* DBPROP_INIT_PROVIDERSTRING should be specified only once. Otherwise, it will be considered as
596 * provider-specific rather than an initialization property */
597 if (provider_string)
599 (*propset)->cProperties++;
600 (*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
601 (*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
602 (*propset)->rgProperties[i].dwStatus = 0;
603 memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
604 V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
605 V_BSTR(&(*propset)->rgProperties[i].vValue) = provider_string;
608 return S_OK;
611 /*** IDataInitialize methods ***/
612 static void datasource_release(BOOL created, IUnknown **datasource)
614 if (!created)
615 return;
617 IUnknown_Release(*datasource);
618 *datasource = NULL;
621 static WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
623 LPWSTR strlower, sublower, r;
624 strlower = CharLowerW(wcsdup(str));
625 sublower = CharLowerW(wcsdup(sub));
626 r = wcsstr(strlower, sublower);
627 if (r)
628 r = (LPWSTR)str + (r - strlower);
629 free(strlower);
630 free(sublower);
631 return r;
634 HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
636 static const WCHAR providerW[] = L"Provider=";
637 BOOL datasource_created = FALSE;
638 IDBProperties *dbprops;
639 DBPROPSET *propset;
640 WCHAR *prov = NULL;
641 CLSID provclsid;
642 HRESULT hr;
645 /* first get provider name */
646 provclsid = IID_NULL;
647 if (initstring && (prov = strstriW(initstring, providerW)))
649 WCHAR *start, *progid;
650 int len;
652 prov += ARRAY_SIZE(providerW)-1;
653 start = prov;
654 while (*prov && *prov != ';')
655 ++prov;
656 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
658 len = prov - start;
659 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
660 if (!progid) return E_OUTOFMEMORY;
662 memcpy(progid, start, len*sizeof(WCHAR));
663 progid[len] = 0;
665 hr = CLSIDFromProgID(progid, &provclsid);
666 CoTaskMemFree(progid);
667 if (FAILED(hr))
669 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
670 return hr;
673 else
675 hr = CLSIDFromProgID(L"MSDASQL", &provclsid);
676 if (FAILED(hr))
677 ERR("ODBC provider for OLE DB not registered\n");
680 /* check for provider mismatch if it was specified in init string */
681 if (*datasource && prov)
683 DBPROPIDSET propidset;
684 DWORD prop;
685 CLSID initprov;
686 ULONG count;
688 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
689 if (FAILED(hr))
691 WARN("provider doesn't support IDBProperties\n");
692 return hr;
695 prop = DBPROP_INIT_DATASOURCE;
696 propidset.rgPropertyIDs = &prop;
697 propidset.cPropertyIDs = 1;
698 propidset.guidPropertySet = DBPROPSET_DBINIT;
699 count = 0;
700 propset = NULL;
701 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
702 IDBProperties_Release(dbprops);
703 if (FAILED(hr))
705 WARN("GetProperties failed for datasource, 0x%08lx\n", hr);
706 return hr;
709 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
710 initprov = IID_NULL;
711 hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
712 free_dbpropset(count, propset);
714 if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
717 if (!*datasource)
719 if (!IsEqualIID(&provclsid, &IID_NULL))
720 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
722 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
723 hr = create_db_init(datasource);
725 datasource_created = *datasource != NULL;
728 /* now set properties */
729 if (initstring)
731 struct dbprops props;
733 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
734 if (FAILED(hr))
736 ERR("provider doesn't support IDBProperties\n");
737 datasource_release(datasource_created, datasource);
738 return hr;
741 hr = parse_init_string(initstring, &props);
742 if (FAILED(hr))
744 datasource_release(datasource_created, datasource);
745 return hr;
748 hr = get_dbpropset_from_proplist(&props, &propset);
749 free_dbprop_list(&props);
750 if (FAILED(hr))
752 datasource_release(datasource_created, datasource);
753 return hr;
756 hr = IDBProperties_SetProperties(dbprops, 1, propset);
757 IDBProperties_Release(dbprops);
758 free_dbpropset(1, propset);
759 if (FAILED(hr))
761 ERR("SetProperties failed, 0x%08lx\n", hr);
762 datasource_release(datasource_created, datasource);
763 return hr;
767 return hr;
770 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
771 LPWSTR initstring, REFIID riid, IUnknown **datasource)
773 datainit *This = impl_from_IDataInitialize(iface);
775 TRACE("(%p)->(%p 0x%lx %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
777 return get_data_source(outer, clsctx, initstring, riid, datasource);
780 /* returns character length of string representation */
781 static int get_propvalue_length(DBPROP *prop)
783 VARIANT str;
784 HRESULT hr;
786 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
788 VariantInit(&str);
789 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
790 if (hr == S_OK)
792 int len = SysStringLen(V_BSTR(&str));
793 VariantClear(&str);
794 return len;
797 return 0;
800 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
802 VARIANT *v = &prop->vValue;
803 VARIANT vstr;
804 HRESULT hr;
806 if (V_VT(v) == VT_BSTR)
808 lstrcatW(str, V_BSTR(v));
809 return;
812 VariantInit(&vstr);
813 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
814 if (hr == S_OK)
816 lstrcatW(str, V_BSTR(&vstr));
817 VariantClear(&vstr);
821 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
823 ULONG i;
825 for (i = 0; i < propinfoset->cPropertyInfos; i++)
826 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
827 return propinfoset->rgPropertyInfos[i].pwszDescription;
829 return NULL;
832 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
834 ULONG i;
836 for (i = 0; i < count; i++)
838 ULONG p;
840 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
841 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
843 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
845 CoTaskMemFree(propinfoset);
848 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
849 boolean include_pass, LPWSTR *init_string)
851 static const WCHAR providerW[] = L"Provider=";
852 datainit *This = impl_from_IDataInitialize(iface);
853 DBPROPINFOSET *propinfoset;
854 IDBProperties *props;
855 DBPROPIDSET propidset;
856 ULONG count, infocount;
857 WCHAR *progid, *desc;
858 DBPROPSET *propset;
859 IPersist *persist;
860 HRESULT hr;
861 CLSID clsid;
862 ULONG i, len;
864 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
866 /* IPersist support is mandatory for data sources */
867 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
868 if (FAILED(hr)) return hr;
870 memset(&clsid, 0, sizeof(clsid));
871 hr = IPersist_GetClassID(persist, &clsid);
872 IPersist_Release(persist);
873 if (FAILED(hr)) return hr;
875 progid = NULL;
876 ProgIDFromCLSID(&clsid, &progid);
877 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
879 /* now get initialization properties */
880 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
881 if (FAILED(hr))
883 WARN("IDBProperties not supported\n");
884 CoTaskMemFree(progid);
885 return hr;
888 propidset.rgPropertyIDs = NULL;
889 propidset.cPropertyIDs = 0;
890 propidset.guidPropertySet = DBPROPSET_DBINIT;
891 propset = NULL;
892 count = 0;
893 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
894 if (FAILED(hr))
896 WARN("failed to get data source properties, 0x%08lx\n", hr);
897 CoTaskMemFree(progid);
898 return hr;
901 infocount = 0;
902 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
903 IDBProperties_Release(props);
905 /* check if we need to skip password */
906 len = lstrlenW(progid) + lstrlenW(providerW) + 1; /* including ';' */
907 for (i = 0; i < count; i++)
909 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
910 if (descr)
912 /* include '=' and ';' */
913 len += lstrlenW(descr) + 2;
914 len += get_propvalue_length(&propset->rgProperties[i]);
917 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
918 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
919 include_pass = FALSE;
922 len *= sizeof(WCHAR);
923 *init_string = CoTaskMemAlloc(len);
924 *init_string[0] = 0;
926 /* provider name */
927 lstrcatW(*init_string, providerW);
928 lstrcatW(*init_string, progid);
929 lstrcatW(*init_string, L";");
930 CoTaskMemFree(progid);
932 for (i = 0; i < count; i++)
934 WCHAR *descr;
936 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
938 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
939 if (descr)
941 lstrcatW(*init_string, descr);
942 lstrcatW(*init_string, L"=");
943 write_propvalue_str(*init_string, &propset->rgProperties[i]);
944 lstrcatW(*init_string, L";");
948 free_dbpropset(count, propset);
949 free_dbpropinfoset(infocount, propinfoset);
950 CoTaskMemFree(desc);
952 if (!include_pass)
953 TRACE("%s\n", debugstr_w(*init_string));
954 return S_OK;
957 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
958 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
959 IUnknown **datasource)
961 datainit *This = impl_from_IDataInitialize(iface);
963 TRACE("(%p)->(%s %p 0x%08lx %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
964 debugstr_guid(riid), datasource);
966 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
969 static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
970 DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
972 datainit *This = impl_from_IDataInitialize(iface);
974 FIXME("(%p)->(%s %p %#lx %s %p %lu %p)\n", This, debugstr_guid(provider), outer, clsctx,
975 debugstr_w(reserved), server_info, cmq, results);
977 return E_NOTIMPL;
980 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
981 LPWSTR *ppwszInitializationString)
983 datainit *This = impl_from_IDataInitialize(iface);
985 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
987 return E_NOTIMPL;
990 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
991 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
993 datainit *This = impl_from_IDataInitialize(iface);
995 FIXME("(%p)->(%s %s %ld)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
997 return E_NOTIMPL;
1001 static const struct IDataInitializeVtbl datainit_vtbl =
1003 datainit_QueryInterface,
1004 datainit_AddRef,
1005 datainit_Release,
1006 datainit_GetDataSource,
1007 datainit_GetInitializationString,
1008 datainit_CreateDBInstance,
1009 datainit_CreateDBInstanceEx,
1010 datainit_LoadStringFromStorage,
1011 datainit_WriteStringToStorage
1014 HRESULT create_data_init(IUnknown *outer, void **obj)
1016 datainit *This;
1018 TRACE("(%p)\n", obj);
1020 if(outer) return CLASS_E_NOAGGREGATION;
1022 *obj = NULL;
1024 This = malloc(sizeof(*This));
1025 if(!This) return E_OUTOFMEMORY;
1027 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
1028 This->ref = 1;
1030 *obj = &This->IDataInitialize_iface;
1032 return S_OK;