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 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
|
//---------------------------------------------------------------------
// <copyright file="ArgumentValidation.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
namespace System.Data.Common.CommandTrees.ExpressionBuilder.Internal
{
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Common.CommandTrees;
using System.Data.Common.CommandTrees.Internal;
using System.Data.Common.Utils;
using System.Data.Metadata.Edm; // for TypeHelpers
using System.Diagnostics;
using System.Globalization;
using System.Linq;
internal static class ArgumentValidation
{
private static TypeUsage _booleanType = EdmProviderManifest.Instance.GetCanonicalModelTypeUsage(PrimitiveTypeKind.Boolean);
// The Metadata ReadOnlyCollection class conflicts with System.Collections.ObjectModel.ReadOnlyCollection...
internal static System.Collections.ObjectModel.ReadOnlyCollection<TElement> NewReadOnlyCollection<TElement>(IList<TElement> list)
{
return new System.Collections.ObjectModel.ReadOnlyCollection<TElement>(list);
}
private static void RequirePolymorphicType(TypeUsage type, string typeArgumentName)
{
Debug.Assert(type != null, "Ensure type is non-null before calling RequirePolymorphicType");
if (!TypeSemantics.IsPolymorphicType(type))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_General_PolymorphicTypeRequired(TypeHelpers.GetFullName(type)), "type");
}
}
private static void RequireCompatibleType(DbExpression expression, TypeUsage requiredResultType, string argumentName)
{
RequireCompatibleType(expression, requiredResultType, argumentName, -1);
}
private static void RequireCompatibleType(DbExpression expression, TypeUsage requiredResultType, string argumentName, int argumentIndex)
{
Debug.Assert(expression != null, "Ensure expression is non-null before checking for type compatibility");
Debug.Assert(requiredResultType != null, "Ensure type is non-null before checking for type compatibility");
if (!TypeSemantics.IsStructurallyEqualOrPromotableTo(expression.ResultType, requiredResultType))
{
// Don't call FormatIndex unless an exception is actually being thrown
if (argumentIndex != -1)
{
argumentName = StringUtil.FormatIndex(argumentName, argumentIndex);
}
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_ExpressionLink_TypeMismatch(
TypeHelpers.GetFullName(expression.ResultType),
TypeHelpers.GetFullName(requiredResultType)
),
argumentName
);
}
}
private static void RequireCompatibleType(DbExpression expression, PrimitiveTypeKind requiredResultType, string argumentName)
{
RequireCompatibleType(expression, requiredResultType, argumentName, -1);
}
private static void RequireCompatibleType(DbExpression expression, PrimitiveTypeKind requiredResultType, string argumentName, int index)
{
Debug.Assert(expression != null, "Ensure expression is non-null before checking for type compatibility");
PrimitiveTypeKind valueTypeKind;
bool valueIsPrimitive = TypeHelpers.TryGetPrimitiveTypeKind(expression.ResultType, out valueTypeKind);
if (!valueIsPrimitive ||
valueTypeKind != requiredResultType)
{
if (index != -1)
{
argumentName = StringUtil.FormatIndex(argumentName, index);
}
throw EntityUtil.Argument(
System.Data.Entity.Strings.Cqt_ExpressionLink_TypeMismatch(
(valueIsPrimitive ?
Enum.GetName(typeof(PrimitiveTypeKind), valueTypeKind)
: TypeHelpers.GetFullName(expression.ResultType)),
Enum.GetName(typeof(PrimitiveTypeKind), requiredResultType)
),
argumentName
);
}
}
private static void RequireCompatibleType(DbExpression from, RelationshipEndMember end, bool allowAllRelationshipsInSameTypeHierarchy)
{
Debug.Assert(from != null, "Ensure navigation source expression is non-null before calling RequireCompatibleType");
Debug.Assert(end != null, "Ensure navigation start end is non-null before calling RequireCompatibleType");
TypeUsage endType = end.TypeUsage;
if (!TypeSemantics.IsReferenceType(endType))
{
//
// The only relation end that is currently allowed to have a non-Reference type is the Child end of
// a composition, in which case the end type must be an entity type.
//
// Debug.Assert(end.Relation.IsComposition && !end.IsParent && (end.Type is EntityType), "Relation end can only have non-Reference type if it is a Composition child end");
endType = TypeHelpers.CreateReferenceTypeUsage(TypeHelpers.GetEdmType<EntityType>(endType));
}
if (allowAllRelationshipsInSameTypeHierarchy)
{
if (TypeHelpers.GetCommonTypeUsage(endType, from.ResultType) == null)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_RelNav_WrongSourceType(TypeHelpers.GetFullName(endType)), "from");
}
}
else if (!TypeSemantics.IsStructurallyEqualOrPromotableTo(from.ResultType.EdmType, endType.EdmType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_RelNav_WrongSourceType(TypeHelpers.GetFullName(endType)), "from");
}
}
private static void RequireCollectionArgument<TExpressionType>(DbExpression argument)
{
Debug.Assert(argument != null, "Validate argument is non-null before calling CheckCollectionArgument");
if (!TypeSemantics.IsCollectionType(argument.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Unary_CollectionRequired(typeof(TExpressionType).Name), "argument");
}
}
private static TypeUsage RequireCollectionArguments<TExpressionType>(DbExpression left, DbExpression right)
{
Debug.Assert(left != null && right != null, "Ensure left and right are non-null before calling RequireCollectionArguments");
if (!TypeSemantics.IsCollectionType(left.ResultType) || !TypeSemantics.IsCollectionType(right.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Binary_CollectionsRequired(typeof(TExpressionType).Name));
}
TypeUsage commonType = TypeHelpers.GetCommonTypeUsage(left.ResultType, right.ResultType);
if (null == commonType)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Binary_CollectionsRequired(typeof(TExpressionType).Name));
}
return commonType;
}
private static TypeUsage RequireComparableCollectionArguments<TExpressionType>(DbExpression left, DbExpression right)
{
TypeUsage resultType = RequireCollectionArguments<TExpressionType>(left, right);
if (!TypeHelpers.IsSetComparableOpType(TypeHelpers.GetElementTypeUsage(left.ResultType)))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_InvalidTypeForSetOperation(TypeHelpers.GetElementTypeUsage(left.ResultType).Identity, typeof(TExpressionType).Name), "left");
}
if (!TypeHelpers.IsSetComparableOpType(TypeHelpers.GetElementTypeUsage(right.ResultType)))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_InvalidTypeForSetOperation(TypeHelpers.GetElementTypeUsage(right.ResultType).Identity, typeof(TExpressionType).Name), "right");
}
return resultType;
}
private static EnumerableValidator<TElementIn, TElementOut, TResult> CreateValidator<TElementIn, TElementOut, TResult>(IEnumerable<TElementIn> argument, string argumentName, Func<TElementIn, int, TElementOut> convertElement, Func<List<TElementOut>, TResult> createResult)
{
EnumerableValidator<TElementIn, TElementOut, TResult> ret = new EnumerableValidator<TElementIn, TElementOut, TResult>(argument, argumentName);
ret.ConvertElement = convertElement;
ret.CreateResult = createResult;
return ret;
}
private static DbExpressionList CreateExpressionList(IEnumerable<DbExpression> arguments, string argumentName, Action<DbExpression, int> validationCallback)
{
return CreateExpressionList(arguments, argumentName, false, validationCallback);
}
private static DbExpressionList CreateExpressionList(IEnumerable<DbExpression> arguments, string argumentName, bool allowEmpty, Action<DbExpression, int> validationCallback)
{
var ev = CreateValidator(arguments, argumentName,
(exp, idx) =>
{
if (validationCallback != null)
{
validationCallback(exp, idx);
}
return exp;
},
expList => new DbExpressionList(expList)
);
ev.AllowEmpty = allowEmpty;
return ev.Validate();
}
private static DbExpressionList CreateExpressionList(IEnumerable<DbExpression> arguments, string argumentName, int expectedElementCount, Action<DbExpression, int> validationCallback)
{
var ev = CreateValidator(arguments, argumentName,
(exp, idx) =>
{
if (validationCallback != null)
{
validationCallback(exp, idx);
}
return exp;
},
(expList) => new DbExpressionList(expList)
);
ev.ExpectedElementCount = expectedElementCount;
ev.AllowEmpty = false;
return ev.Validate();
}
private static TypeUsage ValidateBinary(DbExpression left, DbExpression right)
{
EntityUtil.CheckArgumentNull(left, "left");
EntityUtil.CheckArgumentNull(right, "right");
return TypeHelpers.GetCommonTypeUsage(left.ResultType, right.ResultType);
}
private static void ValidateUnary(DbExpression argument)
{
EntityUtil.CheckArgumentNull(argument, "argument");
}
private static void ValidateTypeUnary(DbExpression argument, TypeUsage type, string typeArgumentName)
{
ValidateUnary(argument);
CheckType(type, typeArgumentName);
}
#region Bindings - Expression and Group
internal static TypeUsage ValidateBindAs(DbExpression input, string varName)
{
//
// Ensure no argument is null
//
EntityUtil.CheckArgumentNull(varName, "varName");
EntityUtil.CheckArgumentNull(input, "input");
//
// Ensure Variable name is non-empty
//
if (string.IsNullOrEmpty(varName))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Binding_VariableNameNotValid, "varName");
}
//
// Ensure the DbExpression has a collection result type
//
TypeUsage elementType = null;
if (!TypeHelpers.TryGetCollectionElementType(input.ResultType, out elementType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Binding_CollectionRequired, "input");
}
Debug.Assert(elementType.IsReadOnly, "DbExpressionBinding Expression ResultType has editable element type");
return elementType;
}
internal static TypeUsage ValidateGroupBindAs(DbExpression input, string varName, string groupVarName)
{
//
// Ensure no argument is null
//
EntityUtil.CheckArgumentNull(varName, "varName");
EntityUtil.CheckArgumentNull(groupVarName, "groupVarName");
EntityUtil.CheckArgumentNull(input, "input");
//
// Ensure Variable and Group names are both non-empty
//
if (string.IsNullOrEmpty(varName))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Binding_VariableNameNotValid, "varName");
}
if (string.IsNullOrEmpty(groupVarName))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GroupBinding_GroupVariableNameNotValid, "groupVarName");
}
//
// Ensure the DbExpression has a collection result type
//
TypeUsage elementType = null;
if (!TypeHelpers.TryGetCollectionElementType(input.ResultType, out elementType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GroupBinding_CollectionRequired, "input");
}
Debug.Assert((elementType.IsReadOnly), "DbGroupExpressionBinding Expression ResultType has editable element type");
return elementType;
}
#endregion
#region Aggregates and Sort Keys
private static FunctionParameter[] GetExpectedParameters(EdmFunction function)
{
Debug.Assert(function != null, "Ensure function is non-null before calling GetExpectedParameters");
return function.Parameters.Where(p => p.Mode == ParameterMode.In || p.Mode == ParameterMode.InOut).ToArray();
}
internal static DbExpressionList ValidateFunctionAggregate(EdmFunction function, IEnumerable<DbExpression> args)
{
//
// Verify that the aggregate function is from the metadata collection and data space of the command tree.
//
ArgumentValidation.CheckFunction(function);
// Verify that the function is actually a valid aggregate function.
// For now, only a single argument is allowed.
if (!TypeSemantics.IsAggregateFunction(function) || null == function.ReturnParameter)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Aggregate_InvalidFunction, "function");
}
FunctionParameter[] expectedParams = GetExpectedParameters(function);
DbExpressionList funcArgs = CreateExpressionList(args, "argument", expectedParams.Length, (exp, idx) =>
{
TypeUsage paramType = expectedParams[idx].TypeUsage;
TypeUsage elementType = null;
if (TypeHelpers.TryGetCollectionElementType(paramType, out elementType))
{
paramType = elementType;
}
ArgumentValidation.RequireCompatibleType(exp, paramType, "argument");
}
);
return funcArgs;
}
internal static DbExpressionList ValidateGroupAggregate(DbExpression argument)
{
EntityUtil.CheckArgumentNull(argument, "argument");
return new DbExpressionList(new[] { argument });
}
internal static void ValidateSortClause(DbExpression key)
{
EntityUtil.CheckArgumentNull(key, "key");
if (!TypeHelpers.IsValidSortOpKeyType(key.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Sort_OrderComparable, "key");
}
}
internal static void ValidateSortClause(DbExpression key, string collation)
{
ValidateSortClause(key);
EntityUtil.CheckArgumentNull(collation, "collation");
if (StringUtil.IsNullOrEmptyOrWhiteSpace(collation))
{
throw EntityUtil.ArgumentOutOfRange(System.Data.Entity.Strings.Cqt_Sort_EmptyCollationInvalid, "collation");
}
if (!TypeSemantics.IsPrimitiveType(key.ResultType, PrimitiveTypeKind.String))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Sort_NonStringCollationInvalid, "collation");
}
}
#endregion
#region DbLambda
internal static System.Collections.ObjectModel.ReadOnlyCollection<DbVariableReferenceExpression> ValidateLambda(IEnumerable<DbVariableReferenceExpression> variables, DbExpression body)
{
EntityUtil.CheckArgumentNull(body, "body");
var varVal = CreateValidator(variables, "variables",
(varExp, idx) =>
{
if (null == varExp)
{
throw EntityUtil.ArgumentNull(StringUtil.FormatIndex("variables", idx));
}
return varExp;
},
(varList) => new System.Collections.ObjectModel.ReadOnlyCollection<DbVariableReferenceExpression>(varList)
);
varVal.AllowEmpty = true;
varVal.GetName = (varDef, idx) => varDef.VariableName;
var result = varVal.Validate();
return result;
}
#endregion
#region Binding-based methods: All, Any, Cross|OuterApply, Cross|FullOuter|Inner|LeftOuterJoin, Filter, GroupBy, Project, Skip, Sort
private static void ValidateBinding(DbExpressionBinding binding, string argumentName)
{
EntityUtil.CheckArgumentNull(binding, argumentName);
}
private static void ValidateGroupBinding(DbGroupExpressionBinding binding, string argumentName)
{
EntityUtil.CheckArgumentNull(binding, argumentName);
}
private static void ValidateBound(DbExpressionBinding input, DbExpression argument, string argumentName)
{
ValidateBinding(input, "input");
EntityUtil.CheckArgumentNull(argument, argumentName);
}
internal static TypeUsage ValidateQuantifier(DbExpressionBinding input, DbExpression predicate)
{
ValidateBound(input, predicate, "predicate");
RequireCompatibleType(predicate, PrimitiveTypeKind.Boolean, "predicate");
return predicate.ResultType;
}
internal static TypeUsage ValidateApply(DbExpressionBinding input, DbExpressionBinding apply)
{
ValidateBinding(input, "input");
ValidateBinding(apply, "apply");
//
// Duplicate Input and Apply binding names are not allowed
//
if (input.VariableName.Equals(apply.VariableName, StringComparison.Ordinal))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Apply_DuplicateVariableNames);
}
//
// Initialize the result type
//
List<KeyValuePair<string, TypeUsage>> recordCols = new List<KeyValuePair<string, TypeUsage>>();
recordCols.Add(new KeyValuePair<string, TypeUsage>(input.VariableName, input.VariableType));
recordCols.Add(new KeyValuePair<string, TypeUsage>(apply.VariableName, apply.VariableType));
return CreateCollectionOfRowResultType(recordCols);
}
internal static System.Collections.ObjectModel.ReadOnlyCollection<DbExpressionBinding> ValidateCrossJoin(IEnumerable<DbExpressionBinding> inputs, out TypeUsage resultType)
{
//
// Ensure that the list of input expression bindings is non-null.
//
EntityUtil.CheckArgumentNull(inputs, "inputs");
//
// Validate the input expression bindings and build the column types for the record type
// that will be the element type of the collection of record type result type of the join.
//
List<DbExpressionBinding> inputList = new List<DbExpressionBinding>();
List<KeyValuePair<string, TypeUsage>> columns = new List<KeyValuePair<string, TypeUsage>>();
Dictionary<string, int> bindingNames = new Dictionary<string, int>();
IEnumerator<DbExpressionBinding> inputEnum = inputs.GetEnumerator();
int iPos = 0;
while (inputEnum.MoveNext())
{
DbExpressionBinding input = inputEnum.Current;
//
// Validate the DbExpressionBinding before accessing its properties
//
ValidateBinding(input, StringUtil.FormatIndex("inputs", iPos));
//
// Duplicate binding names are not allowed
//
int nameIndex = -1;
if (bindingNames.TryGetValue(input.VariableName, out nameIndex))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_CrossJoin_DuplicateVariableNames(nameIndex, iPos, input.VariableName));
}
inputList.Add(input);
bindingNames.Add(input.VariableName, iPos);
columns.Add(new KeyValuePair<string, TypeUsage>(input.VariableName, input.VariableType));
iPos++;
}
if (inputList.Count < 2)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_CrossJoin_AtLeastTwoInputs, "inputs");
}
//
// Initialize the result type
//
resultType = CreateCollectionOfRowResultType(columns);
//
// Initialize state
//
return inputList.AsReadOnly();
}
internal static TypeUsage ValidateJoin(DbExpressionBinding left, DbExpressionBinding right, DbExpression joinCondition)
{
//
// Validate
//
ValidateBinding(left, "left");
ValidateBinding(left, "right");
EntityUtil.CheckArgumentNull(joinCondition, "joinCondition");
//
// Duplicate Left and Right binding names are not allowed
//
if (left.VariableName.Equals(right.VariableName, StringComparison.Ordinal))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Join_DuplicateVariableNames);
}
//
// Validate the JoinCondition)
//
RequireCompatibleType(joinCondition, PrimitiveTypeKind.Boolean, "joinCondition");
//
// Initialize the result type
//
List<KeyValuePair<string, TypeUsage>> columns = new List<KeyValuePair<string, TypeUsage>>(2);
columns.Add(new KeyValuePair<string, TypeUsage>(left.VariableName, left.VariableType));
columns.Add(new KeyValuePair<string, TypeUsage>(right.VariableName, right.VariableType));
return CreateCollectionOfRowResultType(columns);
}
internal static TypeUsage ValidateFilter(DbExpressionBinding input, DbExpression predicate)
{
ValidateBound(input, predicate, "predicate");
RequireCompatibleType(predicate, PrimitiveTypeKind.Boolean, "predicate");
return input.Expression.ResultType;
}
internal static TypeUsage ValidateGroupBy(DbGroupExpressionBinding input, IEnumerable<KeyValuePair<string, DbExpression>> keys, IEnumerable<KeyValuePair<string, DbAggregate>> aggregates, out DbExpressionList validKeys, out System.Collections.ObjectModel.ReadOnlyCollection<DbAggregate> validAggregates)
{
//
// Validate the input set
//
ValidateGroupBinding(input, "input");
//
// Track the cumulative set of column names and types, as well as key column names
//
List<KeyValuePair<string, TypeUsage>> columns = new List<KeyValuePair<string, TypeUsage>>();
HashSet<string> keyNames = new HashSet<string>();
//
// Validate the grouping keys
//
var keyValidator = CreateValidator(keys, "keys",
(keyInfo, index) =>
{
ArgumentValidation.CheckNamed(keyInfo, "keys", index);
//
// The result Type of an expression used as a group key must be equality comparable
//
if (!TypeHelpers.IsValidGroupKeyType(keyInfo.Value.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GroupBy_KeyNotEqualityComparable(keyInfo.Key));
}
keyNames.Add(keyInfo.Key);
columns.Add(new KeyValuePair<string, TypeUsage>(keyInfo.Key, keyInfo.Value.ResultType));
return keyInfo.Value;
},
expList => new DbExpressionList(expList)
);
keyValidator.AllowEmpty = true;
keyValidator.GetName = (keyInfo, idx) => keyInfo.Key;
validKeys = keyValidator.Validate();
bool hasGroupAggregate = false;
var aggValidator = CreateValidator(aggregates, "aggregates",
(aggInfo, idx) =>
{
ArgumentValidation.CheckNamed(aggInfo, "aggregates", idx);
//
// Is there a grouping key with the same name?
//
if (keyNames.Contains(aggInfo.Key))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GroupBy_AggregateColumnExistsAsGroupColumn(aggInfo.Key));
}
//
// At most one group aggregate can be specified
//
if (aggInfo.Value is DbGroupAggregate)
{
if (hasGroupAggregate)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GroupBy_MoreThanOneGroupAggregate);
}
else
{
hasGroupAggregate = true;
}
}
columns.Add(new KeyValuePair<string, TypeUsage>(aggInfo.Key, aggInfo.Value.ResultType));
return aggInfo.Value;
},
aggList => NewReadOnlyCollection(aggList)
);
aggValidator.AllowEmpty = true;
aggValidator.GetName = (aggInfo, idx) => aggInfo.Key;
validAggregates = aggValidator.Validate();
//
// Either the Keys or Aggregates may be omitted, but not both
//
if (0 == validKeys.Count && 0 == validAggregates.Count)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GroupBy_AtLeastOneKeyOrAggregate);
}
//
// Create the result type. This is a collection of the record type produced by the group keys and aggregates.
//
return CreateCollectionOfRowResultType(columns);
}
internal static TypeUsage ValidateProject(DbExpressionBinding input, DbExpression projection)
{
ValidateBound(input, projection, "projection");
return CreateCollectionResultType(projection.ResultType);
}
/// <summary>
/// Validates the input and sort key arguments to both DbSkipExpression and DbSortExpression.
/// </summary>
/// <param name="input">A DbExpressionBinding that provides the collection to be ordered</param>
/// <param name="keys">A list of SortClauses that specifies the sort order to apply to the input collection</param>
private static System.Collections.ObjectModel.ReadOnlyCollection<DbSortClause> ValidateSortArguments(DbExpressionBinding input, IEnumerable<DbSortClause> sortOrder)
{
ValidateBinding(input, "input");
var ev = CreateValidator(sortOrder, "sortOrder",
(key, idx) => key,
keyList => NewReadOnlyCollection(keyList)
);
ev.AllowEmpty = false;
return ev.Validate();
}
internal static System.Collections.ObjectModel.ReadOnlyCollection<DbSortClause> ValidateSkip(DbExpressionBinding input, IEnumerable<DbSortClause> sortOrder, DbExpression count)
{
//
// Validate the input expression binding and sort keys
//
var sortKeys = ValidateSortArguments(input, sortOrder);
//
// Initialize the Count ExpressionLink. In addition to being non-null and from the same command tree,
// the Count expression must also have an integer result type.
//
EntityUtil.CheckArgumentNull(count, "count");
if (!TypeSemantics.IsIntegerNumericType(count.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Skip_IntegerRequired, "count");
}
//
// Currently the Count expression is also required to be either a DbConstantExpression or a DbParameterReferenceExpression.
//
if (count.ExpressionKind != DbExpressionKind.Constant &&
count.ExpressionKind != DbExpressionKind.ParameterReference)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Skip_ConstantOrParameterRefRequired, "count");
}
//
// For constants, verify the count is non-negative.
//
if (ArgumentValidation.IsConstantNegativeInteger(count))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Skip_NonNegativeCountRequired, "count");
}
return sortKeys;
}
internal static System.Collections.ObjectModel.ReadOnlyCollection<DbSortClause> ValidateSort(DbExpressionBinding input, IEnumerable<DbSortClause> sortOrder)
{
//
// Validate the input expression binding and sort keys
//
return ValidateSortArguments(input, sortOrder);
}
#endregion
#region Leaf Expressions - Null, Constant, Parameter, Scan
internal static void ValidateNull(TypeUsage nullType)
{
CheckType(nullType, "nullType");
}
internal static TypeUsage ValidateConstant(object value)
{
EntityUtil.CheckArgumentNull(value, "value");
//
// Check that typeof(value) is actually a valid constant (i.e. primitive) type
//
PrimitiveTypeKind primitiveTypeKind;
if (!ArgumentValidation.TryGetPrimitiveTypeKind(value.GetType(), out primitiveTypeKind))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Constant_InvalidType, "value");
}
return TypeHelpers.GetLiteralTypeUsage(primitiveTypeKind);
}
internal static void ValidateConstant(TypeUsage constantType, object value)
{
//
// Basic validation of constant value and constant type (non-null, read-only, etc)
//
EntityUtil.CheckArgumentNull(value, "value");
ArgumentValidation.CheckType(constantType, "constantType");
//
// Verify that constantType is a primitive or enum type and that the value is an instance of that type
// Note that the value is not validated against applicable facets (such as MaxLength for a string value),
// this is left to the server.
//
EnumType edmEnumType;
if(TypeHelpers.TryGetEdmType<EnumType>(constantType, out edmEnumType))
{
var clrEnumUnderlyingType = edmEnumType.UnderlyingType.ClrEquivalentType;
// type of the value has to match the edm enum type or underlying types have to be the same
if((value.GetType().IsEnum || clrEnumUnderlyingType != value.GetType()) && !ClrEdmEnumTypesMatch(edmEnumType, value.GetType()))
{
throw EntityUtil.Argument(
System.Data.Entity.Strings.Cqt_Constant_ClrEnumTypeDoesNotMatchEdmEnumType(
value.GetType().Name,
edmEnumType.Name,
clrEnumUnderlyingType.Name),
"value");
}
}
else
{
PrimitiveType primitiveType;
if (!TypeHelpers.TryGetEdmType<PrimitiveType>(constantType, out primitiveType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Constant_InvalidConstantType(constantType.ToString()), "constantType");
}
PrimitiveTypeKind valueKind;
if (!ArgumentValidation.TryGetPrimitiveTypeKind(value.GetType(), out valueKind) ||
primitiveType.PrimitiveTypeKind != valueKind)
{
// there are only two O-space types for the 16 C-space spatial types. Allow constants of any geography type to be represented as DbGeography, and
// any geometric type to be represented by Dbgeometry.
if (!(Helper.IsGeographicType(primitiveType) && valueKind == PrimitiveTypeKind.Geography)
&& !(Helper.IsGeometricType(primitiveType) && valueKind == PrimitiveTypeKind.Geometry))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Constant_InvalidValueForType(constantType.ToString()), "value");
}
}
}
}
internal static void ValidateParameter(TypeUsage type, string name)
{
ArgumentValidation.CheckType(type);
EntityUtil.CheckArgumentNull(name, "name");
if (!DbCommandTree.IsValidParameterName(name))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_CommandTree_InvalidParameterName(name), "name");
}
}
internal static TypeUsage ValidateScan(EntitySetBase entitySet)
{
ArgumentValidation.CheckEntitySet(entitySet, "targetSet");
return ArgumentValidation.CreateCollectionResultType(entitySet.ElementType);
}
internal static void ValidateVariable(TypeUsage type, string name)
{
CheckType(type);
EntityUtil.CheckArgumentNull(name, "name");
if (string.IsNullOrEmpty(name))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Binding_VariableNameNotValid, "name");
}
}
#endregion
#region Boolean Operators - And, Or, Not
internal static TypeUsage ValidateAnd(DbExpression left, DbExpression right)
{
TypeUsage resultType = ValidateBinary(left, right);
if (null == resultType || !TypeSemantics.IsPrimitiveType(resultType, PrimitiveTypeKind.Boolean))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_And_BooleanArgumentsRequired);
}
return resultType;
}
internal static TypeUsage ValidateOr(DbExpression left, DbExpression right)
{
TypeUsage resultType = ValidateBinary(left, right);
if (null == resultType || !TypeSemantics.IsPrimitiveType(resultType, PrimitiveTypeKind.Boolean))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Or_BooleanArgumentsRequired);
}
return resultType;
}
internal static TypeUsage ValidateNot(DbExpression argument)
{
EntityUtil.CheckArgumentNull(argument, "argument");
//
// Argument to Not must have Boolean result type
//
if (!TypeSemantics.IsPrimitiveType(argument.ResultType, PrimitiveTypeKind.Boolean))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Not_BooleanArgumentRequired);
}
return argument.ResultType;
}
#endregion
#region Arithmetic Operators
internal static DbExpressionList ValidateArithmetic(DbExpression argument, out TypeUsage resultType)
{
ValidateUnary(argument);
resultType = argument.ResultType;
if (!TypeSemantics.IsNumericType(resultType))
{
//
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Arithmetic_NumericCommonType);
}
//If argument to UnaryMinus is an unsigned type, promote return type to next higher, signed type.
if (TypeSemantics.IsUnsignedNumericType(argument.ResultType))
{
TypeUsage closestPromotableType = null;
if (TypeHelpers.TryGetClosestPromotableType(argument.ResultType, out closestPromotableType))
{
resultType = closestPromotableType;
}
else
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Arithmetic_InvalidUnsignedTypeForUnaryMinus(argument.ResultType.EdmType.FullName));
}
}
return new DbExpressionList(new[] { argument });
}
internal static DbExpressionList ValidateArithmetic(DbExpression left, DbExpression right, out TypeUsage resultType)
{
resultType = ValidateBinary(left, right);
if (null == resultType || !TypeSemantics.IsNumericType(resultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Arithmetic_NumericCommonType);
}
return new DbExpressionList(new[] { left, right });
}
#endregion
#region Comparison
internal static TypeUsage ValidateComparison(DbExpressionKind kind, DbExpression left, DbExpression right)
{
EntityUtil.CheckArgumentNull(left, "left");
EntityUtil.CheckArgumentNull(right, "right");
//
// A comparison of the specified kind must exist between the left and right arguments
//
bool equality = true;
bool order = true;
if (DbExpressionKind.GreaterThanOrEquals == kind ||
DbExpressionKind.LessThanOrEquals == kind)
{
equality = TypeSemantics.IsEqualComparableTo(left.ResultType, right.ResultType);
order = TypeSemantics.IsOrderComparableTo(left.ResultType, right.ResultType);
}
else if (DbExpressionKind.Equals == kind ||
DbExpressionKind.NotEquals == kind)
{
equality = TypeSemantics.IsEqualComparableTo(left.ResultType, right.ResultType);
}
else
{
order = TypeSemantics.IsOrderComparableTo(left.ResultType, right.ResultType);
}
if (!equality || !order)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Comparison_ComparableRequired);
}
return _booleanType;
}
internal static TypeUsage ValidateIsNull(DbExpression argument)
{
return ValidateIsNull(argument, false);
}
internal static TypeUsage ValidateIsNull(DbExpression argument, bool allowRowType)
{
EntityUtil.CheckArgumentNull(argument, "argument");
//
// The argument cannot be of a collection type
//
if (TypeSemantics.IsCollectionType(argument.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_IsNull_CollectionNotAllowed);
}
//
// ensure argument type is valid for this operation
//
if (!TypeHelpers.IsValidIsNullOpType(argument.ResultType))
{
//
if (!allowRowType || !TypeSemantics.IsRowType(argument.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_IsNull_InvalidType);
}
}
return _booleanType;
}
internal static TypeUsage ValidateLike(DbExpression argument, DbExpression pattern)
{
EntityUtil.CheckArgumentNull(argument, "argument");
EntityUtil.CheckArgumentNull(pattern, "pattern");
RequireCompatibleType(argument, PrimitiveTypeKind.String, "argument");
RequireCompatibleType(pattern, PrimitiveTypeKind.String, "pattern");
return _booleanType;
}
internal static TypeUsage ValidateLike(DbExpression argument, DbExpression pattern, DbExpression escape)
{
TypeUsage resultType = ValidateLike(argument, pattern);
EntityUtil.CheckArgumentNull(escape, "escape");
RequireCompatibleType(escape, PrimitiveTypeKind.String, "escape");
return resultType;
}
#endregion
#region Type Operators - Cast, Treat, OfType, OfTypeOnly, IsOf, IsOfOnly
internal static void ValidateCastTo(DbExpression argument, TypeUsage toType)
{
ValidateTypeUnary(argument, toType, "toType");
//
// Verify that the cast is allowed
//
if (!TypeSemantics.IsCastAllowed(argument.ResultType, toType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Cast_InvalidCast(TypeHelpers.GetFullName(argument.ResultType), TypeHelpers.GetFullName(toType)));
}
}
internal static void ValidateTreatAs(DbExpression argument, TypeUsage asType)
{
ValidateTypeUnary(argument, asType, "asType");
//
// Verify the type to treat as. Treat-As (NullType) is not allowed.
//
RequirePolymorphicType(asType, "asType");
//
// Verify that the Treat operation is allowed
//
if (!TypeSemantics.IsValidPolymorphicCast(argument.ResultType, asType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_General_PolymorphicArgRequired(typeof(DbTreatExpression).Name));
}
}
internal static TypeUsage ValidateOfType(DbExpression argument, TypeUsage type)
{
ValidateTypeUnary(argument, type, "type");
//
// Ensure that the type is non-null and valid - from the same metadata collection and dataspace and the command tree.
// The type is also not allowed to be NullType.
//
RequirePolymorphicType(type, "type");
//
// Ensure that the argument is actually of a collection type.
//
RequireCollectionArgument<DbOfTypeExpression>(argument);
//
// Verify that the OfType operation is allowed
//
TypeUsage elementType = null;
if (!TypeHelpers.TryGetCollectionElementType(argument.ResultType, out elementType) ||
!TypeSemantics.IsValidPolymorphicCast(elementType, type))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_General_PolymorphicArgRequired(typeof(DbOfTypeExpression).Name));
}
//
// The type of this DbExpression is a new collection type based on the requested element type.
//
return CreateCollectionResultType(type);
}
internal static TypeUsage ValidateIsOf(DbExpression argument, TypeUsage type)
{
ValidateTypeUnary(argument, type, "type");
//
// Ensure the ofType is non-null, associated with the correct metadata workspace/dataspace,
// is not NullType, and is polymorphic
//
RequirePolymorphicType(type, "type");
//
// Verify that the IsOf operation is allowed
//
if (!TypeSemantics.IsValidPolymorphicCast(argument.ResultType, type))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_General_PolymorphicArgRequired(typeof(DbIsOfExpression).Name));
}
return _booleanType;
}
#endregion
#region Ref Operators - Deref, EntityRef, Ref, RefKey, RelationshipNavigation
internal static TypeUsage ValidateDeref(DbExpression argument)
{
ValidateUnary(argument);
//
// Ensure that the operand is actually of a reference type.
//
EntityType entityType;
if (!TypeHelpers.TryGetRefEntityType(argument.ResultType, out entityType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_DeRef_RefRequired, "argument");
}
//
// Result Type is the element type of the reference type
//
return CreateResultType(entityType);
}
internal static TypeUsage ValidateGetEntityRef(DbExpression argument)
{
ValidateUnary(argument);
EntityType entityType = null;
if (!TypeHelpers.TryGetEdmType<EntityType>(argument.ResultType, out entityType) || null == entityType)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GetEntityRef_EntityRequired, "argument");
}
return CreateReferenceResultType(entityType);
}
internal static TypeUsage ValidateCreateRef(EntitySet entitySet, IEnumerable<DbExpression> keyValues, out DbExpression keyConstructor)
{
EntityUtil.CheckArgumentNull(entitySet, "entitySet");
return ValidateCreateRef(entitySet, entitySet.ElementType, keyValues, out keyConstructor);
}
internal static TypeUsage ValidateCreateRef(EntitySet entitySet, EntityType entityType, IEnumerable<DbExpression> keyValues, out DbExpression keyConstructor)
{
CheckEntitySet(entitySet, "entitySet");
CheckType(entityType, "entityType");
//
// Verify that the specified return type of the Ref operation is actually in
// the same hierarchy as the Entity type of the specified Entity set.
//
if (!TypeSemantics.IsValidPolymorphicCast(entitySet.ElementType, entityType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Ref_PolymorphicArgRequired);
}
// Validate the key values. The count of values must match the count of key members,
// and each key value must have a result type that is compatible with the type of
// the corresponding key member.
IList<EdmMember> keyMembers = entityType.KeyMembers;
var keyValueValidator = CreateValidator(keyValues, "keyValues",
(valueExp, idx) =>
{
RequireCompatibleType(valueExp, keyMembers[idx].TypeUsage, "keyValues", idx);
return new KeyValuePair<string, DbExpression>(keyMembers[idx].Name, valueExp);
},
(columnList) => columnList
);
keyValueValidator.ExpectedElementCount = keyMembers.Count;
var keyColumns = keyValueValidator.Validate();
keyConstructor = DbExpressionBuilder.NewRow(keyColumns);
return CreateReferenceResultType(entityType);
}
internal static TypeUsage ValidateRefFromKey(EntitySet entitySet, DbExpression keyValues)
{
EntityUtil.CheckArgumentNull(entitySet, "entitySet");
return ValidateRefFromKey(entitySet, keyValues, entitySet.ElementType);
}
internal static TypeUsage ValidateRefFromKey(EntitySet entitySet, DbExpression keyValues, EntityType entityType)
{
CheckEntitySet(entitySet, "entitySet");
EntityUtil.CheckArgumentNull(keyValues, "keyValues");
CheckType(entityType);
//
// Verify that the specified return type of the Ref operation is actually in
// the same hierarchy as the Entity type of the specified Entity set.
//
if (!TypeSemantics.IsValidPolymorphicCast(entitySet.ElementType, entityType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Ref_PolymorphicArgRequired);
}
//
// The Argument DbExpression must construct a set of values of the same types as the Key members of the Entity
// The names of the columns in the record type constructed by the Argument are not important, only that the
// number of columns is the same as the number of Key members and that for each Key member the corresponding
// column (based on order) is of a promotable type.
// To enforce this, the argument's result type is compared to a record type based on the names and types of
// the Key members. Since the promotability check used in RequireCompatibleType will ignore the names of the
// expected type's columns, RequireCompatibleType will therefore enforce the required level of type correctness
//
// Set the expected type to be the record type created based on the Key members
//
TypeUsage keyType = CreateResultType(TypeHelpers.CreateKeyRowType(entitySet.ElementType));
RequireCompatibleType(keyValues, keyType, "keyValues");
return CreateReferenceResultType(entityType);
}
internal static TypeUsage ValidateGetRefKey(DbExpression argument)
{
ValidateUnary(argument);
RefType refType = null;
if (!TypeHelpers.TryGetEdmType<RefType>(argument.ResultType, out refType) || null == refType)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_GetRefKey_RefRequired, "argument");
}
// RefType is responsible for basic validation of ElementType
Debug.Assert(refType.ElementType != null, "RefType constructor allowed null ElementType?");
return CreateResultType(TypeHelpers.CreateKeyRowType(refType.ElementType));
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal static TypeUsage ValidateNavigate(DbExpression navigateFrom, RelationshipType type, string fromEndName, string toEndName, out RelationshipEndMember fromEnd, out RelationshipEndMember toEnd)
{
EntityUtil.CheckArgumentNull(navigateFrom, "navigateFrom");
//
// Ensure that the relation type is non-null and from the same metadata workspace as the command tree
//
CheckType(type);
//
// Verify that the from and to relation end names are not null
//
EntityUtil.CheckArgumentNull(fromEndName, "fromEndName");
EntityUtil.CheckArgumentNull(toEndName, "toEndName");
//
// Retrieve the relation end properties with the specified 'from' and 'to' names
//
if (!type.RelationshipEndMembers.TryGetValue(fromEndName, false /*ignoreCase*/, out fromEnd))
{
throw EntityUtil.ArgumentOutOfRange(System.Data.Entity.Strings.Cqt_Factory_NoSuchRelationEnd, fromEndName);
}
if (!type.RelationshipEndMembers.TryGetValue(toEndName, false /*ignoreCase*/, out toEnd))
{
throw EntityUtil.ArgumentOutOfRange(System.Data.Entity.Strings.Cqt_Factory_NoSuchRelationEnd, toEndName);
}
//
// Validate the retrieved relation end against the navigation source
//
RequireCompatibleType(navigateFrom, fromEnd, allowAllRelationshipsInSameTypeHierarchy: false);
return CreateResultType(toEnd);
}
internal static TypeUsage ValidateNavigate(DbExpression navigateFrom, RelationshipEndMember fromEnd, RelationshipEndMember toEnd, out RelationshipType relType, bool allowAllRelationshipsInSameTypeHierarchy)
{
EntityUtil.CheckArgumentNull(navigateFrom, "navigateFrom");
//
// Validate the relationship ends before use
//
CheckMember(fromEnd, "fromEnd");
CheckMember(toEnd, "toEnd");
relType = fromEnd.DeclaringType as RelationshipType;
//
// Ensure that the relation type is non-null and read-only
//
CheckType(relType);
//
// Validate that the 'to' relationship end is defined by the same relationship type as the 'from' end
//
if (!relType.Equals(toEnd.DeclaringType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Factory_IncompatibleRelationEnds, "toEnd");
}
RequireCompatibleType(navigateFrom, fromEnd, allowAllRelationshipsInSameTypeHierarchy);
return CreateResultType(toEnd);
}
#endregion
#region Unary and Binary Set Operators - Distinct, Element, IsEmpty, Except, Intersect, UnionAll, Limit
internal static TypeUsage ValidateDistinct(DbExpression argument)
{
ValidateUnary(argument);
//
// Ensure that the Argument is of a collection type
//
RequireCollectionArgument<DbDistinctExpression>(argument);
//
// Ensure that the Distinct operation is valid for the input
//
CollectionType inputType = TypeHelpers.GetEdmType<CollectionType>(argument.ResultType);
if (!TypeHelpers.IsValidDistinctOpType(inputType.TypeUsage))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Distinct_InvalidCollection, "argument");
}
return argument.ResultType;
}
internal static TypeUsage ValidateElement(DbExpression argument)
{
ValidateUnary(argument);
//
// Ensure that the operand is actually of a collection type.
//
RequireCollectionArgument<DbElementExpression>(argument);
//
// Result Type is the element type of the collection type
//
return TypeHelpers.GetEdmType<CollectionType>(argument.ResultType).TypeUsage;
}
internal static TypeUsage ValidateIsEmpty(DbExpression argument)
{
ValidateUnary(argument);
//
// Ensure that the Argument is of a collection type
//
RequireCollectionArgument<DbIsEmptyExpression>(argument);
return _booleanType;
}
internal static TypeUsage ValidateExcept(DbExpression left, DbExpression right)
{
ValidateBinary(left, right);
//
// Ensures the left and right operands are each of a comparable collection type
//
RequireComparableCollectionArguments<DbExceptExpression>(left, right);
return left.ResultType;
}
internal static TypeUsage ValidateIntersect(DbExpression left, DbExpression right)
{
ValidateBinary(left, right);
//
// Ensures the left and right operands are each of a comparable collection type
//
return RequireComparableCollectionArguments<DbIntersectExpression>(left, right);
}
internal static TypeUsage ValidateUnionAll(DbExpression left, DbExpression right)
{
ValidateBinary(left, right);
//
// Ensure that the left and right operands are each of a collection type and that a common type exists for those types.
//
return RequireCollectionArguments<DbUnionAllExpression>(left, right);
}
internal static TypeUsage ValidateLimit(DbExpression argument, DbExpression limit)
{
//
// Initialize the Argument ExpressionLink. In addition to being non-null and from the same command tree,
// the Argument expression must have a collection result type.
//
EntityUtil.CheckArgumentNull(argument, "argument");
RequireCollectionArgument<DbLimitExpression>(argument);
//
// Initialize the Limit ExpressionLink. In addition to being non-null and from the same command tree,
// the Limit expression must also have an integer result type.
//
EntityUtil.CheckArgumentNull(limit, "count");
if (!TypeSemantics.IsIntegerNumericType(limit.ResultType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Limit_IntegerRequired, "limit");
}
//
// Currently the Limit expression is also required to be either a DbConstantExpression or a DbParameterReferenceExpression.
//
if (limit.ExpressionKind != DbExpressionKind.Constant &&
limit.ExpressionKind != DbExpressionKind.ParameterReference)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Limit_ConstantOrParameterRefRequired, "limit");
}
//
// For constants, verify the limit is non-negative.
//
if (ArgumentValidation.IsConstantNegativeInteger(limit))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Limit_NonNegativeLimitRequired, "limit");
}
return argument.ResultType;
}
#endregion
#region General Operators - Case, Function, NewInstance, Property
internal static TypeUsage ValidateCase(IEnumerable<DbExpression> whenExpressions, IEnumerable<DbExpression> thenExpressions, DbExpression elseExpression, out DbExpressionList validWhens, out DbExpressionList validThens)
{
EntityUtil.CheckArgumentNull(whenExpressions, "whenExpressions");
EntityUtil.CheckArgumentNull(thenExpressions, "thenExpressions");
EntityUtil.CheckArgumentNull(elseExpression, "elseExpression");
//
// All 'When's must produce a Boolean result, and a common (non-null) result type must exist
// for all 'Thens' and 'Else'. At least one When/Then clause is required and the number of
// 'When's must equal the number of 'Then's.
//
validWhens = CreateExpressionList(whenExpressions, "whenExpressions", (exp, idx) =>
{
RequireCompatibleType(exp, PrimitiveTypeKind.Boolean, "whenExpressions", idx);
}
);
Debug.Assert(validWhens.Count > 0, "CreateExpressionList(arguments, argumentName, validationCallback) allowed empty Whens?");
TypeUsage commonResultType = null;
validThens = CreateExpressionList(thenExpressions, "thenExpressions", (exp, idx) =>
{
if (null == commonResultType)
{
commonResultType = exp.ResultType;
}
else
{
commonResultType = TypeHelpers.GetCommonTypeUsage(exp.ResultType, commonResultType);
if (null == commonResultType)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Case_InvalidResultType);
}
}
}
);
Debug.Assert(validWhens.Count > 0, "CreateExpressionList(arguments, argumentName, validationCallback) allowed empty Thens?");
commonResultType = TypeHelpers.GetCommonTypeUsage(elseExpression.ResultType, commonResultType);
if (null == commonResultType)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Case_InvalidResultType);
}
//
// The number of 'When's must equal the number of 'Then's.
//
if (validWhens.Count != validThens.Count)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Case_WhensMustEqualThens);
}
//
// The result type of DbCaseExpression is the common result type
//
return commonResultType;
}
internal static TypeUsage ValidateFunction(EdmFunction function, IEnumerable<DbExpression> arguments, out DbExpressionList validArgs)
{
//
// Ensure that the function metadata is non-null and from the same metadata workspace and dataspace as the command tree.
CheckFunction(function);
//
// Non-composable functions or non-UDF functions including command text are not permitted in expressions -- they can only be
// executed independently
//
if (!function.IsComposableAttribute)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Function_NonComposableInExpression, "function");
}
if (!String.IsNullOrEmpty(function.CommandTextAttribute) && !function.HasUserDefinedBody)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Function_CommandTextInExpression, "function");
}
//
// Functions that return void are not allowed
//
if (null == function.ReturnParameter)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Function_VoidResultInvalid, "function");
}
//
// Validate the arguments
//
FunctionParameter[] expectedParams = GetExpectedParameters(function);
validArgs = CreateExpressionList(arguments, "arguments", expectedParams.Length, (exp, idx) =>
{
ArgumentValidation.RequireCompatibleType(exp, expectedParams[idx].TypeUsage, "arguments", idx);
}
);
return function.ReturnParameter.TypeUsage;
}
internal static TypeUsage ValidateInvoke(DbLambda lambda, IEnumerable<DbExpression> arguments, out DbExpressionList validArguments)
{
EntityUtil.CheckArgumentNull(lambda, "lambda");
EntityUtil.CheckArgumentNull(arguments, "arguments");
// Each argument must be type-compatible with the corresponding lambda variable for which it supplies the value
validArguments = null;
var argValidator = CreateValidator(arguments, "arguments", (exp, idx) =>
{
RequireCompatibleType(exp, lambda.Variables[idx].ResultType, "arguments", idx);
return exp;
},
expList => new DbExpressionList(expList)
);
argValidator.ExpectedElementCount = lambda.Variables.Count;
validArguments = argValidator.Validate();
// The result type of the lambda expression is the result type of the lambda body
return lambda.Body.ResultType;
}
internal static TypeUsage ValidateNewCollection(IEnumerable<DbExpression> elements, out DbExpressionList validElements)
{
TypeUsage commonElementType = null;
validElements = CreateExpressionList(elements, "elements", (exp, idx) =>
{
if (commonElementType == null)
{
commonElementType = exp.ResultType;
}
else
{
commonElementType = TypeSemantics.GetCommonType(commonElementType, exp.ResultType);
}
if (null == commonElementType)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Factory_NewCollectionInvalidCommonType, "collectionElements");
}
}
);
Debug.Assert(validElements.Count > 0, "CreateExpressionList(arguments, argumentName, validationCallback) allowed empty elements list?");
return CreateCollectionResultType(commonElementType);
}
internal static TypeUsage ValidateNewEmptyCollection(TypeUsage collectionType, out DbExpressionList validElements)
{
CheckType(collectionType, "collectionType");
if (!TypeSemantics.IsCollectionType(collectionType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_NewInstance_CollectionTypeRequired, "collectionType");
}
//
validElements = new DbExpressionList(new DbExpression[] { });
return collectionType;
}
internal static TypeUsage ValidateNewRow(IEnumerable<KeyValuePair<string, DbExpression>> columnValues, out DbExpressionList validElements)
{
List<KeyValuePair<string, TypeUsage>> columnTypes = new List<KeyValuePair<string, TypeUsage>>();
var columnValidator = CreateValidator(columnValues, "columnValues", (columnValue, idx) =>
{
CheckNamed(columnValue, "columnValues", idx);
columnTypes.Add(new KeyValuePair<string, TypeUsage>(columnValue.Key, columnValue.Value.ResultType));
return columnValue.Value;
},
expList => new DbExpressionList(expList)
);
columnValidator.GetName = ((columnValue, idx) => columnValue.Key);
validElements = columnValidator.Validate();
return CreateResultType(TypeHelpers.CreateRowType(columnTypes));
}
internal static TypeUsage ValidateNew(TypeUsage instanceType, IEnumerable<DbExpression> arguments, out DbExpressionList validArguments)
{
//
// Ensure that the type is non-null, valid and not NullType
//
CheckType(instanceType, "instanceType");
CollectionType collectionType = null;
if (TypeHelpers.TryGetEdmType<CollectionType>(instanceType, out collectionType) &&
collectionType != null)
{
// Collection arguments may have zero count for empty collection construction
TypeUsage elementType = collectionType.TypeUsage;
validArguments = CreateExpressionList(arguments, "arguments", true, (exp, idx) =>
{
RequireCompatibleType(exp, elementType, "arguments", idx);
});
}
else
{
List<TypeUsage> expectedTypes = GetStructuralMemberTypes(instanceType);
int pos = 0;
validArguments = CreateExpressionList(arguments, "arguments", expectedTypes.Count, (exp, idx) =>
{
RequireCompatibleType(exp, expectedTypes[pos++], "arguments", idx);
});
}
return instanceType;
}
private static List<TypeUsage> GetStructuralMemberTypes(TypeUsage instanceType)
{
StructuralType structType = instanceType.EdmType as StructuralType;
if (null == structType)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_NewInstance_StructuralTypeRequired, "instanceType");
}
if (structType.Abstract)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_NewInstance_CannotInstantiateAbstractType(TypeHelpers.GetFullName(instanceType)), "instanceType");
}
var members = TypeHelpers.GetAllStructuralMembers(structType);
if (members == null || members.Count < 1)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_NewInstance_CannotInstantiateMemberlessType(TypeHelpers.GetFullName(instanceType)), "instanceType");
}
List<TypeUsage> memberTypes = new List<TypeUsage>(members.Count);
for (int idx = 0; idx < members.Count; idx++)
{
memberTypes.Add(Helper.GetModelTypeUsage(members[idx]));
}
return memberTypes;
}
internal static TypeUsage ValidateNewEntityWithRelationships(EntityType entityType, IEnumerable<DbExpression> attributeValues, IList<DbRelatedEntityRef> relationships, out DbExpressionList validArguments, out System.Collections.ObjectModel.ReadOnlyCollection<DbRelatedEntityRef> validRelatedRefs)
{
EntityUtil.CheckArgumentNull(entityType, "entityType");
EntityUtil.CheckArgumentNull(attributeValues, "attributeValues");
EntityUtil.CheckArgumentNull(relationships, "relationships");
TypeUsage resultType = CreateResultType(entityType);
resultType = ArgumentValidation.ValidateNew(resultType, attributeValues, out validArguments);
if (relationships.Count > 0)
{
List<DbRelatedEntityRef> relatedRefs = new List<DbRelatedEntityRef>(relationships.Count);
for (int idx = 0; idx < relationships.Count; idx++)
{
DbRelatedEntityRef relatedRef = relationships[idx];
EntityUtil.CheckArgumentNull(relatedRef, StringUtil.FormatIndex("relationships", idx));
// The source end type must be the same type or a supertype of the Entity instance type
EntityTypeBase expectedSourceType = TypeHelpers.GetEdmType<RefType>(relatedRef.SourceEnd.TypeUsage).ElementType;
//
if (!entityType.EdmEquals(expectedSourceType) &&
!entityType.IsSubtypeOf(expectedSourceType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_NewInstance_IncompatibleRelatedEntity_SourceTypeNotValid, StringUtil.FormatIndex("relationships", idx));
}
relatedRefs.Add(relatedRef);
}
validRelatedRefs = relatedRefs.AsReadOnly();
}
else
{
validRelatedRefs = new System.Collections.ObjectModel.ReadOnlyCollection<DbRelatedEntityRef>(new DbRelatedEntityRef[] { });
}
return resultType;
}
internal static TypeUsage ValidateProperty(DbExpression instance, EdmMember property, string propertyArgumentName)
{
//
// Validate the member
//
CheckMember(property, propertyArgumentName);
//
// Validate the instance
//
if (null == instance)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Property_InstanceRequiredForInstance, "instance");
}
TypeUsage expectedInstanceType = TypeUsage.Create(property.DeclaringType);
RequireCompatibleType(instance, expectedInstanceType, "instance");
Debug.Assert(null != Helper.GetModelTypeUsage(property), "EdmMember metadata has a TypeUsage of null");
return Helper.GetModelTypeUsage(property);
}
internal static TypeUsage ValidateProperty(DbExpression instance, string propertyName, bool ignoreCase, out EdmMember foundMember)
{
EntityUtil.CheckArgumentNull(instance, "instance");
EntityUtil.CheckArgumentNull(propertyName, "propertyName");
//
// EdmProperty, NavigationProperty and RelationshipEndMember are the only valid members for DbPropertyExpression.
// Since these all derive from EdmMember they are declared by subtypes of StructuralType,
// so a non-StructuralType instance is invalid.
//
StructuralType structType;
if (TypeHelpers.TryGetEdmType<StructuralType>(instance.ResultType, out structType))
{
//
// Does the type declare a member with the given name?
//
if (structType.Members.TryGetValue(propertyName, ignoreCase, out foundMember) &&
foundMember != null)
{
//
// If the member is a RelationshipEndMember, call the corresponding overload.
//
if (Helper.IsRelationshipEndMember(foundMember) ||
Helper.IsEdmProperty(foundMember) ||
Helper.IsNavigationProperty(foundMember))
{
return Helper.GetModelTypeUsage(foundMember);
}
}
}
throw EntityUtil.ArgumentOutOfRange(System.Data.Entity.Strings.Cqt_Factory_NoSuchProperty(propertyName, TypeHelpers.GetFullName(instance.ResultType)), "propertyName");
}
#endregion
private static void CheckNamed<T>(KeyValuePair<string, T> element, string argumentName, int index)
{
if (string.IsNullOrEmpty(element.Key))
{
if (index != -1)
{
argumentName = StringUtil.FormatIndex(argumentName, index);
}
throw EntityUtil.ArgumentNull(string.Format(CultureInfo.InvariantCulture, "{0}.Key", argumentName));
}
if (null == element.Value)
{
if (index != -1)
{
argumentName = StringUtil.FormatIndex(argumentName, index);
}
throw EntityUtil.ArgumentNull(string.Format(CultureInfo.InvariantCulture, "{0}.Value", argumentName));
}
}
private static void CheckReadOnly(GlobalItem item, string varName)
{
EntityUtil.CheckArgumentNull(item, varName);
if (!(item.IsReadOnly))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_General_MetadataNotReadOnly, varName);
}
}
private static void CheckReadOnly(TypeUsage item, string varName)
{
EntityUtil.CheckArgumentNull(item, varName);
if (!(item.IsReadOnly))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_General_MetadataNotReadOnly, varName);
}
}
private static void CheckReadOnly(EntitySetBase item, string varName)
{
EntityUtil.CheckArgumentNull(item, varName);
if (!(item.IsReadOnly))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_General_MetadataNotReadOnly, varName);
}
}
private static void CheckType(EdmType type)
{
CheckType(type, "type");
}
private static void CheckType(EdmType type, string argumentName)
{
EntityUtil.CheckArgumentNull(type, argumentName);
CheckReadOnly(type, argumentName);
}
/// <summary>
/// Ensures that the specified type is non-null, associated with the correct metadata workspace/dataspace, and is not NullType.
/// </summary>
/// <param name="type">The type usage instance to verify.</param>
/// <exception cref="ArgumentNullException">If the specified type metadata is null</exception>
/// <exception cref="ArgumentException">If the specified type metadata belongs to a metadata workspace other than the workspace of the command tree</exception>
/// <exception cref="ArgumentException">If the specified type metadata belongs to a dataspace other than the dataspace of the command tree</exception>
private static void CheckType(TypeUsage type)
{
CheckType(type, "type");
}
private static void CheckType(TypeUsage type, string varName)
{
EntityUtil.CheckArgumentNull(type, varName);
CheckReadOnly(type, varName);
// TypeUsage constructor is responsible for basic validation of EdmType
Debug.Assert(type.EdmType != null, "TypeUsage constructor allowed null EdmType?");
if (!CheckDataSpace(type))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_TypeUsageIncorrectSpace, "type");
}
}
/// <summary>
/// Verifies that the specified member is valid - non-null, from the same metadata workspace and data space as the command tree, etc
/// </summary>
/// <param name="memberMeta">The member to verify</param>
/// <param name="varName">The name of the variable to which this member instance is being assigned</param>
private static void CheckMember(EdmMember memberMeta, string varName)
{
EntityUtil.CheckArgumentNull(memberMeta, varName);
CheckReadOnly(memberMeta.DeclaringType, varName);
// EdmMember constructor is responsible for basic validation
Debug.Assert(memberMeta.Name != null, "EdmMember constructor allowed null name?");
Debug.Assert(null != memberMeta.TypeUsage, "EdmMember constructor allowed null for TypeUsage?");
Debug.Assert(null != memberMeta.DeclaringType, "EdmMember constructor allowed null for DeclaringType?");
if(!CheckDataSpace(memberMeta.TypeUsage) || !CheckDataSpace(memberMeta.DeclaringType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_EdmMemberIncorrectSpace, varName);
}
}
private static void CheckParameter(FunctionParameter paramMeta, string varName)
{
EntityUtil.CheckArgumentNull(paramMeta, varName);
CheckReadOnly(paramMeta.DeclaringFunction, varName);
// FunctionParameter constructor is responsible for basic validation
Debug.Assert(paramMeta.Name != null, "FunctionParameter constructor allowed null name?");
// Verify that the parameter is from the same workspace as the DbCommandTree
if (!CheckDataSpace(paramMeta.TypeUsage))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_FunctionParameterIncorrectSpace, varName);
}
}
/// <summary>
/// Verifies that the specified function metadata is valid - non-null and either created by this command tree (if a LambdaFunction) or from the same metadata collection and data space as the command tree (for ordinary function metadata)
/// </summary>
/// <param name="function">The function metadata to verify</param>
private static void CheckFunction(EdmFunction function)
{
EntityUtil.CheckArgumentNull(function, "function");
CheckReadOnly(function, "function");
Debug.Assert(function.Name != null, "EdmType constructor allowed null name?");
if (!CheckDataSpace(function))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_FunctionIncorrectSpace, "function");
}
// Composable functions must have a return parameter.
if (function.IsComposableAttribute && null == function.ReturnParameter)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_FunctionReturnParameterNull, "function");
}
// Verify that the function ReturnType - if present - is from the DbCommandTree's metadata collection and dataspace
// A return parameter is not required for non-composable functions.
if (function.ReturnParameter != null)
{
if (!CheckDataSpace(function.ReturnParameter.TypeUsage))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_FunctionParameterIncorrectSpace, "function.ReturnParameter");
}
}
// Verify that the function parameters collection is non-null and,
// if non-empty, contains valid IParameterMetadata instances.
IList<FunctionParameter> functionParams = function.Parameters;
Debug.Assert(functionParams != null, "EdmFunction constructor did not initialize Parameters?");
for (int idx = 0; idx < functionParams.Count; idx++)
{
CheckParameter(functionParams[idx], StringUtil.FormatIndex("function.Parameters", idx));
}
}
/// <summary>
/// Verifies that the specified EntitySet is valid with respect to the command tree
/// </summary>
/// <param name="entitySet">The EntitySet to verify</param>
/// <param name="varName">The variable name to use if an exception should be thrown</param>
private static void CheckEntitySet(EntitySetBase entitySet, string varName)
{
EntityUtil.CheckArgumentNull(entitySet, varName);
CheckReadOnly(entitySet, varName);
// EntitySetBase constructor is responsible for basic validation of set name and element type
Debug.Assert(!string.IsNullOrEmpty(entitySet.Name), "EntitySetBase constructor allowed null/empty set name?");
//
// Verify the Extent's Container
//
if (null == entitySet.EntityContainer)
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_EntitySetEntityContainerNull, varName);
}
if(!CheckDataSpace(entitySet.EntityContainer))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_EntitySetIncorrectSpace, varName);
}
//
// Verify the Extent's Entity Type
//
// EntitySetBase constructor is responsible for basic validation of set name and element type
Debug.Assert(entitySet.ElementType != null, "EntitySetBase constructor allowed null container?");
if(!CheckDataSpace(entitySet.ElementType))
{
throw EntityUtil.Argument(System.Data.Entity.Strings.Cqt_Metadata_EntitySetIncorrectSpace, varName);
}
}
private static bool CheckDataSpace(TypeUsage type)
{
return CheckDataSpace(type.EdmType);
}
private static bool CheckDataSpace(GlobalItem item)
{
// Since the set of primitive types and canonical functions are shared, we don't need to check for them.
// Additionally, any non-canonical function in the C-Space must be a cached store function, which will
// also not be present in the workspace.
if (BuiltInTypeKind.PrimitiveType == item.BuiltInTypeKind ||
(BuiltInTypeKind.EdmFunction == item.BuiltInTypeKind && DataSpace.CSpace == item.DataSpace))
{
return true;
}
// Transient types should be checked according to their non-transient element types
if (Helper.IsRowType(item))
{
foreach (EdmProperty prop in ((RowType)item).Properties)
{
if (!CheckDataSpace(prop.TypeUsage))
{
return false;
}
}
return true;
}
else if (Helper.IsCollectionType(item))
{
return CheckDataSpace(((CollectionType)item).TypeUsage);
}
else if (Helper.IsRefType(item))
{
return CheckDataSpace(((RefType)item).ElementType);
}
else
{
return (item.DataSpace == DataSpace.SSpace || item.DataSpace == DataSpace.CSpace);
}
}
private static TypeUsage CreateCollectionOfRowResultType(List<KeyValuePair<string, TypeUsage>> columns)
{
TypeUsage retUsage = TypeUsage.Create(
TypeHelpers.CreateCollectionType(
TypeUsage.Create(
TypeHelpers.CreateRowType(columns)
)
)
);
return retUsage;
}
private static TypeUsage CreateCollectionResultType(EdmType type)
{
TypeUsage retUsage = TypeUsage.Create(
TypeHelpers.CreateCollectionType(
TypeUsage.Create(type)
)
);
return retUsage;
}
private static TypeUsage CreateCollectionResultType(TypeUsage type)
{
TypeUsage retUsage = TypeUsage.Create(TypeHelpers.CreateCollectionType(type));
return retUsage;
}
private static TypeUsage CreateResultType(EdmType resultType)
{
return TypeUsage.Create(resultType);
}
private static TypeUsage CreateResultType(RelationshipEndMember end)
{
TypeUsage retType = end.TypeUsage;
if (!TypeSemantics.IsReferenceType(retType))
{
//
// The only relation end that is currently allowed to have a non-Reference type is the Child end of
// a composition, in which case the end type must be an entity type.
//
//Debug.Assert(end.Relation.IsComposition && !end.IsParent && (end.Type is EntityType), "Relation end can only have non-Reference type if it is a Composition child end");
retType = TypeHelpers.CreateReferenceTypeUsage(TypeHelpers.GetEdmType<EntityType>(retType));
}
//
// If the upper bound is not 1 the result type is a collection of the given type
//
if (RelationshipMultiplicity.Many == end.RelationshipMultiplicity)
{
retType = TypeHelpers.CreateCollectionTypeUsage(retType);
}
return retType;
}
private static TypeUsage CreateReferenceResultType(EntityTypeBase referencedEntityType)
{
return TypeUsage.Create(TypeHelpers.CreateReferenceType(referencedEntityType));
}
/// <summary>
/// Requires: non-null expression
/// Determines whether the expression is a constant negative integer value. Always returns
/// false for non-constant, non-integer expression instances.
/// </summary>
private static bool IsConstantNegativeInteger(DbExpression expression)
{
return (expression.ExpressionKind == DbExpressionKind.Constant &&
TypeSemantics.IsIntegerNumericType(expression.ResultType) &&
Convert.ToInt64(((DbConstantExpression)expression).Value, CultureInfo.InvariantCulture) < 0);
}
private static bool TryGetPrimitiveTypeKind(Type clrType, out PrimitiveTypeKind primitiveTypeKind)
{
return ClrProviderManifest.Instance.TryGetPrimitiveTypeKind(clrType, out primitiveTypeKind);
}
/// <summary>
/// Checks whether the clr enum type matched the edm enum type.
/// </summary>
/// <param name="edmEnumType">Edm enum type.</param>
/// <param name="clrEnumType">Clr enum type.</param>
/// <returns>
/// <c>true</c> if types match otherwise <c>false</c>.
/// </returns>
/// <remarks>
/// The clr enum type matches the edm enum type if:
/// - type names are the same
/// - both types have the same underlying type (note that this prevents from over- and underflows)
/// - both types have the same number of members
/// - members have the same names
/// - members have the same values
/// </remarks>
private static bool ClrEdmEnumTypesMatch(EnumType edmEnumType, Type clrEnumType)
{
Debug.Assert(edmEnumType != null, "edmEnumType != null");
Debug.Assert(clrEnumType != null, "clrEnumType != null");
Debug.Assert(clrEnumType.IsEnum, "non enum clr type.");
// check that type names are the same and both types have the same number of members
if (clrEnumType.Name != edmEnumType.Name
|| clrEnumType.GetEnumNames().Length != edmEnumType.Members.Count)
{
return false;
}
// check that both types have the same underlying type (note that this also prevents from over- and underflows)
PrimitiveTypeKind clrEnumUnderlyingTypeKind;
if(!TryGetPrimitiveTypeKind(clrEnumType.GetEnumUnderlyingType(), out clrEnumUnderlyingTypeKind)
|| clrEnumUnderlyingTypeKind != edmEnumType.UnderlyingType.PrimitiveTypeKind)
{
return false;
}
// check that all the members have the same names and values
foreach (var edmEnumTypeMember in edmEnumType.Members)
{
Debug.Assert(
edmEnumTypeMember.Value.GetType() == clrEnumType.GetEnumUnderlyingType(),
"Enum underlying types matched so types of member values must match the enum underlying type as well");
if (!clrEnumType.GetEnumNames().Contains(edmEnumTypeMember.Name)
|| !edmEnumTypeMember.Value.Equals(
Convert.ChangeType(Enum.Parse(clrEnumType, edmEnumTypeMember.Name), clrEnumType.GetEnumUnderlyingType(), CultureInfo.InvariantCulture)))
{
return false;
}
}
return true;
}
}
}
|