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
|
//------------------------------------------------------------------------------
// <copyright file="SqlMetaData.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
namespace Microsoft.SqlServer.Server {
using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Globalization;
using System.Data.SqlTypes;
using System.Data.SqlClient;
// class SqlMetaData
// Simple immutable implementation of the a metadata-holding class. Only
// complexities are:
// 1) enforcing immutability.
// 2) Inferring type from a value.
// 3) Adjusting a value to match the metadata.
public sealed class SqlMetaData
{
private string m_strName;
private long m_lMaxLength;
private SqlDbType m_sqlDbType;
private byte m_bPrecision;
private byte m_bScale;
private long m_lLocale;
private SqlCompareOptions m_eCompareOptions;
private string m_XmlSchemaCollectionDatabase;
private string m_XmlSchemaCollectionOwningSchema;
private string m_XmlSchemaCollectionName;
private string m_serverTypeName;
private bool m_bPartialLength;
private Type m_udttype;
private bool m_useServerDefault;
private bool m_isUniqueKey;
private SortOrder m_columnSortOrder;
private int m_sortOrdinal;
// unlimited (except by implementation) max-length.
private const long x_lMax = -1;
private const long x_lServerMaxUnicode = 4000;
private const long x_lServerMaxANSI = 8000;
private const long x_lServerMaxBinary = 8000;
private const bool x_defaultUseServerDefault = false;
private const bool x_defaultIsUniqueKey = false;
private const SortOrder x_defaultColumnSortOrder = SortOrder.Unspecified;
private const int x_defaultSortOrdinal = -1;
private const SqlCompareOptions x_eDefaultStringCompareOptions = SqlCompareOptions.IgnoreCase
| SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
// scalar types constructor without tvp extended properties
public SqlMetaData(String name, SqlDbType dbType) {
Construct(name, dbType, x_defaultUseServerDefault,
x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal);
}
// scalar types constructor
public SqlMetaData(String name, SqlDbType dbType, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
Construct(name, dbType, useServerDefault,
isUniqueKey, columnSortOrder, sortOrdinal);
}
// binary or string constructor with only max length
// (for string types, locale and compare options will be picked up from the thread.
public SqlMetaData(String name, SqlDbType dbType, long maxLength) {
Construct(name, dbType, maxLength, x_defaultUseServerDefault,
x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal);
}
// binary or string constructor with only max length and tvp extended properties
// (for string types, locale and compare options will be picked up from the thread.
public SqlMetaData(String name, SqlDbType dbType, long maxLength, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
Construct(name, dbType, maxLength, useServerDefault,
isUniqueKey, columnSortOrder, sortOrdinal);
}
// udt ctor without tvp extended properties
public SqlMetaData(String name, SqlDbType dbType, Type userDefinedType) {
Construct(name, dbType, userDefinedType, null, x_defaultUseServerDefault,
x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal);
}
// udt ctor without tvp extended properties
public SqlMetaData(String name, SqlDbType dbType, Type userDefinedType, string serverTypeName) {
Construct(name, dbType, userDefinedType, serverTypeName, x_defaultUseServerDefault,
x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal);
}
// udt ctor
public SqlMetaData(String name, SqlDbType dbType, Type userDefinedType, string serverTypeName,
bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
Construct(name, dbType, userDefinedType, serverTypeName, useServerDefault,
isUniqueKey, columnSortOrder, sortOrdinal);
}
// decimal ctor without tvp extended properties
public SqlMetaData(String name, SqlDbType dbType, byte precision, byte scale) {
Construct(name, dbType, precision, scale, x_defaultUseServerDefault,
x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal);
}
// decimal ctor
public SqlMetaData(string name, SqlDbType dbType, byte precision, byte scale, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
Construct(name, dbType, precision, scale, useServerDefault,
isUniqueKey, columnSortOrder, sortOrdinal);
}
// string type constructor with locale and compare options, no tvp extended properties
public SqlMetaData(String name, SqlDbType dbType, long maxLength, long locale,
SqlCompareOptions compareOptions) {
Construct(name, dbType, maxLength, locale, compareOptions, x_defaultUseServerDefault,
x_defaultIsUniqueKey, x_defaultColumnSortOrder, x_defaultSortOrdinal);
}
// string type constructor with locale and compare options
public SqlMetaData(String name, SqlDbType dbType, long maxLength, long locale,
SqlCompareOptions compareOptions, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
Construct(name, dbType, maxLength, locale, compareOptions, useServerDefault,
isUniqueKey, columnSortOrder, sortOrdinal);
}
// typed xml ctor
public SqlMetaData(String name, SqlDbType dbType, string database, string owningSchema,
string objectName, bool useServerDefault, bool isUniqueKey,
SortOrder columnSortOrder, int sortOrdinal) {
Construct(name, dbType, database, owningSchema, objectName, useServerDefault,
isUniqueKey, columnSortOrder, sortOrdinal);
}
// everything except xml schema and tvp properties ctor
public SqlMetaData(String name, SqlDbType dbType, long maxLength, byte precision,
byte scale, long locale, SqlCompareOptions compareOptions,
Type userDefinedType) :
this(name, dbType, maxLength, precision, scale, locale, compareOptions,
userDefinedType, x_defaultUseServerDefault, x_defaultIsUniqueKey,
x_defaultColumnSortOrder, x_defaultSortOrdinal) {
}
// everything except xml schema ctor
public SqlMetaData(String name, SqlDbType dbType, long maxLength, byte precision,
byte scale, long localeId, SqlCompareOptions compareOptions,
Type userDefinedType, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
switch (dbType) {
case SqlDbType.BigInt:
case SqlDbType.Image:
case SqlDbType.Timestamp:
case SqlDbType.Bit:
case SqlDbType.DateTime:
case SqlDbType.SmallDateTime:
case SqlDbType.Real:
case SqlDbType.Int:
case SqlDbType.Money:
case SqlDbType.SmallMoney:
case SqlDbType.Float:
case SqlDbType.UniqueIdentifier:
case SqlDbType.SmallInt:
case SqlDbType.TinyInt:
case SqlDbType.Xml:
case SqlDbType.Date:
Construct(name, dbType, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal);
break;
case SqlDbType.Binary:
case SqlDbType.VarBinary:
Construct(name, dbType, maxLength, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal);
break;
case SqlDbType.Char:
case SqlDbType.NChar:
case SqlDbType.NVarChar:
case SqlDbType.VarChar:
Construct(name, dbType, maxLength, localeId, compareOptions, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal);
break;
case SqlDbType.NText:
case SqlDbType.Text:
// SQLBU # : We should ignore user's max length and use Max instead to avoid exception
Construct(name, dbType, Max, localeId, compareOptions, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal);
break;
case SqlDbType.Decimal:
case SqlDbType.Time:
case SqlDbType.DateTime2:
case SqlDbType.DateTimeOffset:
Construct(name, dbType, precision, scale, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal);
break;
case SqlDbType.Variant:
Construct(name, dbType, useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal);
break;
case SqlDbType.Udt:
Construct(name, dbType, userDefinedType, "", useServerDefault, isUniqueKey, columnSortOrder, sortOrdinal);
break;
default:
SQL.InvalidSqlDbTypeForConstructor(dbType);
break;
}
}
public SqlMetaData(String name, SqlDbType dbType, string database, string owningSchema, string objectName) {
Construct(name, dbType, database, owningSchema, objectName, x_defaultUseServerDefault, x_defaultIsUniqueKey,
x_defaultColumnSortOrder, x_defaultSortOrdinal);
}
// Most general constructor, should be able to intialize all SqlMetaData fields.(Used by SqlParameter)
internal SqlMetaData (String name,
SqlDbType sqlDBType,
long maxLength,
byte precision,
byte scale,
long localeId,
SqlCompareOptions compareOptions,
string xmlSchemaCollectionDatabase,
string xmlSchemaCollectionOwningSchema,
string xmlSchemaCollectionName,
bool partialLength,
Type udtType) {
AssertNameIsValid(name);
m_strName = name;
m_sqlDbType = sqlDBType;
m_lMaxLength = maxLength;
m_bPrecision = precision;
m_bScale = scale;
m_lLocale = localeId;
m_eCompareOptions = compareOptions;
m_XmlSchemaCollectionDatabase = xmlSchemaCollectionDatabase;
m_XmlSchemaCollectionOwningSchema = xmlSchemaCollectionOwningSchema;
m_XmlSchemaCollectionName = xmlSchemaCollectionName;
m_bPartialLength = partialLength;
m_udttype = udtType;
}
// Private constructor used to initialize default instance array elements.
// DO NOT EXPOSE OUTSIDE THIS CLASS! It performs no validation.
private SqlMetaData (String name,
SqlDbType sqlDbType,
long maxLength,
byte precision,
byte scale,
long localeId,
SqlCompareOptions compareOptions,
bool partialLength) {
AssertNameIsValid(name);
m_strName = name;
m_sqlDbType = sqlDbType;
m_lMaxLength = maxLength;
m_bPrecision = precision;
m_bScale = scale;
m_lLocale = localeId;
m_eCompareOptions = compareOptions;
m_bPartialLength = partialLength;
m_udttype = null;
}
public SqlCompareOptions CompareOptions {
get {
return m_eCompareOptions;
}
}
public DbType DbType {
get {
return sxm_rgSqlDbTypeToDbType[(int)m_sqlDbType];
}
}
public bool IsUniqueKey {
get {
return m_isUniqueKey;
}
}
public long LocaleId {
get{
return m_lLocale;
}
}
public static long Max {
get {
return x_lMax;
}
}
public long MaxLength {
get {
return m_lMaxLength;
}
}
public string Name {
get {
return m_strName;
}
}
public byte Precision {
get {
return m_bPrecision;
}
}
public byte Scale {
get {
return m_bScale;
}
}
public SortOrder SortOrder {
get {
return m_columnSortOrder;
}
}
public int SortOrdinal {
get {
return m_sortOrdinal;
}
}
public SqlDbType SqlDbType {
get {
return m_sqlDbType;
}
}
public Type Type {
get {
return m_udttype;
}
}
public string TypeName {
get {
if (m_serverTypeName != null) {
return m_serverTypeName;
}
else if (SqlDbType == SqlDbType.Udt) {
return UdtTypeName;
}
else {
return sxm_rgDefaults[(int)SqlDbType].Name;
}
}
}
internal string ServerTypeName {
get {
return m_serverTypeName;
}
}
public bool UseServerDefault {
get {
return m_useServerDefault;
}
}
public string XmlSchemaCollectionDatabase {
get {
return m_XmlSchemaCollectionDatabase;
}
}
public string XmlSchemaCollectionName {
get {
return m_XmlSchemaCollectionName;
}
}
public string XmlSchemaCollectionOwningSchema {
get {
return m_XmlSchemaCollectionOwningSchema;
}
}
internal bool IsPartialLength {
get {
return m_bPartialLength;
}
}
internal string UdtTypeName {
get{
if(SqlDbType != SqlDbType.Udt) {
return null;
}
else if (m_udttype == null) {
return null;
}
else {
return m_udttype.FullName;
}
}
}
// Construction for all types that do not have variable attributes
private void Construct(String name, SqlDbType dbType, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
AssertNameIsValid(name);
ValidateSortOrder(columnSortOrder, sortOrdinal);
// Check for absense of explicitly-allowed types to avoid unexpected additions when new types are added
if (!( SqlDbType.BigInt == dbType ||
SqlDbType.Bit == dbType ||
SqlDbType.DateTime == dbType ||
SqlDbType.Date == dbType ||
SqlDbType.DateTime2 == dbType ||
SqlDbType.DateTimeOffset == dbType ||
SqlDbType.Decimal == dbType ||
SqlDbType.Float == dbType ||
SqlDbType.Image == dbType ||
SqlDbType.Int == dbType ||
SqlDbType.Money == dbType ||
SqlDbType.NText == dbType ||
SqlDbType.Real == dbType ||
SqlDbType.SmallDateTime == dbType ||
SqlDbType.SmallInt == dbType ||
SqlDbType.SmallMoney == dbType ||
SqlDbType.Text == dbType ||
SqlDbType.Time == dbType ||
SqlDbType.Timestamp == dbType ||
SqlDbType.TinyInt == dbType ||
SqlDbType.UniqueIdentifier == dbType ||
SqlDbType.Variant == dbType ||
SqlDbType.Xml == dbType) )
throw SQL.InvalidSqlDbTypeForConstructor(dbType);
SetDefaultsForType( dbType );
if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) {
m_lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID;
}
m_strName = name;
m_useServerDefault = useServerDefault;
m_isUniqueKey = isUniqueKey;
m_columnSortOrder = columnSortOrder;
m_sortOrdinal = sortOrdinal;
}
// Construction for all types that vary by user-specified length (not Udts)
private void Construct(String name, SqlDbType dbType, long maxLength, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
AssertNameIsValid(name);
ValidateSortOrder(columnSortOrder, sortOrdinal);
long lLocale = 0;
if (SqlDbType.Char == dbType) {
if (maxLength > x_lServerMaxANSI || maxLength < 0)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID;
}
else if (SqlDbType.VarChar == dbType) {
if ((maxLength > x_lServerMaxANSI || maxLength < 0) && maxLength != Max)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID;
}
else if (SqlDbType.NChar == dbType) {
if (maxLength > x_lServerMaxUnicode || maxLength < 0)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID;
}
else if (SqlDbType.NVarChar == dbType) {
if ((maxLength > x_lServerMaxUnicode || maxLength < 0) && maxLength != Max)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID;
}
else if (SqlDbType.NText == dbType || SqlDbType.Text == dbType) {
// old-style lobs only allowed with Max length
if (SqlMetaData.Max != maxLength)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
lLocale = System.Globalization.CultureInfo.CurrentCulture.LCID;
}
else if (SqlDbType.Binary == dbType) {
if (maxLength > x_lServerMaxBinary || maxLength < 0)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else if (SqlDbType.VarBinary == dbType) {
if ((maxLength > x_lServerMaxBinary || maxLength < 0) && maxLength != Max)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else if (SqlDbType.Image == dbType) {
// old-style lobs only allowed with Max length
if (SqlMetaData.Max != maxLength)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else
throw SQL.InvalidSqlDbTypeForConstructor(dbType);
SetDefaultsForType( dbType );
m_strName = name;
m_lMaxLength = maxLength;
m_lLocale = lLocale;
m_useServerDefault = useServerDefault;
m_isUniqueKey = isUniqueKey;
m_columnSortOrder = columnSortOrder;
m_sortOrdinal = sortOrdinal;
}
// Construction for string types with specified locale/compare options
private void Construct(String name,
SqlDbType dbType,
long maxLength,
long locale,
SqlCompareOptions compareOptions,
bool useServerDefault,
bool isUniqueKey,
SortOrder columnSortOrder,
int sortOrdinal) {
AssertNameIsValid(name);
ValidateSortOrder(columnSortOrder, sortOrdinal);
// Validate type and max length.
if (SqlDbType.Char == dbType)
{
if (maxLength > x_lServerMaxANSI || maxLength < 0)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else if (SqlDbType.VarChar == dbType)
{
if ((maxLength > x_lServerMaxANSI || maxLength < 0) && maxLength != Max)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else if (SqlDbType.NChar == dbType)
{
if (maxLength > x_lServerMaxUnicode || maxLength < 0)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else if (SqlDbType.NVarChar == dbType)
{
if ((maxLength > x_lServerMaxUnicode || maxLength < 0) && maxLength != Max)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else if (SqlDbType.NText == dbType || SqlDbType.Text == dbType)
{
// old-style lobs only allowed with Max length
if (SqlMetaData.Max != maxLength)
throw ADP.Argument(Res.GetString(Res.ADP_InvalidDataLength2, maxLength.ToString(CultureInfo.InvariantCulture)), "maxLength");
}
else
throw SQL.InvalidSqlDbTypeForConstructor(dbType);
// Validate locale?
// Validate compare options
// Binary sort must be by itself.
// Nothing else but the Ignore bits is allowed.
if (SqlCompareOptions.BinarySort != compareOptions &&
0 != (~((int)SqlCompareOptions.IgnoreCase | (int)SqlCompareOptions.IgnoreNonSpace |
(int)SqlCompareOptions.IgnoreKanaType | (int)SqlCompareOptions.IgnoreWidth) &
(int)compareOptions))
throw ADP.InvalidEnumerationValue(typeof(SqlCompareOptions), (int)compareOptions);
SetDefaultsForType( dbType );
m_strName = name;
m_lMaxLength = maxLength;
m_lLocale = locale;
m_eCompareOptions = compareOptions;
m_useServerDefault = useServerDefault;
m_isUniqueKey = isUniqueKey;
m_columnSortOrder = columnSortOrder;
m_sortOrdinal = sortOrdinal;
}
private static byte[] __maxLenFromPrecision = new byte[] {5,5,5,5,5,5,5,5,5,9,9,9,9,9,
9,9,9,9,9,13,13,13,13,13,13,13,13,13,17,17,17,17,17,17,17,17,17,17};
private const byte MaxTimeScale = 7;
private static byte[] __maxVarTimeLenOffsetFromScale = new byte[] { 2, 2, 2, 1, 1, 0, 0, 0 };
// Construction for Decimal type and new Katmai Date/Time types
private void Construct( String name, SqlDbType dbType, byte precision, byte scale, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal ) {
AssertNameIsValid(name);
ValidateSortOrder(columnSortOrder, sortOrdinal);
if (SqlDbType.Decimal == dbType) {
if (precision > SqlDecimal.MaxPrecision || scale > precision)
throw SQL.PrecisionValueOutOfRange(precision);
if (scale > SqlDecimal.MaxScale)
throw SQL.ScaleValueOutOfRange(scale);
} else if (SqlDbType.Time == dbType || SqlDbType.DateTime2 == dbType || SqlDbType.DateTimeOffset == dbType) {
if (scale > MaxTimeScale) {
throw SQL.TimeScaleValueOutOfRange(scale);
}
} else {
throw SQL.InvalidSqlDbTypeForConstructor(dbType);
}
SetDefaultsForType(dbType);
m_strName = name;
m_bPrecision = precision;
m_bScale = scale;
if (SqlDbType.Decimal == dbType) {
m_lMaxLength = __maxLenFromPrecision[precision - 1];
} else {
m_lMaxLength -= __maxVarTimeLenOffsetFromScale[scale];
}
m_useServerDefault = useServerDefault;
m_isUniqueKey = isUniqueKey;
m_columnSortOrder = columnSortOrder;
m_sortOrdinal = sortOrdinal;
}
// Construction for Udt type
private void Construct(String name, SqlDbType dbType, Type userDefinedType, string serverTypeName, bool useServerDefault,
bool isUniqueKey, SortOrder columnSortOrder, int sortOrdinal) {
AssertNameIsValid(name);
ValidateSortOrder(columnSortOrder, sortOrdinal);
if (SqlDbType.Udt != dbType)
throw SQL.InvalidSqlDbTypeForConstructor(dbType);
if (null == userDefinedType)
throw ADP.ArgumentNull("userDefinedType");
SetDefaultsForType(SqlDbType.Udt);
m_strName = name;
m_lMaxLength = SerializationHelperSql9.GetUdtMaxLength( userDefinedType );
m_udttype = userDefinedType;
m_serverTypeName = serverTypeName;
m_useServerDefault = useServerDefault;
m_isUniqueKey = isUniqueKey;
m_columnSortOrder = columnSortOrder;
m_sortOrdinal = sortOrdinal;
}
// Construction for Xml type
private void Construct(String name, SqlDbType dbType, string database, string owningSchema,
string objectName, bool useServerDefault, bool isUniqueKey, SortOrder columnSortOrder,
int sortOrdinal) {
AssertNameIsValid(name);
ValidateSortOrder(columnSortOrder, sortOrdinal);
if (SqlDbType.Xml != dbType)
throw SQL.InvalidSqlDbTypeForConstructor(dbType);
if (null != database || null != owningSchema) {
if (null == objectName) {
throw ADP.ArgumentNull("objectName");
}
}
SetDefaultsForType(SqlDbType.Xml);
m_strName = name;
m_XmlSchemaCollectionDatabase = database;
m_XmlSchemaCollectionOwningSchema = owningSchema;
m_XmlSchemaCollectionName = objectName;
m_useServerDefault = useServerDefault;
m_isUniqueKey = isUniqueKey;
m_columnSortOrder = columnSortOrder;
m_sortOrdinal = sortOrdinal;
}
private void AssertNameIsValid(string name) {
if (null == name)
throw ADP.ArgumentNull("name");
if (Microsoft.SqlServer.Server.SmiMetaData.MaxNameLength < name.Length)
throw SQL.NameTooLong("name");
}
private void ValidateSortOrder(SortOrder columnSortOrder, int sortOrdinal) {
// Check that sort order is valid enum value.
if (SortOrder.Unspecified != columnSortOrder &&
SortOrder.Ascending != columnSortOrder &&
SortOrder.Descending != columnSortOrder) {
throw SQL.InvalidSortOrder(columnSortOrder);
}
// Must specify both sort order and ordinal, or neither
if ( (SortOrder.Unspecified == columnSortOrder) != (x_defaultSortOrdinal == sortOrdinal)) {
throw SQL.MustSpecifyBothSortOrderAndOrdinal(columnSortOrder, sortOrdinal);
}
}
public Int16 Adjust(Int16 value) {
if (SqlDbType.SmallInt != SqlDbType)
ThrowInvalidType();
return value;
}
public Int32 Adjust(Int32 value) {
if (SqlDbType.Int != SqlDbType)
ThrowInvalidType();
return value;
}
public Int64 Adjust(Int64 value) {
if (SqlDbType.BigInt != SqlDbType)
ThrowInvalidType();
return value;
}
public float Adjust(float value) {
if (SqlDbType.Real != SqlDbType)
ThrowInvalidType();
return value;
}
public double Adjust(double value) {
if (SqlDbType.Float != SqlDbType)
ThrowInvalidType();
return value;
}
public string Adjust(string value) {
if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) {
//DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(string): Fixed-length type with Max length!");
// Don't pad null values
if (null != value) {
// Pad if necessary
if (value.Length < MaxLength)
value = value.PadRight((int)MaxLength);
}
}
else if (SqlDbType.VarChar != SqlDbType &&
SqlDbType.NVarChar != SqlDbType &&
SqlDbType.Text != SqlDbType &&
SqlDbType.NText != SqlDbType) {
ThrowInvalidType();
}
// Handle null values after type check
if (null == value) {
return null;
}
if (value.Length > MaxLength && Max != MaxLength)
value = value.Remove((int)MaxLength, (int)(value.Length - MaxLength));
return value;
}
public decimal Adjust(decimal value) {
if (SqlDbType.Decimal != SqlDbType &&
SqlDbType.Money != SqlDbType &&
SqlDbType.SmallMoney != SqlDbType) {
ThrowInvalidType();
}
if (SqlDbType.Decimal != SqlDbType) {
VerifyMoneyRange(new SqlMoney(value));
return value;
}
else {
SqlDecimal sdValue = InternalAdjustSqlDecimal(new SqlDecimal(value));
return sdValue.Value;
}
}
public DateTime Adjust(DateTime value) {
if (SqlDbType.DateTime == SqlDbType || SqlDbType.SmallDateTime == SqlDbType) {
VerifyDateTimeRange(value);
} else if (SqlDbType.DateTime2 == SqlDbType) {
return new DateTime(InternalAdjustTimeTicks(value.Ticks));
} else if (SqlDbType.Date == SqlDbType) {
return value.Date;
} else {
ThrowInvalidType();
}
return value;
}
public Guid Adjust(Guid value) {
if (SqlDbType.UniqueIdentifier != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlBoolean Adjust(SqlBoolean value) {
if (SqlDbType.Bit != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlByte Adjust(SqlByte value) {
if (SqlDbType.TinyInt != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlInt16 Adjust(SqlInt16 value) {
if (SqlDbType.SmallInt != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlInt32 Adjust(SqlInt32 value) {
if (SqlDbType.Int != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlInt64 Adjust(SqlInt64 value) {
if (SqlDbType.BigInt != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlSingle Adjust(SqlSingle value) {
if (SqlDbType.Real != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlDouble Adjust(SqlDouble value) {
if (SqlDbType.Float != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlMoney Adjust(SqlMoney value) {
if (SqlDbType.Money != SqlDbType &&
SqlDbType.SmallMoney != SqlDbType)
ThrowInvalidType();
if (!value.IsNull)
VerifyMoneyRange(value);
return value;
}
public SqlDateTime Adjust(SqlDateTime value) {
if (SqlDbType.DateTime != SqlDbType &&
SqlDbType.SmallDateTime != SqlDbType)
ThrowInvalidType();
if (!value.IsNull)
VerifyDateTimeRange(value.Value);
return value;
}
public SqlDecimal Adjust(SqlDecimal value) {
if (SqlDbType.Decimal != SqlDbType)
ThrowInvalidType();
return InternalAdjustSqlDecimal(value);
}
public SqlString Adjust(SqlString value) {
if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType)
{
//DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(SqlString): Fixed-length type with Max length!");
// Don't pad null values
if (!value.IsNull)
{
// Pad fixed-length types
if (value.Value.Length < MaxLength)
return new SqlString(value.Value.PadRight((int)MaxLength));
}
}
else if (SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType &&
SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType)
ThrowInvalidType();
// Handle null values after type check
if (value.IsNull)
{
return value;
}
// trim all types
if (value.Value.Length > MaxLength && Max != MaxLength)
value = new SqlString(value.Value.Remove((int)MaxLength, (int)(value.Value.Length - MaxLength)));
return value;
}
public SqlBinary Adjust(SqlBinary value) {
if (SqlDbType.Binary == SqlDbType ||
SqlDbType.Timestamp == SqlDbType) {
if (!value.IsNull) {
// Pad fixed-length types
if (value.Length < MaxLength) {
byte[] rgbValue = value.Value;
byte[] rgbNewValue = new byte[ MaxLength ];
Array.Copy(rgbValue, rgbNewValue, rgbValue.Length);
Array.Clear(rgbNewValue, rgbValue.Length, rgbNewValue.Length - rgbValue.Length);
return new SqlBinary(rgbNewValue);
}
}
}
else if (SqlDbType.VarBinary != SqlDbType &&
SqlDbType.Image != SqlDbType)
ThrowInvalidType();
// Handle null values
if (value.IsNull) {
return value;
}
// trim all types
if (value.Length > MaxLength && Max != MaxLength) {
byte[] rgbValue = value.Value;
byte[] rgbNewValue = new byte[ MaxLength ];
Array.Copy(rgbValue, rgbNewValue, (int)MaxLength);
value = new SqlBinary(rgbNewValue);
}
return value;
}
public SqlGuid Adjust(SqlGuid value) {
if (SqlDbType.UniqueIdentifier != SqlDbType)
ThrowInvalidType();
return value;
}
public SqlChars Adjust(SqlChars value) {
if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType) {
//DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(SqlChars): Fixed-length type with Max length!");
// Don't pad null values
if (null != value && !value.IsNull) {
// Pad fixed-length types
long oldLength = value.Length;
if (oldLength < MaxLength) {
// Make sure buffer is long enough
if (value.MaxLength < MaxLength) {
char[] rgchNew = new char[ (int)MaxLength ];
Array.Copy(value.Buffer, rgchNew, (int)oldLength);
value = new SqlChars(rgchNew);
}
// pad extra space
char[] rgchTemp = value.Buffer;
for(long i=oldLength; i<MaxLength; i++)
rgchTemp[i] = ' ';
value.SetLength(MaxLength);
return value;
}
}
}
else if ( SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType &&
SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType)
ThrowInvalidType();
// Handle null values after type check.
if (null == value || value.IsNull) {
return value;
}
// trim all types
if (value.Length > MaxLength && Max != MaxLength)
value.SetLength(MaxLength);
return value;
}
public SqlBytes Adjust(SqlBytes value) {
if (SqlDbType.Binary == SqlDbType || SqlDbType.Timestamp == SqlDbType) {
//DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(SqlBytes): Fixed-length type with Max length!");
// Don't pad null values
if (null != value && !value.IsNull)
{
// Pad fixed-length types
int oldLength = (int) value.Length;
if (oldLength < MaxLength)
{
// Make sure buffer is long enough
if (value.MaxLength < MaxLength)
{
byte[] rgbNew = new byte[ MaxLength ];
Array.Copy(value.Buffer, rgbNew, (int)oldLength);
value = new SqlBytes(rgbNew);
}
// pad extra space
byte[] rgbTemp = value.Buffer;
Array.Clear(rgbTemp, oldLength, rgbTemp.Length - oldLength);
value.SetLength(MaxLength);
return value;
}
}
}
else if (SqlDbType.VarBinary != SqlDbType &&
SqlDbType.Image != SqlDbType)
ThrowInvalidType();
// Handle null values after type check.
if (null == value || value.IsNull) {
return value;
}
// trim all types
if (value.Length > MaxLength && Max != MaxLength)
value.SetLength(MaxLength);
return value;
}
public SqlXml Adjust(SqlXml value) {
if (SqlDbType.Xml != SqlDbType)
ThrowInvalidType();
return value;
}
public TimeSpan Adjust(TimeSpan value) {
if (SqlDbType.Time != SqlDbType)
ThrowInvalidType();
VerifyTimeRange(value);
return new TimeSpan(InternalAdjustTimeTicks(value.Ticks));
}
public DateTimeOffset Adjust(DateTimeOffset value) {
if (SqlDbType.DateTimeOffset != SqlDbType)
ThrowInvalidType();
return new DateTimeOffset(InternalAdjustTimeTicks(value.Ticks), value.Offset);
}
public object Adjust(object value) {
// Pass null references through
if (null == value)
return null;
Type dataType = value.GetType();
switch (Type.GetTypeCode(dataType)) {
case TypeCode.Boolean: value = this.Adjust((Boolean)value); break;
case TypeCode.Byte: value = this.Adjust((Byte)value); break;
case TypeCode.Char: value = this.Adjust((Char)value); break;
case TypeCode.DateTime: value = this.Adjust((DateTime)value); break;
case TypeCode.DBNull: /* DBNull passes through as is for all types */ break;
case TypeCode.Decimal: value = this.Adjust((Decimal)value); break;
case TypeCode.Double: value = this.Adjust((Double)value); break;
case TypeCode.Empty: throw ADP.InvalidDataType(TypeCode.Empty);
case TypeCode.Int16: value = this.Adjust((Int16)value); break;
case TypeCode.Int32: value = this.Adjust((Int32)value); break;
case TypeCode.Int64: value = this.Adjust((Int64)value); break;
case TypeCode.SByte: throw ADP.InvalidDataType(TypeCode.SByte);
case TypeCode.Single: value = this.Adjust((Single)value); break;
case TypeCode.String: value = this.Adjust((String)value); break;
case TypeCode.UInt16: throw ADP.InvalidDataType(TypeCode.UInt16);
case TypeCode.UInt32: throw ADP.InvalidDataType(TypeCode.UInt32);
case TypeCode.UInt64: throw ADP.InvalidDataType(TypeCode.UInt64);
case TypeCode.Object:
if (dataType == typeof(System.Byte[]))
value = this.Adjust((System.Byte[])value);
else if (dataType == typeof(System.Char[]))
value = this.Adjust((System.Char[])value);
else if (dataType == typeof(System.Guid))
value = this.Adjust((System.Guid)value);
else if (dataType == typeof(System.Object)) {
//
throw ADP.InvalidDataType(TypeCode.UInt64);
}
else if (dataType == typeof(SqlBinary))
value = this.Adjust((SqlBinary)value);
else if (dataType == typeof(SqlBoolean))
value = this.Adjust((SqlBoolean)value);
else if (dataType == typeof(SqlByte))
value = this.Adjust((SqlByte)value);
else if (dataType == typeof(SqlDateTime))
value = this.Adjust((SqlDateTime)value);
else if (dataType == typeof(SqlDouble))
value = this.Adjust((SqlDouble)value);
else if (dataType == typeof(SqlGuid))
value = this.Adjust((SqlGuid)value);
else if (dataType == typeof(SqlInt16))
value = this.Adjust((SqlInt16)value);
else if (dataType == typeof(SqlInt32))
value = this.Adjust((SqlInt32)value);
else if (dataType == typeof(SqlInt64))
value = this.Adjust((SqlInt64)value);
else if (dataType == typeof(SqlMoney))
value = this.Adjust((SqlMoney)value);
else if (dataType == typeof(SqlDecimal))
value = this.Adjust((SqlDecimal)value);
else if (dataType == typeof(SqlSingle))
value = this.Adjust((SqlSingle)value);
else if (dataType == typeof(SqlString))
value = this.Adjust((SqlString)value);
else if (dataType == typeof(SqlChars))
value = this.Adjust((SqlChars)value);
else if (dataType == typeof(SqlBytes))
value = this.Adjust((SqlBytes)value);
else if (dataType == typeof(SqlXml))
value = this.Adjust((SqlXml)value);
else if (dataType == typeof(TimeSpan))
value = this.Adjust((TimeSpan)value);
else if (dataType == typeof(DateTimeOffset))
value = this.Adjust((DateTimeOffset)value);
else {
// Handle UDTs?
throw ADP.UnknownDataType(dataType);
}
break;
default: throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType));
}
return value;
}
public static SqlMetaData InferFromValue(object value, String name) {
if (value == null)
throw ADP.ArgumentNull("value");
SqlMetaData smd = null;
Type dataType = value.GetType();
switch (Type.GetTypeCode(dataType))
{
case TypeCode.Boolean: smd = new SqlMetaData(name, SqlDbType.Bit); break;
case TypeCode.Byte: smd = new SqlMetaData(name, SqlDbType.TinyInt); break;
case TypeCode.Char: smd = new SqlMetaData(name, SqlDbType.NVarChar, 1 ); break;
case TypeCode.DateTime: smd = new SqlMetaData(name, SqlDbType.DateTime); break;
case TypeCode.DBNull: throw ADP.InvalidDataType(TypeCode.DBNull);
case TypeCode.Decimal:
{ // Add brackets in order to contain scope declare local variable "sd"
// use logic inside SqlDecimal to infer precision and scale.
SqlDecimal sd = new SqlDecimal((Decimal)value);
smd = new SqlMetaData(name, SqlDbType.Decimal, sd.Precision, sd.Scale);
}
break;
case TypeCode.Double: smd = new SqlMetaData(name, SqlDbType.Float); break;
case TypeCode.Empty: throw ADP.InvalidDataType(TypeCode.Empty);
case TypeCode.Int16: smd = new SqlMetaData(name, SqlDbType.SmallInt); break;
case TypeCode.Int32: smd = new SqlMetaData(name, SqlDbType.Int); break;
case TypeCode.Int64: smd = new SqlMetaData(name, SqlDbType.BigInt); break;
case TypeCode.SByte: throw ADP.InvalidDataType(TypeCode.SByte);
case TypeCode.Single: smd = new SqlMetaData(name, SqlDbType.Real); break;
case TypeCode.String:
{
long maxLen = ((String)value).Length;
if (maxLen < 1) maxLen = 1;
if (x_lServerMaxUnicode < maxLen)
maxLen = Max;
smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen);
}
break;
case TypeCode.UInt16: throw ADP.InvalidDataType(TypeCode.UInt16);
case TypeCode.UInt32: throw ADP.InvalidDataType(TypeCode.UInt32);
case TypeCode.UInt64: throw ADP.InvalidDataType(TypeCode.UInt64);
case TypeCode.Object:
if (dataType == typeof(System.Byte[]))
{
long maxLen = ((System.Byte[])value).Length;
if (maxLen < 1) maxLen = 1;
if (x_lServerMaxBinary < maxLen)
maxLen = Max;
smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen);
}
else if (dataType == typeof(System.Char[]))
{
long maxLen = ((System.Char[])value).Length;
if (maxLen < 1) maxLen = 1;
if (x_lServerMaxUnicode < maxLen)
maxLen = Max;
smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen);
}
else if (dataType == typeof(System.Guid))
smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier);
else if (dataType == typeof(System.Object))
smd = new SqlMetaData(name, SqlDbType.Variant);
else if (dataType == typeof(SqlBinary))
{
long maxLen;
SqlBinary sb = ((SqlBinary) value);
if (!sb.IsNull)
{
maxLen = sb.Length;
if (maxLen < 1) maxLen = 1;
if (x_lServerMaxBinary < maxLen)
maxLen = Max;
}
else
maxLen = sxm_rgDefaults[(int)SqlDbType.VarBinary].MaxLength;
smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen);
}
else if (dataType == typeof(SqlBoolean))
smd = new SqlMetaData(name, SqlDbType.Bit);
else if (dataType == typeof(SqlByte))
smd = new SqlMetaData(name, SqlDbType.TinyInt);
else if (dataType == typeof(SqlDateTime))
smd = new SqlMetaData(name, SqlDbType.DateTime);
else if (dataType == typeof(SqlDouble))
smd = new SqlMetaData(name, SqlDbType.Float);
else if (dataType == typeof(SqlGuid))
smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier);
else if (dataType == typeof(SqlInt16))
smd = new SqlMetaData(name, SqlDbType.SmallInt);
else if (dataType == typeof(SqlInt32))
smd = new SqlMetaData(name, SqlDbType.Int);
else if (dataType == typeof(SqlInt64))
smd = new SqlMetaData(name, SqlDbType.BigInt);
else if (dataType == typeof(SqlMoney))
smd = new SqlMetaData(name, SqlDbType.Money);
else if (dataType == typeof(SqlDecimal))
{
byte bPrec;
byte scale;
SqlDecimal sd = (SqlDecimal) value;
if (!sd.IsNull)
{
bPrec = sd.Precision;
scale = sd.Scale;
}
else
{
bPrec = sxm_rgDefaults[(int)SqlDbType.Decimal].Precision;
scale = sxm_rgDefaults[(int)SqlDbType.Decimal].Scale;
}
smd = new SqlMetaData(name, SqlDbType.Decimal, bPrec, scale);
}
else if (dataType == typeof(SqlSingle))
smd = new SqlMetaData(name, SqlDbType.Real);
else if (dataType == typeof(SqlString))
{
SqlString ss = (SqlString)value;
if (!ss.IsNull)
{
long maxLen = ss.Value.Length;
if (maxLen < 1) maxLen = 1;
if (maxLen > x_lServerMaxUnicode)
maxLen = Max;
smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen, ss.LCID, ss.SqlCompareOptions);
}
else
{
smd = new SqlMetaData(name, SqlDbType.NVarChar, sxm_rgDefaults[(int)SqlDbType.NVarChar].MaxLength);
}
}
else if (dataType == typeof(SqlChars))
{
long maxLen;
SqlChars sch = (SqlChars)value;
if (!sch.IsNull)
{
maxLen = sch.Length;
if (maxLen < 1) maxLen = 1;
if (maxLen > x_lServerMaxUnicode)
maxLen = Max;
}
else
maxLen = sxm_rgDefaults[(int)SqlDbType.NVarChar].MaxLength;
smd = new SqlMetaData(name, SqlDbType.NVarChar, maxLen);
}
else if (dataType == typeof(SqlBytes))
{
long maxLen;
SqlBytes sb = (SqlBytes)value;
if (!sb.IsNull)
{
maxLen = sb.Length;
if (maxLen < 1) maxLen = 1;
else if (x_lServerMaxBinary < maxLen) maxLen = Max;
}
else
maxLen = sxm_rgDefaults[(int)SqlDbType.VarBinary].MaxLength;
smd = new SqlMetaData(name, SqlDbType.VarBinary, maxLen);
}
else if (dataType == typeof(SqlXml))
smd = new SqlMetaData(name, SqlDbType.Xml);
else if (dataType == typeof(TimeSpan))
smd = new SqlMetaData(name, SqlDbType.Time, 0, InferScaleFromTimeTicks(((TimeSpan)value).Ticks));
else if (dataType == typeof(DateTimeOffset))
smd = new SqlMetaData(name, SqlDbType.DateTimeOffset, 0, InferScaleFromTimeTicks(((DateTimeOffset)value).Ticks));
else
throw ADP.UnknownDataType(dataType);
break;
default: throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType));
}
return smd;
}
public bool Adjust(bool value) {
if (SqlDbType.Bit != SqlDbType)
ThrowInvalidType();
return value;
}
public byte Adjust(byte value) {
if (SqlDbType.TinyInt != SqlDbType)
ThrowInvalidType();
return value;
}
public byte[] Adjust(byte[] value) {
if (SqlDbType.Binary == SqlDbType || SqlDbType.Timestamp == SqlDbType)
{
//DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(byte[]): Fixed-length type with Max length!");
// Don't pad null values
if (null != value)
{
// Pad fixed-length types
if (value.Length < MaxLength)
{
byte[] rgbNewValue = new byte[ MaxLength ];
Array.Copy(value, rgbNewValue, value.Length);
Array.Clear(rgbNewValue, value.Length, (int) rgbNewValue.Length - value.Length);
return rgbNewValue;
}
}
}
else if (SqlDbType.VarBinary != SqlDbType &&
SqlDbType.Image != SqlDbType)
ThrowInvalidType();
// Handle null values after type check
if (null == value)
{
return null;
}
// trim all types
if (value.Length > MaxLength && Max != MaxLength)
{
byte[] rgbNewValue = new byte[ MaxLength];
Array.Copy(value, rgbNewValue, (int)MaxLength);
value = rgbNewValue;
}
return value;
}
public char Adjust(char value)
{
if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType)
{
if (1 != MaxLength)
ThrowInvalidType();
}
else if ( (1 > MaxLength) || // char must have max length of at least 1
(SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType &&
SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType)
)
ThrowInvalidType();
return value;
}
public char[] Adjust(char[] value)
{
if (SqlDbType.Char == SqlDbType || SqlDbType.NChar == SqlDbType)
{
//DBG.Assert(Max!=MaxLength, "SqlMetaData.Adjust(byte[]): Fixed-length type with Max length!");
// Don't pad null values
if (null != value)
{
// Pad fixed-length types
long oldLength = value.Length;
if (oldLength < MaxLength)
{
char[] rgchNew = new char[ (int)MaxLength ];
Array.Copy(value, rgchNew, (int)oldLength);
// pad extra space
for(long i=oldLength; i<rgchNew.Length; i++)
rgchNew[i] = ' ';
return rgchNew;
}
}
}
else if ( SqlDbType.VarChar != SqlDbType && SqlDbType.NVarChar != SqlDbType &&
SqlDbType.Text != SqlDbType && SqlDbType.NText != SqlDbType)
ThrowInvalidType();
// Handle null values after type check
if (null == value)
{
return null;
}
// trim all types
if (value.Length > MaxLength && Max != MaxLength)
{
char[] rgchNewValue = new char[ MaxLength ];
Array.Copy(value, rgchNewValue, (int)MaxLength);
value = rgchNewValue;
}
return value;
}
internal static SqlMetaData GetPartialLengthMetaData(SqlMetaData md) {
if (md.IsPartialLength == true) {
return md;
}
if (md.SqlDbType == SqlDbType.Xml)
ThrowInvalidType(); //Xml should always have IsPartialLength = true
if (md.SqlDbType == SqlDbType.NVarChar || md.SqlDbType == SqlDbType.VarChar ||
md.SqlDbType == SqlDbType.VarBinary) {
SqlMetaData mdnew = new SqlMetaData(md.Name, md.SqlDbType, SqlMetaData.Max, 0, 0, md.LocaleId,
md.CompareOptions, null, null, null, true, md.Type);
return mdnew;
}
else
return md;
}
private static void ThrowInvalidType() {
throw ADP.InvalidMetaDataValue();
}
// Hard coding smalldatetime limits...
private static readonly DateTime x_dtSmallMax = new DateTime(2079, 06, 06, 23, 59, 29, 998);
private static readonly DateTime x_dtSmallMin = new DateTime(1899, 12, 31, 23, 59, 29, 999);
void VerifyDateTimeRange(DateTime value)
{
if (SqlDbType.SmallDateTime == SqlDbType && (x_dtSmallMax < value || x_dtSmallMin > value))
ThrowInvalidType();
}
private static readonly SqlMoney x_smSmallMax = new SqlMoney(((Decimal)Int32.MaxValue)/10000);
private static readonly SqlMoney x_smSmallMin = new SqlMoney(((Decimal)Int32.MinValue)/10000);
void VerifyMoneyRange(SqlMoney value)
{
if (SqlDbType.SmallMoney == SqlDbType && ((x_smSmallMax < value).Value || (x_smSmallMin > value).Value))
ThrowInvalidType();
}
private SqlDecimal InternalAdjustSqlDecimal(SqlDecimal value)
{
if (!value.IsNull && (value.Precision != Precision || value.Scale != Scale))
{
// SQLBU 402377 Force truncation if target scale is smaller than actual scale.
if (value.Scale != Scale)
{
value = SqlDecimal.AdjustScale(value, Scale - value.Scale, false /* Don't round, truncate. */);
}
return SqlDecimal.ConvertToPrecScale(value, Precision, Scale);
}
return value;
}
private static readonly TimeSpan x_timeMin = TimeSpan.Zero;
private static readonly TimeSpan x_timeMax = new TimeSpan(TimeSpan.TicksPerDay - 1);
private void VerifyTimeRange(TimeSpan value) {
if (SqlDbType.Time == SqlDbType && (x_timeMin > value || value > x_timeMax)) {
ThrowInvalidType();
}
}
private static readonly Int64[] __unitTicksFromScale = {
10000000,
1000000,
100000,
10000,
1000,
100,
10,
1,
};
private Int64 InternalAdjustTimeTicks(Int64 ticks) {
return (ticks / __unitTicksFromScale[Scale] * __unitTicksFromScale[Scale]);
}
private static byte InferScaleFromTimeTicks(Int64 ticks) {
for (byte scale = 0; scale < MaxTimeScale; ++scale) {
if ((ticks / __unitTicksFromScale[scale] * __unitTicksFromScale[scale]) == ticks) {
return scale;
}
}
return MaxTimeScale;
}
private static DbType[] sxm_rgSqlDbTypeToDbType = {
DbType.Int64, // SqlDbType.BigInt
DbType.Binary, // SqlDbType.Binary
DbType.Boolean, // SqlDbType.Bit
DbType.AnsiString, // SqlDbType.Char
DbType.DateTime, // SqlDbType.DateTime
DbType.Decimal, // SqlDbType.Decimal
DbType.Double, // SqlDbType.Float
DbType.Binary, // SqlDbType.Image
DbType.Int32, // SqlDbType.Int
DbType.Currency, // SqlDbType.Money
DbType.String, // SqlDbType.NChar
DbType.String, // SqlDbType.NText
DbType.String, // SqlDbType.NVarChar
DbType.Single, // SqlDbType.Real
DbType.Guid, // SqlDbType.UniqueIdentifier
DbType.DateTime, // SqlDbType.SmallDateTime
DbType.Int16, // SqlDbType.SmallInt
DbType.Currency, // SqlDbType.SmallMoney
DbType.AnsiString, // SqlDbType.Text
DbType.Binary, // SqlDbType.Timestamp
DbType.Byte, // SqlDbType.TinyInt
DbType.Binary, // SqlDbType.VarBinary
DbType.AnsiString, // SqlDbType.VarChar
DbType.Object, // SqlDbType.Variant
DbType.Object, // SqlDbType.Row
DbType.Xml, // SqlDbType.Xml
DbType.String, // SqlDbType.NVarChar, place holder
DbType.String, // SqlDbType.NVarChar, place holder
DbType.String, // SqlDbType.NVarChar, place holder
DbType.Object, // SqlDbType.Udt
DbType.Object, // SqlDbType.Structured
DbType.Date, // SqlDbType.Date
DbType.Time, // SqlDbType.Time
DbType.DateTime2, // SqlDbType.DateTime2
DbType.DateTimeOffset // SqlDbType.DateTimeOffset
};
private void SetDefaultsForType( SqlDbType dbType )
{
if ( SqlDbType.BigInt <= dbType && SqlDbType.DateTimeOffset >= dbType )
{
SqlMetaData smdDflt = sxm_rgDefaults[(int)dbType];
m_sqlDbType = dbType;
m_lMaxLength = smdDflt.MaxLength;
m_bPrecision = smdDflt.Precision;
m_bScale = smdDflt.Scale;
m_lLocale = smdDflt.LocaleId;
m_eCompareOptions = smdDflt.CompareOptions;
}
}
// Array of default-valued metadata ordered by corresponding SqlDbType.
internal static SqlMetaData[] sxm_rgDefaults =
{
// new SqlMetaData(name, DbType, SqlDbType, MaxLen, Prec, Scale, Locale, DatabaseName, SchemaName, isPartialLength)
new SqlMetaData("bigint", SqlDbType.BigInt,
8, 19, 0, 0, SqlCompareOptions.None, false), // SqlDbType.BigInt
new SqlMetaData("binary", SqlDbType.Binary,
1, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Binary
new SqlMetaData("bit", SqlDbType.Bit,
1, 1, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Bit
new SqlMetaData("char", SqlDbType.Char,
1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.Char
new SqlMetaData("datetime", SqlDbType.DateTime,
8, 23, 3, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime
new SqlMetaData("decimal", SqlDbType.Decimal,
9, 18, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Decimal
new SqlMetaData("float", SqlDbType.Float,
8, 53, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Float
new SqlMetaData("image", SqlDbType.Image,
x_lMax, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Image
new SqlMetaData("int", SqlDbType.Int,
4, 10, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Int
new SqlMetaData("money", SqlDbType.Money,
8, 19, 4, 0, SqlCompareOptions.None, false), // SqlDbType.Money
new SqlMetaData("nchar", SqlDbType.NChar,
1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NChar
new SqlMetaData("ntext", SqlDbType.NText,
x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NText
new SqlMetaData("nvarchar", SqlDbType.NVarChar,
x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.NVarChar
new SqlMetaData("real", SqlDbType.Real,
4, 24, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Real
new SqlMetaData("uniqueidentifier", SqlDbType.UniqueIdentifier,
16, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.UniqueIdentifier
new SqlMetaData("smalldatetime", SqlDbType.SmallDateTime,
4, 16, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallDateTime
new SqlMetaData("smallint", SqlDbType.SmallInt,
2, 5, 0, 0, SqlCompareOptions.None, false), // SqlDbType.SmallInt
new SqlMetaData("smallmoney", SqlDbType.SmallMoney,
4, 10, 4, 0, SqlCompareOptions.None, false), // SqlDbType.SmallMoney
new SqlMetaData("text", SqlDbType.Text,
x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.Text
new SqlMetaData("timestamp", SqlDbType.Timestamp,
8, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Timestamp
new SqlMetaData("tinyint", SqlDbType.TinyInt,
1, 3, 0, 0, SqlCompareOptions.None, false), // SqlDbType.TinyInt
new SqlMetaData("varbinary", SqlDbType.VarBinary,
x_lServerMaxBinary, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.VarBinary
new SqlMetaData("varchar", SqlDbType.VarChar,
x_lServerMaxANSI, 0, 0, 0, x_eDefaultStringCompareOptions, false), // SqlDbType.VarChar
new SqlMetaData("sql_variant", SqlDbType.Variant,
8016, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Variant
new SqlMetaData("nvarchar", SqlDbType.NVarChar,
1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 24
new SqlMetaData("xml", SqlDbType.Xml,
x_lMax, 0, 0, 0, x_eDefaultStringCompareOptions, true), // SqlDbType.Xml
new SqlMetaData("nvarchar", SqlDbType.NVarChar,
1, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 26
new SqlMetaData("nvarchar", SqlDbType.NVarChar,
x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 27
new SqlMetaData("nvarchar", SqlDbType.NVarChar,
x_lServerMaxUnicode, 0, 0, 0, x_eDefaultStringCompareOptions, false), // Placeholder for value 28
new SqlMetaData("udt", SqlDbType.Udt,
0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Udt = 29 Bug Fix: 302698
new SqlMetaData("table", SqlDbType.Structured,
0, 0, 0, 0, SqlCompareOptions.None, false), // SqlDbType.Structured
new SqlMetaData("date", SqlDbType.Date,
3, 10,0, 0, SqlCompareOptions.None, false), // SqlDbType.Date
new SqlMetaData("time", SqlDbType.Time,
5, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.Time
new SqlMetaData("datetime2", SqlDbType.DateTime2,
8, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTime2
new SqlMetaData("datetimeoffset", SqlDbType.DateTimeOffset,
10, 0, 7, 0, SqlCompareOptions.None, false), // SqlDbType.DateTimeOffset
};
}
}
|