PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Npgsql/NpgsqlTypes/NpgsqlTypesHelper.cs

https://bitbucket.org/danipen/mono
C# | 982 lines | 514 code | 198 blank | 270 comment | 44 complexity | ff290eb9349628ee877025525055c75c MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // NpgsqlTypes.NpgsqlTypesHelper.cs
  2. //
  3. // Author:
  4. // Francisco Jr. (fxjrlists@yahoo.com.br)
  5. //
  6. // Copyright (C) 2002 The Npgsql Development Team
  7. // npgsql-general@gborg.postgresql.org
  8. // http://gborg.postgresql.org/project/npgsql/projdisplay.php
  9. //
  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.
  14. //
  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.
  19. //
  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
  23. using System;
  24. using System.Collections;
  25. using System.Globalization;
  26. using System.Data;
  27. using System.Net;
  28. using System.Text;
  29. using System.Resources;
  30. using Npgsql;
  31. namespace NpgsqlTypes
  32. {
  33. /// <summary>
  34. /// This class contains helper methods for type conversion between
  35. /// the .Net type system and postgresql.
  36. /// </summary>
  37. internal abstract class NpgsqlTypesHelper
  38. {
  39. // Logging related values
  40. private static readonly String CLASSNAME = "NpgsqlTypesHelper";
  41. private static ResourceManager resman = new ResourceManager(typeof(NpgsqlTypesHelper));
  42. /// <summary>
  43. /// A cache of basic datatype mappings keyed by server version. This way we don't
  44. /// have to load the basic type mappings for every connection.
  45. /// </summary>
  46. private static Hashtable BackendTypeMappingCache = new Hashtable();
  47. private static NpgsqlNativeTypeMapping NativeTypeMapping = null;
  48. /// <summary>
  49. /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
  50. /// of the given NpgsqlDbType.
  51. /// </summary>
  52. public static NpgsqlNativeTypeInfo GetNativeTypeInfo(NpgsqlDbType NpgsqlDbType)
  53. {
  54. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
  55. VerifyDefaultTypesMap();
  56. return NativeTypeMapping[NpgsqlDbType];
  57. }
  58. /// <summary>
  59. /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
  60. /// of the given DbType.
  61. /// </summary>
  62. public static NpgsqlNativeTypeInfo GetNativeTypeInfo(DbType DbType)
  63. {
  64. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
  65. VerifyDefaultTypesMap();
  66. return NativeTypeMapping[DbType];
  67. }
  68. /// <summary>
  69. /// Find a NpgsqlNativeTypeInfo in the default types map that can handle objects
  70. /// of the given System.Type.
  71. /// </summary>
  72. public static NpgsqlNativeTypeInfo GetNativeTypeInfo(Type Type)
  73. {
  74. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBackendTypeNameFromNpgsqlDbType");
  75. VerifyDefaultTypesMap();
  76. return NativeTypeMapping[Type];
  77. }
  78. // CHECKME
  79. // Not sure what to do with this one. I don't believe we ever ask for a binary
  80. // formatting, so this shouldn't even be used right now.
  81. // At some point this will need to be merged into the type converter system somehow?
  82. public static Object ConvertBackendBytesToSystemType(NpgsqlBackendTypeInfo TypeInfo, Byte[] data, Encoding encoding, Int32 fieldValueSize, Int32 typeModifier)
  83. {
  84. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendBytesToStytemType");
  85. // We are never guaranteed to know about every possible data type the server can send us.
  86. // When we encounter an unknown type, we punt and return the data without modification.
  87. if (TypeInfo == null)
  88. return data;
  89. switch (TypeInfo.NpgsqlDbType)
  90. {
  91. case NpgsqlDbType.Bytea:
  92. return data;
  93. /*case NpgsqlDbType.Boolean:
  94. return BitConverter.ToBoolean(data, 0);
  95. case NpgsqlDbType.DateTime:
  96. return DateTime.MinValue.AddTicks(IPAddress.NetworkToHostOrder(BitConverter.ToInt64(data, 0)));
  97. case NpgsqlDbType.Int16:
  98. return IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, 0));
  99. case NpgsqlDbType.Int32:
  100. return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 0));
  101. case NpgsqlDbType.Int64:
  102. return IPAddress.NetworkToHostOrder(BitConverter.ToInt64(data, 0));
  103. case NpgsqlDbType.String:
  104. case NpgsqlDbType.AnsiString:
  105. case NpgsqlDbType.StringFixedLength:
  106. return encoding.GetString(data, 0, fieldValueSize);*/
  107. default:
  108. throw new InvalidCastException("Type not supported in binary format");
  109. }
  110. return null;
  111. }
  112. ///<summary>
  113. /// This method is responsible to convert the string received from the backend
  114. /// to the corresponding NpgsqlType.
  115. /// The given TypeInfo is called upon to do the conversion.
  116. /// If no TypeInfo object is provided, no conversion is performed.
  117. /// </summary>
  118. public static Object ConvertBackendStringToSystemType(NpgsqlBackendTypeInfo TypeInfo, String data, Int16 typeSize, Int32 typeModifier)
  119. {
  120. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ConvertBackendStringToSystemType");
  121. if (TypeInfo != null) {
  122. return TypeInfo.ConvertToNative(data, typeSize, typeModifier);
  123. } else {
  124. return data;
  125. }
  126. }
  127. /// <summary>
  128. /// Create the one and only native to backend type map.
  129. /// This map is used when formatting native data
  130. /// types to backend representations.
  131. /// </summary>
  132. private static void VerifyDefaultTypesMap()
  133. {
  134. lock(CLASSNAME) {
  135. if (NativeTypeMapping != null) {
  136. return;
  137. }
  138. NativeTypeMapping = new NpgsqlNativeTypeMapping();
  139. // Conflicting types should have mapped first the non default mappings.
  140. // For example, char, varchar and text map to DbType.String. As the most
  141. // common is to use text with string, it has to be the last mapped, in order
  142. // to type mapping has the last entry, in this case, text, as the map value
  143. // for DbType.String.
  144. NativeTypeMapping.AddType("refcursor", NpgsqlDbType.Refcursor, DbType.String, true, null);
  145. NativeTypeMapping.AddType("char", NpgsqlDbType.Char, DbType.String, true, null);
  146. NativeTypeMapping.AddType("varchar", NpgsqlDbType.Varchar, DbType.String, true, null);
  147. NativeTypeMapping.AddType("text", NpgsqlDbType.Text, DbType.String, true, null);
  148. NativeTypeMapping.AddDbTypeAlias("text", DbType.StringFixedLength);
  149. NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiString);
  150. NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiStringFixedLength);
  151. NativeTypeMapping.AddTypeAlias("text", typeof(String));
  152. NativeTypeMapping.AddType("bytea", NpgsqlDbType.Bytea, DbType.Binary, true,
  153. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBinary));
  154. NativeTypeMapping.AddTypeAlias("bytea", typeof(Byte[]));
  155. NativeTypeMapping.AddType("bit", NpgsqlDbType.Bit, DbType.Boolean, true,
  156. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBit));
  157. NativeTypeMapping.AddType("bool", NpgsqlDbType.Boolean, DbType.Boolean, false,
  158. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBoolean));
  159. NativeTypeMapping.AddTypeAlias("bool", typeof(Boolean));
  160. NativeTypeMapping.AddType("int2", NpgsqlDbType.Smallint, DbType.Int16, false,
  161. null);
  162. NativeTypeMapping.AddTypeAlias("int2", typeof(Int16));
  163. NativeTypeMapping.AddDbTypeAlias("int2", DbType.Byte);
  164. NativeTypeMapping.AddTypeAlias("int2", typeof(Byte));
  165. NativeTypeMapping.AddType("int4", NpgsqlDbType.Integer, DbType.Int32, false,
  166. null);
  167. NativeTypeMapping.AddTypeAlias("int4", typeof(Int32));
  168. NativeTypeMapping.AddType("int8", NpgsqlDbType.Bigint, DbType.Int64, false,
  169. null);
  170. NativeTypeMapping.AddTypeAlias("int8", typeof(Int64));
  171. NativeTypeMapping.AddType("float4", NpgsqlDbType.Real, DbType.Single, true,
  172. null);
  173. NativeTypeMapping.AddTypeAlias("float4", typeof(Single));
  174. NativeTypeMapping.AddType("float8", NpgsqlDbType.Double, DbType.Double, true,
  175. null);
  176. NativeTypeMapping.AddTypeAlias("float8", typeof(Double));
  177. NativeTypeMapping.AddType("numeric", NpgsqlDbType.Numeric, DbType.Decimal, false,
  178. null);
  179. NativeTypeMapping.AddTypeAlias("numeric", typeof(Decimal));
  180. NativeTypeMapping.AddType("currency", NpgsqlDbType.Money, DbType.Currency, true,
  181. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToMoney));
  182. NativeTypeMapping.AddType("date", NpgsqlDbType.Date, DbType.Date, true,
  183. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDate));
  184. NativeTypeMapping.AddType("time", NpgsqlDbType.Time, DbType.Time, true,
  185. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToTime));
  186. NativeTypeMapping.AddType("timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, true,
  187. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDateTime));
  188. NativeTypeMapping.AddTypeAlias("timestamp", typeof(DateTime));
  189. NativeTypeMapping.AddType("timestamptz", NpgsqlDbType.TimestampTZ, DbType.DateTime, true,
  190. new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToDateTime));
  191. NativeTypeMapping.AddType("point", NpgsqlDbType.Point, DbType.Object, true,
  192. new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPoint));
  193. NativeTypeMapping.AddTypeAlias("point", typeof(NpgsqlPoint));
  194. NativeTypeMapping.AddType("box", NpgsqlDbType.Box, DbType.Object, true,
  195. new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToBox));
  196. NativeTypeMapping.AddTypeAlias("box", typeof(NpgsqlBox));
  197. NativeTypeMapping.AddType("lseg", NpgsqlDbType.LSeg, DbType.Object, true,
  198. new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToLSeg));
  199. NativeTypeMapping.AddTypeAlias("lseg", typeof(NpgsqlLSeg));
  200. NativeTypeMapping.AddType("path", NpgsqlDbType.Path, DbType.Object, true,
  201. new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPath));
  202. NativeTypeMapping.AddTypeAlias("path", typeof(NpgsqlPath));
  203. NativeTypeMapping.AddType("polygon", NpgsqlDbType.Polygon, DbType.Object, true,
  204. new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToPolygon));
  205. NativeTypeMapping.AddTypeAlias("polygon", typeof(NpgsqlPolygon));
  206. NativeTypeMapping.AddType("circle", NpgsqlDbType.Circle, DbType.Object, true,
  207. new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToCircle));
  208. NativeTypeMapping.AddTypeAlias("circle", typeof(NpgsqlCircle));
  209. NativeTypeMapping.AddType("inet", NpgsqlDbType.Inet, DbType.Object, true,
  210. new ConvertNativeToBackendHandler(ExtendedNativeToBackendTypeConverter.ToIPAddress));
  211. NativeTypeMapping.AddTypeAlias("inet", typeof(IPAddress));
  212. NativeTypeMapping.AddTypeAlias("inet", typeof(NpgsqlInet));
  213. }
  214. }
  215. ///<summary>
  216. /// This method creates (or retrieves from cache) a mapping between type and OID
  217. /// of all natively supported postgresql data types.
  218. /// This is needed as from one version to another, this mapping can be changed and
  219. /// so we avoid hardcoding them.
  220. /// </summary>
  221. /// <returns>NpgsqlTypeMapping containing all known data types. The mapping must be
  222. /// cloned before it is modified because it is cached; changes made by one connection may
  223. /// effect another connection.</returns>
  224. public static NpgsqlBackendTypeMapping CreateAndLoadInitialTypesMapping(NpgsqlConnector conn)
  225. {
  226. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "LoadTypesMapping");
  227. // [TODO] Verify another way to get higher concurrency.
  228. lock(CLASSNAME)
  229. {
  230. // Check the cache for an initial types map.
  231. NpgsqlBackendTypeMapping oidToNameMapping = (NpgsqlBackendTypeMapping) BackendTypeMappingCache[conn.ServerVersion];
  232. if (oidToNameMapping != null)
  233. {
  234. return oidToNameMapping;
  235. }
  236. // Not in cache, create a new one.
  237. oidToNameMapping = new NpgsqlBackendTypeMapping();
  238. // Create a list of all natively supported postgresql data types.
  239. NpgsqlBackendTypeInfo[] TypeInfoList = new NpgsqlBackendTypeInfo[]
  240. {
  241. new NpgsqlBackendTypeInfo(0, "unknown", NpgsqlDbType.Text, DbType.String, typeof(String),
  242. null),
  243. new NpgsqlBackendTypeInfo(0, "refcursor", NpgsqlDbType.Refcursor, DbType.String, typeof(String),
  244. null),
  245. new NpgsqlBackendTypeInfo(0, "char", NpgsqlDbType.Char, DbType.String, typeof(String),
  246. null),
  247. new NpgsqlBackendTypeInfo(0, "bpchar", NpgsqlDbType.Text, DbType.String, typeof(String),
  248. null),
  249. new NpgsqlBackendTypeInfo(0, "varchar", NpgsqlDbType.Varchar, DbType.String, typeof(String),
  250. null),
  251. new NpgsqlBackendTypeInfo(0, "text", NpgsqlDbType.Text, DbType.String, typeof(String),
  252. null),
  253. new NpgsqlBackendTypeInfo(0, "name", NpgsqlDbType.Text, DbType.String, typeof(String),
  254. null),
  255. new NpgsqlBackendTypeInfo(0, "bytea", NpgsqlDbType.Bytea, DbType.Binary, typeof(Byte[]),
  256. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBinary)),
  257. new NpgsqlBackendTypeInfo(0, "bit", NpgsqlDbType.Bit, DbType.Boolean, typeof(Boolean),
  258. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBit)),
  259. new NpgsqlBackendTypeInfo(0, "bool", NpgsqlDbType.Boolean, DbType.Boolean, typeof(Boolean),
  260. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToBoolean)),
  261. new NpgsqlBackendTypeInfo(0, "int2", NpgsqlDbType.Smallint, DbType.Int16, typeof(Int16),
  262. null),
  263. new NpgsqlBackendTypeInfo(0, "int4", NpgsqlDbType.Integer, DbType.Int32, typeof(Int32),
  264. null),
  265. new NpgsqlBackendTypeInfo(0, "int8", NpgsqlDbType.Bigint, DbType.Int64, typeof(Int64),
  266. null),
  267. new NpgsqlBackendTypeInfo(0, "oid", NpgsqlDbType.Bigint, DbType.Int64, typeof(Int64),
  268. null),
  269. new NpgsqlBackendTypeInfo(0, "float4", NpgsqlDbType.Real, DbType.Single, typeof(Single),
  270. null),
  271. new NpgsqlBackendTypeInfo(0, "float8", NpgsqlDbType.Double, DbType.Double, typeof(Double),
  272. null),
  273. new NpgsqlBackendTypeInfo(0, "numeric", NpgsqlDbType.Numeric, DbType.Decimal, typeof(Decimal),
  274. null),
  275. new NpgsqlBackendTypeInfo(0, "inet", NpgsqlDbType.Inet, DbType.Object, typeof(NpgsqlInet), new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToInet)),
  276. new NpgsqlBackendTypeInfo(0, "money", NpgsqlDbType.Money, DbType.Decimal, typeof(Decimal),
  277. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToMoney)),
  278. new NpgsqlBackendTypeInfo(0, "date", NpgsqlDbType.Date, DbType.Date, typeof(DateTime),
  279. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDate)),
  280. new NpgsqlBackendTypeInfo(0, "time", NpgsqlDbType.Time, DbType.Time, typeof(DateTime),
  281. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToTime)),
  282. new NpgsqlBackendTypeInfo(0, "timetz", NpgsqlDbType.Time, DbType.Time, typeof(DateTime),
  283. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToTime)),
  284. new NpgsqlBackendTypeInfo(0, "timestamp", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(DateTime),
  285. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDateTime)),
  286. new NpgsqlBackendTypeInfo(0, "timestamptz", NpgsqlDbType.Timestamp, DbType.DateTime, typeof(DateTime),
  287. new ConvertBackendToNativeHandler(BasicBackendToNativeTypeConverter.ToDateTime)),
  288. new NpgsqlBackendTypeInfo(0, "point", NpgsqlDbType.Point, DbType.Object, typeof(NpgsqlPoint),
  289. new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPoint)),
  290. new NpgsqlBackendTypeInfo(0, "lseg", NpgsqlDbType.LSeg, DbType.Object, typeof(NpgsqlLSeg),
  291. new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToLSeg)),
  292. new NpgsqlBackendTypeInfo(0, "path", NpgsqlDbType.Path, DbType.Object, typeof(NpgsqlPath),
  293. new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPath)),
  294. new NpgsqlBackendTypeInfo(0, "box", NpgsqlDbType.Box, DbType.Object, typeof(NpgsqlBox),
  295. new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToBox)),
  296. new NpgsqlBackendTypeInfo(0, "circle", NpgsqlDbType.Circle, DbType.Object, typeof(NpgsqlCircle),
  297. new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToCircle)),
  298. new NpgsqlBackendTypeInfo(0, "polygon", NpgsqlDbType.Polygon, DbType.Object, typeof(NpgsqlPolygon),
  299. new ConvertBackendToNativeHandler(ExtendedBackendToNativeTypeConverter.ToPolygon)),
  300. };
  301. // Attempt to map each type info in the list to an OID on the backend and
  302. // add each mapped type to the new type mapping object.
  303. LoadTypesMappings(conn, oidToNameMapping, TypeInfoList);
  304. // Add this mapping to the per-server-version cache so we don't have to
  305. // do these expensive queries on every connection startup.
  306. BackendTypeMappingCache.Add(conn.ServerVersion, oidToNameMapping);
  307. return oidToNameMapping;
  308. }
  309. }
  310. /// <summary>
  311. /// Attempt to map types by issuing a query against pg_type.
  312. /// This function takes a list of NpgsqlTypeInfo and attempts to resolve the OID field
  313. /// of each by querying pg_type. If the mapping is found, the type info object is
  314. /// updated (OID) and added to the provided NpgsqlTypeMapping object.
  315. /// </summary>
  316. /// <param name="conn">NpgsqlConnector to send query through.</param>
  317. /// <param name="TypeMappings">Mapping object to add types too.</param>
  318. /// <param name="TypeInfoList">List of types that need to have OID's mapped.</param>
  319. public static void LoadTypesMappings(NpgsqlConnector conn, NpgsqlBackendTypeMapping TypeMappings, IList TypeInfoList)
  320. {
  321. StringBuilder InList = new StringBuilder();
  322. Hashtable NameIndex = new Hashtable();
  323. // Build a clause for the SELECT statement.
  324. // Build a name->typeinfo mapping so we can match the results of the query
  325. /// with the list of type objects efficiently.
  326. foreach (NpgsqlBackendTypeInfo TypeInfo in TypeInfoList) {
  327. NameIndex.Add(TypeInfo.Name, TypeInfo);
  328. InList.AppendFormat("{0}'{1}'", ((InList.Length > 0) ? ", " : ""), TypeInfo.Name);
  329. }
  330. if (InList.Length == 0) {
  331. return;
  332. }
  333. NpgsqlCommand command = new NpgsqlCommand("SELECT oid, typname FROM pg_type WHERE typname IN (" + InList.ToString() + ")", conn);
  334. NpgsqlDataReader dr = command.ExecuteReader();
  335. while (dr.Read()) {
  336. NpgsqlBackendTypeInfo TypeInfo = (NpgsqlBackendTypeInfo)NameIndex[dr[1].ToString()];
  337. TypeInfo._OID = Convert.ToInt32(dr[0]);
  338. TypeMappings.AddType(TypeInfo);
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// Delegate called to convert the given backend data to its native representation.
  344. /// </summary>
  345. internal delegate Object ConvertBackendToNativeHandler(NpgsqlBackendTypeInfo TypeInfo, String BackendData, Int16 TypeSize, Int32 TypeModifier);
  346. /// <summary>
  347. /// Delegate called to convert the given native data to its backand representation.
  348. /// </summary>
  349. internal delegate String ConvertNativeToBackendHandler(NpgsqlNativeTypeInfo TypeInfo, Object NativeData);
  350. /// <summary>
  351. /// Represents a backend data type.
  352. /// This class can be called upon to convert a backend field representation to a native object.
  353. /// </summary>
  354. internal class NpgsqlBackendTypeInfo
  355. {
  356. private ConvertBackendToNativeHandler _ConvertBackendToNative;
  357. internal Int32 _OID;
  358. private String _Name;
  359. private NpgsqlDbType _NpgsqlDbType;
  360. private DbType _DbType;
  361. private Type _Type;
  362. /// <summary>
  363. /// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
  364. /// </summary>
  365. /// <param name="OID">Type OID provided by the backend server.</param>
  366. /// <param name="Name">Type name provided by the backend server.</param>
  367. /// <param name="NpgsqlDbType">NpgsqlDbType</param>
  368. /// <param name="Type">System type to convert fields of this type to.</param>
  369. /// <param name="ConvertBackendToNative">Data conversion handler.</param>
  370. public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type, ConvertBackendToNativeHandler ConvertBackendToNative)
  371. {
  372. _OID = OID;
  373. _Name = Name;
  374. _NpgsqlDbType = NpgsqlDbType;
  375. _DbType = DbType;
  376. _Type = Type;
  377. _ConvertBackendToNative = ConvertBackendToNative;
  378. }
  379. /// <summary>
  380. /// Type OID provided by the backend server.
  381. /// </summary>
  382. public Int32 OID
  383. {
  384. get { return _OID; }
  385. }
  386. /// <summary>
  387. /// Type name provided by the backend server.
  388. /// </summary>
  389. public String Name
  390. { get { return _Name; } }
  391. /// <summary>
  392. /// NpgsqlDbType.
  393. /// </summary>
  394. public NpgsqlDbType NpgsqlDbType
  395. { get { return _NpgsqlDbType; } }
  396. /// <summary>
  397. /// NpgsqlDbType.
  398. /// </summary>
  399. public DbType DbType
  400. { get { return _DbType; } }
  401. /// <summary>
  402. /// System type to convert fields of this type to.
  403. /// </summary>
  404. public Type Type
  405. { get { return _Type; } }
  406. /// <summary>
  407. /// Perform a data conversion from a backend representation to
  408. /// a native object.
  409. /// </summary>
  410. /// <param name="BackendData">Data sent from the backend.</param>
  411. /// <param name="TypeModifier">Type modifier field sent from the backend.</param>
  412. public Object ConvertToNative(String BackendData, Int16 TypeSize, Int32 TypeModifier)
  413. {
  414. if (_ConvertBackendToNative != null) {
  415. return _ConvertBackendToNative(this, BackendData, TypeSize, TypeModifier);
  416. } else {
  417. try {
  418. return Convert.ChangeType(BackendData, Type, CultureInfo.InvariantCulture);
  419. } catch {
  420. return BackendData;
  421. }
  422. }
  423. }
  424. }
  425. /// <summary>
  426. /// Represents a backend data type.
  427. /// This class can be called upon to convert a native object to its backend field representation,
  428. /// </summary>
  429. internal class NpgsqlNativeTypeInfo
  430. {
  431. private static NumberFormatInfo ni;
  432. private ConvertNativeToBackendHandler _ConvertNativeToBackend;
  433. private String _Name;
  434. private NpgsqlDbType _NpgsqlDbType;
  435. private DbType _DbType;
  436. private Boolean _Quote;
  437. private Boolean _UseSize;
  438. static NpgsqlNativeTypeInfo()
  439. {
  440. ni = (NumberFormatInfo) CultureInfo.InvariantCulture.NumberFormat.Clone();
  441. ni.NumberDecimalDigits = 15;
  442. }
  443. /// <summary>
  444. /// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
  445. /// </summary>
  446. /// <param name="OID">Type OID provided by the backend server.</param>
  447. /// <param name="Name">Type name provided by the backend server.</param>
  448. /// <param name="NpgsqlDbType">NpgsqlDbType</param>
  449. /// <param name="Type">System type to convert fields of this type to.</param>
  450. /// <param name="ConvertBackendToNative">Data conversion handler.</param>
  451. /// <param name="ConvertNativeToBackend">Data conversion handler.</param>
  452. public NpgsqlNativeTypeInfo(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote, ConvertNativeToBackendHandler ConvertNativeToBackend)
  453. {
  454. _Name = Name;
  455. _NpgsqlDbType = NpgsqlDbType;
  456. _DbType = DbType;
  457. _Quote = Quote;
  458. _ConvertNativeToBackend = ConvertNativeToBackend;
  459. // The only parameters types which use length currently supported are char and varchar. Check for them.
  460. if ( (NpgsqlDbType == NpgsqlDbType.Char)
  461. || (NpgsqlDbType == NpgsqlDbType.Varchar))
  462. _UseSize = true;
  463. else
  464. _UseSize = false;
  465. }
  466. /// <summary>
  467. /// Type name provided by the backend server.
  468. /// </summary>
  469. public String Name
  470. { get { return _Name; } }
  471. /// <summary>
  472. /// NpgsqlDbType.
  473. /// </summary>
  474. public NpgsqlDbType NpgsqlDbType
  475. { get { return _NpgsqlDbType; } }
  476. /// <summary>
  477. /// DbType.
  478. /// </summary>
  479. public DbType DbType
  480. { get { return _DbType; } }
  481. /// <summary>
  482. /// Apply quoting.
  483. /// </summary>
  484. public Boolean Quote
  485. { get { return _Quote; } }
  486. /// <summary>
  487. /// Use parameter size information.
  488. /// </summary>
  489. public Boolean UseSize
  490. { get { return _UseSize; } }
  491. /// <summary>
  492. /// Perform a data conversion from a native object to
  493. /// a backend representation.
  494. /// DBNull and null values are handled differently depending if a plain query is used
  495. /// When
  496. /// </summary>
  497. /// <param name="NativeData">Native .NET object to be converted.</param>
  498. /// <param name="ForExtendedQuery">Flag indicating if the conversion has to be done for
  499. /// plain queries or extended queries</param>
  500. public String ConvertToBackend(Object NativeData, Boolean ForExtendedQuery)
  501. {
  502. if (ForExtendedQuery)
  503. return ConvertToBackendExtendedQuery(NativeData);
  504. else
  505. return ConvertToBackendPlainQuery(NativeData);
  506. }
  507. private String ConvertToBackendPlainQuery(Object NativeData)
  508. {
  509. if ((NativeData == DBNull.Value) || (NativeData == null))
  510. return "NULL"; // Plain queries exptects null values as string NULL.
  511. if (_ConvertNativeToBackend != null)
  512. return (this.Quote ? QuoteString(_ConvertNativeToBackend(this, NativeData)) : _ConvertNativeToBackend(this, NativeData));
  513. else
  514. {
  515. if (NativeData is System.Enum)
  516. {
  517. // Do a special handling of Enum values.
  518. // Translate enum value to its underlying type.
  519. return QuoteString((String)Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof(String), CultureInfo.InvariantCulture));
  520. }
  521. else if (NativeData is IFormattable)
  522. {
  523. return (this.Quote ? QuoteString(((IFormattable) NativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\")) :
  524. ((IFormattable) NativeData).ToString(null, ni).Replace("'", "''").Replace("\\", "\\\\"));
  525. }
  526. // Do special handling of strings when in simple query. Escape quotes and backslashes.
  527. return (this.Quote ? QuoteString(NativeData.ToString().Replace("'", "''").Replace("\\", "\\\\").Replace("\0", "\\0")) :
  528. NativeData.ToString().Replace("'", "''").Replace("\\", "\\\\").Replace("\0", "\\0"));
  529. }
  530. }
  531. private String ConvertToBackendExtendedQuery(Object NativeData)
  532. {
  533. if ((NativeData == DBNull.Value) || (NativeData == null))
  534. return null; // Extended query expects null values be represented as null.
  535. if (_ConvertNativeToBackend != null)
  536. return _ConvertNativeToBackend(this, NativeData);
  537. else
  538. {
  539. if (NativeData is System.Enum)
  540. {
  541. // Do a special handling of Enum values.
  542. // Translate enum value to its underlying type.
  543. return (String)Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof(String), CultureInfo.InvariantCulture);
  544. }
  545. else if (NativeData is IFormattable)
  546. {
  547. return ((IFormattable)NativeData).ToString(null, ni);
  548. }
  549. return NativeData.ToString();
  550. }
  551. }
  552. private static String QuoteString(String S)
  553. {
  554. return String.Format("E'{0}'", S);
  555. }
  556. }
  557. /// <summary>
  558. /// Provide mapping between type OID, type name, and a NpgsqlBackendTypeInfo object that represents it.
  559. /// </summary>
  560. internal class NpgsqlBackendTypeMapping
  561. {
  562. private Hashtable OIDIndex;
  563. private Hashtable NameIndex;
  564. /// <summary>
  565. /// Construct an empty mapping.
  566. /// </summary>
  567. public NpgsqlBackendTypeMapping()
  568. {
  569. OIDIndex = new Hashtable();
  570. NameIndex = new Hashtable();
  571. }
  572. /// <summary>
  573. /// Copy constuctor.
  574. /// </summary>
  575. private NpgsqlBackendTypeMapping(NpgsqlBackendTypeMapping Other)
  576. {
  577. OIDIndex = (Hashtable)Other.OIDIndex.Clone();
  578. NameIndex = (Hashtable)Other.NameIndex.Clone();
  579. }
  580. /// <summary>
  581. /// Add the given NpgsqlBackendTypeInfo to this mapping.
  582. /// </summary>
  583. public void AddType(NpgsqlBackendTypeInfo T)
  584. {
  585. if (OIDIndex.Contains(T.OID)) {
  586. throw new Exception("Type already mapped");
  587. }
  588. OIDIndex[T.OID] = T;
  589. NameIndex[T.Name] = T;
  590. }
  591. /// <summary>
  592. /// Add a new NpgsqlBackendTypeInfo with the given attributes and conversion handlers to this mapping.
  593. /// </summary>
  594. /// <param name="OID">Type OID provided by the backend server.</param>
  595. /// <param name="Name">Type name provided by the backend server.</param>
  596. /// <param name="NpgsqlDbType">NpgsqlDbType</param>
  597. /// <param name="Type">System type to convert fields of this type to.</param>
  598. /// <param name="ConvertBackendToNative">Data conversion handler.</param>
  599. public void AddType(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
  600. ConvertBackendToNativeHandler BackendConvert)
  601. {
  602. AddType(new NpgsqlBackendTypeInfo(OID, Name, NpgsqlDbType, DbType, Type, BackendConvert));
  603. }
  604. /// <summary>
  605. /// Get the number of type infos held.
  606. /// </summary>
  607. public Int32 Count
  608. { get { return NameIndex.Count; } }
  609. /// <summary>
  610. /// Retrieve the NpgsqlBackendTypeInfo with the given backend type OID, or null if none found.
  611. /// </summary>
  612. public NpgsqlBackendTypeInfo this [Int32 OID]
  613. {
  614. get
  615. {
  616. return (NpgsqlBackendTypeInfo)OIDIndex[OID];
  617. }
  618. }
  619. /// <summary>
  620. /// Retrieve the NpgsqlBackendTypeInfo with the given backend type name, or null if none found.
  621. /// </summary>
  622. public NpgsqlBackendTypeInfo this [String Name]
  623. {
  624. get
  625. {
  626. return (NpgsqlBackendTypeInfo)NameIndex[Name];
  627. }
  628. }
  629. /// <summary>
  630. /// Make a shallow copy of this type mapping.
  631. /// </summary>
  632. public NpgsqlBackendTypeMapping Clone()
  633. {
  634. return new NpgsqlBackendTypeMapping(this);
  635. }
  636. /// <summary>
  637. /// Determine if a NpgsqlBackendTypeInfo with the given backend type OID exists in this mapping.
  638. /// </summary>
  639. public Boolean ContainsOID(Int32 OID)
  640. {
  641. return OIDIndex.ContainsKey(OID);
  642. }
  643. /// <summary>
  644. /// Determine if a NpgsqlBackendTypeInfo with the given backend type name exists in this mapping.
  645. /// </summary>
  646. public Boolean ContainsName(String Name)
  647. {
  648. return NameIndex.ContainsKey(Name);
  649. }
  650. }
  651. /// <summary>
  652. /// Provide mapping between type Type, NpgsqlDbType and a NpgsqlNativeTypeInfo object that represents it.
  653. /// </summary>
  654. internal class NpgsqlNativeTypeMapping
  655. {
  656. private Hashtable NameIndex;
  657. private Hashtable NpgsqlDbTypeIndex;
  658. private Hashtable DbTypeIndex;
  659. private Hashtable TypeIndex;
  660. /// <summary>
  661. /// Construct an empty mapping.
  662. /// </summary>
  663. public NpgsqlNativeTypeMapping()
  664. {
  665. NameIndex = new Hashtable();
  666. NpgsqlDbTypeIndex = new Hashtable();
  667. DbTypeIndex = new Hashtable();
  668. TypeIndex = new Hashtable();
  669. }
  670. /// <summary>
  671. /// Add the given NpgsqlNativeTypeInfo to this mapping.
  672. /// </summary>
  673. public void AddType(NpgsqlNativeTypeInfo T)
  674. {
  675. if (NameIndex.Contains(T.Name)) {
  676. throw new Exception("Type already mapped");
  677. }
  678. NameIndex[T.Name] = T;
  679. NpgsqlDbTypeIndex[T.NpgsqlDbType] = T;
  680. DbTypeIndex[T.DbType] = T;
  681. }
  682. /// <summary>
  683. /// Add a new NpgsqlNativeTypeInfo with the given attributes and conversion handlers to this mapping.
  684. /// </summary>
  685. /// <param name="OID">Type OID provided by the backend server.</param>
  686. /// <param name="Name">Type name provided by the backend server.</param>
  687. /// <param name="NpgsqlDbType">NpgsqlDbType</param>
  688. /// <param name="ConvertBackendToNative">Data conversion handler.</param>
  689. /// <param name="ConvertNativeToBackend">Data conversion handler.</param>
  690. public void AddType(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote,
  691. ConvertNativeToBackendHandler NativeConvert)
  692. {
  693. AddType(new NpgsqlNativeTypeInfo(Name, NpgsqlDbType, DbType, Quote, NativeConvert));
  694. }
  695. public void AddNpgsqlDbTypeAlias(String Name, NpgsqlDbType NpgsqlDbType)
  696. {
  697. if (NpgsqlDbTypeIndex.Contains(NpgsqlDbType)) {
  698. throw new Exception("NpgsqlDbType already aliased");
  699. }
  700. NpgsqlDbTypeIndex[NpgsqlDbType] = NameIndex[Name];
  701. }
  702. public void AddDbTypeAlias(String Name, DbType DbType)
  703. {
  704. if (DbTypeIndex.Contains(DbType)) {
  705. throw new Exception("NpgsqlDbType already aliased");
  706. }
  707. DbTypeIndex[DbType] = NameIndex[Name];
  708. }
  709. public void AddTypeAlias(String Name, Type Type)
  710. {
  711. if (TypeIndex.Contains(Type)) {
  712. throw new Exception("Type already aliased");
  713. }
  714. TypeIndex[Type] = NameIndex[Name];
  715. }
  716. /// <summary>
  717. /// Get the number of type infos held.
  718. /// </summary>
  719. public Int32 Count
  720. { get { return NameIndex.Count; } }
  721. /// <summary>
  722. /// Retrieve the NpgsqlNativeTypeInfo with the given backend type name, or null if none found.
  723. /// </summary>
  724. public NpgsqlNativeTypeInfo this [String Name]
  725. {
  726. get
  727. {
  728. return (NpgsqlNativeTypeInfo)NameIndex[Name];
  729. }
  730. }
  731. /// <summary>
  732. /// Retrieve the NpgsqlNativeTypeInfo with the given NpgsqlDbType, or null if none found.
  733. /// </summary>
  734. public NpgsqlNativeTypeInfo this [NpgsqlDbType NpgsqlDbType]
  735. {
  736. get
  737. {
  738. return (NpgsqlNativeTypeInfo)NpgsqlDbTypeIndex[NpgsqlDbType];
  739. }
  740. }
  741. /// <summary>
  742. /// Retrieve the NpgsqlNativeTypeInfo with the given DbType, or null if none found.
  743. /// </summary>
  744. public NpgsqlNativeTypeInfo this [DbType DbType]
  745. {
  746. get
  747. {
  748. return (NpgsqlNativeTypeInfo)DbTypeIndex[DbType];
  749. }
  750. }
  751. /// <summary>
  752. /// Retrieve the NpgsqlNativeTypeInfo with the given Type, or null if none found.
  753. /// </summary>
  754. public NpgsqlNativeTypeInfo this [Type Type]
  755. {
  756. get
  757. {
  758. return (NpgsqlNativeTypeInfo)TypeIndex[Type];
  759. }
  760. }
  761. /// <summary>
  762. /// Determine if a NpgsqlNativeTypeInfo with the given backend type name exists in this mapping.
  763. /// </summary>
  764. public Boolean ContainsName(String Name)
  765. {
  766. return NameIndex.ContainsKey(Name);
  767. }
  768. /// <summary>
  769. /// Determine if a NpgsqlNativeTypeInfo with the given NpgsqlDbType exists in this mapping.
  770. /// </summary>
  771. public Boolean ContainsNpgsqlDbType(NpgsqlDbType NpgsqlDbType)
  772. {
  773. return NpgsqlDbTypeIndex.ContainsKey(NpgsqlDbType);
  774. }
  775. /// <summary>
  776. /// Determine if a NpgsqlNativeTypeInfo with the given Type name exists in this mapping.
  777. /// </summary>
  778. public Boolean ContainsType(Type Type)
  779. {
  780. return TypeIndex.ContainsKey(Type);
  781. }
  782. }
  783. }