/Languages/IronPython/IronPython.SQLite/Exceptions.cs

https://github.com/kumaryu/IronLanguages-main · C# · 162 lines · 123 code · 24 blank · 15 comment · 2 complexity · 97341a7015a00e1b7f06f036b91c89fa MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Jeff Hardy 2010-2012.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using Community.CsharpSqlite;
  17. using IronPython.Runtime;
  18. using IronPython.Runtime.Exceptions;
  19. using IronPython.Runtime.Operations;
  20. using IronPython.Runtime.Types;
  21. namespace IronPython.SQLite
  22. {
  23. public static partial class PythonSQLite
  24. {
  25. private static void InitModuleExceptions(PythonContext context, PythonDictionary dict)
  26. {
  27. Warning = context.EnsureModuleException("sqlite.Warning", PythonExceptions.StandardError, dict, "Warning", "_sqlite3");
  28. Error = context.EnsureModuleException("sqlite.Error", PythonExceptions.StandardError, dict, "Error", "_sqlite3");
  29. InterfaceError = context.EnsureModuleException("sqlite.InterfaceError", Error, dict, "InterfaceError", "_sqlite3");
  30. DatabaseError = context.EnsureModuleException("sqlite.DatabaseError", Error, dict, "DatabaseError", "_sqlite3");
  31. DataError = context.EnsureModuleException("sqlite.DataError", DatabaseError, dict, "DataError", "_sqlite3");
  32. OperationalError = context.EnsureModuleException("sqlite.OperationalError", DatabaseError, dict, "OperationalError", "_sqlite3");
  33. IntegrityError = context.EnsureModuleException("sqlite.IntegrityError", DatabaseError, dict, "IntegrityError", "_sqlite3");
  34. InternalError = context.EnsureModuleException("sqlite.InternalError", DatabaseError, dict, "InternalError", "_sqlite3");
  35. ProgrammingError = context.EnsureModuleException("sqlite.ProgrammingError", DatabaseError, dict, "ProgrammingError", "_sqlite3");
  36. NotSupportedError = context.EnsureModuleException("sqlite.NotSupportedError", DatabaseError, dict, "NotSupportedError", "_sqlite3");
  37. }
  38. public static PythonType Warning;
  39. internal static Exception MakeWarning(params object[] args)
  40. {
  41. return CreateThrowable(Warning, args);
  42. }
  43. public static PythonType Error;
  44. internal static Exception MakeError(params object[] args)
  45. {
  46. return CreateThrowable(Error, args);
  47. }
  48. public static PythonType InterfaceError;
  49. internal static Exception MakeInterfaceError(params object[] args)
  50. {
  51. return CreateThrowable(InterfaceError, args);
  52. }
  53. public static PythonType DatabaseError;
  54. internal static Exception MakeDatabaseError(params object[] args)
  55. {
  56. return CreateThrowable(DatabaseError, args);
  57. }
  58. public static PythonType DataError;
  59. internal static Exception MakeDataError(params object[] args)
  60. {
  61. return CreateThrowable(DataError, args);
  62. }
  63. public static PythonType OperationalError;
  64. internal static Exception MakeOperationalError(params object[] args)
  65. {
  66. return CreateThrowable(OperationalError, args);
  67. }
  68. public static PythonType IntegrityError;
  69. internal static Exception MakeIntegrityError(params object[] args)
  70. {
  71. return CreateThrowable(IntegrityError, args);
  72. }
  73. public static PythonType InternalError;
  74. internal static Exception MakeInternalError(params object[] args)
  75. {
  76. return CreateThrowable(InternalError, args);
  77. }
  78. public static PythonType ProgrammingError;
  79. internal static Exception MakeProgrammingError(params object[] args)
  80. {
  81. return CreateThrowable(ProgrammingError, args);
  82. }
  83. public static PythonType NotSupportedError;
  84. internal static Exception MakeNotSupportedError(params object[] args)
  85. {
  86. return CreateThrowable(NotSupportedError, args);
  87. }
  88. internal static Exception GetSqliteError(Sqlite3.sqlite3 db, Sqlite3.Vdbe st)
  89. {
  90. /* SQLite often doesn't report anything useful, unless you reset the statement first */
  91. if(st != null)
  92. {
  93. Sqlite3.sqlite3_reset(st);
  94. }
  95. int errorcode = Sqlite3.sqlite3_errcode(db);
  96. string errmsg = Sqlite3.sqlite3_errmsg(db);
  97. switch(errorcode)
  98. {
  99. case SQLITE_OK:
  100. return null;
  101. case Sqlite3.SQLITE_INTERNAL:
  102. case Sqlite3.SQLITE_NOTFOUND:
  103. return MakeInternalError(errmsg);
  104. case Sqlite3.SQLITE_NOMEM:
  105. return new OutOfMemoryException();
  106. case Sqlite3.SQLITE_ERROR:
  107. case Sqlite3.SQLITE_PERM:
  108. case Sqlite3.SQLITE_ABORT:
  109. case Sqlite3.SQLITE_BUSY:
  110. case Sqlite3.SQLITE_LOCKED:
  111. case Sqlite3.SQLITE_READONLY:
  112. case Sqlite3.SQLITE_INTERRUPT:
  113. case Sqlite3.SQLITE_IOERR:
  114. case Sqlite3.SQLITE_FULL:
  115. case Sqlite3.SQLITE_CANTOPEN:
  116. case Sqlite3.SQLITE_PROTOCOL:
  117. case Sqlite3.SQLITE_EMPTY:
  118. case Sqlite3.SQLITE_SCHEMA:
  119. return MakeOperationalError(errmsg);
  120. case Sqlite3.SQLITE_CORRUPT:
  121. return MakeDatabaseError(errmsg);
  122. case Sqlite3.SQLITE_TOOBIG:
  123. return MakeDataError(errmsg);
  124. case Sqlite3.SQLITE_CONSTRAINT:
  125. case Sqlite3.SQLITE_MISMATCH:
  126. return MakeIntegrityError(errmsg);
  127. case Sqlite3.SQLITE_MISUSE:
  128. return MakeProgrammingError(errmsg);
  129. default:
  130. return MakeDatabaseError(errmsg);
  131. }
  132. }
  133. private static Exception CreateThrowable(PythonType type, params object[] args)
  134. {
  135. return PythonOps.CreateThrowable(type, args);
  136. }
  137. }
  138. }