winmm/tests: Add EOF and buffer pointer tests for mmio.
[wine.git] / dlls / oledb32 / datainit.c
blobb53e08abd147021a0de7e8a9c94d2b0af1f04854
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)
177 HeapFree(GetProcessHeap(), 0, This);
180 return ref;
183 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
185 dbinit *This = impl_from_IDBInitialize(iface);
187 FIXME("(%p) stub\n", This);
189 return S_OK;
192 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
194 dbinit *This = impl_from_IDBInitialize(iface);
196 FIXME("(%p) stub\n", This);
198 return S_OK;
201 static const IDBInitializeVtbl dbinit_vtbl =
203 dbinit_QueryInterface,
204 dbinit_AddRef,
205 dbinit_Release,
206 dbinit_Initialize,
207 dbinit_Uninitialize
210 static HRESULT create_db_init(void **obj)
212 dbinit *This;
214 TRACE("()\n");
216 *obj = NULL;
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;
223 This->ref = 1;
225 *obj = &This->IDBInitialize_iface;
227 return S_OK;
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);
235 *obj = NULL;
237 if(IsEqualIID(riid, &IID_IUnknown) ||
238 IsEqualIID(riid, &IID_IDataInitialize))
240 *obj = &This->IDataInitialize_iface;
242 else
244 FIXME("interface %s not implemented\n", debugstr_guid(riid));
245 return E_NOINTERFACE;
248 IDataInitialize_AddRef(iface);
249 return S_OK;
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);
263 LONG ref;
265 TRACE("(%p)\n", This);
267 ref = InterlockedDecrement(&This->ref);
268 if(ref == 0)
270 HeapFree(GetProcessHeap(), 0, This);
273 return ref;
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);
290 return E_NOTIMPL;
293 /* returns character length of string representation */
294 static int get_propvalue_length(DBPROP *prop)
296 VARIANT str;
297 HRESULT hr;
299 if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
301 VariantInit(&str);
302 hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
303 if (hr == S_OK)
305 int len = SysStringLen(V_BSTR(&str));
306 VariantClear(&str);
307 return len;
310 return 0;
313 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
315 VARIANT *v = &prop->vValue;
316 VARIANT vstr;
317 HRESULT hr;
319 if (V_VT(v) == VT_BSTR)
321 strcatW(str, V_BSTR(v));
322 return;
325 VariantInit(&vstr);
326 hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
327 if (hr == S_OK)
329 strcatW(str, V_BSTR(&vstr));
330 VariantClear(&vstr);
334 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
336 int i;
338 for (i = 0; i < propinfoset->cPropertyInfos; i++)
339 if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
340 return propinfoset->rgPropertyInfos[i].pwszDescription;
342 return NULL;
345 static void free_dbpropset(ULONG count, DBPROPSET *propset)
347 int i;
349 for (i = 0; i < count; i++)
351 int p;
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)
364 int i;
366 for (i = 0; i < count; i++)
368 int p;
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;
390 DBPROPSET *propset;
391 IPersist *persist;
392 HRESULT hr;
393 CLSID clsid;
394 int i, len;
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;
407 progid = NULL;
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);
413 if (FAILED(hr))
415 WARN("IDBProperties not supported\n");
416 CoTaskMemFree(progid);
417 return hr;
420 propidset.rgPropertyIDs = NULL;
421 propidset.cPropertyIDs = 0;
422 propidset.guidPropertySet = DBPROPSET_DBINIT;
423 propset = NULL;
424 count = 0;
425 hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
426 if (FAILED(hr))
428 WARN("failed to get data source properties, 0x%08x\n", hr);
429 CoTaskMemFree(progid);
430 return hr;
433 infocount = 0;
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);
442 if (descr)
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);
456 *init_string[0] = 0;
458 /* provider name */
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++)
466 WCHAR *descr;
468 if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
470 descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
471 if (descr)
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);
483 CoTaskMemFree(desc);
485 if (!include_pass)
486 TRACE("%s\n", debugstr_w(*init_string));
487 return S_OK;
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);
510 return E_NOTIMPL;
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);
520 return E_NOTIMPL;
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);
530 return E_NOTIMPL;
534 static const struct IDataInitializeVtbl datainit_vtbl =
536 datainit_QueryInterface,
537 datainit_AddRef,
538 datainit_Release,
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)
549 datainit *This;
551 TRACE("(%p)\n", obj);
553 if(outer) return CLASS_E_NOAGGREGATION;
555 *obj = NULL;
557 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
558 if(!This) return E_OUTOFMEMORY;
560 This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
561 This->ref = 1;
563 *obj = &This->IDataInitialize_iface;
565 return S_OK;