// 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 { /// /// This class contains helper methods for type conversion between /// the .Net type system and postgresql. /// internal abstract class NpgsqlTypesHelper { // Logging related values private static readonly String CLASSNAME = "NpgsqlTypesHelper"; private static ResourceManager resman = new ResourceManager(typeof(NpgsqlTypesHelper)); /// /// 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. /// private static Hashtable BackendTypeMappingCache = new Hashtable(); private static NpgsqlNativeTypeMapping NativeTypeMapping = null; /// /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects /// of the given NpgsqlDbType. /// public static NpgsqlNativeTypeInfo GetNativeTypeInfo(NpgsqlDbType NpgsqlDbType) { NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType"); VerifyDefaultTypesMap(); return NativeTypeMapping[NpgsqlDbType]; } /// /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects /// of the given DbType. /// public static NpgsqlNativeTypeInfo GetNativeTypeInfo(DbType DbType) { NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType"); VerifyDefaultTypesMap(); return NativeTypeMapping[DbType]; } /// /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects /// of the given System.Type. /// 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.Binary: 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; } /// /// 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. /// 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; } } /// /// Create the one and only native to backend type map. /// This map is used when formatting native data /// types to backend representations. /// private static void VerifyDefaultTypesMap() { lock(CLASSNAME) { if (NativeTypeMapping != null) { return; } NativeTypeMapping = new NpgsqlNativeTypeMapping(); 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("varchar", NpgsqlDbType.Varchar, DbType.String, true, null); NativeTypeMapping.AddType("bytea", NpgsqlDbType.Bytea, DbType.Binary, true, new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBinary)); NativeTypeMapping.AddTypeAlias("bytea", typeof(Byte[])); 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.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, false, null); NativeTypeMapping.AddTypeAlias("float4", typeof(Single)); NativeTypeMapping.AddType("float8", NpgsqlDbType.Double, DbType.Double, false, 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("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)); } } /// /// 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. /// /// 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. 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, "char", NpgsqlDbType.Text, 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, "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, "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; } } /// /// 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. /// /// NpgsqlConnector to send query through. /// Mapping object to add types too. /// List of types that need to have OID's mapped. 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); } } } /// /// Delegate called to convert the given backend data to its native representation. /// internal delegate Object ConvertBackendToNativeHandler(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier); /// /// Delegate called to convert the given native data to its backand representation. /// internal delegate String ConvertNativeToBackendHandler(NpgsqlNativeTypeInfo TypeInfo, Object NativeData); /// /// Represents a backend data type. /// This class can be called upon to convert a backend field representation to a native object. /// internal class NpgsqlBackendTypeInfo { private ConvertBackendToNativeHandler _ConvertBackendToNative; internal Int32 _OID; private String _Name; private NpgsqlDbType _NpgsqlDbType; private DbType _DbType; private Type _Type; /// /// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers. /// /// Type OID provided by the backend server. /// Type name provided by the backend server. /// NpgsqlDbType /// System type to convert fields of this type to. /// Data conversion handler. 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; } /// /// Type OID provided by the backend server. /// public Int32 OID { get { return _OID; } } /// /// Type name provided by the backend server. /// public String Name { get { return _Name; } } /// /// NpgsqlDbType. /// public NpgsqlDbType NpgsqlDbType { get { return _NpgsqlDbType; } } /// /// NpgsqlDbType. /// public DbType DbType { get { return _DbType; } } /// /// System type to convert fields of this type to. /// public Type Type { get { return _Type; } } /// /// Perform a data conversion from a backend representation to /// a native object. /// /// Data sent from the backend. /// Type modifier field sent from the backend. 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; } } } } /// /// Represents a backend data type. /// This class can be called upon to convert a native object to its backend field representation, /// internal class NpgsqlNativeTypeInfo { private static NumberFormatInfo ni; private ConvertNativeToBackendHandler _ConvertNativeToBackend; private String _Name; private NpgsqlDbType _NpgsqlDbType; private DbType _DbType; private Boolean _Quote; static NpgsqlNativeTypeInfo() { ni = new CultureInfo("en-US").NumberFormat; ni.NumberGroupSeparator = ""; } /// /// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers. /// /// Type OID provided by the backend server. /// Type name provided by the backend server. /// NpgsqlDbType /// System type to convert fields of this type to. /// Data conversion handler. /// Data conversion handler. public NpgsqlNativeTypeInfo(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote, ConvertNativeToBackendHandler ConvertNativeToBackend) { _Name = Name; _NpgsqlDbType = NpgsqlDbType; _DbType = DbType; _Quote = Quote; _ConvertNativeToBackend = ConvertNativeToBackend; } /// /// Type name provided by the backend server. /// public String Name { get { return _Name; } } /// /// NpgsqlDbType. /// public NpgsqlDbType NpgsqlDbType { get { return _NpgsqlDbType; } } /// /// DbType. /// public DbType DbType { get { return _DbType; } } /// /// Apply quoting. /// public Boolean Quote { get { return _Quote; } } /// /// 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 /// /// Native .NET object to be converted. /// Flag indicating if the conversion has to be done for /// plain queries or extended queries 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 QuoteString(_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 Double) { return QuoteString(((Double)NativeData).ToString("N", ni).Replace("'", "''").Replace("\\", "\\\\")); } else if (NativeData is Decimal) { return QuoteString(((Decimal)NativeData).ToString("N", ni).Replace("'", "''").Replace("\\", "\\\\")); } // Do special handling of strings when in simple query. Escape quotes and backslashes. return QuoteString(NativeData.ToString().Replace("'", "''").Replace("\\", "\\\\")); } } 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 Double) { return ((Double)NativeData).ToString("N", ni); } else if (NativeData is Decimal) { return ((Decimal)NativeData).ToString("N", ni); } return NativeData.ToString(); } } private static String QuoteString(String S) { return String.Format("'{0}'", S); } } /// /// Provide mapping between type OID, type name, and a NpgsqlBackendTypeInfo object that represents it. /// internal class NpgsqlBackendTypeMapping { private Hashtable OIDIndex; private Hashtable NameIndex; /// /// Construct an empty mapping. /// public NpgsqlBackendTypeMapping() { OIDIndex = new Hashtable(); NameIndex = new Hashtable(); } /// /// Copy constuctor. /// private NpgsqlBackendTypeMapping(NpgsqlBackendTypeMapping Other) { OIDIndex = (Hashtable)Other.OIDIndex.Clone(); NameIndex = (Hashtable)Other.NameIndex.Clone(); } /// /// Add the given NpgsqlBackendTypeInfo to this mapping. /// public void AddType(NpgsqlBackendTypeInfo T) { if (OIDIndex.Contains(T.OID)) { throw new Exception("Type already mapped"); } OIDIndex[T.OID] = T; NameIndex[T.Name] = T; } /// /// Add a new NpgsqlBackendTypeInfo with the given attributes and conversion handlers to this mapping. /// /// Type OID provided by the backend server. /// Type name provided by the backend server. /// NpgsqlDbType /// System type to convert fields of this type to. /// Data conversion handler. public void AddType(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type, ConvertBackendToNativeHandler BackendConvert) { AddType(new NpgsqlBackendTypeInfo(OID, Name, NpgsqlDbType, DbType, Type, BackendConvert)); } /// /// Get the number of type infos held. /// public Int32 Count { get { return NameIndex.Count; } } /// /// Retrieve the NpgsqlBackendTypeInfo with the given backend type OID, or null if none found. /// public NpgsqlBackendTypeInfo this [Int32 OID] { get { return (NpgsqlBackendTypeInfo)OIDIndex[OID]; } } /// /// Retrieve the NpgsqlBackendTypeInfo with the given backend type name, or null if none found. /// public NpgsqlBackendTypeInfo this [String Name] { get { return (NpgsqlBackendTypeInfo)NameIndex[Name]; } } /// /// Make a shallow copy of this type mapping. /// public NpgsqlBackendTypeMapping Clone() { return new NpgsqlBackendTypeMapping(this); } /// /// Determine if a NpgsqlBackendTypeInfo with the given backend type OID exists in this mapping. /// public Boolean ContainsOID(Int32 OID) { return OIDIndex.ContainsKey(OID); } /// /// Determine if a NpgsqlBackendTypeInfo with the given backend type name exists in this mapping. /// public Boolean ContainsName(String Name) { return NameIndex.ContainsKey(Name); } } /// /// Provide mapping between type Type, NpgsqlDbType and a NpgsqlNativeTypeInfo object that represents it. /// internal class NpgsqlNativeTypeMapping { private Hashtable NameIndex; private Hashtable NpgsqlDbTypeIndex; private Hashtable DbTypeIndex; private Hashtable TypeIndex; /// /// Construct an empty mapping. /// public NpgsqlNativeTypeMapping() { NameIndex = new Hashtable(); NpgsqlDbTypeIndex = new Hashtable(); DbTypeIndex = new Hashtable(); TypeIndex = new Hashtable(); } /// /// Add the given NpgsqlNativeTypeInfo to this mapping. /// 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; } /// /// Add a new NpgsqlNativeTypeInfo with the given attributes and conversion handlers to this mapping. /// /// Type OID provided by the backend server. /// Type name provided by the backend server. /// NpgsqlDbType /// Data conversion handler. /// Data conversion handler. 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]; } /// /// Get the number of type infos held. /// public Int32 Count { get { return NameIndex.Count; } } /// /// Retrieve the NpgsqlNativeTypeInfo with the given backend type name, or null if none found. /// public NpgsqlNativeTypeInfo this [String Name] { get { return (NpgsqlNativeTypeInfo)NameIndex[Name]; } } /// /// Retrieve the NpgsqlNativeTypeInfo with the given NpgsqlDbType, or null if none found. /// public NpgsqlNativeTypeInfo this [NpgsqlDbType NpgsqlDbType] { get { return (NpgsqlNativeTypeInfo)NpgsqlDbTypeIndex[NpgsqlDbType]; } } /// /// Retrieve the NpgsqlNativeTypeInfo with the given DbType, or null if none found. /// public NpgsqlNativeTypeInfo this [DbType DbType] { get { return (NpgsqlNativeTypeInfo)DbTypeIndex[DbType]; } } /// /// Retrieve the NpgsqlNativeTypeInfo with the given Type, or null if none found. /// public NpgsqlNativeTypeInfo this [Type Type] { get { return (NpgsqlNativeTypeInfo)TypeIndex[Type]; } } /// /// Determine if a NpgsqlNativeTypeInfo with the given backend type name exists in this mapping. /// public Boolean ContainsName(String Name) { return NameIndex.ContainsKey(Name); } /// /// Determine if a NpgsqlNativeTypeInfo with the given NpgsqlDbType exists in this mapping. /// public Boolean ContainsNpgsqlDbType(NpgsqlDbType NpgsqlDbType) { return NpgsqlDbTypeIndex.ContainsKey(NpgsqlDbType); } /// /// Determine if a NpgsqlNativeTypeInfo with the given Type name exists in this mapping. /// public Boolean ContainsType(Type Type) { return TypeIndex.ContainsKey(Type); } } }