makefiles: Generate explicit build rules for C source files from makedep.
[wine.git] / dlls / oledb32 / datainit.c
blob9fe3c4fae04f8683bd660666e799781ab807fe24
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
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "ole2.h"
28 #include "msdasc.h"
29 #include "oledberr.h"
30 #include "initguid.h"
31 #include "oledb_private.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
38 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
40 typedef struct datainit
42 IDataInitialize IDataInitialize_iface;
44 LONG ref;
45 } datainit;
47 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
49 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
52 typedef struct
54 IDBInitialize IDBInitialize_iface;
55 IDBProperties IDBProperties_iface;
57 LONG ref;
58 } dbinit;
60 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
62 return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
65 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
67 return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
70 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
72 dbinit *This = impl_from_IDBProperties(iface);
74 return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
77 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
79 dbinit *This = impl_from_IDBProperties(iface);
81 return IDBInitialize_AddRef(&This->IDBInitialize_iface);
84 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
86 dbinit *This = impl_from_IDBProperties(iface);
88 return IDBInitialize_Release(&This->IDBInitialize_iface);
91 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
92 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
94 dbinit *This = impl_from_IDBProperties(iface);
96 FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
98 return E_NOTIMPL;
101 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
102 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
103 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
105 dbinit *This = impl_from_IDBProperties(iface);
107 FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
108 prgPropertyInfoSets, ppDescBuffer);
110 return E_NOTIMPL;
113 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
114 DBPROPSET rgPropertySets[])
116 dbinit *This = impl_from_IDBProperties(iface);
118 FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
120 return E_NOTIMPL;
123 static const struct IDBPropertiesVtbl dbprops_vtbl =
125 dbprops_QueryInterface,
126 dbprops_AddRef,
127 dbprops_Release,
128 dbprops_GetProperties,
129 dbprops_GetPropertyInfo,
130 dbprops_SetProperties
133 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
135 dbinit *This = impl_from_IDBInitialize(iface);
136 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
138 *obj = NULL;
140 if(IsEqualIID(riid, &IID_IUnknown) ||
141 IsEqualIID(riid, &IID_IDBInitialize))
143 *obj = iface;
145 else if(IsEqualIID(riid, &IID_IDBProperties))
147 *obj = &This->IDBProperties_iface;
149 else
151 FIXME("interface %s not implemented\n", debugstr_guid(riid));
152 return E_NOINTERFACE;
155 IDBInitialize_AddRef(iface);
156 return S_OK;
159 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
161 dbinit *This = impl_from_IDBInitialize(iface);
162 TRACE("(%p)\n", This);
164 return InterlockedIncrement(&This->ref);
167 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
169 dbinit *This = impl_from_IDBInitialize(iface);
170 LONG ref;
172 TRACE("(%p)\n", This);
174 ref = InterlockedDecrement(&This->ref);
175 if(ref == 0)
176 heap_free(This);
178 return ref;
181 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
183 dbinit *This = impl_from_IDBInitialize(iface);
185 FIXME("(%p) stub\n", This);
187 return S_OK;
190 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
192 dbinit *This = impl_from_IDBInitialize(iface);
194 FIXME("(%p) stub\n", This);
196 return S_OK;
199 static const IDBInitializeVtbl dbinit_vtbl =
201 dbinit_QueryInterface,
202 dbinit_AddRef,
203 dbinit_Release,
204 dbinit_Initialize,
205 dbinit_Uninitialize
208 static HRESULT create_db_init(void **obj)
210 dbinit *This;
212 TRACE("()\n");
214 *obj = NULL;
216 This = heap_alloc(sizeof(*This));
217 if(!This) return E_OUTOFMEMORY;
219 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
220 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
221 This->ref = 1;
223 *obj = &This->IDBInitialize_iface;
225 return S_OK;
228 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
230 datainit *This = impl_from_IDataInitialize(iface);
231 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
233 *obj = NULL;
235 if(IsEqualIID(riid, &IID_IUnknown) ||
236 IsEqualIID(riid, &IID_IDataInitialize))
238 *obj = &This->IDataInitialize_iface;
240 else
242 FIXME("interface %s not implemented\n", debugstr_guid(riid));
243 return E_NOINTERFACE;
246 IDataInitialize_AddRef(iface);
247 return S_OK;
250 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
252 datainit *This = impl_from_IDataInitialize(iface);
253 TRACE("(%p)\n", This);
255 return InterlockedIncrement(&This->ref);
258 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
260 datainit *This = impl_from_IDataInitialize(iface);
261 LONG ref;
263 TRACE("(%p)\n", This);
265 ref = InterlockedDecrement(&This->ref);
266 if(ref == 0)
267 heap_free(This);
269 return ref;
272 static void free_dbpropset(ULONG count, DBPROPSET *propset)
274 ULONG i;
276 for (i = 0; i < count; i++)
278 ULONG p;
280 for (p = 0; p < propset[i].cProperties; p++)
281 VariantClear(&propset[i].rgProperties[p].vValue);
283 CoTaskMemFree(propset[i].rgProperties);
285 CoTaskMemFree(propset);
288 struct dbproperty {
289 const WCHAR *name;
290 DBPROPID id;
291 DBPROPOPTIONS options;
292 VARTYPE type;
295 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};
296 static const WCHAR bindW[] = {'B','i','n','d',' ','F','l','a','g','s',0};
297 static const WCHAR cacheW[] = {'C','a','c','h','e',' ','A','u','t','h','e','n','i','c','a','t','i','o','n',0};
298 static const WCHAR conn_timeout[] = {'C','o','n','n','e','c','t',' ','T','i','m','e','o','u','t',0};
299 static const WCHAR datasourceW[] = {'D','a','t','a',' ','S','o','u','r','c','e',0};
300 static const WCHAR encryptW[] = {'E','n','c','r','y','p','t',' ','P','a','s','s','w','o','r','d',0};
301 static const WCHAR extendedW[] = {'E','x','t','e','n','d','e','d',' ','P','r','o','p','e','r','t','i','e','s',0};
302 static const WCHAR gen_timeout[] = {'G','e','n','e','r','a','l',' ','T','i','m','e','o','u','t',0};
303 static const WCHAR impersonW[] = {'I','m','p','e','r','s','o','n','a','t','i','o','n',' ','L','e','v','e','l',0};
304 static const WCHAR initcatW[] = {'I','n','i','t','i','a','l',' ','C','a','t','a','l','o','g',0};
305 static const WCHAR integratedW[] = {'I','n','t','e','g','r','a','t','e','d',' ','S','e','c','u','r','i','t','y',0};
306 static const WCHAR localeIDW[] = {'L','o','c','a','l','e',' ','I','d','e','n','t','i','f','i','e','r',0};
307 static const WCHAR locationW[] = {'L','o','c','a','t','i','o','n',0};
308 static const WCHAR lockownerW[] = {'L','o','c','k',' ','O','w','n','e','r',0};
309 static const WCHAR maskpassW[] = {'M','a','s','k',' ','P','a','s','s','w','o','r','d',0};
310 static const WCHAR modeW[] = {'M','o','d','e',0};
311 static const WCHAR oledbservW[] = {'O','L','E',' ','D','B',' ','S','e','r','v','i','c','i','e','s',0};
312 static const WCHAR passwordW[] = {'P','a','s','s','w','o','r','d',0};
313 static const WCHAR persistW[] = {'P','e','r','s','i','s','t',' ','S','e','c','u','r','i','t','y',' ','I','n','f','o',0};
314 static const WCHAR persistEncW[] = {'P','e','r','s','i','s','t',' ','E','n','c','r','y','p','t','e','d',0};
315 static const WCHAR promptW[] = {'P','r','o','m','p','t',0};
316 static const WCHAR protectW[] = {'P','r','o','t','e','c','t','i','o','n',' ','l','e','v','e','l',0};
317 static const WCHAR useridW[] = {'U','s','e','r',' ','I','D',0};
318 static const WCHAR winhandleW[] = {'W','i','n','d','o','w',' ','H','a','n','d','l','e',0};
320 static const struct dbproperty dbproperties[] = {
321 { asyncW, DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
322 { bindW, DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
323 { cacheW, DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
324 { conn_timeout,DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
325 { datasourceW, DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
326 { extendedW, DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
327 { encryptW, DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
328 { gen_timeout, DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
329 { impersonW, DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
330 { initcatW, DBPROP_CATALOGLOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
331 { integratedW, DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
332 { localeIDW, DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
333 { locationW, DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
334 { lockownerW, DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
335 { maskpassW, DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
336 { modeW, DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4 },
337 { oledbservW, DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
338 { passwordW, DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
339 { persistEncW, DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
340 { persistW, DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
341 { promptW, DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
342 { protectW, DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
343 { useridW, DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
344 #ifndef _WIN64
345 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
346 #else
347 { winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
348 #endif
352 static HRESULT set_dbpropset(BSTR name, BSTR value, DBPROPSET **propset)
354 VARIANT src, dest;
355 int min, max, n;
356 HRESULT hr;
358 min = 0;
359 max = sizeof(dbproperties)/sizeof(struct dbproperty) - 1;
361 while (min <= max)
363 int r;
365 n = (min+max)/2;
367 r = strcmpiW(dbproperties[n].name, name);
368 if (!r)
369 break;
371 if (r < 0)
372 min = n+1;
373 else
374 max = n-1;
377 if (min > max)
379 *propset = NULL;
380 FIXME("unsupported property %s\n", debugstr_w(name));
381 return E_FAIL;
384 VariantInit(&dest);
386 V_VT(&src) = VT_BSTR;
387 V_BSTR(&src) = value;
389 hr = VariantChangeType(&dest, &src, 0, dbproperties[n].type);
390 if (FAILED(hr))
392 ERR("failed to init property %s value as type %d\n", debugstr_w(name), dbproperties[n].type);
393 return hr;
396 *propset = CoTaskMemAlloc(sizeof(DBPROPSET));
397 if (!*propset)
399 VariantClear(&dest);
400 return E_OUTOFMEMORY;
403 (*propset)->rgProperties = CoTaskMemAlloc(sizeof(DBPROP));
404 if (!(*propset)->rgProperties)
406 VariantClear(&dest);
407 CoTaskMemFree(*propset);
408 return E_OUTOFMEMORY;
411 (*propset)->cProperties = 1;
412 (*propset)->guidPropertySet = DBPROPSET_DBINIT;
413 (*propset)->rgProperties[0].dwPropertyID = dbproperties[n].id;
414 (*propset)->rgProperties[0].dwOptions = dbproperties[n].options;
415 (*propset)->rgProperties[0].dwStatus = 0;
416 memset(&(*propset)->rgProperties[0].colid, 0, sizeof(DBID));
417 (*propset)->rgProperties[0].vValue = dest;
419 return S_OK;
422 /*** IDataInitialize methods ***/
423 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
424 LPWSTR initstring, REFIID riid, IUnknown **datasource)
426 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r','=',0};
427 static const WCHAR msdasqlW[] = {'M','S','D','A','S','Q','L',0};
428 datainit *This = impl_from_IDataInitialize(iface);
429 IDBProperties *dbprops;
430 DBPROPSET *propset;
431 WCHAR *prov = NULL;
432 CLSID provclsid;
433 HRESULT hr;
435 TRACE("(%p)->(%p 0x%x %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
437 /* first get provider name */
438 provclsid = IID_NULL;
439 if (initstring && (prov = strstrW(initstring, providerW)))
441 WCHAR *start, *progid;
442 int len;
444 prov += sizeof(providerW)/sizeof(WCHAR)-1;
445 start = prov;
446 while (*prov && *prov != ';')
447 ++prov;
448 TRACE("got provider %s\n", debugstr_wn(start, prov-start));
450 len = prov - start;
451 progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
452 if (!progid) return E_OUTOFMEMORY;
454 memcpy(progid, start, len*sizeof(WCHAR));
455 progid[len] = 0;
457 hr = CLSIDFromProgID(progid, &provclsid);
458 CoTaskMemFree(progid);
459 if (FAILED(hr))
461 ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
462 return hr;
465 else
467 hr = CLSIDFromProgID(msdasqlW, &provclsid);
468 if (FAILED(hr))
469 ERR("ODBC provider for OLE DB not registered\n");
472 /* check for provider mismatch if it was specified in init string */
473 if (*datasource && prov)
475 DBPROPIDSET propidset;
476 enum DBPROPENUM prop;
477 CLSID initprov;
478 ULONG count;
480 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
481 if (FAILED(hr))
483 WARN("provider doesn't support IDBProperties\n");
484 return hr;
487 prop = DBPROP_INIT_DATASOURCE;
488 propidset.rgPropertyIDs = &prop;
489 propidset.cPropertyIDs = 1;
490 propidset.guidPropertySet = DBPROPSET_DBINIT;
491 count = 0;
492 propset = NULL;
493 hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
494 IDBProperties_Release(dbprops);
495 if (FAILED(hr))
497 WARN("GetProperties failed for datasource, 0x%08x\n", hr);
498 return hr;
501 TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
502 initprov = IID_NULL;
503 CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
504 free_dbpropset(count, propset);
506 if (!IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
509 if (!*datasource)
511 if (!IsEqualIID(&provclsid, &IID_NULL))
512 hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
514 if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
515 hr = create_db_init((void**)datasource);
518 /* now set properties */
519 if (initstring)
521 WCHAR *eq, *start;
523 hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
524 if (FAILED(hr))
526 WARN("provider doesn't support IDBProperties\n");
527 return hr;
530 start = initstring;
531 while (start && (eq = strchrW(start, '=')))
533 static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r',0};
534 WCHAR *scol = strchrW(eq+1, ';');
535 BSTR value, name;
537 name = SysAllocStringLen(start, eq - start);
538 /* skip equal sign to get value */
539 eq++;
540 value = SysAllocStringLen(eq, scol ? scol - eq : -1);
542 /* skip semicolon if present */
543 if (scol) scol++;
544 start = scol;
546 if (!strcmpW(name, providerW))
548 SysFreeString(name);
549 SysFreeString(value);
550 continue;
553 TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
555 hr = set_dbpropset(name, value, &propset);
556 SysFreeString(name);
557 SysFreeString(value);
558 if (FAILED(hr)) return hr;
560 hr = IDBProperties_SetProperties(dbprops, 1, propset);
561 free_dbpropset(1, propset);
562 TRACE("provider ret 0x%08x\n", hr);
565 IDBProperties_Release(dbprops);
568 return hr;
571 /* returns character length of string representation */
572 static int get_propvalue_length(DBPROP *prop)
574 VARIANT str;
575 HRESULT hr;
577 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
579 VariantInit(&str);
580 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
581 if (hr == S_OK)
583 int len = SysStringLen(V_BSTR(&str));
584 VariantClear(&str);
585 return len;
588 return 0;
591 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
593 VARIANT *v = &prop->vValue;
594 VARIANT vstr;
595 HRESULT hr;
597 if (V_VT(v) == VT_BSTR)
599 strcatW(str, V_BSTR(v));
600 return;
603 VariantInit(&vstr);
604 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
605 if (hr == S_OK)
607 strcatW(str, V_BSTR(&vstr));
608 VariantClear(&vstr);
612 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
614 ULONG i;
616 for (i = 0; i < propinfoset->cPropertyInfos; i++)
617 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
618 return propinfoset->rgPropertyInfos[i].pwszDescription;
620 return NULL;
623 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
625 ULONG i;
627 for (i = 0; i < count; i++)
629 ULONG p;
631 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
632 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
634 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
636 CoTaskMemFree(propinfoset);
639 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
640 boolean include_pass, LPWSTR *init_string)
642 static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
643 static const WCHAR colW[] = {';',0};
644 datainit *This = impl_from_IDataInitialize(iface);
645 DBPROPINFOSET *propinfoset;
646 IDBProperties *props;
647 DBPROPIDSET propidset;
648 ULONG count, infocount;
649 WCHAR *progid, *desc;
650 DBPROPSET *propset;
651 IPersist *persist;
652 HRESULT hr;
653 CLSID clsid;
654 ULONG i, len;
656 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
658 /* IPersist support is mandatory for data sources */
659 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
660 if (FAILED(hr)) return hr;
662 memset(&clsid, 0, sizeof(clsid));
663 hr = IPersist_GetClassID(persist, &clsid);
664 IPersist_Release(persist);
665 if (FAILED(hr)) return hr;
667 progid = NULL;
668 ProgIDFromCLSID(&clsid, &progid);
669 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
671 /* now get initialization properties */
672 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
673 if (FAILED(hr))
675 WARN("IDBProperties not supported\n");
676 CoTaskMemFree(progid);
677 return hr;
680 propidset.rgPropertyIDs = NULL;
681 propidset.cPropertyIDs = 0;
682 propidset.guidPropertySet = DBPROPSET_DBINIT;
683 propset = NULL;
684 count = 0;
685 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
686 if (FAILED(hr))
688 WARN("failed to get data source properties, 0x%08x\n", hr);
689 CoTaskMemFree(progid);
690 return hr;
693 infocount = 0;
694 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
695 IDBProperties_Release(props);
697 /* check if we need to skip password */
698 len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
699 for (i = 0; i < count; i++)
701 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
702 if (descr)
704 /* include '=' and ';' */
705 len += strlenW(descr) + 2;
706 len += get_propvalue_length(&propset->rgProperties[i]);
709 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
710 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
711 include_pass = FALSE;
714 len *= sizeof(WCHAR);
715 *init_string = CoTaskMemAlloc(len);
716 *init_string[0] = 0;
718 /* provider name */
719 strcatW(*init_string, provW);
720 strcatW(*init_string, progid);
721 strcatW(*init_string, colW);
722 CoTaskMemFree(progid);
724 for (i = 0; i < count; i++)
726 WCHAR *descr;
728 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
730 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
731 if (descr)
733 static const WCHAR eqW[] = {'=',0};
734 strcatW(*init_string, descr);
735 strcatW(*init_string, eqW);
736 write_propvalue_str(*init_string, &propset->rgProperties[i]);
737 strcatW(*init_string, colW);
741 free_dbpropset(count, propset);
742 free_dbpropinfoset(infocount, propinfoset);
743 CoTaskMemFree(desc);
745 if (!include_pass)
746 TRACE("%s\n", debugstr_w(*init_string));
747 return S_OK;
750 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
751 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
752 IUnknown **datasource)
754 datainit *This = impl_from_IDataInitialize(iface);
756 TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
757 debugstr_guid(riid), datasource);
759 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
762 static HRESULT WINAPI datainit_RemoteCreateDBInstanceEx(IDataInitialize *iface, REFCLSID clsidProvider,
763 IUnknown *pUnkOuter, DWORD dwClsCtx, LPWSTR pwszReserved, COSERVERINFO *pServerInfo,
764 DWORD cmq, GUID **rgpIID, IUnknown **rgpItf, HRESULT *rghr)
766 datainit *This = impl_from_IDataInitialize(iface);
768 FIXME("(%p)->()\n", This);
770 return E_NOTIMPL;
773 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
774 LPWSTR *ppwszInitializationString)
776 datainit *This = impl_from_IDataInitialize(iface);
778 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
780 return E_NOTIMPL;
783 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
784 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
786 datainit *This = impl_from_IDataInitialize(iface);
788 FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
790 return E_NOTIMPL;
794 static const struct IDataInitializeVtbl datainit_vtbl =
796 datainit_QueryInterface,
797 datainit_AddRef,
798 datainit_Release,
799 datainit_GetDataSource,
800 datainit_GetInitializationString,
801 datainit_CreateDBInstance,
802 datainit_RemoteCreateDBInstanceEx,
803 datainit_LoadStringFromStorage,
804 datainit_WriteStringToStorage
807 HRESULT create_data_init(IUnknown *outer, void **obj)
809 datainit *This;
811 TRACE("(%p)\n", obj);
813 if(outer) return CLASS_E_NOAGGREGATION;
815 *obj = NULL;
817 This = heap_alloc(sizeof(*This));
818 if(!This) return E_OUTOFMEMORY;
820 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
821 This->ref = 1;
823 *obj = &This->IDataInitialize_iface;
825 return S_OK;