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
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
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;
47 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
49 return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
54 IDBInitialize IDBInitialize_iface;
55 IDBProperties IDBProperties_iface;
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);
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);
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);
123 static const struct IDBPropertiesVtbl dbprops_vtbl =
125 dbprops_QueryInterface,
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);
140 if(IsEqualIID(riid, &IID_IUnknown) ||
141 IsEqualIID(riid, &IID_IDBInitialize))
145 else if(IsEqualIID(riid, &IID_IDBProperties))
147 *obj = &This->IDBProperties_iface;
151 FIXME("interface %s not implemented\n", debugstr_guid(riid));
152 return E_NOINTERFACE;
155 IDBInitialize_AddRef(iface);
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);
172 TRACE("(%p)\n", This);
174 ref = InterlockedDecrement(&This->ref);
177 HeapFree(GetProcessHeap(), 0, This);
183 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
185 dbinit *This = impl_from_IDBInitialize(iface);
187 FIXME("(%p) stub\n", This);
192 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
194 dbinit *This = impl_from_IDBInitialize(iface);
196 FIXME("(%p) stub\n", This);
201 static const IDBInitializeVtbl dbinit_vtbl =
203 dbinit_QueryInterface,
210 static HRESULT create_db_init(void **obj)
218 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
219 if(!This) return E_OUTOFMEMORY;
221 This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
222 This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
225 *obj = &This->IDBInitialize_iface;
230 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
232 datainit *This = impl_from_IDataInitialize(iface);
233 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
237 if(IsEqualIID(riid, &IID_IUnknown) ||
238 IsEqualIID(riid, &IID_IDataInitialize))
240 *obj = &This->IDataInitialize_iface;
244 FIXME("interface %s not implemented\n", debugstr_guid(riid));
245 return E_NOINTERFACE;
248 IDataInitialize_AddRef(iface);
252 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
254 datainit *This = impl_from_IDataInitialize(iface);
255 TRACE("(%p)\n", This);
257 return InterlockedIncrement(&This->ref);
260 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
262 datainit *This = impl_from_IDataInitialize(iface);
265 TRACE("(%p)\n", This);
267 ref = InterlockedDecrement(&This->ref);
270 HeapFree(GetProcessHeap(), 0, This);
276 /*** IDataInitialize methods ***/
277 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *pUnkOuter, DWORD dwClsCtx,
278 LPWSTR pwszInitializationString, REFIID riid, IUnknown **ppDataSource)
280 datainit *This = impl_from_IDataInitialize(iface);
282 FIXME("(%p)->(%p %d %s %s %p)\n", This, pUnkOuter, dwClsCtx, debugstr_w(pwszInitializationString),
283 debugstr_guid(riid), ppDataSource);
285 if(IsEqualIID(riid, &IID_IDBInitialize))
287 return create_db_init( (LPVOID*)ppDataSource);
293 /* returns character length of string representation */
294 static int get_propvalue_length(DBPROP *prop)
299 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
302 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
305 int len = SysStringLen(V_BSTR(&str));
313 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
315 VARIANT *v = &prop->vValue;
319 if (V_VT(v) == VT_BSTR)
321 strcatW(str, V_BSTR(v));
326 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
329 strcatW(str, V_BSTR(&vstr));
334 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
338 for (i = 0; i < propinfoset->cPropertyInfos; i++)
339 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
340 return propinfoset->rgPropertyInfos[i].pwszDescription;
345 static void free_dbpropset(ULONG count, DBPROPSET *propset)
349 for (i = 0; i < count; i++)
353 for (p = 0; p < propset[i].cProperties; p++)
354 VariantClear(&propset[i].rgProperties[p].vValue);
356 CoTaskMemFree(propset[i].rgProperties);
358 CoTaskMemFree(propset);
361 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
365 for (i = 0; i < count; i++)
369 for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
370 VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
372 CoTaskMemFree(propinfoset[i].rgPropertyInfos);
374 CoTaskMemFree(propinfoset);
377 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
378 boolean include_pass, LPWSTR *init_string)
380 static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
381 static const WCHAR colW[] = {';',0};
382 datainit *This = impl_from_IDataInitialize(iface);
383 DBPROPINFOSET *propinfoset;
384 IDBProperties *props;
385 DBPROPIDSET propidset;
386 ULONG count, infocount;
387 WCHAR *progid, *desc;
394 TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
396 /* IPersist support is mandatory for data sources */
397 hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
398 if (FAILED(hr)) return hr;
400 memset(&clsid, 0, sizeof(clsid));
401 hr = IPersist_GetClassID(persist, &clsid);
402 IPersist_Release(persist);
403 if (FAILED(hr)) return hr;
406 ProgIDFromCLSID(&clsid, &progid);
407 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
409 /* now get initialization properties */
410 hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
413 WARN("IDBProperties not supported\n");
414 CoTaskMemFree(progid);
418 propidset.rgPropertyIDs = NULL;
419 propidset.cPropertyIDs = 0;
420 propidset.guidPropertySet = DBPROPSET_DBINIT;
423 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
426 WARN("failed to get data source properties, 0x%08x\n", hr);
427 CoTaskMemFree(progid);
432 IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
433 IDBProperties_Release(props);
435 /* check if we need to skip password */
436 len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
437 for (i = 0; i < count; i++)
439 WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
442 /* include '=' and ';' */
443 len += strlenW(descr) + 2;
444 len += get_propvalue_length(&propset->rgProperties[i]);
447 if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
448 (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
449 include_pass = FALSE;
452 len *= sizeof(WCHAR);
453 *init_string = CoTaskMemAlloc(len);
457 strcatW(*init_string, provW);
458 strcatW(*init_string, progid);
459 strcatW(*init_string, colW);
460 CoTaskMemFree(progid);
462 for (i = 0; i < count; i++)
466 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
468 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
471 static const WCHAR eqW[] = {'=',0};
472 strcatW(*init_string, descr);
473 strcatW(*init_string, eqW);
474 write_propvalue_str(*init_string, &propset->rgProperties[i]);
475 strcatW(*init_string, colW);
479 free_dbpropset(count, propset);
480 free_dbpropinfoset(infocount, propinfoset);
484 TRACE("%s\n", debugstr_w(*init_string));
488 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
489 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
490 IUnknown **datasource)
492 datainit *This = impl_from_IDataInitialize(iface);
494 TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
495 debugstr_guid(riid), datasource);
497 return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
500 static HRESULT WINAPI datainit_RemoteCreateDBInstanceEx(IDataInitialize *iface, REFCLSID clsidProvider,
501 IUnknown *pUnkOuter, DWORD dwClsCtx, LPWSTR pwszReserved, COSERVERINFO *pServerInfo,
502 DWORD cmq, GUID **rgpIID, IUnknown **rgpItf, HRESULT *rghr)
504 datainit *This = impl_from_IDataInitialize(iface);
506 FIXME("(%p)->()\n", This);
511 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
512 LPWSTR *ppwszInitializationString)
514 datainit *This = impl_from_IDataInitialize(iface);
516 FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
521 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
522 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
524 datainit *This = impl_from_IDataInitialize(iface);
526 FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
532 static const struct IDataInitializeVtbl datainit_vtbl =
534 datainit_QueryInterface,
537 datainit_GetDataSource,
538 datainit_GetInitializationString,
539 datainit_CreateDBInstance,
540 datainit_RemoteCreateDBInstanceEx,
541 datainit_LoadStringFromStorage,
542 datainit_WriteStringToStorage
545 HRESULT create_data_init(IUnknown *outer, void **obj)
549 TRACE("(%p)\n", obj);
551 if(outer) return CLASS_E_NOAGGREGATION;
555 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
556 if(!This) return E_OUTOFMEMORY;
558 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
561 *obj = &This->IDataInitialize_iface;