1 // NpgsqlTypes.NpgsqlTypesHelper.cs
4 // Francisco Jr. (fxjrlists@yahoo.com.br)
6 // Copyright (C) 2002 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
8 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 using System.Collections;
26 using System.Globalization;
30 using System.Resources;
37 /// This class contains helper methods for type conversion between
38 /// the .Net type system and postgresql.
40 internal abstract class NpgsqlTypesHelper
42 // Logging related values
43 private static readonly String CLASSNAME = "NpgsqlTypesHelper";
44 private static ResourceManager resman = new ResourceManager(typeof(NpgsqlTypesHelper));
47 /// A cache of basic datatype mappings keyed by server version. This way we don't
48 /// have to load the basic type mappings for every connection.
50 private static Hashtable BackendTypeMappingCache = new Hashtable();
51 private static NpgsqlNativeTypeMapping NativeTypeMapping = null;
55 /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
56 /// of the given NpgsqlDbType.
58 public static NpgsqlNativeTypeInfo GetNativeTypeInfo(NpgsqlDbType NpgsqlDbType)
60 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
62 VerifyDefaultTypesMap();
63 return NativeTypeMapping[NpgsqlDbType];
67 /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
68 /// of the given DbType.
70 public static NpgsqlNativeTypeInfo GetNativeTypeInfo(DbType DbType)
72 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
74 VerifyDefaultTypesMap();
75 return NativeTypeMapping[DbType];
81 /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
82 /// of the given System.Type.
84 public static NpgsqlNativeTypeInfo GetNativeTypeInfo(Type Type)
86 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
88 VerifyDefaultTypesMap();
89 return NativeTypeMapping[Type];
93 // Not sure what to do with this one. I don't believe we ever ask for a binary
94 // formatting, so this shouldn't even be used right now.
95 // At some point this will need to be merged into the type converter system somehow?
96 public static Object ConvertBackendBytesToSystemType(NpgsqlBackendTypeInfo TypeInfo, Byte[] data, Encoding encoding, Int32 fieldValueSize, Int32 typeModifier)
98 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendBytesToStytemType");
101 // We are never guaranteed to know about every possible data type the server can send us.
102 // When we encounter an unknown type, we punt and return the data without modification.
103 if (TypeInfo == null)
106 switch (TypeInfo.NpgsqlDbType)
108 case NpgsqlDbType.Binary:
110 case NpgsqlDbType.Boolean:
111 return BitConverter.ToBoolean(data, 0);
112 case NpgsqlDbType.DateTime:
113 return DateTime.MinValue.AddTicks(IPAddress.NetworkToHostOrder(BitConverter.ToInt64(data, 0)));
115 case NpgsqlDbType.Int16:
116 return IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 0));
117 case NpgsqlDbType.Int32:
118 return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 0));
119 case NpgsqlDbType.Int64:
120 return IPAddress.NetworkToHostOrder(BitConverter.ToInt64(data, 0));
121 case NpgsqlDbType.String:
122 case NpgsqlDbType.AnsiString:
123 case NpgsqlDbType.StringFixedLength:
124 return encoding.GetString(data, 0, fieldValueSize);
126 throw new InvalidCastException("Type not supported in binary format");
133 /// This method is responsible to convert the string received from the backend
134 /// to the corresponding NpgsqlType.
135 /// The given TypeInfo is called upon to do the conversion.
136 /// If no TypeInfo object is provided, no conversion is performed.
138 public static Object ConvertBackendStringToSystemType(NpgsqlBackendTypeInfo TypeInfo, String data, Int16 typeSize, Int32 typeModifier)
140 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendStringToSystemType");
142 if (TypeInfo != null) {
143 return TypeInfo.ConvertToNative(data, typeSize, typeModifier);
150 /// Create the one and only native to backend type map.
151 /// This map is used when formatting native data
152 /// types to backend representations.
154 private static void VerifyDefaultTypesMap()
157 if (NativeTypeMapping != null) {
161 NativeTypeMapping = new NpgsqlNativeTypeMapping();
163 NativeTypeMapping.AddType("text", NpgsqlDbType.Text, DbType.String, true,
164 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToString));
166 NativeTypeMapping.AddDbTypeAlias("text", DbType.StringFixedLength);
167 NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiString);
168 NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiStringFixedLength);
169 NativeTypeMapping.AddTypeAlias("text", typeof(String));
171 NativeTypeMapping.AddType("bytea", NpgsqlDbType.Bytea, DbType.Binary, true,
172 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBinary));
174 NativeTypeMapping.AddTypeAlias("bytea", typeof(Byte[]));
176 NativeTypeMapping.AddType("bool", NpgsqlDbType.Boolean, DbType.Boolean, false,
177 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBoolean));
179 NativeTypeMapping.AddTypeAlias("bool", typeof(Boolean));
181 NativeTypeMapping.AddType("int2", NpgsqlDbType.Smallint, DbType.Int16, false,
184 NativeTypeMapping.AddTypeAlias("int2", typeof(Int16));
186 NativeTypeMapping.AddDbTypeAlias("int2", DbType.Byte);
188 NativeTypeMapping.AddType("int4", NpgsqlDbType.Integer, DbType.Int32, false,
191 NativeTypeMapping.AddTypeAlias("int4", typeof(Int32));
193 NativeTypeMapping.AddType("int8", NpgsqlDbType.Bigint, DbType.Int64, false,
196 NativeTypeMapping.AddTypeAlias("int8", typeof(Int64));
198 NativeTypeMapping.AddType("float4", NpgsqlDbType.Real, DbType.Single, false,
201 NativeTypeMapping.AddTypeAlias("float4", typeof(Single));
203 NativeTypeMapping.AddType("float8", NpgsqlDbType.Double, DbType.Double, false,
206 NativeTypeMapping.AddTypeAlias("float8", typeof(Double));
208 NativeTypeMapping.AddType("numeric", NpgsqlDbType.Numeric, DbType.Decimal, false,
211 NativeTypeMapping.AddTypeAlias("numeric", typeof(Decimal));
213 NativeTypeMapping.AddType("currency", NpgsqlDbType.Money, DbType.Currency, true,
214 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToMoney));
216 NativeTypeMapping.AddType("date", NpgsqlDbType.Date, DbType.Date, true,
217 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDate));
219 NativeTypeMapping.AddType("time", NpgsqlDbType.Time, DbType.Time, true,
220 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToTime));
222 NativeTypeMapping.AddType("timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, true,
223 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDateTime));
225 NativeTypeMapping.AddTypeAlias("timestamp", typeof(DateTime));
227 NativeTypeMapping.AddType("point", NpgsqlDbType.Point, DbType.Object, true,
228 new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPoint));
230 NativeTypeMapping.AddTypeAlias("point", typeof(NpgsqlPoint));
232 NativeTypeMapping.AddType("box", NpgsqlDbType.Box, DbType.Object, true,
233 new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToBox));
235 NativeTypeMapping.AddTypeAlias("box", typeof(NpgsqlBox));
237 NativeTypeMapping.AddType("lseg", NpgsqlDbType.LSeg, DbType.Object, true,
238 new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToLSeg));
240 NativeTypeMapping.AddTypeAlias("lseg", typeof(NpgsqlLSeg));
242 NativeTypeMapping.AddType("path", NpgsqlDbType.Path, DbType.Object, true,
243 new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPath));
245 NativeTypeMapping.AddTypeAlias("path", typeof(NpgsqlPath));
247 NativeTypeMapping.AddType("polygon", NpgsqlDbType.Polygon, DbType.Object, true,
248 new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPolygon));
250 NativeTypeMapping.AddTypeAlias("polygon", typeof(NpgsqlPolygon));
252 NativeTypeMapping.AddType("circle", NpgsqlDbType.Circle, DbType.Object, true,
253 new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToCircle));
255 NativeTypeMapping.AddTypeAlias("circle", typeof(NpgsqlCircle));
260 /// This method creates (or retrieves from cache) a mapping between type and OID
261 /// of all natively supported postgresql data types.
262 /// This is needed as from one version to another, this mapping can be changed and
263 /// so we avoid hardcoding them.
265 /// <returns>NpgsqlTypeMapping containing all known data types. The mapping must be
266 /// cloned before it is modified because it is cached; changes made by one connection may
267 /// effect another connection.</returns>
268 public static NpgsqlBackendTypeMapping CreateAndLoadInitialTypesMapping(NpgsqlConnector conn)
270 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "LoadTypesMapping");
272 // [TODO] Verify another way to get higher concurrency.
275 // Check the cache for an initial types map.
276 NpgsqlBackendTypeMapping oidToNameMapping = (NpgsqlBackendTypeMapping) BackendTypeMappingCache[conn.ServerVersion];
278 if (oidToNameMapping != null)
280 return oidToNameMapping;
283 // Not in cache, create a new one.
284 oidToNameMapping = new NpgsqlBackendTypeMapping();
286 // Create a list of all natively supported postgresql data types.
287 NpgsqlBackendTypeInfo[] TypeInfoList = new NpgsqlBackendTypeInfo[]
289 new NpgsqlBackendTypeInfo(0, "unknown", NpgsqlDbType.Text, DbType.String, typeof(String),
292 new NpgsqlBackendTypeInfo(0, "char", NpgsqlDbType.Text, DbType.String, typeof(String),
295 new NpgsqlBackendTypeInfo(0, "bpchar", NpgsqlDbType.Text, DbType.String, typeof(String),
298 new NpgsqlBackendTypeInfo(0, "varchar", NpgsqlDbType.Text, DbType.String, typeof(String),
301 new NpgsqlBackendTypeInfo(0, "text", NpgsqlDbType.Text, DbType.String, typeof(String),
304 new NpgsqlBackendTypeInfo(0, "name", NpgsqlDbType.Text, DbType.String, typeof(String),
307 new NpgsqlBackendTypeInfo(0, "bytea", NpgsqlDbType.Bytea, DbType.Binary, typeof(Byte[]),
308 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBinary)),
311 new NpgsqlBackendTypeInfo(0, "bool", NpgsqlDbType.Boolean, DbType.Boolean, typeof(Boolean),
312 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBoolean)),
315 new NpgsqlBackendTypeInfo(0, "int2", NpgsqlDbType.Smallint, DbType.Int16, typeof(Int16),
318 new NpgsqlBackendTypeInfo(0, "int4", NpgsqlDbType.Integer, DbType.Int32, typeof(Int32),
321 new NpgsqlBackendTypeInfo(0, "int8", NpgsqlDbType.Bigint, DbType.Int64, typeof(Int64),
324 new NpgsqlBackendTypeInfo(0, "oid", NpgsqlDbType.Bigint, DbType.Int64, typeof(Int64),
328 new NpgsqlBackendTypeInfo(0, "float4", NpgsqlDbType.Real, DbType.Single, typeof(Single),
331 new NpgsqlBackendTypeInfo(0, "float8", NpgsqlDbType.Double, DbType.Double, typeof(Double),
334 new NpgsqlBackendTypeInfo(0, "numeric", NpgsqlDbType.Numeric, DbType.Decimal, typeof(Decimal),
337 new NpgsqlBackendTypeInfo(0, "money", NpgsqlDbType.Money, DbType.Decimal, typeof(Decimal),
338 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToMoney)),
341 new NpgsqlBackendTypeInfo(0, "date", NpgsqlDbType.Date, DbType.Date, typeof(DateTime),
342 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDate)),
344 new NpgsqlBackendTypeInfo(0, "time", NpgsqlDbType.Time, DbType.Time, typeof(DateTime),
345 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToTime)),
347 new NpgsqlBackendTypeInfo(0, "timetz", NpgsqlDbType.Time, DbType.Time, typeof(DateTime),
348 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToTime)),
350 new NpgsqlBackendTypeInfo(0, "timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(DateTime),
351 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDateTime)),
353 new NpgsqlBackendTypeInfo(0, "timestamptz", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(DateTime),
354 new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDateTime)),
357 new NpgsqlBackendTypeInfo(0, "point", NpgsqlDbType.Point, DbType.Object, typeof(NpgsqlPoint),
358 new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPoint)),
360 new NpgsqlBackendTypeInfo(0, "lseg", NpgsqlDbType.LSeg, DbType.Object, typeof(NpgsqlLSeg),
361 new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToLSeg)),
363 new NpgsqlBackendTypeInfo(0, "path", NpgsqlDbType.Path, DbType.Object, typeof(NpgsqlPath),
364 new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPath)),
366 new NpgsqlBackendTypeInfo(0, "box", NpgsqlDbType.Box, DbType.Object, typeof(NpgsqlBox),
367 new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToBox)),
369 new NpgsqlBackendTypeInfo(0, "circle", NpgsqlDbType.Circle, DbType.Object, typeof(NpgsqlCircle),
370 new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToCircle)),
372 new NpgsqlBackendTypeInfo(0, "polygon", NpgsqlDbType.Polygon, DbType.Object, typeof(NpgsqlPolygon),
373 new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPolygon)),
376 // Attempt to map each type info in the list to an OID on the backend and
377 // add each mapped type to the new type mapping object.
378 LoadTypesMappings(conn, oidToNameMapping, TypeInfoList);
380 // Add this mapping to the per-server-version cache so we don't have to
381 // do these expensive queries on every connection startup.
382 BackendTypeMappingCache.Add(conn.ServerVersion, oidToNameMapping);
384 return oidToNameMapping;
391 /// Attempt to map types by issuing a query against pg_type.
392 /// This function takes a list of NpgsqlTypeInfo and attempts to resolve the OID field
393 /// of each by querying pg_type. If the mapping is found, the type info object is
394 /// updated (OID) and added to the provided NpgsqlTypeMapping object.
396 /// <param name="conn">NpgsqlConnector to send query through.</param>
397 /// <param name="TypeMappings">Mapping object to add types too.</param>
398 /// <param name="TypeInfoList">List of types that need to have OID's mapped.</param>
399 public static void LoadTypesMappings(NpgsqlConnector conn, NpgsqlBackendTypeMapping TypeMappings, IList TypeInfoList)
401 StringBuilder InList = new StringBuilder();
402 Hashtable NameIndex = new Hashtable();
404 // Build a clause for the SELECT statement.
405 // Build a name->typeinfo mapping so we can match the results of the query
406 /// with the list of type objects efficiently.
407 foreach (NpgsqlBackendTypeInfo TypeInfo in TypeInfoList) {
408 NameIndex.Add(TypeInfo.Name, TypeInfo);
409 InList.AppendFormat("{0}'{1}'", ((InList.Length > 0) ? ", " : ""), TypeInfo.Name);
412 if (InList.Length == 0) {
416 NpgsqlCommand command = new NpgsqlCommand("SELECT oid, typname FROM pg_type WHERE typname IN (" + InList.ToString() + ")", conn);
417 NpgsqlDataReader dr = command.ExecuteReader();
420 NpgsqlBackendTypeInfo TypeInfo = (NpgsqlBackendTypeInfo)NameIndex[dr[1].ToString()];
422 TypeInfo._OID = Convert.ToInt32(dr[0]);
424 TypeMappings.AddType(TypeInfo);
430 /// Delegate called to convert the given backend data to its native representation.
432 internal delegate Object ConvertBackendToNativeHandler(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier);
434 /// Delegate called to convert the given native data to its backand representation.
436 internal delegate String ConvertNativeToBackendHandler(NpgsqlNativeTypeInfo TypeInfo, Object NativeData);
439 /// Represents a backend data type.
440 /// This class can be called upon to convert a backend field representation to a native object.
442 internal class NpgsqlBackendTypeInfo
444 private event ConvertBackendToNativeHandler _ConvertBackendToNative;
447 private String _Name;
448 private NpgsqlDbType _NpgsqlDbType;
449 private DbType _DbType;
453 /// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
455 /// <param name="OID">Type OID provided by the backend server.</param>
456 /// <param name="Name">Type name provided by the backend server.</param>
457 /// <param name="NpgsqlDbType">NpgsqlDbType</param>
458 /// <param name="Type">System type to convert fields of this type to.</param>
459 /// <param name="ConvertBackendToNative">Data conversion handler.</param>
460 public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
461 ConvertBackendToNativeHandler ConvertBackendToNative)
465 _NpgsqlDbType = NpgsqlDbType;
468 _ConvertBackendToNative = ConvertBackendToNative;
472 /// Type OID provided by the backend server.
480 /// Type name provided by the backend server.
483 { get { return _Name; } }
488 public NpgsqlDbType NpgsqlDbType
489 { get { return _NpgsqlDbType; } }
495 { get { return _DbType; } }
498 /// System type to convert fields of this type to.
501 { get { return _Type; } }
504 /// Perform a data conversion from a backend representation to
507 /// <param name="BackendData">Data sent from the backend.</param>
508 /// <param name="TypeModifier">Type modifier field sent from the backend.</param>
509 public Object ConvertToNative(String BackendData, Int16 TypeSize, Int32 TypeModifier)
511 if (_ConvertBackendToNative != null) {
512 return _ConvertBackendToNative(this, BackendData, TypeSize, TypeModifier);
515 return Convert.ChangeType(BackendData, Type, CultureInfo.InvariantCulture);
524 /// Represents a backend data type.
525 /// This class can be called upon to convert a native object to its backend field representation,
527 internal class NpgsqlNativeTypeInfo
529 private event ConvertNativeToBackendHandler _ConvertNativeToBackend;
531 private String _Name;
532 private NpgsqlDbType _NpgsqlDbType;
533 private DbType _DbType;
534 private Boolean _Quote;
537 /// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
539 /// <param name="OID">Type OID provided by the backend server.</param>
540 /// <param name="Name">Type name provided by the backend server.</param>
541 /// <param name="NpgsqlDbType">NpgsqlDbType</param>
542 /// <param name="Type">System type to convert fields of this type to.</param>
543 /// <param name="ConvertBackendToNative">Data conversion handler.</param>
544 /// <param name="ConvertNativeToBackend">Data conversion handler.</param>
545 public NpgsqlNativeTypeInfo(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote,
546 ConvertNativeToBackendHandler ConvertNativeToBackend)
549 _NpgsqlDbType = NpgsqlDbType;
552 _ConvertNativeToBackend = ConvertNativeToBackend;
556 /// Type name provided by the backend server.
559 { get { return _Name; } }
564 public NpgsqlDbType NpgsqlDbType
565 { get { return _NpgsqlDbType; } }
571 { get { return _DbType; } }
578 { get { return _Quote; } }
581 /// Perform a data conversion from a native object to
582 /// a backend representation.
583 /// DBNull and null values are handled differently depending if a plain query is used
586 /// <param name="NativeData">Native .NET object to be converted.</param>
587 /// <param name="ForExtendedQuery">Flag indicating if the conversion has to be done for
588 /// plain queries or extended queries</param>
589 public String ConvertToBackend(Object NativeData, Boolean ForExtendedQuery)
591 if ((NativeData == DBNull.Value) || (NativeData == null))
592 if (ForExtendedQuery)
593 return null; // Extended query expects null values be represented as null.
595 return "NULL"; // Plain queries exptects null values as string NULL.
597 else if (_ConvertNativeToBackend != null) {
598 return QuoteString(! ForExtendedQuery, _ConvertNativeToBackend(this, NativeData));
601 return QuoteString(! ForExtendedQuery, (String)Convert.ChangeType(NativeData, typeof(String), CultureInfo.InvariantCulture));
605 private static String QuoteString(Boolean Quote, String S)
608 return String.Format("'{0}'", S);
616 /// Provide mapping between type OID, type name, and a NpgsqlBackendTypeInfo object that represents it.
618 internal class NpgsqlBackendTypeMapping
620 private Hashtable OIDIndex;
621 private Hashtable NameIndex;
624 /// Construct an empty mapping.
626 public NpgsqlBackendTypeMapping()
628 OIDIndex = new Hashtable();
629 NameIndex = new Hashtable();
635 private NpgsqlBackendTypeMapping(NpgsqlBackendTypeMapping Other)
637 OIDIndex = (Hashtable)Other.OIDIndex.Clone();
638 NameIndex = (Hashtable)Other.NameIndex.Clone();
642 /// Add the given NpgsqlBackendTypeInfo to this mapping.
644 public void AddType(NpgsqlBackendTypeInfo T)
646 if (OIDIndex.Contains(T.OID)) {
647 throw new Exception("Type already mapped");
651 NameIndex[T.Name] = T;
655 /// Add a new NpgsqlBackendTypeInfo with the given attributes and conversion handlers to this mapping.
657 /// <param name="OID">Type OID provided by the backend server.</param>
658 /// <param name="Name">Type name provided by the backend server.</param>
659 /// <param name="NpgsqlDbType">NpgsqlDbType</param>
660 /// <param name="Type">System type to convert fields of this type to.</param>
661 /// <param name="ConvertBackendToNative">Data conversion handler.</param>
662 public void AddType(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
663 ConvertBackendToNativeHandler BackendConvert)
665 AddType(new NpgsqlBackendTypeInfo(OID, Name, NpgsqlDbType, DbType, Type, BackendConvert));
669 /// Get the number of type infos held.
672 { get { return NameIndex.Count; } }
675 /// Retrieve the NpgsqlBackendTypeInfo with the given backend type OID, or null if none found.
677 public NpgsqlBackendTypeInfo this [Int32 OID]
681 return (NpgsqlBackendTypeInfo)OIDIndex[OID];
686 /// Retrieve the NpgsqlBackendTypeInfo with the given backend type name, or null if none found.
688 public NpgsqlBackendTypeInfo this [String Name]
692 return (NpgsqlBackendTypeInfo)NameIndex[Name];
697 /// Make a shallow copy of this type mapping.
699 public NpgsqlBackendTypeMapping Clone()
701 return new NpgsqlBackendTypeMapping(this);
705 /// Determine if a NpgsqlBackendTypeInfo with the given backend type OID exists in this mapping.
707 public Boolean ContainsOID(Int32 OID)
709 return OIDIndex.ContainsKey(OID);
713 /// Determine if a NpgsqlBackendTypeInfo with the given backend type name exists in this mapping.
715 public Boolean ContainsName(String Name)
717 return NameIndex.ContainsKey(Name);
724 /// Provide mapping between type Type, NpgsqlDbType and a NpgsqlNativeTypeInfo object that represents it.
726 internal class NpgsqlNativeTypeMapping
728 private Hashtable NameIndex;
729 private Hashtable NpgsqlDbTypeIndex;
730 private Hashtable DbTypeIndex;
731 private Hashtable TypeIndex;
734 /// Construct an empty mapping.
736 public NpgsqlNativeTypeMapping()
738 NameIndex = new Hashtable();
739 NpgsqlDbTypeIndex = new Hashtable();
740 DbTypeIndex = new Hashtable();
741 TypeIndex = new Hashtable();
745 /// Add the given NpgsqlNativeTypeInfo to this mapping.
747 public void AddType(NpgsqlNativeTypeInfo T)
749 if (NameIndex.Contains(T.Name)) {
750 throw new Exception("Type already mapped");
753 NameIndex[T.Name] = T;
754 NpgsqlDbTypeIndex[T.NpgsqlDbType] = T;
755 DbTypeIndex[T.DbType] = T;
759 /// Add a new NpgsqlNativeTypeInfo with the given attributes and conversion handlers to this mapping.
761 /// <param name="OID">Type OID provided by the backend server.</param>
762 /// <param name="Name">Type name provided by the backend server.</param>
763 /// <param name="NpgsqlDbType">NpgsqlDbType</param>
764 /// <param name="ConvertBackendToNative">Data conversion handler.</param>
765 /// <param name="ConvertNativeToBackend">Data conversion handler.</param>
766 public void AddType(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote,
767 ConvertNativeToBackendHandler NativeConvert)
769 AddType(new NpgsqlNativeTypeInfo(Name, NpgsqlDbType, DbType, Quote, NativeConvert));
772 public void AddNpgsqlDbTypeAlias(String Name, NpgsqlDbType NpgsqlDbType)
774 if (NpgsqlDbTypeIndex.Contains(NpgsqlDbType)) {
775 throw new Exception("NpgsqlDbType already aliased");
778 NpgsqlDbTypeIndex[NpgsqlDbType] = NameIndex[Name];
781 public void AddDbTypeAlias(String Name, DbType DbType)
783 if (DbTypeIndex.Contains(DbType)) {
784 throw new Exception("NpgsqlDbType already aliased");
787 DbTypeIndex[DbType] = NameIndex[Name];
790 public void AddTypeAlias(String Name, Type Type)
792 if (TypeIndex.Contains(Type)) {
793 throw new Exception("Type already aliased");
796 TypeIndex[Type] = NameIndex[Name];
800 /// Get the number of type infos held.
803 { get { return NameIndex.Count; } }
806 /// Retrieve the NpgsqlNativeTypeInfo with the given backend type name, or null if none found.
808 public NpgsqlNativeTypeInfo this [String Name]
812 return (NpgsqlNativeTypeInfo)NameIndex[Name];
817 /// Retrieve the NpgsqlNativeTypeInfo with the given NpgsqlDbType, or null if none found.
819 public NpgsqlNativeTypeInfo this [NpgsqlDbType NpgsqlDbType]
823 return (NpgsqlNativeTypeInfo)NpgsqlDbTypeIndex[NpgsqlDbType];
828 /// Retrieve the NpgsqlNativeTypeInfo with the given DbType, or null if none found.
830 public NpgsqlNativeTypeInfo this [DbType DbType]
834 return (NpgsqlNativeTypeInfo)DbTypeIndex[DbType];
841 /// Retrieve the NpgsqlNativeTypeInfo with the given Type, or null if none found.
843 public NpgsqlNativeTypeInfo this [Type Type]
847 return (NpgsqlNativeTypeInfo)TypeIndex[Type];
852 /// Determine if a NpgsqlNativeTypeInfo with the given backend type name exists in this mapping.
854 public Boolean ContainsName(String Name)
856 return NameIndex.ContainsKey(Name);
860 /// Determine if a NpgsqlNativeTypeInfo with the given NpgsqlDbType exists in this mapping.
862 public Boolean ContainsNpgsqlDbType(NpgsqlDbType NpgsqlDbType)
864 return NpgsqlDbTypeIndex.ContainsKey(NpgsqlDbType);
868 /// Determine if a NpgsqlNativeTypeInfo with the given Type name exists in this mapping.
870 public Boolean ContainsType(Type Type)
872 return TypeIndex.ContainsKey(Type);