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
++)
355 VariantClear(&propset
[i
].rgProperties
[p
].vValue
);
356 CoTaskMemFree(&propset
[i
].rgProperties
[p
]);
358 CoTaskMemFree(&propset
[i
]);
362 static void free_dbpropinfoset(ULONG count
, DBPROPINFOSET
*propinfoset
)
366 for (i
= 0; i
< count
; i
++)
370 for (p
= 0; p
< propinfoset
[i
].cPropertyInfos
; p
++)
372 VariantClear(&propinfoset
[i
].rgPropertyInfos
[p
].vValues
);
373 CoTaskMemFree(&propinfoset
[i
].rgPropertyInfos
[p
]);
375 CoTaskMemFree(&propinfoset
[i
]);
379 static HRESULT WINAPI
datainit_GetInitializationString(IDataInitialize
*iface
, IUnknown
*datasource
,
380 boolean include_pass
, LPWSTR
*init_string
)
382 static const WCHAR provW
[] = {'P','r','o','v','i','d','e','r','=',0};
383 static const WCHAR colW
[] = {';',0};
384 datainit
*This
= impl_from_IDataInitialize(iface
);
385 DBPROPINFOSET
*propinfoset
;
386 IDBProperties
*props
;
387 DBPROPIDSET propidset
;
388 ULONG count
, infocount
;
389 WCHAR
*progid
, *desc
;
396 TRACE("(%p)->(%p %d %p)\n", This
, datasource
, include_pass
, init_string
);
398 /* IPersist support is mandatory for data sources */
399 hr
= IUnknown_QueryInterface(datasource
, &IID_IPersist
, (void**)&persist
);
400 if (FAILED(hr
)) return hr
;
402 memset(&clsid
, 0, sizeof(clsid
));
403 hr
= IPersist_GetClassID(persist
, &clsid
);
404 IPersist_Release(persist
);
405 if (FAILED(hr
)) return hr
;
408 ProgIDFromCLSID(&clsid
, &progid
);
409 TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid
), debugstr_w(progid
));
411 /* now get initialization properties */
412 hr
= IUnknown_QueryInterface(datasource
, &IID_IDBProperties
, (void**)&props
);
415 WARN("IDBProperties not supported\n");
416 CoTaskMemFree(progid
);
420 propidset
.rgPropertyIDs
= NULL
;
421 propidset
.cPropertyIDs
= 0;
422 propidset
.guidPropertySet
= DBPROPSET_DBINIT
;
425 hr
= IDBProperties_GetProperties(props
, 1, &propidset
, &count
, &propset
);
428 WARN("failed to get data source properties, 0x%08x\n", hr
);
429 CoTaskMemFree(progid
);
434 IDBProperties_GetPropertyInfo(props
, 1, &propidset
, &infocount
, &propinfoset
, &desc
);
435 IDBProperties_Release(props
);
437 /* check if we need to skip password */
438 len
= strlenW(progid
) + strlenW(provW
) + 1; /* including ';' */
439 for (i
= 0; i
< count
; i
++)
441 WCHAR
*descr
= get_propinfo_descr(&propset
->rgProperties
[i
], propinfoset
);
444 /* include '=' and ';' */
445 len
+= strlenW(descr
) + 2;
446 len
+= get_propvalue_length(&propset
->rgProperties
[i
]);
449 if ((propset
->rgProperties
[i
].dwPropertyID
== DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO
) &&
450 (V_BOOL(&propset
->rgProperties
[i
].vValue
) == VARIANT_FALSE
))
451 include_pass
= FALSE
;
454 len
*= sizeof(WCHAR
);
455 *init_string
= CoTaskMemAlloc(len
);
459 strcatW(*init_string
, provW
);
460 strcatW(*init_string
, progid
);
461 strcatW(*init_string
, colW
);
462 CoTaskMemFree(progid
);
464 for (i
= 0; i
< count
; i
++)
468 if (!include_pass
&& propset
->rgProperties
[i
].dwPropertyID
== DBPROP_AUTH_PASSWORD
) continue;
470 descr
= get_propinfo_descr(&propset
->rgProperties
[i
], propinfoset
);
473 static const WCHAR eqW
[] = {'=',0};
474 strcatW(*init_string
, descr
);
475 strcatW(*init_string
, eqW
);
476 write_propvalue_str(*init_string
, &propset
->rgProperties
[i
]);
477 strcatW(*init_string
, colW
);
481 free_dbpropset(count
, propset
);
482 free_dbpropinfoset(infocount
, propinfoset
);
486 TRACE("%s\n", debugstr_w(*init_string
));
490 static HRESULT WINAPI
datainit_CreateDBInstance(IDataInitialize
*iface
, REFCLSID provider
,
491 IUnknown
*outer
, DWORD clsctx
, LPWSTR reserved
, REFIID riid
,
492 IUnknown
**datasource
)
494 datainit
*This
= impl_from_IDataInitialize(iface
);
496 TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This
, debugstr_guid(provider
), outer
, clsctx
, debugstr_w(reserved
),
497 debugstr_guid(riid
), datasource
);
499 return CoCreateInstance(provider
, outer
, clsctx
, riid
, (void**)datasource
);
502 static HRESULT WINAPI
datainit_RemoteCreateDBInstanceEx(IDataInitialize
*iface
, REFCLSID clsidProvider
,
503 IUnknown
*pUnkOuter
, DWORD dwClsCtx
, LPWSTR pwszReserved
, COSERVERINFO
*pServerInfo
,
504 DWORD cmq
, GUID
**rgpIID
, IUnknown
**rgpItf
, HRESULT
*rghr
)
506 datainit
*This
= impl_from_IDataInitialize(iface
);
508 FIXME("(%p)->()\n", This
);
513 static HRESULT WINAPI
datainit_LoadStringFromStorage(IDataInitialize
*iface
, LPWSTR pwszFileName
,
514 LPWSTR
*ppwszInitializationString
)
516 datainit
*This
= impl_from_IDataInitialize(iface
);
518 FIXME("(%p)->(%s %p)\n", This
, debugstr_w(pwszFileName
), ppwszInitializationString
);
523 static HRESULT WINAPI
datainit_WriteStringToStorage(IDataInitialize
*iface
, LPWSTR pwszFileName
,
524 LPWSTR pwszInitializationString
, DWORD dwCreationDisposition
)
526 datainit
*This
= impl_from_IDataInitialize(iface
);
528 FIXME("(%p)->(%s %s %d)\n", This
, debugstr_w(pwszFileName
), debugstr_w(pwszInitializationString
), dwCreationDisposition
);
534 static const struct IDataInitializeVtbl datainit_vtbl
=
536 datainit_QueryInterface
,
539 datainit_GetDataSource
,
540 datainit_GetInitializationString
,
541 datainit_CreateDBInstance
,
542 datainit_RemoteCreateDBInstanceEx
,
543 datainit_LoadStringFromStorage
,
544 datainit_WriteStringToStorage
547 HRESULT
create_data_init(IUnknown
*outer
, void **obj
)
551 TRACE("(%p)\n", obj
);
553 if(outer
) return CLASS_E_NOAGGREGATION
;
557 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
558 if(!This
) return E_OUTOFMEMORY
;
560 This
->IDataInitialize_iface
.lpVtbl
= &datainit_vtbl
;
563 *obj
= &This
->IDataInitialize_iface
;