1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
|
/*
* Copyright (C) 2012 Alistair Leslie-Hughes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#define COBJMACROS
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "ole2.h"
#include "msdasc.h"
#include "oledberr.h"
#include "initguid.h"
#include "oledb_private.h"
#include "wine/debug.h"
#include "wine/heap.h"
#include "wine/list.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(oledb);
DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
typedef struct datainit
{
IDataInitialize IDataInitialize_iface;
LONG ref;
} datainit;
static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
{
return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
}
typedef struct
{
IDBInitialize IDBInitialize_iface;
IDBProperties IDBProperties_iface;
LONG ref;
} dbinit;
static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
{
return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
}
static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
{
return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
}
static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
{
dbinit *This = impl_from_IDBProperties(iface);
return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
}
static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
{
dbinit *This = impl_from_IDBProperties(iface);
return IDBInitialize_AddRef(&This->IDBInitialize_iface);
}
static ULONG WINAPI dbprops_Release(IDBProperties *iface)
{
dbinit *This = impl_from_IDBProperties(iface);
return IDBInitialize_Release(&This->IDBInitialize_iface);
}
static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
{
dbinit *This = impl_from_IDBProperties(iface);
FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
return E_NOTIMPL;
}
static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
{
dbinit *This = impl_from_IDBProperties(iface);
FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
prgPropertyInfoSets, ppDescBuffer);
return E_NOTIMPL;
}
static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
DBPROPSET rgPropertySets[])
{
dbinit *This = impl_from_IDBProperties(iface);
FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
return E_NOTIMPL;
}
static const struct IDBPropertiesVtbl dbprops_vtbl =
{
dbprops_QueryInterface,
dbprops_AddRef,
dbprops_Release,
dbprops_GetProperties,
dbprops_GetPropertyInfo,
dbprops_SetProperties
};
static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
{
dbinit *This = impl_from_IDBInitialize(iface);
TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
*obj = NULL;
if(IsEqualIID(riid, &IID_IUnknown) ||
IsEqualIID(riid, &IID_IDBInitialize))
{
*obj = iface;
}
else if(IsEqualIID(riid, &IID_IDBProperties))
{
*obj = &This->IDBProperties_iface;
}
else
{
FIXME("interface %s not implemented\n", debugstr_guid(riid));
return E_NOINTERFACE;
}
IDBInitialize_AddRef(iface);
return S_OK;
}
static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
{
dbinit *This = impl_from_IDBInitialize(iface);
TRACE("(%p)\n", This);
return InterlockedIncrement(&This->ref);
}
static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
{
dbinit *This = impl_from_IDBInitialize(iface);
LONG ref;
TRACE("(%p)\n", This);
ref = InterlockedDecrement(&This->ref);
if(ref == 0)
heap_free(This);
return ref;
}
static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
{
dbinit *This = impl_from_IDBInitialize(iface);
FIXME("(%p) stub\n", This);
return S_OK;
}
static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
{
dbinit *This = impl_from_IDBInitialize(iface);
FIXME("(%p) stub\n", This);
return S_OK;
}
static const IDBInitializeVtbl dbinit_vtbl =
{
dbinit_QueryInterface,
dbinit_AddRef,
dbinit_Release,
dbinit_Initialize,
dbinit_Uninitialize
};
static HRESULT create_db_init(IUnknown **obj)
{
dbinit *This;
TRACE("()\n");
*obj = NULL;
This = heap_alloc(sizeof(*This));
if(!This) return E_OUTOFMEMORY;
This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
This->ref = 1;
*obj = (IUnknown*)&This->IDBInitialize_iface;
return S_OK;
}
static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
{
datainit *This = impl_from_IDataInitialize(iface);
TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
*obj = NULL;
if(IsEqualIID(riid, &IID_IUnknown) ||
IsEqualIID(riid, &IID_IDataInitialize))
{
*obj = &This->IDataInitialize_iface;
}
else
{
FIXME("interface %s not implemented\n", debugstr_guid(riid));
return E_NOINTERFACE;
}
IDataInitialize_AddRef(iface);
return S_OK;
}
static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
{
datainit *This = impl_from_IDataInitialize(iface);
TRACE("(%p)\n", This);
return InterlockedIncrement(&This->ref);
}
static ULONG WINAPI datainit_Release(IDataInitialize *iface)
{
datainit *This = impl_from_IDataInitialize(iface);
LONG ref;
TRACE("(%p)\n", This);
ref = InterlockedDecrement(&This->ref);
if(ref == 0)
heap_free(This);
return ref;
}
static void free_dbpropset(ULONG count, DBPROPSET *propset)
{
ULONG i;
for (i = 0; i < count; i++)
{
ULONG p;
for (p = 0; p < propset[i].cProperties; p++)
VariantClear(&propset[i].rgProperties[p].vValue);
CoTaskMemFree(propset[i].rgProperties);
}
CoTaskMemFree(propset);
}
struct dbproperty {
const WCHAR *name;
DBPROPID id;
DBPROPOPTIONS options;
VARTYPE type;
HRESULT (*convert_dbproperty)(const WCHAR *src, VARIANT *dest);
};
struct mode_propval
{
const WCHAR *name;
DWORD value;
};
static int dbmodeprop_compare(const void *a, const void *b)
{
const WCHAR *src = a;
const struct mode_propval *propval = b;
return strcmpiW(src, propval->name);
}
static HRESULT convert_dbproperty_mode(const WCHAR *src, VARIANT *dest)
{
static const WCHAR readW[] = {'R','e','a','d',0};
static const WCHAR readwriteW[] = {'R','e','a','d','W','r','i','t','e',0};
static const WCHAR sharedenynoneW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','N','o','n','e',0};
static const WCHAR sharedenyreadW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','R','e','a','d',0};
static const WCHAR sharedenywriteW[] = {'S','h','a','r','e',' ','D','e','n','y',' ','W','r','i','t','e',0};
static const WCHAR shareexclusiveW[] = {'S','h','a','r','e',' ','E','x','c','l','u','s','i','v','e',0};
static const WCHAR writeW[] = {'W','r','i','t','e',0};
struct mode_propval mode_propvals[] =
{
{ readW, DB_MODE_READ },
{ readwriteW, DB_MODE_READWRITE },
{ sharedenynoneW, DB_MODE_SHARE_DENY_NONE },
{ sharedenyreadW, DB_MODE_SHARE_DENY_READ },
{ sharedenywriteW, DB_MODE_SHARE_DENY_WRITE },
{ shareexclusiveW, DB_MODE_SHARE_EXCLUSIVE },
{ writeW, DB_MODE_WRITE },
};
struct mode_propval *prop;
if ((prop = bsearch(src, mode_propvals, ARRAY_SIZE(mode_propvals),
sizeof(struct mode_propval), dbmodeprop_compare)))
{
V_VT(dest) = VT_I4;
V_I4(dest) = prop->value;
TRACE("%s = %#x\n", debugstr_w(src), prop->value);
return S_OK;
}
return E_FAIL;
}
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};
static const WCHAR bindW[] = {'B','i','n','d',' ','F','l','a','g','s',0};
static const WCHAR cacheW[] = {'C','a','c','h','e',' ','A','u','t','h','e','n','i','c','a','t','i','o','n',0};
static const WCHAR conn_timeout[] = {'C','o','n','n','e','c','t',' ','T','i','m','e','o','u','t',0};
static const WCHAR datasourceW[] = {'D','a','t','a',' ','S','o','u','r','c','e',0};
static const WCHAR encryptW[] = {'E','n','c','r','y','p','t',' ','P','a','s','s','w','o','r','d',0};
static const WCHAR extendedW[] = {'E','x','t','e','n','d','e','d',' ','P','r','o','p','e','r','t','i','e','s',0};
static const WCHAR gen_timeout[] = {'G','e','n','e','r','a','l',' ','T','i','m','e','o','u','t',0};
static const WCHAR impersonW[] = {'I','m','p','e','r','s','o','n','a','t','i','o','n',' ','L','e','v','e','l',0};
static const WCHAR initcatW[] = {'I','n','i','t','i','a','l',' ','C','a','t','a','l','o','g',0};
static const WCHAR integratedW[] = {'I','n','t','e','g','r','a','t','e','d',' ','S','e','c','u','r','i','t','y',0};
static const WCHAR localeIDW[] = {'L','o','c','a','l','e',' ','I','d','e','n','t','i','f','i','e','r',0};
static const WCHAR locationW[] = {'L','o','c','a','t','i','o','n',0};
static const WCHAR lockownerW[] = {'L','o','c','k',' ','O','w','n','e','r',0};
static const WCHAR maskpassW[] = {'M','a','s','k',' ','P','a','s','s','w','o','r','d',0};
static const WCHAR modeW[] = {'M','o','d','e',0};
static const WCHAR oledbservW[] = {'O','L','E',' ','D','B',' ','S','e','r','v','i','c','i','e','s',0};
static const WCHAR passwordW[] = {'P','a','s','s','w','o','r','d',0};
static const WCHAR persistW[] = {'P','e','r','s','i','s','t',' ','S','e','c','u','r','i','t','y',' ','I','n','f','o',0};
static const WCHAR persistEncW[] = {'P','e','r','s','i','s','t',' ','E','n','c','r','y','p','t','e','d',0};
static const WCHAR promptW[] = {'P','r','o','m','p','t',0};
static const WCHAR protectW[] = {'P','r','o','t','e','c','t','i','o','n',' ','l','e','v','e','l',0};
static const WCHAR useridW[] = {'U','s','e','r',' ','I','D',0};
static const WCHAR winhandleW[] = {'W','i','n','d','o','w',' ','H','a','n','d','l','e',0};
static const struct dbproperty dbproperties[] = {
{ asyncW, DBPROP_INIT_ASYNCH, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ bindW, DBPROP_INIT_BINDFLAGS, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ cacheW, DBPROP_AUTH_CACHE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
{ conn_timeout,DBPROP_INIT_TIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ datasourceW, DBPROP_INIT_DATASOURCE, DBPROPOPTIONS_REQUIRED, VT_BSTR },
{ extendedW, DBPROP_INIT_PROVIDERSTRING, DBPROPOPTIONS_REQUIRED, VT_BSTR },
{ encryptW, DBPROP_AUTH_ENCRYPT_PASSWORD, DBPROPOPTIONS_REQUIRED, VT_BOOL },
{ gen_timeout, DBPROP_INIT_GENERALTIMEOUT, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ impersonW, DBPROP_INIT_IMPERSONATION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ initcatW, DBPROP_CATALOGLOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
{ integratedW, DBPROP_AUTH_INTEGRATED, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
{ localeIDW, DBPROP_INIT_LCID, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ locationW, DBPROP_INIT_LOCATION, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
{ lockownerW, DBPROP_INIT_LOCKOWNER, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
{ maskpassW, DBPROP_AUTH_MASK_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
{ modeW, DBPROP_INIT_MODE, DBPROPOPTIONS_OPTIONAL, VT_I4, convert_dbproperty_mode },
{ oledbservW, DBPROP_INIT_OLEDBSERVICES, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ passwordW, DBPROP_AUTH_PASSWORD, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
{ persistEncW, DBPROP_AUTH_PERSIST_ENCRYPTED, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
{ persistW, DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO, DBPROPOPTIONS_OPTIONAL, VT_BOOL },
{ promptW, DBPROP_INIT_PROMPT, DBPROPOPTIONS_OPTIONAL, VT_I2 },
{ protectW, DBPROP_INIT_PROTECTION_LEVEL, DBPROPOPTIONS_OPTIONAL, VT_I4 },
{ useridW, DBPROP_AUTH_USERID, DBPROPOPTIONS_OPTIONAL, VT_BSTR },
#ifndef _WIN64
{ winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I4 },
#else
{ winhandleW, DBPROP_INIT_HWND, DBPROPOPTIONS_OPTIONAL, VT_I8 },
#endif
};
struct dbprop_pair
{
struct list entry;
BSTR name;
BSTR value;
};
struct dbprops
{
struct list props;
unsigned int count;
};
/* name/value strings are reused, so they shouldn't be freed after this call */
static HRESULT add_dbprop_to_list(struct dbprops *props, BSTR name, BSTR value)
{
struct dbprop_pair *pair;
pair = heap_alloc(sizeof(*pair));
if (!pair)
return E_OUTOFMEMORY;
pair->name = name;
pair->value = value;
list_add_tail(&props->props, &pair->entry);
props->count++;
return S_OK;
}
static void free_dbprop_list(struct dbprops *props)
{
struct dbprop_pair *p, *p2;
LIST_FOR_EACH_ENTRY_SAFE(p, p2, &props->props, struct dbprop_pair, entry)
{
list_remove(&p->entry);
SysFreeString(p->name);
SysFreeString(p->value);
heap_free(p);
}
}
static HRESULT parse_init_string(const WCHAR *initstring, struct dbprops *props)
{
const WCHAR *start;
WCHAR *eq;
HRESULT hr = S_OK;
list_init(&props->props);
props->count = 0;
start = initstring;
while (start && (eq = strchrW(start, '=')))
{
static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r',0};
WCHAR *delim, quote;
BSTR value, name;
name = SysAllocStringLen(start, eq - start);
/* skip equal sign to get value */
eq++;
quote = (*eq == '"' || *eq == '\'') ? *eq : 0;
if (quote)
{
/* for quoted value string, skip opening mark, look for terminating one */
eq++;
delim = strchrW(eq, quote);
}
else
delim = strchrW(eq, ';');
if (delim)
value = SysAllocStringLen(eq, delim - eq);
else
value = SysAllocString(eq);
/* skip semicolon if present */
if (delim)
{
if (*delim == quote)
delim++;
if (*delim == ';')
delim++;
}
start = delim;
if (!strcmpiW(name, providerW))
{
SysFreeString(name);
SysFreeString(value);
continue;
}
TRACE("property (name=%s, value=%s)\n", debugstr_w(name), debugstr_w(value));
hr = add_dbprop_to_list(props, name, value);
if (FAILED(hr))
{
SysFreeString(name);
SysFreeString(value);
break;
}
}
if (FAILED(hr))
free_dbprop_list(props);
return hr;
}
static const struct dbproperty *get_known_dprop_descr(BSTR name)
{
int min, max, n;
min = 0;
max = ARRAY_SIZE(dbproperties) - 1;
while (min <= max)
{
int r;
n = (min+max)/2;
r = strcmpiW(dbproperties[n].name, name);
if (!r)
break;
if (r < 0)
min = n+1;
else
max = n-1;
}
return (min <= max) ? &dbproperties[n] : NULL;
}
static HRESULT get_dbpropset_from_proplist(struct dbprops *props, DBPROPSET **propset)
{
struct dbprop_pair *pair;
int i = 0;
HRESULT hr;
*propset = CoTaskMemAlloc(sizeof(DBPROPSET));
if (!*propset)
return E_OUTOFMEMORY;
(*propset)->rgProperties = CoTaskMemAlloc(props->count*sizeof(DBPROP));
if (!(*propset)->rgProperties)
{
CoTaskMemFree(*propset);
*propset = NULL;
return E_OUTOFMEMORY;
}
(*propset)->cProperties = 0;
LIST_FOR_EACH_ENTRY(pair, &props->props, struct dbprop_pair, entry)
{
const struct dbproperty *descr = get_known_dprop_descr(pair->name);
VARIANT dest, src;
if (!descr)
{
static const WCHAR eqW[] = {'=',0};
BSTR str;
int len;
/* provider specific property is always VT_BSTR */
len = SysStringLen(pair->name) + SysStringLen(pair->value) + 1 /* for '=' */;
str = SysAllocStringLen(NULL, len);
strcpyW(str, pair->name);
strcatW(str, eqW);
strcatW(str, pair->value);
(*propset)->cProperties++;
(*propset)->guidPropertySet = DBPROPSET_DBINIT;
(*propset)->rgProperties[i].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
(*propset)->rgProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
(*propset)->rgProperties[i].dwStatus = 0;
memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
V_VT(&(*propset)->rgProperties[i].vValue) = VT_BSTR;
V_BSTR(&(*propset)->rgProperties[i].vValue) = str;
i++;
continue;
}
V_VT(&src) = VT_BSTR;
V_BSTR(&src) = pair->value;
VariantInit(&dest);
hr = VariantChangeType(&dest, &src, 0, descr->type);
if (FAILED(hr) && descr->convert_dbproperty)
hr = descr->convert_dbproperty(pair->value, &dest);
if (FAILED(hr))
{
ERR("failed to init property %s value as type %d\n", debugstr_w(pair->name), descr->type);
free_dbpropset(1, *propset);
*propset = NULL;
return hr;
}
(*propset)->cProperties++;
(*propset)->guidPropertySet = DBPROPSET_DBINIT;
(*propset)->rgProperties[i].dwPropertyID = descr->id;
(*propset)->rgProperties[i].dwOptions = descr->options;
(*propset)->rgProperties[i].dwStatus = 0;
memset(&(*propset)->rgProperties[i].colid, 0, sizeof(DBID));
(*propset)->rgProperties[i].vValue = dest;
i++;
}
return S_OK;
}
/*** IDataInitialize methods ***/
static void datasource_release(BOOL created, IUnknown **datasource)
{
if (!created)
return;
IUnknown_Release(*datasource);
*datasource = NULL;
}
static inline WCHAR *strdupW(const WCHAR *src)
{
WCHAR *dest;
if (!src) return NULL;
dest = heap_alloc((strlenW(src)+1)*sizeof(WCHAR));
if (dest)
strcpyW(dest, src);
return dest;
}
static WCHAR *strstriW(const WCHAR *str, const WCHAR *sub)
{
LPWSTR strlower, sublower, r;
strlower = CharLowerW(strdupW(str));
sublower = CharLowerW(strdupW(sub));
r = strstrW(strlower, sublower);
if (r)
r = (LPWSTR)str + (r - strlower);
heap_free(strlower);
heap_free(sublower);
return r;
}
HRESULT get_data_source(IUnknown *outer, DWORD clsctx, LPWSTR initstring, REFIID riid, IUnknown **datasource)
{
static const WCHAR providerW[] = {'P','r','o','v','i','d','e','r','=',0};
static const WCHAR msdasqlW[] = {'M','S','D','A','S','Q','L',0};
BOOL datasource_created = FALSE;
IDBProperties *dbprops;
DBPROPSET *propset;
WCHAR *prov = NULL;
CLSID provclsid;
HRESULT hr;
/* first get provider name */
provclsid = IID_NULL;
if (initstring && (prov = strstriW(initstring, providerW)))
{
WCHAR *start, *progid;
int len;
prov += ARRAY_SIZE(providerW)-1;
start = prov;
while (*prov && *prov != ';')
++prov;
TRACE("got provider %s\n", debugstr_wn(start, prov-start));
len = prov - start;
progid = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
if (!progid) return E_OUTOFMEMORY;
memcpy(progid, start, len*sizeof(WCHAR));
progid[len] = 0;
hr = CLSIDFromProgID(progid, &provclsid);
CoTaskMemFree(progid);
if (FAILED(hr))
{
ERR("provider %s not registered\n", debugstr_wn(start, prov-start));
return hr;
}
}
else
{
hr = CLSIDFromProgID(msdasqlW, &provclsid);
if (FAILED(hr))
ERR("ODBC provider for OLE DB not registered\n");
}
/* check for provider mismatch if it was specified in init string */
if (*datasource && prov)
{
DBPROPIDSET propidset;
enum DBPROPENUM prop;
CLSID initprov;
ULONG count;
hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
if (FAILED(hr))
{
WARN("provider doesn't support IDBProperties\n");
return hr;
}
prop = DBPROP_INIT_DATASOURCE;
propidset.rgPropertyIDs = ∝
propidset.cPropertyIDs = 1;
propidset.guidPropertySet = DBPROPSET_DBINIT;
count = 0;
propset = NULL;
hr = IDBProperties_GetProperties(dbprops, 1, &propidset, &count, &propset);
IDBProperties_Release(dbprops);
if (FAILED(hr))
{
WARN("GetProperties failed for datasource, 0x%08x\n", hr);
return hr;
}
TRACE("initial data source provider %s\n", debugstr_w(V_BSTR(&propset->rgProperties[0].vValue)));
initprov = IID_NULL;
hr = CLSIDFromProgID(V_BSTR(&propset->rgProperties[0].vValue), &initprov);
free_dbpropset(count, propset);
if (FAILED(hr) || !IsEqualIID(&provclsid, &initprov)) return DB_E_MISMATCHEDPROVIDER;
}
if (!*datasource)
{
if (!IsEqualIID(&provclsid, &IID_NULL))
hr = CoCreateInstance(&provclsid, outer, clsctx, riid, (void**)datasource);
if (FAILED(hr) && IsEqualIID(riid, &IID_IDBInitialize))
hr = create_db_init(datasource);
datasource_created = *datasource != NULL;
}
/* now set properties */
if (initstring)
{
struct dbprops props;
hr = IUnknown_QueryInterface(*datasource, &IID_IDBProperties, (void**)&dbprops);
if (FAILED(hr))
{
ERR("provider doesn't support IDBProperties\n");
datasource_release(datasource_created, datasource);
return hr;
}
hr = parse_init_string(initstring, &props);
if (FAILED(hr))
{
datasource_release(datasource_created, datasource);
return hr;
}
hr = get_dbpropset_from_proplist(&props, &propset);
free_dbprop_list(&props);
if (FAILED(hr))
{
datasource_release(datasource_created, datasource);
return hr;
}
hr = IDBProperties_SetProperties(dbprops, 1, propset);
IDBProperties_Release(dbprops);
free_dbpropset(1, propset);
if (FAILED(hr))
{
ERR("SetProperties failed, 0x%08x\n", hr);
datasource_release(datasource_created, datasource);
return hr;
}
}
return hr;
}
static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *outer, DWORD clsctx,
LPWSTR initstring, REFIID riid, IUnknown **datasource)
{
datainit *This = impl_from_IDataInitialize(iface);
TRACE("(%p)->(%p 0x%x %s %s %p)\n", This, outer, clsctx, debugstr_w(initstring), debugstr_guid(riid), datasource);
return get_data_source(outer, clsctx, initstring, riid, datasource);
}
/* returns character length of string representation */
static int get_propvalue_length(DBPROP *prop)
{
VARIANT str;
HRESULT hr;
if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
VariantInit(&str);
hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
if (hr == S_OK)
{
int len = SysStringLen(V_BSTR(&str));
VariantClear(&str);
return len;
}
return 0;
}
static void write_propvalue_str(WCHAR *str, DBPROP *prop)
{
VARIANT *v = &prop->vValue;
VARIANT vstr;
HRESULT hr;
if (V_VT(v) == VT_BSTR)
{
strcatW(str, V_BSTR(v));
return;
}
VariantInit(&vstr);
hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
if (hr == S_OK)
{
strcatW(str, V_BSTR(&vstr));
VariantClear(&vstr);
}
}
static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
{
ULONG i;
for (i = 0; i < propinfoset->cPropertyInfos; i++)
if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
return propinfoset->rgPropertyInfos[i].pwszDescription;
return NULL;
}
static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
{
ULONG i;
for (i = 0; i < count; i++)
{
ULONG p;
for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
CoTaskMemFree(propinfoset[i].rgPropertyInfos);
}
CoTaskMemFree(propinfoset);
}
static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
boolean include_pass, LPWSTR *init_string)
{
static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
static const WCHAR colW[] = {';',0};
datainit *This = impl_from_IDataInitialize(iface);
DBPROPINFOSET *propinfoset;
IDBProperties *props;
DBPROPIDSET propidset;
ULONG count, infocount;
WCHAR *progid, *desc;
DBPROPSET *propset;
IPersist *persist;
HRESULT hr;
CLSID clsid;
ULONG i, len;
TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
/* IPersist support is mandatory for data sources */
hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
if (FAILED(hr)) return hr;
memset(&clsid, 0, sizeof(clsid));
hr = IPersist_GetClassID(persist, &clsid);
IPersist_Release(persist);
if (FAILED(hr)) return hr;
progid = NULL;
ProgIDFromCLSID(&clsid, &progid);
TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
/* now get initialization properties */
hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
if (FAILED(hr))
{
WARN("IDBProperties not supported\n");
CoTaskMemFree(progid);
return hr;
}
propidset.rgPropertyIDs = NULL;
propidset.cPropertyIDs = 0;
propidset.guidPropertySet = DBPROPSET_DBINIT;
propset = NULL;
count = 0;
hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
if (FAILED(hr))
{
WARN("failed to get data source properties, 0x%08x\n", hr);
CoTaskMemFree(progid);
return hr;
}
infocount = 0;
IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
IDBProperties_Release(props);
/* check if we need to skip password */
len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
for (i = 0; i < count; i++)
{
WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
if (descr)
{
/* include '=' and ';' */
len += strlenW(descr) + 2;
len += get_propvalue_length(&propset->rgProperties[i]);
}
if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
(V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
include_pass = FALSE;
}
len *= sizeof(WCHAR);
*init_string = CoTaskMemAlloc(len);
*init_string[0] = 0;
/* provider name */
strcatW(*init_string, provW);
strcatW(*init_string, progid);
strcatW(*init_string, colW);
CoTaskMemFree(progid);
for (i = 0; i < count; i++)
{
WCHAR *descr;
if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
if (descr)
{
static const WCHAR eqW[] = {'=',0};
strcatW(*init_string, descr);
strcatW(*init_string, eqW);
write_propvalue_str(*init_string, &propset->rgProperties[i]);
strcatW(*init_string, colW);
}
}
free_dbpropset(count, propset);
free_dbpropinfoset(infocount, propinfoset);
CoTaskMemFree(desc);
if (!include_pass)
TRACE("%s\n", debugstr_w(*init_string));
return S_OK;
}
static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
IUnknown **datasource)
{
datainit *This = impl_from_IDataInitialize(iface);
TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
debugstr_guid(riid), datasource);
return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
}
static HRESULT WINAPI datainit_CreateDBInstanceEx(IDataInitialize *iface, REFCLSID provider, IUnknown *outer,
DWORD clsctx, LPWSTR reserved, COSERVERINFO *server_info, DWORD cmq, MULTI_QI *results)
{
datainit *This = impl_from_IDataInitialize(iface);
FIXME("(%p)->(%s %p %#x %s %p %u %p)\n", This, debugstr_guid(provider), outer, clsctx,
debugstr_w(reserved), server_info, cmq, results);
return E_NOTIMPL;
}
static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
LPWSTR *ppwszInitializationString)
{
datainit *This = impl_from_IDataInitialize(iface);
FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
return E_NOTIMPL;
}
static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
{
datainit *This = impl_from_IDataInitialize(iface);
FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
return E_NOTIMPL;
}
static const struct IDataInitializeVtbl datainit_vtbl =
{
datainit_QueryInterface,
datainit_AddRef,
datainit_Release,
datainit_GetDataSource,
datainit_GetInitializationString,
datainit_CreateDBInstance,
datainit_CreateDBInstanceEx,
datainit_LoadStringFromStorage,
datainit_WriteStringToStorage
};
HRESULT create_data_init(IUnknown *outer, void **obj)
{
datainit *This;
TRACE("(%p)\n", obj);
if(outer) return CLASS_E_NOAGGREGATION;
*obj = NULL;
This = heap_alloc(sizeof(*This));
if(!This) return E_OUTOFMEMORY;
This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
This->ref = 1;
*obj = &This->IDataInitialize_iface;
return S_OK;
}
|