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

/mcs/class/Npgsql/Npgsql/NpgsqlError.cs

http://github.com/mono/mono
C# | 360 lines | 234 code | 45 blank | 81 comment | 12 complexity | f1a19ea54d72eda055bbba6ed08c8d3f MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Unlicense, Apache-2.0
  1. // created on 12/7/2003 at 18:36
  2. // Npgsql.NpgsqlError.cs
  3. //
  4. // Author:
  5. // Francisco Jr. (fxjrlists@yahoo.com.br)
  6. //
  7. // Copyright (C) 2002 The Npgsql Development Team
  8. // npgsql-general@gborg.postgresql.org
  9. // http://gborg.postgresql.org/project/npgsql/projdisplay.php
  10. //
  11. // This library is free software; you can redistribute it and/or
  12. // modify it under the terms of the GNU Lesser General Public
  13. // License as published by the Free Software Foundation; either
  14. // version 2.1 of the License, or (at your option) any later version.
  15. //
  16. // This library is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. // Lesser General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU Lesser General Public
  22. // License along with this library; if not, write to the Free Software
  23. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. using System;
  25. using System.IO;
  26. using System.Text;
  27. namespace Npgsql
  28. {
  29. /// <summary>
  30. /// EventArgs class to send Notice parameters, which are just NpgsqlError's in a lighter context.
  31. /// </summary>
  32. public class NpgsqlNoticeEventArgs : EventArgs
  33. {
  34. /// <summary>
  35. /// Notice information.
  36. /// </summary>
  37. public NpgsqlError Notice = null;
  38. internal NpgsqlNoticeEventArgs(NpgsqlError eNotice)
  39. {
  40. Notice = eNotice;
  41. }
  42. }
  43. /// <summary>
  44. /// This class represents the ErrorResponse and NoticeResponse
  45. /// message sent from PostgreSQL server.
  46. /// </summary>
  47. [Serializable]
  48. public sealed class NpgsqlError
  49. {
  50. // Logging related values
  51. private static readonly String CLASSNAME = "NpgsqlError";
  52. private ProtocolVersion protocol_version;
  53. private String _severity = String.Empty;
  54. private String _code = String.Empty;
  55. private String _message = String.Empty;
  56. private String _detail = String.Empty;
  57. private String _hint = String.Empty;
  58. private String _position = String.Empty;
  59. private String _where = String.Empty;
  60. private String _file = String.Empty;
  61. private String _line = String.Empty;
  62. private String _routine = String.Empty;
  63. private String _errorSql = String.Empty;
  64. /// <summary>
  65. /// Severity code. All versions.
  66. /// </summary>
  67. public String Severity
  68. {
  69. get
  70. {
  71. return _severity;
  72. }
  73. }
  74. /// <summary>
  75. /// Error code. PostgreSQL 7.4 and up.
  76. /// </summary>
  77. public String Code
  78. {
  79. get
  80. {
  81. return _code;
  82. }
  83. }
  84. /// <summary>
  85. /// Terse error message. All versions.
  86. /// </summary>
  87. public String Message
  88. {
  89. get
  90. {
  91. return _message;
  92. }
  93. }
  94. /// <summary>
  95. /// Detailed error message. PostgreSQL 7.4 and up.
  96. /// </summary>
  97. public String Detail
  98. {
  99. get
  100. {
  101. return _detail;
  102. }
  103. }
  104. /// <summary>
  105. /// Suggestion to help resolve the error. PostgreSQL 7.4 and up.
  106. /// </summary>
  107. public String Hint
  108. {
  109. get
  110. {
  111. return _hint;
  112. }
  113. }
  114. /// <summary>
  115. /// Position (one based) within the query string where the error was encounterd. PostgreSQL 7.4 and up.
  116. /// </summary>
  117. public String Position
  118. {
  119. get
  120. {
  121. return _position;
  122. }
  123. }
  124. /// <summary>
  125. /// Trace back information. PostgreSQL 7.4 and up.
  126. /// </summary>
  127. public String Where
  128. {
  129. get
  130. {
  131. return _where;
  132. }
  133. }
  134. /// <summary>
  135. /// Source file (in backend) reporting the error. PostgreSQL 7.4 and up.
  136. /// </summary>
  137. public String File
  138. {
  139. get
  140. {
  141. return _file;
  142. }
  143. }
  144. /// <summary>
  145. /// Source file line number (in backend) reporting the error. PostgreSQL 7.4 and up.
  146. /// </summary>
  147. public String Line
  148. {
  149. get
  150. {
  151. return _line;
  152. }
  153. }
  154. /// <summary>
  155. /// Source routine (in backend) reporting the error. PostgreSQL 7.4 and up.
  156. /// </summary>
  157. public String Routine
  158. {
  159. get
  160. {
  161. return _routine;
  162. }
  163. }
  164. /// <summary>
  165. /// String containing the sql sent which produced this error.
  166. /// </summary>
  167. public String ErrorSql
  168. {
  169. set
  170. {
  171. _errorSql = value;
  172. }
  173. get
  174. {
  175. return _errorSql;
  176. }
  177. }
  178. /// <summary>
  179. /// Return a string representation of this error object.
  180. /// </summary>
  181. public override String ToString()
  182. {
  183. StringBuilder B = new StringBuilder();
  184. if (Severity.Length > 0)
  185. {
  186. B.AppendFormat("{0}: ", Severity);
  187. }
  188. if (Code.Length > 0)
  189. {
  190. B.AppendFormat("{0}: ", Code);
  191. }
  192. B.AppendFormat("{0}", Message);
  193. // CHECKME - possibly multi-line, that is yucky
  194. // if (Hint.Length > 0) {
  195. // B.AppendFormat(" ({0})", Hint);
  196. // }
  197. return B.ToString();
  198. }
  199. internal NpgsqlError(ProtocolVersion protocolVersion)
  200. {
  201. protocol_version = protocolVersion;
  202. }
  203. internal NpgsqlError(ProtocolVersion protocolVersion, String errorMessage)
  204. {
  205. protocol_version = protocolVersion;
  206. _message = errorMessage;
  207. }
  208. /// <summary>
  209. /// Backend protocol version in use.
  210. /// </summary>
  211. internal ProtocolVersion BackendProtocolVersion
  212. {
  213. get
  214. {
  215. return protocol_version;
  216. }
  217. }
  218. internal void ReadFromStream(Stream inputStream, Encoding encoding)
  219. {
  220. switch (protocol_version) {
  221. case ProtocolVersion.Version2 :
  222. ReadFromStream_Ver_2(inputStream, encoding);
  223. break;
  224. case ProtocolVersion.Version3 :
  225. ReadFromStream_Ver_3(inputStream, encoding);
  226. break;
  227. }
  228. }
  229. private void ReadFromStream_Ver_2(Stream inputStream, Encoding encoding)
  230. {
  231. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_2");
  232. String Raw;
  233. String[] Parts;
  234. Raw = PGUtil.ReadString(inputStream, encoding);
  235. Parts = Raw.Split(new char[] {':'}, 2);
  236. if (Parts.Length == 2)
  237. {
  238. _severity = Parts[0].Trim();
  239. _message = Parts[1].Trim();
  240. }
  241. else
  242. {
  243. _message = Parts[0].Trim();
  244. }
  245. }
  246. private void ReadFromStream_Ver_3(Stream inputStream, Encoding encoding)
  247. {
  248. NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadFromStream_Ver_3");
  249. Int32 messageLength = PGUtil.ReadInt32(inputStream, new Byte[4]);
  250. // [TODO] Would this be the right way to do?
  251. // Check the messageLength value. If it is 1178686529, this would be the
  252. // "FATA" string, which would mean a protocol 2.0 error string.
  253. if (messageLength == 1178686529)
  254. {
  255. String Raw;
  256. String[] Parts;
  257. Raw = "FATA" + PGUtil.ReadString(inputStream, encoding);
  258. Parts = Raw.Split(new char[] {':'}, 2);
  259. if (Parts.Length == 2)
  260. {
  261. _severity = Parts[0].Trim();
  262. _message = Parts[1].Trim();
  263. }
  264. else
  265. {
  266. _message = Parts[0].Trim();
  267. }
  268. protocol_version = ProtocolVersion.Version2;
  269. return;
  270. }
  271. Char field;
  272. String fieldValue;
  273. field = (Char) inputStream.ReadByte();
  274. // Now start to read fields.
  275. while (field != 0)
  276. {
  277. fieldValue = PGUtil.ReadString(inputStream, encoding);
  278. switch (field)
  279. {
  280. case 'S':
  281. _severity = fieldValue;
  282. break;
  283. case 'C':
  284. _code = fieldValue;
  285. break;
  286. case 'M':
  287. _message = fieldValue;
  288. break;
  289. case 'D':
  290. _detail = fieldValue;
  291. break;
  292. case 'H':
  293. _hint = fieldValue;
  294. break;
  295. case 'P':
  296. _position = fieldValue;
  297. break;
  298. case 'W':
  299. _where = fieldValue;
  300. break;
  301. case 'F':
  302. _file = fieldValue;
  303. break;
  304. case 'L':
  305. _line = fieldValue;
  306. break;
  307. case 'R':
  308. _routine = fieldValue;
  309. break;
  310. }
  311. field = (Char) inputStream.ReadByte();
  312. }
  313. }
  314. }
  315. }