msdasql: Fix printf format warnings with long types.
[wine.git] / dlls / msdasql / msdasql_main.c
blob3e1af95b1a7f7ecd8834ff0339e2c290b91526b2
1 /*
2 * Copyright 2019 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
19 #define COBJMACROS
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "objbase.h"
26 #include "oledb.h"
27 #include "rpcproxy.h"
28 #include "wine/debug.h"
29 #include "oledberr.h"
31 #include "initguid.h"
32 #include "msdasql.h"
33 #include "odbcinst.h"
34 #include "sqlext.h"
35 #include "sqlucode.h"
37 #include "msdasql_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msdasql);
41 DEFINE_GUID(DBPROPSET_DATASOURCEINFO, 0xc8b522bb, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
42 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
44 DEFINE_GUID(DBGUID_DEFAULT, 0xc8b521fb, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
46 void dump_sql_diag_records(SQLSMALLINT type, SQLHANDLE handle)
48 SQLCHAR state[6], msg[SQL_MAX_MESSAGE_LENGTH];
49 SQLINTEGER native;
50 SQLSMALLINT i = 1, len;
52 if (!TRACE_ON(msdasql))
53 return;
55 while(SQLGetDiagRec(type, handle, i, state, &native, msg, sizeof(msg), &len) == SQL_SUCCESS)
57 WARN("%d: %s: %s\n", i, state, msg);
58 i++;
62 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
64 *ppv = NULL;
66 if(IsEqualGUID(&IID_IUnknown, riid)) {
67 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
68 *ppv = iface;
69 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
70 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
71 *ppv = iface;
74 if(*ppv) {
75 IUnknown_AddRef((IUnknown*)*ppv);
76 return S_OK;
79 WARN("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
80 return E_NOINTERFACE;
83 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
85 TRACE("(%p)\n", iface);
86 return 2;
89 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
91 TRACE("(%p)\n", iface);
92 return 1;
95 static HRESULT create_msdasql_provider(REFIID riid, void **ppv);
96 static HRESULT create_msdasql_enumerator(REFIID riid, void **ppv);
98 HRESULT WINAPI msdasql_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
100 TRACE("(%p %s %p)\n", outer, debugstr_guid(riid), ppv);
102 return create_msdasql_provider(riid, ppv);
105 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
107 TRACE("(%p)->(%x)\n", iface, fLock);
108 return S_OK;
111 static const IClassFactoryVtbl cfmsdasqlVtbl = {
112 ClassFactory_QueryInterface,
113 ClassFactory_AddRef,
114 ClassFactory_Release,
115 msdasql_CreateInstance,
116 ClassFactory_LockServer
119 HRESULT WINAPI enumerationcf_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
121 TRACE("(%p %s %p)\n", outer, debugstr_guid(riid), ppv);
123 return create_msdasql_enumerator(riid, ppv);
126 static const IClassFactoryVtbl enumfactoryVtbl =
128 ClassFactory_QueryInterface,
129 ClassFactory_AddRef,
130 ClassFactory_Release,
131 enumerationcf_CreateInstance,
132 ClassFactory_LockServer
135 static IClassFactory cfmsdasql = { &cfmsdasqlVtbl };
136 static IClassFactory enumfactory = { &enumfactoryVtbl };
138 HRESULT WINAPI DllGetClassObject( REFCLSID rclsid, REFIID riid, void **ppv )
140 TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
142 if (IsEqualGUID(&CLSID_MSDASQL, rclsid))
143 return IClassFactory_QueryInterface(&cfmsdasql, riid, ppv);
144 else if (IsEqualGUID(&CLSID_MSDASQL_ENUMERATOR, rclsid))
145 return IClassFactory_QueryInterface(&enumfactory, riid, ppv);
147 return CLASS_E_CLASSNOTAVAILABLE;
150 struct dbproperty
152 const WCHAR *name;
153 DBPROPID id;
154 DBPROPOPTIONS options;
155 VARTYPE type;
156 HRESULT (*convert_dbproperty)(const WCHAR *src, VARIANT *dest);
159 struct mode_propval
161 const WCHAR *name;
162 DWORD value;
165 static int __cdecl dbmodeprop_compare(const void *a, const void *b)
167 const WCHAR *src = a;
168 const struct mode_propval *propval = b;
169 return wcsicmp(src, propval->name);
172 static HRESULT convert_dbproperty_mode(const WCHAR *src, VARIANT *dest)
174 struct mode_propval mode_propvals[] =
176 { L"Read", DB_MODE_READ },
177 { L"ReadWrite", DB_MODE_READWRITE },
178 { L"Share Deny None", DB_MODE_SHARE_DENY_NONE },
179 { L"Share Deny Read", DB_MODE_SHARE_DENY_READ },
180 { L"Share Deny Write", DB_MODE_SHARE_DENY_WRITE },
181 { L"Share Exclusive", DB_MODE_SHARE_EXCLUSIVE },
182 { L"Write", DB_MODE_WRITE },
184 struct mode_propval *prop;
186 if ((prop = bsearch(src, mode_propvals, ARRAY_SIZE(mode_propvals),
187 sizeof(struct mode_propval), dbmodeprop_compare)))
189 V_VT(dest) = VT_I4;
190 V_I4(dest) = prop->value;
191 TRACE("%s = %#lx\n", debugstr_w(src), prop->value);
192 return S_OK;
195 return E_FAIL;
198 static const struct dbproperty dbproperties[] =
200 { L"Password", DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
201 { L"Persist Security Info", DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
202 { L"User ID", DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
203 { L"Data Source", DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
204 { L"Window Handle", DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, sizeof(void *) == 8 ? VT_I8 : VT_I4 },
205 { L"Location", DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
206 { L"Mode", DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4, convert_dbproperty_mode },
207 { L"Prompt", DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
208 { L"Connect Timeout", DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
209 { L"Extended Properties", DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
210 { L"Locale Identifier", DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
211 { L"Initial Catalog", DBPROP_INIT_CATALOG, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
212 { L"OLE DB Services", DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
213 { L"General Timeout", DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
216 struct msdasql_prop
218 VARTYPE id;
219 VARIANT value;
222 struct msdasql
224 IUnknown MSDASQL_iface;
225 IDBProperties IDBProperties_iface;
226 IDBInitialize IDBInitialize_iface;
227 IDBCreateSession IDBCreateSession_iface;
228 IPersist IPersist_iface;
230 LONG ref;
231 struct msdasql_prop properties[14];
233 /* ODBC Support */
234 HENV henv;
235 HDBC hdbc;
238 static inline struct msdasql *impl_from_IUnknown(IUnknown *iface)
240 return CONTAINING_RECORD(iface, struct msdasql, MSDASQL_iface);
243 static inline struct msdasql *impl_from_IDBProperties(IDBProperties *iface)
245 return CONTAINING_RECORD(iface, struct msdasql, IDBProperties_iface);
248 static inline struct msdasql *impl_from_IDBInitialize(IDBInitialize *iface)
250 return CONTAINING_RECORD(iface, struct msdasql, IDBInitialize_iface);
253 static inline struct msdasql *impl_from_IDBCreateSession(IDBCreateSession *iface)
255 return CONTAINING_RECORD(iface, struct msdasql, IDBCreateSession_iface);
258 static inline struct msdasql *impl_from_IPersist( IPersist *iface )
260 return CONTAINING_RECORD( iface, struct msdasql, IPersist_iface );
263 static HRESULT WINAPI msdsql_QueryInterface(IUnknown *iface, REFIID riid, void **out)
265 struct msdasql *provider = impl_from_IUnknown(iface);
267 TRACE("(%p)->(%s %p)\n", provider, debugstr_guid(riid), out);
269 if(IsEqualGUID(riid, &IID_IUnknown) ||
270 IsEqualGUID(riid, &CLSID_MSDASQL))
272 *out = &provider->MSDASQL_iface;
274 else if(IsEqualGUID(riid, &IID_IDBProperties))
276 *out = &provider->IDBProperties_iface;
278 else if ( IsEqualGUID(riid, &IID_IDBInitialize))
280 *out = &provider->IDBInitialize_iface;
282 else if (IsEqualGUID(riid, &IID_IDBCreateSession))
284 *out = &provider->IDBCreateSession_iface;
286 else if(IsEqualGUID(&IID_IPersist, riid))
288 *out = &provider->IPersist_iface;
290 else
292 FIXME("(%s, %p)\n", debugstr_guid(riid), out);
293 *out = NULL;
294 return E_NOINTERFACE;
297 IUnknown_AddRef((IUnknown*)*out);
298 return S_OK;
301 static ULONG WINAPI msdsql_AddRef(IUnknown *iface)
303 struct msdasql *provider = impl_from_IUnknown(iface);
304 ULONG ref = InterlockedIncrement(&provider->ref);
306 TRACE("(%p) ref=%lu\n", provider, ref);
308 return ref;
311 static ULONG WINAPI msdsql_Release(IUnknown *iface)
313 struct msdasql *provider = impl_from_IUnknown(iface);
314 ULONG ref = InterlockedDecrement(&provider->ref);
316 TRACE("(%p) ref=%lu\n", provider, ref);
318 if (!ref)
320 SQLDisconnect(provider->hdbc);
322 SQLFreeHandle(SQL_HANDLE_DBC, provider->hdbc);
323 SQLFreeHandle(SQL_HANDLE_ENV, provider->henv);
324 free(provider);
327 return ref;
330 static const IUnknownVtbl msdsql_vtbl =
332 msdsql_QueryInterface,
333 msdsql_AddRef,
334 msdsql_Release
337 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
339 struct msdasql *provider = impl_from_IDBProperties(iface);
341 return IUnknown_QueryInterface(&provider->MSDASQL_iface, riid, ppvObject);
344 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
346 struct msdasql *provider = impl_from_IDBProperties(iface);
348 return IUnknown_AddRef(&provider->MSDASQL_iface);
351 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
353 struct msdasql *provider = impl_from_IDBProperties(iface);
355 return IUnknown_Release(&provider->MSDASQL_iface);
358 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
359 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
361 struct msdasql *provider = impl_from_IDBProperties(iface);
362 int i, j, k;
363 DBPROPSET *propset;
365 TRACE("(%p)->(%ld %p %p %p)\n", provider, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
367 *pcPropertySets = 1;
369 if (cPropertyIDSets != 1)
371 FIXME("Currently only 1 property set supported.\n");
372 cPropertyIDSets = 1;
375 propset = CoTaskMemAlloc(cPropertyIDSets * sizeof(DBPROPSET));
377 if (IsEqualGUID(&rgPropertyIDSets[0].guidPropertySet, &DBPROPSET_DATASOURCEINFO))
379 TRACE("Propertyset DBPROPSET_DATASOURCEINFO not supported\n");
380 propset->guidPropertySet = rgPropertyIDSets[0].guidPropertySet;
381 propset->cProperties = rgPropertyIDSets[0].cPropertyIDs;
383 propset->rgProperties = CoTaskMemAlloc(propset->cProperties * sizeof(DBPROP));
385 for (j=0; j < propset->cProperties; j++)
387 propset->rgProperties[j].dwPropertyID = rgPropertyIDSets[0].rgPropertyIDs[j];
388 propset->rgProperties[j].dwStatus = DBPROPSTATUS_NOTSUPPORTED;
391 *prgPropertySets = propset;
393 return DB_E_ERRORSOCCURRED;
396 propset->guidPropertySet = DBPROPSET_DBINIT;
398 for (i=0; i < cPropertyIDSets; i++)
400 TRACE("Property id %d (count %ld, set %s)\n", i, rgPropertyIDSets[i].cPropertyIDs,
401 debugstr_guid(&rgPropertyIDSets[i].guidPropertySet));
403 propset->cProperties = rgPropertyIDSets[i].cPropertyIDs;
404 propset->rgProperties = CoTaskMemAlloc(propset->cProperties * sizeof(DBPROP));
406 for (j=0; j < propset->cProperties; j++)
408 propset->rgProperties[j].dwPropertyID = rgPropertyIDSets[i].rgPropertyIDs[j];
410 for(k = 0; k < ARRAY_SIZE(provider->properties); k++)
412 if (provider->properties[k].id == rgPropertyIDSets[i].rgPropertyIDs[j])
414 V_VT(&propset->rgProperties[j].vValue) = VT_EMPTY;
415 VariantCopy(&propset->rgProperties[j].vValue, &provider->properties[k].value);
416 break;
422 *prgPropertySets = propset;
424 return S_OK;
427 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
428 const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
429 DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
431 struct msdasql *provider = impl_from_IDBProperties(iface);
432 int i;
433 DBPROPINFOSET *infoset;
434 int size = 1;
435 OLECHAR *ptr;
437 TRACE("(%p)->(%ld %p %p %p %p)\n", provider, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
438 prgPropertyInfoSets, ppDescBuffer);
440 infoset = CoTaskMemAlloc(sizeof(DBPROPINFOSET));
441 memcpy(&infoset->guidPropertySet, &DBPROPSET_DBINIT, sizeof(GUID));
442 infoset->cPropertyInfos = ARRAY_SIZE(dbproperties);
443 infoset->rgPropertyInfos = CoTaskMemAlloc(sizeof(DBPROPINFO) * ARRAY_SIZE(dbproperties));
445 for(i=0; i < ARRAY_SIZE(dbproperties); i++)
447 size += lstrlenW(dbproperties[i].name) + 1;
450 ptr = *ppDescBuffer = CoTaskMemAlloc(size * sizeof(WCHAR));
451 memset(*ppDescBuffer, 0, size * sizeof(WCHAR));
453 for(i=0; i < ARRAY_SIZE(dbproperties); i++)
455 lstrcpyW(ptr, dbproperties[i].name);
456 infoset->rgPropertyInfos[i].pwszDescription = ptr;
457 infoset->rgPropertyInfos[i].dwPropertyID = dbproperties[i].id;
458 infoset->rgPropertyInfos[i].dwFlags = DBPROPFLAGS_DBINIT | DBPROPFLAGS_READ | DBPROPFLAGS_WRITE;
459 infoset->rgPropertyInfos[i].vtType = dbproperties[i].type;
460 V_VT(&infoset->rgPropertyInfos[i].vValues) = VT_EMPTY;
462 ptr += lstrlenW(dbproperties[i].name) + 1;
465 *pcPropertyInfoSets = 1;
466 *prgPropertyInfoSets = infoset;
468 return S_OK;
471 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
472 DBPROPSET rgPropertySets[])
474 struct msdasql *provider = impl_from_IDBProperties(iface);
475 int i, j, k;
477 TRACE("(%p)->(%ld %p)\n", provider, cPropertySets, rgPropertySets);
479 for (i=0; i < cPropertySets; i++)
481 for (j=0; j < rgPropertySets[i].cProperties; j++)
483 for(k=0; k < ARRAY_SIZE(provider->properties); k++)
485 if (provider->properties[k].id == rgPropertySets[i].rgProperties[j].dwPropertyID)
487 TRACE("Found property %d\n", provider->properties[k].id);
488 VariantCopy(&provider->properties[k].value, &rgPropertySets[i].rgProperties[j].vValue);
489 break;
495 return S_OK;
498 static const struct IDBPropertiesVtbl dbprops_vtbl =
500 dbprops_QueryInterface,
501 dbprops_AddRef,
502 dbprops_Release,
503 dbprops_GetProperties,
504 dbprops_GetPropertyInfo,
505 dbprops_SetProperties
508 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **ppvObject)
510 struct msdasql *provider = impl_from_IDBInitialize(iface);
512 return IUnknown_QueryInterface(&provider->MSDASQL_iface, riid, ppvObject);
515 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
517 struct msdasql *provider = impl_from_IDBInitialize(iface);
519 return IUnknown_AddRef(&provider->MSDASQL_iface);
522 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
524 struct msdasql *provider = impl_from_IDBInitialize(iface);
526 return IUnknown_Release(&provider->MSDASQL_iface);
529 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
531 struct msdasql *provider = impl_from_IDBInitialize(iface);
532 int i;
533 SQLRETURN ret;
535 FIXME("%p semi-stub\n", provider);
537 for(i=0; i < sizeof(provider->properties); i++)
539 if (provider->properties[i].id == DBPROP_INIT_DATASOURCE)
540 break;
543 if (i >= sizeof(provider->properties))
545 ERR("Datasource not found\n");
546 return E_FAIL;
549 ret = SQLConnectW( provider->hdbc, (SQLWCHAR *)V_BSTR(&provider->properties[i].value),
550 SQL_NTS, (SQLWCHAR *)NULL, SQL_NTS, (SQLWCHAR *)NULL, SQL_NTS );
551 TRACE("SQLConnectW ret %d\n", ret);
552 if (ret != SQL_SUCCESS)
554 dump_sql_diag_records(SQL_HANDLE_DBC, provider->hdbc);
556 if (ret != SQL_SUCCESS_WITH_INFO)
557 return E_FAIL;
560 return S_OK;
563 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
565 struct msdasql *provider = impl_from_IDBInitialize(iface);
567 FIXME("%p stub\n", provider);
569 return S_OK;
573 static const struct IDBInitializeVtbl dbinit_vtbl =
575 dbinit_QueryInterface,
576 dbinit_AddRef,
577 dbinit_Release,
578 dbinit_Initialize,
579 dbinit_Uninitialize
582 static HRESULT WINAPI dbsess_QueryInterface(IDBCreateSession *iface, REFIID riid, void **ppvObject)
584 struct msdasql *provider = impl_from_IDBCreateSession(iface);
586 return IUnknown_QueryInterface(&provider->MSDASQL_iface, riid, ppvObject);
589 static ULONG WINAPI dbsess_AddRef(IDBCreateSession *iface)
591 struct msdasql *provider = impl_from_IDBCreateSession(iface);
593 return IUnknown_AddRef(&provider->MSDASQL_iface);
596 static ULONG WINAPI dbsess_Release(IDBCreateSession *iface)
598 struct msdasql *provider = impl_from_IDBCreateSession(iface);
600 return IUnknown_Release(&provider->MSDASQL_iface);
603 static HRESULT WINAPI dbsess_CreateSession(IDBCreateSession *iface, IUnknown *outer, REFIID riid,
604 IUnknown **session)
606 struct msdasql *provider = impl_from_IDBCreateSession(iface);
607 HRESULT hr;
609 TRACE("%p, outer %p, riid %s, session %p stub\n", provider, outer, debugstr_guid(riid), session);
611 if (outer)
612 FIXME("outer currently not supported.\n");
614 hr = create_db_session(riid, &provider->MSDASQL_iface, provider->hdbc, (void**)session);
616 return hr;
619 static const struct IDBCreateSessionVtbl dbsess_vtbl =
621 dbsess_QueryInterface,
622 dbsess_AddRef,
623 dbsess_Release,
624 dbsess_CreateSession
627 static HRESULT WINAPI persist_QueryInterface(IPersist *iface, REFIID riid, void **ppv)
629 struct msdasql *provider = impl_from_IPersist( iface );
630 return IUnknown_QueryInterface(&provider->MSDASQL_iface, riid, ppv);
633 static ULONG WINAPI persist_AddRef(IPersist *iface)
635 struct msdasql *provider = impl_from_IPersist( iface );
636 return IUnknown_AddRef(&provider->MSDASQL_iface);
639 static ULONG WINAPI persist_Release(IPersist *iface)
641 struct msdasql *provider = impl_from_IPersist( iface );
642 return IUnknown_Release(&provider->MSDASQL_iface);
645 static HRESULT WINAPI persist_GetClassID(IPersist *iface, CLSID *classid)
647 struct msdasql *provider = impl_from_IPersist( iface );
649 TRACE("(%p)->(%p)\n", provider, classid);
651 if(!classid)
652 return E_INVALIDARG;
654 *classid = CLSID_MSDASQL;
655 return S_OK;
659 static const IPersistVtbl persistVtbl = {
660 persist_QueryInterface,
661 persist_AddRef,
662 persist_Release,
663 persist_GetClassID
666 static HRESULT create_msdasql_provider(REFIID riid, void **ppv)
668 struct msdasql *provider;
669 HRESULT hr;
670 int i;
672 provider = malloc(sizeof(struct msdasql));
673 if (!provider)
674 return E_OUTOFMEMORY;
676 provider->MSDASQL_iface.lpVtbl = &msdsql_vtbl;
677 provider->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
678 provider->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
679 provider->IDBCreateSession_iface.lpVtbl = &dbsess_vtbl;
680 provider->IPersist_iface.lpVtbl = &persistVtbl;
681 provider->ref = 1;
683 for(i=0; i < ARRAY_SIZE(dbproperties); i++)
685 provider->properties[i].id = dbproperties[i].id;
686 VariantInit(&provider->properties[i].value);
688 /* Only the follow are initialized to a value */
689 switch(dbproperties[i].id)
691 case DBPROP_INIT_PROMPT:
692 V_VT(&provider->properties[i].value) = dbproperties[i].type;
693 V_I2(&provider->properties[i].value) = 4;
694 break;
695 case DBPROP_INIT_LCID:
696 V_VT(&provider->properties[i].value) = dbproperties[i].type;
697 V_I4(&provider->properties[i].value) = GetUserDefaultLCID();
698 break;
699 case DBPROP_INIT_OLEDBSERVICES:
700 V_VT(&provider->properties[i].value) = dbproperties[i].type;
701 V_I4(&provider->properties[i].value) = -1;
702 break;
703 default:
704 V_VT(&provider->properties[i].value) = VT_EMPTY;
708 SQLAllocHandle(SQL_HANDLE_ENV, NULL, &provider->henv );
709 if (SQLSetEnvAttr(provider->henv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3_80, 0) == SQL_ERROR)
711 WARN("Falling back to SQL_OV_ODBC3\n");
712 SQLSetEnvAttr(provider->henv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
715 SQLAllocHandle(SQL_HANDLE_DBC, provider->henv, &provider->hdbc);
717 hr = IUnknown_QueryInterface(&provider->MSDASQL_iface, riid, ppv);
718 IUnknown_Release(&provider->MSDASQL_iface);
719 return hr;
722 struct msdasql_enum
724 ISourcesRowset ISourcesRowset_iface;
725 LONG ref;
728 static inline struct msdasql_enum *msdasql_enum_from_ISourcesRowset(ISourcesRowset *iface)
730 return CONTAINING_RECORD(iface, struct msdasql_enum, ISourcesRowset_iface);
733 static HRESULT WINAPI msdasql_enum_QueryInterface(ISourcesRowset *iface, REFIID riid, void **out)
735 struct msdasql_enum *enumerator = msdasql_enum_from_ISourcesRowset(iface);
737 TRACE("(%p)->(%s %p)\n", enumerator, debugstr_guid(riid), out);
739 if(IsEqualGUID(riid, &IID_IUnknown) ||
740 IsEqualGUID(riid, &IID_ISourcesRowset ) )
742 *out = &enumerator->ISourcesRowset_iface;
744 else
746 FIXME("(%s, %p)\n", debugstr_guid(riid), out);
747 *out = NULL;
748 return E_NOINTERFACE;
751 IUnknown_AddRef((IUnknown*)*out);
752 return S_OK;
755 static ULONG WINAPI msdasql_enum_AddRef(ISourcesRowset *iface)
757 struct msdasql_enum *enumerator = msdasql_enum_from_ISourcesRowset(iface);
758 ULONG ref = InterlockedIncrement(&enumerator->ref);
760 TRACE("(%p) ref=%lu\n", enumerator, ref);
762 return ref;
765 static ULONG WINAPI msdasql_enum_Release(ISourcesRowset *iface)
767 struct msdasql_enum *enumerator = msdasql_enum_from_ISourcesRowset(iface);
768 ULONG ref = InterlockedDecrement(&enumerator->ref);
770 TRACE("(%p) ref=%lu\n", enumerator, ref);
772 if (!ref)
774 free(enumerator);
777 return ref;
780 struct msdasql_enum_rowset
782 IRowset IRowset_iface;
783 IAccessor IAccessor_iface;
784 LONG ref;
787 static inline struct msdasql_enum_rowset *msdasql_rs_from_IRowset(IRowset *iface)
789 return CONTAINING_RECORD(iface, struct msdasql_enum_rowset, IRowset_iface);
792 static inline struct msdasql_enum_rowset *msdasql_enum_from_IAccessor ( IAccessor *iface )
794 return CONTAINING_RECORD( iface, struct msdasql_enum_rowset, IAccessor_iface );
797 static HRESULT WINAPI enum_rowset_QueryInterface(IRowset *iface, REFIID riid, void **ppv)
799 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
801 TRACE( "%p, %s, %p\n", rowset, debugstr_guid(riid), ppv );
802 *ppv = NULL;
804 if(IsEqualGUID(&IID_IUnknown, riid) ||
805 IsEqualGUID(&IID_IRowset, riid))
807 *ppv = &rowset->IRowset_iface;
809 else if(IsEqualGUID(&IID_IAccessor, riid))
811 *ppv = &rowset->IAccessor_iface;
814 if(*ppv)
816 IUnknown_AddRef((IUnknown*)*ppv);
817 return S_OK;
820 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
821 return E_NOINTERFACE;
824 static ULONG WINAPI enum_rowset_AddRef(IRowset *iface)
826 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
827 LONG refs = InterlockedIncrement( &rowset->ref );
828 TRACE( "%p new refcount %ld\n", rowset, refs );
829 return refs;
832 static ULONG WINAPI enum_rowset_Release(IRowset *iface)
834 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
835 LONG refs = InterlockedDecrement( &rowset->ref );
836 TRACE( "%p new refcount %ld\n", rowset, refs );
837 if (!refs)
839 TRACE( "destroying %p\n", rowset );
840 free( rowset );
842 return refs;
845 static HRESULT WINAPI enum_rowset_AddRefRows(IRowset *iface, DBCOUNTITEM count,
846 const HROW rows[], DBREFCOUNT ref_counts[], DBROWSTATUS status[])
848 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
850 FIXME("%p, %Iu, %p, %p, %p\n", rowset, count, rows, ref_counts, status);
852 return E_NOTIMPL;
855 static HRESULT WINAPI enum_rowset_GetData(IRowset *iface, HROW row, HACCESSOR accessor, void *data)
857 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
859 FIXME("%p, %Iu, %Iu, %p\n", rowset, row, accessor, data);
861 return E_NOTIMPL;
864 static HRESULT WINAPI enum_rowset_GetNextRows(IRowset *iface, HCHAPTER reserved, DBROWOFFSET offset,
865 DBROWCOUNT count, DBCOUNTITEM *obtained, HROW **rows)
867 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
869 FIXME("%p, %Iu, %Iu, %Iu, %p, %p\n", rowset, reserved, offset, count, obtained, rows);
871 if (!obtained || !rows)
872 return E_INVALIDARG;
874 *obtained = 0;
876 if (!count)
877 return S_OK;
879 return DB_S_ENDOFROWSET;
882 static HRESULT WINAPI enum_rowset_ReleaseRows(IRowset *iface, DBCOUNTITEM count,
883 const HROW rows[], DBROWOPTIONS options[], DBREFCOUNT ref_counts[], DBROWSTATUS status[])
885 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
887 FIXME("%p, %Iu, %p, %p, %p, %p\n", rowset, count, rows, options, ref_counts, status);
889 return S_OK;
892 static HRESULT WINAPI enum_rowset_RestartPosition(IRowset *iface, HCHAPTER reserved)
894 struct msdasql_enum_rowset *rowset = msdasql_rs_from_IRowset( iface );
896 FIXME("%p, %Iu\n", rowset, reserved);
898 return S_OK;
901 static const struct IRowsetVtbl enum_rowset_vtbl =
903 enum_rowset_QueryInterface,
904 enum_rowset_AddRef,
905 enum_rowset_Release,
906 enum_rowset_AddRefRows,
907 enum_rowset_GetData,
908 enum_rowset_GetNextRows,
909 enum_rowset_ReleaseRows,
910 enum_rowset_RestartPosition
913 static HRESULT WINAPI enum_rs_accessor_QueryInterface(IAccessor *iface, REFIID riid, void **out)
915 struct msdasql_enum_rowset *rowset = msdasql_enum_from_IAccessor( iface );
916 return IRowset_QueryInterface(&rowset->IRowset_iface, riid, out);
919 static ULONG WINAPI enum_rs_accessor_AddRef(IAccessor *iface)
921 struct msdasql_enum_rowset *rowset = msdasql_enum_from_IAccessor( iface );
922 return IRowset_AddRef(&rowset->IRowset_iface);
925 static ULONG WINAPI enum_rs_accessor_Release(IAccessor *iface)
927 struct msdasql_enum_rowset *rowset = msdasql_enum_from_IAccessor( iface );
928 return IRowset_Release(&rowset->IRowset_iface);
931 static HRESULT WINAPI enum_rs_accessor_AddRefAccessor(IAccessor *iface, HACCESSOR accessor, DBREFCOUNT *count)
933 struct msdasql_enum_rowset *rowset = msdasql_enum_from_IAccessor( iface );
934 FIXME("%p, %Iu, %p\n", rowset, accessor, count);
935 return E_NOTIMPL;
938 static HRESULT WINAPI enum_rs_accessor_CreateAccessor(IAccessor *iface, DBACCESSORFLAGS flags,
939 DBCOUNTITEM count, const DBBINDING bindings[], DBLENGTH row_size, HACCESSOR *accessor,
940 DBBINDSTATUS status[])
942 struct msdasql_enum_rowset *rowset = msdasql_enum_from_IAccessor( iface );
944 FIXME("%p 0x%08lx, %Iu, %p, %Iu, %p, %p\n", rowset, flags, count, bindings, row_size, accessor, status);
946 if (accessor)
947 *accessor = 0xdeadbeef;
949 return S_OK;
952 static HRESULT WINAPI enum_rs_accessor_GetBindings(IAccessor *iface, HACCESSOR accessor,
953 DBACCESSORFLAGS *flags, DBCOUNTITEM *count, DBBINDING **bindings)
955 struct msdasql_enum_rowset *rowset = msdasql_enum_from_IAccessor( iface );
956 FIXME("%p, %Iu, %p, %p, %p\n", rowset, accessor, flags, count, bindings);
957 return E_NOTIMPL;
960 static HRESULT WINAPI enum_rs_accessor_ReleaseAccessor(IAccessor *iface, HACCESSOR accessor, DBREFCOUNT *count)
962 struct msdasql_enum_rowset *rowset = msdasql_enum_from_IAccessor( iface );
964 FIXME("%p, %Iu, %p\n", rowset, accessor, count);
966 if (count)
967 *count = 0;
969 return S_OK;
972 struct IAccessorVtbl enum_accessor_vtbl =
974 enum_rs_accessor_QueryInterface,
975 enum_rs_accessor_AddRef,
976 enum_rs_accessor_Release,
977 enum_rs_accessor_AddRefAccessor,
978 enum_rs_accessor_CreateAccessor,
979 enum_rs_accessor_GetBindings,
980 enum_rs_accessor_ReleaseAccessor
983 static HRESULT WINAPI msdasql_enum_GetSourcesRowset(ISourcesRowset *iface, IUnknown *outer, REFIID riid, ULONG sets,
984 DBPROPSET properties[], IUnknown **rowset)
986 struct msdasql_enum *enumerator = msdasql_enum_from_ISourcesRowset(iface);
987 struct msdasql_enum_rowset *enum_rs;
988 HRESULT hr;
990 TRACE("(%p) %p, %s, %lu, %p, %p\n", enumerator, outer, debugstr_guid(riid), sets, properties, rowset);
992 enum_rs = malloc(sizeof(*enum_rs));
993 enum_rs->IRowset_iface.lpVtbl = &enum_rowset_vtbl;
994 enum_rs->IAccessor_iface.lpVtbl = &enum_accessor_vtbl;
995 enum_rs->ref = 1;
997 hr = IRowset_QueryInterface(&enum_rs->IRowset_iface, riid, (void**)rowset);
998 IRowset_Release(&enum_rs->IRowset_iface);
999 return hr;
1002 static const ISourcesRowsetVtbl msdsqlenum_vtbl =
1004 msdasql_enum_QueryInterface,
1005 msdasql_enum_AddRef,
1006 msdasql_enum_Release,
1007 msdasql_enum_GetSourcesRowset
1010 static HRESULT create_msdasql_enumerator(REFIID riid, void **ppv)
1012 struct msdasql_enum *enumerator;
1013 HRESULT hr;
1015 enumerator = malloc(sizeof(*enumerator));
1016 if (!enumerator)
1017 return E_OUTOFMEMORY;
1019 enumerator->ISourcesRowset_iface.lpVtbl = &msdsqlenum_vtbl;
1020 enumerator->ref = 1;
1022 hr = IUnknown_QueryInterface(&enumerator->ISourcesRowset_iface, riid, ppv);
1023 IUnknown_Release(&enumerator->ISourcesRowset_iface);
1024 return hr;