/******************************************************** * ADO.NET 2.0 Data Provider for SQLite Version 3.X * Written by Robert Simpson (robert@blackcastlesoft.com) * * Released to the public domain, use at your own risk! ********************************************************/ namespace System.Data.SQLite { using System; using System.Data.Common; #if !PLATFORM_COMPACTFRAMEWORK using System.Reflection; using System.Runtime.Serialization; using System.Security.Permissions; #endif /// /// SQLite exception class. /// #if !PLATFORM_COMPACTFRAMEWORK [Serializable()] public sealed class SQLiteException : DbException, ISerializable #else public sealed class SQLiteException : Exception #endif { private SQLiteErrorCode _errorCode; #if !PLATFORM_COMPACTFRAMEWORK /// /// Private constructor for use with serialization. /// /// /// Holds the serialized object data about the exception being thrown. /// /// /// Contains contextual information about the source or destination. /// private SQLiteException(SerializationInfo info, StreamingContext context) : base(info, context) { _errorCode = (SQLiteErrorCode)info.GetInt32("errorCode"); } #endif /// /// Public constructor for generating a SQLite exception given the error /// code and message. /// /// /// The SQLite return code to report. /// /// /// Message text to go along with the return code message text. /// public SQLiteException(SQLiteErrorCode errorCode, string message) : base(GetStockErrorMessage(errorCode, message)) { _errorCode = errorCode; } /// /// Public constructor that uses the base class constructor for the error /// message. /// /// Error message text. public SQLiteException(string message) : this(SQLiteErrorCode.Unknown, message) { } /// /// Public constructor that uses the default base class constructor. /// public SQLiteException() { } /// /// Public constructor that uses the base class constructor for the error /// message and inner exception. /// /// Error message text. /// The original (inner) exception. public SQLiteException(string message, Exception innerException) : base(message, innerException) { } #if !PLATFORM_COMPACTFRAMEWORK /// /// Adds extra information to the serialized object data specific to this /// class type. This is only used for serialization. /// /// /// Holds the serialized object data about the exception being thrown. /// /// /// Contains contextual information about the source or destination. /// [SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] public override void GetObjectData( SerializationInfo info, StreamingContext context) { if (info != null) info.AddValue("errorCode", _errorCode); base.GetObjectData(info, context); } #endif /// /// Gets the associated SQLite result code for this exception as a /// . This property returns the same /// underlying value as the property. /// public SQLiteErrorCode ResultCode { get { return _errorCode; } } /// /// Gets the associated SQLite return code for this exception as an /// . For desktop versions of the .NET Framework, /// this property overrides the property of the same name within the /// /// class. This property returns the same underlying value as the /// property. /// #if !PLATFORM_COMPACTFRAMEWORK public override int ErrorCode #else public int ErrorCode #endif { get { return (int)_errorCode; } } /// /// Returns the error message for the specified SQLite return code. /// /// The SQLite return code. /// The error message or null if it cannot be found. private static string GetErrorString( SQLiteErrorCode errorCode ) { #if !PLATFORM_COMPACTFRAMEWORK // // HACK: This must be done via reflection in order to prevent // the RuntimeHelpers.PrepareDelegate method from over- // eagerly attempting to locate the new (and optional) // sqlite3_errstr() function in the SQLite core library // because it happens to be in the static call graph for // the AppDomain.DomainUnload event handler registered // by the SQLiteLog class. // BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod; return typeof(SQLiteBase).InvokeMember("GetErrorString", flags, null, null, new object[] { errorCode }) as string; #else return SQLiteBase.GetErrorString(errorCode); #endif } /// /// Returns the composite error message based on the SQLite return code /// and the optional detailed error message. /// /// The SQLite return code. /// Optional detailed error message. /// Error message text for the return code. private static string GetStockErrorMessage( SQLiteErrorCode errorCode, string message ) { return String.Format("{0}{1}{2}", GetErrorString(errorCode), #if !NET_COMPACT_20 Environment.NewLine, message).Trim(); #else "\r\n", message).Trim(); #endif } } /// /// SQLite error codes. Actually, this enumeration represents a return code, /// which may also indicate success in one of several ways (e.g. SQLITE_OK, /// SQLITE_ROW, and SQLITE_DONE). Therefore, the name of this enumeration is /// something of a misnomer. /// public enum SQLiteErrorCode { /// /// The error code is unknown. This error code /// is only used by the managed wrapper itself. /// Unknown = -1, /// /// Successful result /// Ok /* 0 */, /// /// SQL error or missing database /// Error /* 1 */, /// /// Internal logic error in SQLite /// Internal /* 2 */, /// /// Access permission denied /// Perm /* 3 */, /// /// Callback routine requested an abort /// Abort /* 4 */, /// /// The database file is locked /// Busy /* 5 */, /// /// A table in the database is locked /// Locked /* 6 */, /// /// A malloc() failed /// NoMem /* 7 */, /// /// Attempt to write a readonly database /// ReadOnly /* 8 */, /// /// Operation terminated by sqlite3_interrupt() /// Interrupt /* 9 */, /// /// Some kind of disk I/O error occurred /// IoErr /* 10 */, /// /// The database disk image is malformed /// Corrupt /* 11 */, /// /// Unknown opcode in sqlite3_file_control() /// NotFound /* 12 */, /// /// Insertion failed because database is full /// Full /* 13 */, /// /// Unable to open the database file /// CantOpen /* 14 */, /// /// Database lock protocol error /// Protocol /* 15 */, /// /// Database is empty /// Empty /* 16 */, /// /// The database schema changed /// Schema /* 17 */, /// /// String or BLOB exceeds size limit /// TooBig /* 18 */, /// /// Abort due to constraint violation /// Constraint /* 19 */, /// /// Data type mismatch /// Mismatch /* 20 */, /// /// Library used incorrectly /// Misuse /* 21 */, /// /// Uses OS features not supported on host /// NoLfs /* 22 */, /// /// Authorization denied /// Auth /* 23 */, /// /// Auxiliary database format error /// Format /* 24 */, /// /// 2nd parameter to sqlite3_bind out of range /// Range /* 25 */, /// /// File opened that is not a database file /// NotADb /* 26 */, /// /// Notifications from sqlite3_log() /// Notice /* 27 */, /// /// Warnings from sqlite3_log() /// Warning /* 28 */, /// /// sqlite3_step() has another row ready /// Row = 100, /// /// sqlite3_step() has finished executing /// Done, /* 101 */ /// /// Used to mask off extended result codes /// NonExtendedMask = 0xFF } }