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
|
// NpgsqlTypes.NpgsqlTypesHelper.cs
//
// Author:
// Francisco Jr. (fxjrlists@yahoo.com.br)
//
// Copyright (C) 2002 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections;
using System.Globalization;
using System.Data;
using System.Net;
using System.Text;
using System.Resources;
using Npgsql;
namespace NpgsqlTypes
{
/// <summary>
/// This class contains helper methods for type conversion between
/// the .Net type system and postgresql.
/// </summary>
internal abstract class NpgsqlTypesHelper
{
// Logging related values
private static readonly String CLASSNAME = "NpgsqlTypesHelper";
private static ResourceManager resman = new ResourceManager(typeof(NpgsqlTypesHelper));
/// <summary>
/// A cache of basic datatype mappings keyed by server version. This way we don't
/// have to load the basic type mappings for every connection.
/// </summary>
private static Hashtable BackendTypeMappingCache = new Hashtable();
private static NpgsqlNativeTypeMapping NativeTypeMapping = null;
/// <summary>
/// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
/// of the given NpgsqlDbType.
/// </summary>
public static NpgsqlNativeTypeInfo GetNativeTypeInfo(NpgsqlDbType NpgsqlDbType)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
VerifyDefaultTypesMap();
return NativeTypeMapping[NpgsqlDbType];
}
/// <summary>
/// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
/// of the given DbType.
/// </summary>
public static NpgsqlNativeTypeInfo GetNativeTypeInfo(DbType DbType)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
VerifyDefaultTypesMap();
return NativeTypeMapping[DbType];
}
/// <summary>
/// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
/// of the given System.Type.
/// </summary>
public static NpgsqlNativeTypeInfo GetNativeTypeInfo(Type Type)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
VerifyDefaultTypesMap();
return NativeTypeMapping[Type];
}
// CHECKME
// Not sure what to do with this one. I don't believe we ever ask for a binary
// formatting, so this shouldn't even be used right now.
// At some point this will need to be merged into the type converter system somehow?
public static Object ConvertBackendBytesToSystemType(NpgsqlBackendTypeInfo TypeInfo, Byte[] data, Encoding encoding, Int32 fieldValueSize, Int32 typeModifier)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendBytesToStytemType");
// We are never guaranteed to know about every possible data type the server can send us.
// When we encounter an unknown type, we punt and return the data without modification.
if (TypeInfo == null)
return data;
switch (TypeInfo.NpgsqlDbType)
{
case NpgsqlDbType.Bytea:
return data;
/*case NpgsqlDbType.Boolean:
return BitConverter.ToBoolean(data, 0);
case NpgsqlDbType.DateTime:
return DateTime.MinValue.AddTicks(IPAddress.NetworkToHostOrder(BitConverter.ToInt64(data, 0)));
case NpgsqlDbType.Int16:
return IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 0));
case NpgsqlDbType.Int32:
return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 0));
case NpgsqlDbType.Int64:
return IPAddress.NetworkToHostOrder(BitConverter.ToInt64(data, 0));
case NpgsqlDbType.String:
case NpgsqlDbType.AnsiString:
case NpgsqlDbType.StringFixedLength:
return encoding.GetString(data, 0, fieldValueSize);*/
default:
throw new InvalidCastException("Type not supported in binary format");
}
return null;
}
///<summary>
/// This method is responsible to convert the string received from the backend
/// to the corresponding NpgsqlType.
/// The given TypeInfo is called upon to do the conversion.
/// If no TypeInfo object is provided, no conversion is performed.
/// </summary>
public static Object ConvertBackendStringToSystemType(NpgsqlBackendTypeInfo TypeInfo, String data, Int16 typeSize, Int32 typeModifier)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendStringToSystemType");
if (TypeInfo != null) {
return TypeInfo.ConvertToNative(data, typeSize, typeModifier);
} else {
return data;
}
}
/// <summary>
/// Create the one and only native to backend type map.
/// This map is used when formatting native data
/// types to backend representations.
/// </summary>
private static void VerifyDefaultTypesMap()
{
lock(CLASSNAME) {
if (NativeTypeMapping != null) {
return;
}
NativeTypeMapping = new NpgsqlNativeTypeMapping();
// Conflicting types should have mapped first the non default mappings.
// For example, char, varchar and text map to DbType.String. As the most
// common is to use text with string, it has to be the last mapped, in order
// to type mapping has the last entry, in this case, text, as the map value
// for DbType.String.
NativeTypeMapping.AddType("refcursor", NpgsqlDbType.Refcursor, DbType.String, true, null);
NativeTypeMapping.AddType("char", NpgsqlDbType.Char, DbType.String, true, null);
NativeTypeMapping.AddType("varchar", NpgsqlDbType.Varchar, DbType.String, true, null);
NativeTypeMapping.AddType("text", NpgsqlDbType.Text, DbType.String, true, null);
NativeTypeMapping.AddDbTypeAlias("text", DbType.StringFixedLength);
NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiString);
NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiStringFixedLength);
NativeTypeMapping.AddTypeAlias("text", typeof(String));
NativeTypeMapping.AddType("bytea", NpgsqlDbType.Bytea, DbType.Binary, true,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBinary));
NativeTypeMapping.AddTypeAlias("bytea", typeof(Byte[]));
NativeTypeMapping.AddType("bit", NpgsqlDbType.Bit, DbType.Boolean, true,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBit));
NativeTypeMapping.AddType("bool", NpgsqlDbType.Boolean, DbType.Boolean, false,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBoolean));
NativeTypeMapping.AddTypeAlias("bool", typeof(Boolean));
NativeTypeMapping.AddType("int2", NpgsqlDbType.Smallint, DbType.Int16, false,
null);
NativeTypeMapping.AddTypeAlias("int2", typeof(Int16));
NativeTypeMapping.AddDbTypeAlias("int2", DbType.Byte);
NativeTypeMapping.AddTypeAlias("int2", typeof(Byte));
NativeTypeMapping.AddType("int4", NpgsqlDbType.Integer, DbType.Int32, false,
null);
NativeTypeMapping.AddTypeAlias("int4", typeof(Int32));
NativeTypeMapping.AddType("int8", NpgsqlDbType.Bigint, DbType.Int64, false,
null);
NativeTypeMapping.AddTypeAlias("int8", typeof(Int64));
NativeTypeMapping.AddType("float4", NpgsqlDbType.Real, DbType.Single, true,
null);
NativeTypeMapping.AddTypeAlias("float4", typeof(Single));
NativeTypeMapping.AddType("float8", NpgsqlDbType.Double, DbType.Double, true,
null);
NativeTypeMapping.AddTypeAlias("float8", typeof(Double));
NativeTypeMapping.AddType("numeric", NpgsqlDbType.Numeric, DbType.Decimal, false,
null);
NativeTypeMapping.AddTypeAlias("numeric", typeof(Decimal));
NativeTypeMapping.AddType("currency", NpgsqlDbType.Money, DbType.Currency, true,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToMoney));
NativeTypeMapping.AddType("date", NpgsqlDbType.Date, DbType.Date, true,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDate));
NativeTypeMapping.AddType("time", NpgsqlDbType.Time, DbType.Time, true,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToTime));
NativeTypeMapping.AddType("timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, true,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDateTime));
NativeTypeMapping.AddTypeAlias("timestamp", typeof(DateTime));
NativeTypeMapping.AddType("timestamptz", NpgsqlDbType.TimestampTZ, DbType.DateTime, true,
new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDateTime));
NativeTypeMapping.AddType("point", NpgsqlDbType.Point, DbType.Object, true,
new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPoint));
NativeTypeMapping.AddTypeAlias("point", typeof(NpgsqlPoint));
NativeTypeMapping.AddType("box", NpgsqlDbType.Box, DbType.Object, true,
new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToBox));
NativeTypeMapping.AddTypeAlias("box", typeof(NpgsqlBox));
NativeTypeMapping.AddType("lseg", NpgsqlDbType.LSeg, DbType.Object, true,
new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToLSeg));
NativeTypeMapping.AddTypeAlias("lseg", typeof(NpgsqlLSeg));
NativeTypeMapping.AddType("path", NpgsqlDbType.Path, DbType.Object, true,
new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPath));
NativeTypeMapping.AddTypeAlias("path", typeof(NpgsqlPath));
NativeTypeMapping.AddType("polygon", NpgsqlDbType.Polygon, DbType.Object, true,
new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPolygon));
NativeTypeMapping.AddTypeAlias("polygon", typeof(NpgsqlPolygon));
NativeTypeMapping.AddType("circle", NpgsqlDbType.Circle, DbType.Object, true,
new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToCircle));
NativeTypeMapping.AddTypeAlias("circle", typeof(NpgsqlCircle));
NativeTypeMapping.AddType("inet", NpgsqlDbType.Inet, DbType.Object, true,
new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToIPAddress));
NativeTypeMapping.AddTypeAlias("inet", typeof(IPAddress));
NativeTypeMapping.AddTypeAlias("inet", typeof(NpgsqlInet));
}
}
///<summary>
/// This method creates (or retrieves from cache) a mapping between type and OID
/// of all natively supported postgresql data types.
/// This is needed as from one version to another, this mapping can be changed and
/// so we avoid hardcoding them.
/// </summary>
/// <returns>NpgsqlTypeMapping containing all known data types. The mapping must be
/// cloned before it is modified because it is cached; changes made by one connection may
/// effect another connection.</returns>
public static NpgsqlBackendTypeMapping CreateAndLoadInitialTypesMapping(NpgsqlConnector conn)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "LoadTypesMapping");
// [TODO] Verify another way to get higher concurrency.
lock(CLASSNAME)
{
// Check the cache for an initial types map.
NpgsqlBackendTypeMapping oidToNameMapping = (NpgsqlBackendTypeMapping) BackendTypeMappingCache[conn.ServerVersion];
if (oidToNameMapping != null)
{
return oidToNameMapping;
}
// Not in cache, create a new one.
oidToNameMapping = new NpgsqlBackendTypeMapping();
// Create a list of all natively supported postgresql data types.
NpgsqlBackendTypeInfo[] TypeInfoList = new NpgsqlBackendTypeInfo[]
{
new NpgsqlBackendTypeInfo(0, "unknown", NpgsqlDbType.Text, DbType.String, typeof(String),
null),
new NpgsqlBackendTypeInfo(0, "refcursor", NpgsqlDbType.Refcursor, DbType.String, typeof(String),
null),
new NpgsqlBackendTypeInfo(0, "char", NpgsqlDbType.Char, DbType.String, typeof(String),
null),
new NpgsqlBackendTypeInfo(0, "bpchar", NpgsqlDbType.Text, DbType.String, typeof(String),
null),
new NpgsqlBackendTypeInfo(0, "varchar", NpgsqlDbType.Varchar, DbType.String, typeof(String),
null),
new NpgsqlBackendTypeInfo(0, "text", NpgsqlDbType.Text, DbType.String, typeof(String),
null),
new NpgsqlBackendTypeInfo(0, "name", NpgsqlDbType.Text, DbType.String, typeof(String),
null),
new NpgsqlBackendTypeInfo(0, "bytea", NpgsqlDbType.Bytea, DbType.Binary, typeof(Byte[]),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBinary)),
new NpgsqlBackendTypeInfo(0, "bit", NpgsqlDbType.Bit, DbType.Boolean, typeof(Boolean),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBit)),
new NpgsqlBackendTypeInfo(0, "bool", NpgsqlDbType.Boolean, DbType.Boolean, typeof(Boolean),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBoolean)),
new NpgsqlBackendTypeInfo(0, "int2", NpgsqlDbType.Smallint, DbType.Int16, typeof(Int16),
null),
new NpgsqlBackendTypeInfo(0, "int4", NpgsqlDbType.Integer, DbType.Int32, typeof(Int32),
null),
new NpgsqlBackendTypeInfo(0, "int8", NpgsqlDbType.Bigint, DbType.Int64, typeof(Int64),
null),
new NpgsqlBackendTypeInfo(0, "oid", NpgsqlDbType.Bigint, DbType.Int64, typeof(Int64),
null),
new NpgsqlBackendTypeInfo(0, "float4", NpgsqlDbType.Real, DbType.Single, typeof(Single),
null),
new NpgsqlBackendTypeInfo(0, "float8", NpgsqlDbType.Double, DbType.Double, typeof(Double),
null),
new NpgsqlBackendTypeInfo(0, "numeric", NpgsqlDbType.Numeric, DbType.Decimal, typeof(Decimal),
null),
new NpgsqlBackendTypeInfo(0, "inet", NpgsqlDbType.Inet, DbType.Object, typeof(NpgsqlInet), new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToInet)),
new NpgsqlBackendTypeInfo(0, "money", NpgsqlDbType.Money, DbType.Decimal, typeof(Decimal),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToMoney)),
new NpgsqlBackendTypeInfo(0, "date", NpgsqlDbType.Date, DbType.Date, typeof(DateTime),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDate)),
new NpgsqlBackendTypeInfo(0, "time", NpgsqlDbType.Time, DbType.Time, typeof(DateTime),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToTime)),
new NpgsqlBackendTypeInfo(0, "timetz", NpgsqlDbType.Time, DbType.Time, typeof(DateTime),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToTime)),
new NpgsqlBackendTypeInfo(0, "timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(DateTime),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDateTime)),
new NpgsqlBackendTypeInfo(0, "timestamptz", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(DateTime),
new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDateTime)),
new NpgsqlBackendTypeInfo(0, "point", NpgsqlDbType.Point, DbType.Object, typeof(NpgsqlPoint),
new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPoint)),
new NpgsqlBackendTypeInfo(0, "lseg", NpgsqlDbType.LSeg, DbType.Object, typeof(NpgsqlLSeg),
new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToLSeg)),
new NpgsqlBackendTypeInfo(0, "path", NpgsqlDbType.Path, DbType.Object, typeof(NpgsqlPath),
new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPath)),
new NpgsqlBackendTypeInfo(0, "box", NpgsqlDbType.Box, DbType.Object, typeof(NpgsqlBox),
new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToBox)),
new NpgsqlBackendTypeInfo(0, "circle", NpgsqlDbType.Circle, DbType.Object, typeof(NpgsqlCircle),
new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToCircle)),
new NpgsqlBackendTypeInfo(0, "polygon", NpgsqlDbType.Polygon, DbType.Object, typeof(NpgsqlPolygon),
new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPolygon)),
};
// Attempt to map each type info in the list to an OID on the backend and
// add each mapped type to the new type mapping object.
LoadTypesMappings(conn, oidToNameMapping, TypeInfoList);
// Add this mapping to the per-server-version cache so we don't have to
// do these expensive queries on every connection startup.
BackendTypeMappingCache.Add(conn.ServerVersion, oidToNameMapping);
return oidToNameMapping;
}
}
/// <summary>
/// Attempt to map types by issuing a query against pg_type.
/// This function takes a list of NpgsqlTypeInfo and attempts to resolve the OID field
/// of each by querying pg_type. If the mapping is found, the type info object is
/// updated (OID) and added to the provided NpgsqlTypeMapping object.
/// </summary>
/// <param name="conn">NpgsqlConnector to send query through.</param>
/// <param name="TypeMappings">Mapping object to add types too.</param>
/// <param name="TypeInfoList">List of types that need to have OID's mapped.</param>
public static void LoadTypesMappings(NpgsqlConnector conn, NpgsqlBackendTypeMapping TypeMappings, IList TypeInfoList)
{
StringBuilder InList = new StringBuilder();
Hashtable NameIndex = new Hashtable();
// Build a clause for the SELECT statement.
// Build a name->typeinfo mapping so we can match the results of the query
/// with the list of type objects efficiently.
foreach (NpgsqlBackendTypeInfo TypeInfo in TypeInfoList) {
NameIndex.Add(TypeInfo.Name, TypeInfo);
InList.AppendFormat("{0}'{1}'", ((InList.Length > 0) ? ", " : ""), TypeInfo.Name);
}
if (InList.Length == 0) {
return;
}
NpgsqlCommand command = new NpgsqlCommand("SELECT oid, typname FROM pg_type WHERE typname IN (" + InList.ToString() + ")", conn);
NpgsqlDataReader dr = command.ExecuteReader();
while (dr.Read()) {
NpgsqlBackendTypeInfo TypeInfo = (NpgsqlBackendTypeInfo)NameIndex[dr[1].ToString()];
TypeInfo._OID = Convert.ToInt32(dr[0]);
TypeMappings.AddType(TypeInfo);
}
}
}
/// <summary>
/// Delegate called to convert the given backend data to its native representation.
/// </summary>
internal delegate Object ConvertBackendToNativeHandler(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier);
/// <summary>
/// Delegate called to convert the given native data to its backand representation.
/// </summary>
internal delegate String ConvertNativeToBackendHandler(NpgsqlNativeTypeInfo TypeInfo, Object NativeData);
/// <summary>
/// Represents a backend data type.
/// This class can be called upon to convert a backend field representation to a native object.
/// </summary>
internal class NpgsqlBackendTypeInfo
{
private ConvertBackendToNativeHandler _ConvertBackendToNative;
internal Int32 _OID;
private String _Name;
private NpgsqlDbType _NpgsqlDbType;
private DbType _DbType;
private Type _Type;
/// <summary>
/// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
/// </summary>
/// <param name="OID">Type OID provided by the backend server.</param>
/// <param name="Name">Type name provided by the backend server.</param>
/// <param name="NpgsqlDbType">NpgsqlDbType</param>
/// <param name="Type">System type to convert fields of this type to.</param>
/// <param name="ConvertBackendToNative">Data conversion handler.</param>
public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type, ConvertBackendToNativeHandler ConvertBackendToNative)
{
_OID = OID;
_Name = Name;
_NpgsqlDbType = NpgsqlDbType;
_DbType = DbType;
_Type = Type;
_ConvertBackendToNative = ConvertBackendToNative;
}
/// <summary>
/// Type OID provided by the backend server.
/// </summary>
public Int32 OID
{
get { return _OID; }
}
/// <summary>
/// Type name provided by the backend server.
/// </summary>
public String Name
{ get { return _Name; } }
/// <summary>
/// NpgsqlDbType.
/// </summary>
public NpgsqlDbType NpgsqlDbType
{ get { return _NpgsqlDbType; } }
/// <summary>
/// NpgsqlDbType.
/// </summary>
public DbType DbType
{ get { return _DbType; } }
/// <summary>
/// System type to convert fields of this type to.
/// </summary>
public Type Type
{ get { return _Type; } }
/// <summary>
/// Perform a data conversion from a backend representation to
/// a native object.
/// </summary>
/// <param name="BackendData">Data sent from the backend.</param>
/// <param name="TypeModifier">Type modifier field sent from the backend.</param>
public Object ConvertToNative(String BackendData, Int16 TypeSize, Int32 TypeModifier)
{
if (_ConvertBackendToNative != null) {
return _ConvertBackendToNative(this, BackendData, TypeSize, TypeModifier);
} else {
try {
return Convert.ChangeType(BackendData, Type, CultureInfo.InvariantCulture);
} catch {
return BackendData;
}
}
}
}
/// <summary>
/// Represents a backend data type.
/// This class can be called upon to convert a native object to its backend field representation,
/// </summary>
internal class NpgsqlNativeTypeInfo
{
private static NumberFormatInfo ni;
private ConvertNativeToBackendHandler _ConvertNativeToBackend;
private String _Name;
private NpgsqlDbType _NpgsqlDbType;
private DbType _DbType;
private Boolean _Quote;
private Boolean _UseSize;
static NpgsqlNativeTypeInfo()
{
ni = (NumberFormatInfo) CultureInfo.InvariantCulture.NumberFormat.Clone();
ni.NumberDecimalDigits = 15;
}
/// <summary>
/// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
/// </summary>
/// <param name="OID">Type OID provided by the backend server.</param>
/// <param name="Name">Type name provided by the backend server.</param>
/// <param name="NpgsqlDbType">NpgsqlDbType</param>
/// <param name="Type">System type to convert fields of this type to.</param>
/// <param name="ConvertBackendToNative">Data conversion handler.</param>
/// <param name="ConvertNativeToBackend">Data conversion handler.</param>
public NpgsqlNativeTypeInfo(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote, ConvertNativeToBackendHandler ConvertNativeToBackend)
{
_Name = Name;
_NpgsqlDbType = NpgsqlDbType;
_DbType = DbType;
_Quote = Quote;
_ConvertNativeToBackend = ConvertNativeToBackend;
// The only parameters types which use length currently supported are char and varchar. Check for them.
if ( (NpgsqlDbType == NpgsqlDbType.Char)
|| (NpgsqlDbType == NpgsqlDbType.Varchar))
_UseSize = true;
else
_UseSize = false;
}
/// <summary>
/// Type name provided by the backend server.
/// </summary>
public String Name
{ get { return _Name; } }
/// <summary>
/// NpgsqlDbType.
/// </summary>
public NpgsqlDbType NpgsqlDbType
{ get { return _NpgsqlDbType; } }
/// <summary>
/// DbType.
/// </summary>
public DbType DbType
{ get { return _DbType; } }
/// <summary>
/// Apply quoting.
/// </summary>
public Boolean Quote
{ get { return _Quote; } }
/// <summary>
/// Use parameter size information.
/// </summary>
public Boolean UseSize
{ get { return _UseSize; } }
/// <summary>
/// Perform a data conversion from a native object to
/// a backend representation.
/// DBNull and null values are handled differently depending if a plain query is used
/// When
/// </summary>
/// <param name="NativeData">Native .NET object to be converted.</param>
/// <param name="ForExtendedQuery">Flag indicating if the conversion has to be done for
/// plain queries or extended queries</param>
public String ConvertToBackend(Object NativeData, Boolean ForExtendedQuery)
{
if (ForExtendedQuery)
return ConvertToBackendExtendedQuery(NativeData);
else
return ConvertToBackendPlainQuery(NativeData);
}
private String ConvertToBackendPlainQuery(Object NativeData)
{
if ((NativeData == DBNull.Value) || (NativeData == null))
return "NULL"; // Plain queries exptects null values as string NULL.
if (_ConvertNativeToBackend != null)
return (this.Quote ? QuoteString(_ConvertNativeToBackend(this, NativeData)) : _ConvertNativeToBackend(this, NativeData));
else
{
if (NativeData is System.Enum)
{
// Do a special handling of Enum values.
// Translate enum value to its underlying type.
return QuoteString((String)Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof(String), CultureInfo.InvariantCulture));
}
else if (NativeData is IFormattable)
{
return (this.Quote ? QuoteString(((IFormattable) NativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\")) :
((IFormattable) NativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\"));
}
// Do special handling of strings when in simple query. Escape quotes and backslashes.
return (this.Quote ? QuoteString(NativeData.ToString().Replace("'", "''").Replace("\\", "\\\\").Replace("\0", "\\0")) :
NativeData.ToString().Replace("'", "''").Replace("\\", "\\\\").Replace("\0", "\\0"));
}
}
private String ConvertToBackendExtendedQuery(Object NativeData)
{
if ((NativeData == DBNull.Value) || (NativeData == null))
return null; // Extended query expects null values be represented as null.
if (_ConvertNativeToBackend != null)
return _ConvertNativeToBackend(this, NativeData);
else
{
if (NativeData is System.Enum)
{
// Do a special handling of Enum values.
// Translate enum value to its underlying type.
return (String)Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof(String), CultureInfo.InvariantCulture);
}
else if (NativeData is IFormattable)
{
return ((IFormattable)NativeData).ToString(null, ni);
}
return NativeData.ToString();
}
}
private static String QuoteString(String S)
{
return String.Format("E'{0}'", S);
}
}
/// <summary>
/// Provide mapping between type OID, type name, and a NpgsqlBackendTypeInfo object that represents it.
/// </summary>
internal class NpgsqlBackendTypeMapping
{
private Hashtable OIDIndex;
private Hashtable NameIndex;
/// <summary>
/// Construct an empty mapping.
/// </summary>
public NpgsqlBackendTypeMapping()
{
OIDIndex = new Hashtable();
NameIndex = new Hashtable();
}
/// <summary>
/// Copy constuctor.
/// </summary>
private NpgsqlBackendTypeMapping(NpgsqlBackendTypeMapping Other)
{
OIDIndex = (Hashtable)Other.OIDIndex.Clone();
NameIndex = (Hashtable)Other.NameIndex.Clone();
}
/// <summary>
/// Add the given NpgsqlBackendTypeInfo to this mapping.
/// </summary>
public void AddType(NpgsqlBackendTypeInfo T)
{
if (OIDIndex.Contains(T.OID)) {
throw new Exception("Type already mapped");
}
OIDIndex[T.OID] = T;
NameIndex[T.Name] = T;
}
/// <summary>
/// Add a new NpgsqlBackendTypeInfo with the given attributes and conversion handlers to this mapping.
/// </summary>
/// <param name="OID">Type OID provided by the backend server.</param>
/// <param name="Name">Type name provided by the backend server.</param>
/// <param name="NpgsqlDbType">NpgsqlDbType</param>
/// <param name="Type">System type to convert fields of this type to.</param>
/// <param name="ConvertBackendToNative">Data conversion handler.</param>
public void AddType(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
ConvertBackendToNativeHandler BackendConvert)
{
AddType(new NpgsqlBackendTypeInfo(OID, Name, NpgsqlDbType, DbType, Type, BackendConvert));
}
/// <summary>
/// Get the number of type infos held.
/// </summary>
public Int32 Count
{ get { return NameIndex.Count; } }
/// <summary>
/// Retrieve the NpgsqlBackendTypeInfo with the given backend type OID, or null if none found.
/// </summary>
public NpgsqlBackendTypeInfo this [Int32 OID]
{
get
{
return (NpgsqlBackendTypeInfo)OIDIndex[OID];
}
}
/// <summary>
/// Retrieve the NpgsqlBackendTypeInfo with the given backend type name, or null if none found.
/// </summary>
public NpgsqlBackendTypeInfo this [String Name]
{
get
{
return (NpgsqlBackendTypeInfo)NameIndex[Name];
}
}
/// <summary>
/// Make a shallow copy of this type mapping.
/// </summary>
public NpgsqlBackendTypeMapping Clone()
{
return new NpgsqlBackendTypeMapping(this);
}
/// <summary>
/// Determine if a NpgsqlBackendTypeInfo with the given backend type OID exists in this mapping.
/// </summary>
public Boolean ContainsOID(Int32 OID)
{
return OIDIndex.ContainsKey(OID);
}
/// <summary>
/// Determine if a NpgsqlBackendTypeInfo with the given backend type name exists in this mapping.
/// </summary>
public Boolean ContainsName(String Name)
{
return NameIndex.ContainsKey(Name);
}
}
/// <summary>
/// Provide mapping between type Type, NpgsqlDbType and a NpgsqlNativeTypeInfo object that represents it.
/// </summary>
internal class NpgsqlNativeTypeMapping
{
private Hashtable NameIndex;
private Hashtable NpgsqlDbTypeIndex;
private Hashtable DbTypeIndex;
private Hashtable TypeIndex;
/// <summary>
/// Construct an empty mapping.
/// </summary>
public NpgsqlNativeTypeMapping()
{
NameIndex = new Hashtable();
NpgsqlDbTypeIndex = new Hashtable();
DbTypeIndex = new Hashtable();
TypeIndex = new Hashtable();
}
/// <summary>
/// Add the given NpgsqlNativeTypeInfo to this mapping.
/// </summary>
public void AddType(NpgsqlNativeTypeInfo T)
{
if (NameIndex.Contains(T.Name)) {
throw new Exception("Type already mapped");
}
NameIndex[T.Name] = T;
NpgsqlDbTypeIndex[T.NpgsqlDbType] = T;
DbTypeIndex[T.DbType] = T;
}
/// <summary>
/// Add a new NpgsqlNativeTypeInfo with the given attributes and conversion handlers to this mapping.
/// </summary>
/// <param name="OID">Type OID provided by the backend server.</param>
/// <param name="Name">Type name provided by the backend server.</param>
/// <param name="NpgsqlDbType">NpgsqlDbType</param>
/// <param name="ConvertBackendToNative">Data conversion handler.</param>
/// <param name="ConvertNativeToBackend">Data conversion handler.</param>
public void AddType(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote,
ConvertNativeToBackendHandler NativeConvert)
{
AddType(new NpgsqlNativeTypeInfo(Name, NpgsqlDbType, DbType, Quote, NativeConvert));
}
public void AddNpgsqlDbTypeAlias(String Name, NpgsqlDbType NpgsqlDbType)
{
if (NpgsqlDbTypeIndex.Contains(NpgsqlDbType)) {
throw new Exception("NpgsqlDbType already aliased");
}
NpgsqlDbTypeIndex[NpgsqlDbType] = NameIndex[Name];
}
public void AddDbTypeAlias(String Name, DbType DbType)
{
if (DbTypeIndex.Contains(DbType)) {
throw new Exception("NpgsqlDbType already aliased");
}
DbTypeIndex[DbType] = NameIndex[Name];
}
public void AddTypeAlias(String Name, Type Type)
{
if (TypeIndex.Contains(Type)) {
throw new Exception("Type already aliased");
}
TypeIndex[Type] = NameIndex[Name];
}
/// <summary>
/// Get the number of type infos held.
/// </summary>
public Int32 Count
{ get { return NameIndex.Count; } }
/// <summary>
/// Retrieve the NpgsqlNativeTypeInfo with the given backend type name, or null if none found.
/// </summary>
public NpgsqlNativeTypeInfo this [String Name]
{
get
{
return (NpgsqlNativeTypeInfo)NameIndex[Name];
}
}
/// <summary>
/// Retrieve the NpgsqlNativeTypeInfo with the given NpgsqlDbType, or null if none found.
/// </summary>
public NpgsqlNativeTypeInfo this [NpgsqlDbType NpgsqlDbType]
{
get
{
return (NpgsqlNativeTypeInfo)NpgsqlDbTypeIndex[NpgsqlDbType];
}
}
/// <summary>
/// Retrieve the NpgsqlNativeTypeInfo with the given DbType, or null if none found.
/// </summary>
public NpgsqlNativeTypeInfo this [DbType DbType]
{
get
{
return (NpgsqlNativeTypeInfo)DbTypeIndex[DbType];
}
}
/// <summary>
/// Retrieve the NpgsqlNativeTypeInfo with the given Type, or null if none found.
/// </summary>
public NpgsqlNativeTypeInfo this [Type Type]
{
get
{
return (NpgsqlNativeTypeInfo)TypeIndex[Type];
}
}
/// <summary>
/// Determine if a NpgsqlNativeTypeInfo with the given backend type name exists in this mapping.
/// </summary>
public Boolean ContainsName(String Name)
{
return NameIndex.ContainsKey(Name);
}
/// <summary>
/// Determine if a NpgsqlNativeTypeInfo with the given NpgsqlDbType exists in this mapping.
/// </summary>
public Boolean ContainsNpgsqlDbType(NpgsqlDbType NpgsqlDbType)
{
return NpgsqlDbTypeIndex.ContainsKey(NpgsqlDbType);
}
/// <summary>
/// Determine if a NpgsqlNativeTypeInfo with the given Type name exists in this mapping.
/// </summary>
public Boolean ContainsType(Type Type)
{
return TypeIndex.ContainsKey(Type);
}
}
}
|