refactoring
[mono.git] / mcs / class / Mono.Data.Sqlite / Mono.Data.Sqlite_2.0 / SQLiteException.cs
1 //
2 // Mono.Data.Sqlite.SQLiteException.cs
3 //
4 // Author(s):
5 //   Robert Simpson (robert@blackcastlesoft.com)
6 //
7 // Adapted and modified for the Mono Project by
8 //   Marek Habersack (grendello@gmail.com)
9 //
10 //
11 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2007 Marek Habersack
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 /********************************************************
35  * ADO.NET 2.0 Data Provider for Sqlite Version 3.X
36  * Written by Robert Simpson (robert@blackcastlesoft.com)
37  * 
38  * Released to the public domain, use at your own risk!
39  ********************************************************/
40 #if NET_2_0
41 namespace Mono.Data.Sqlite
42 {
43   using System;
44   using System.Collections.Generic;
45   using System.Text;
46   using System.Data.Common;
47
48 #if !PLATFORM_COMPACTFRAMEWORK
49   using System.Runtime.Serialization;
50 #endif
51
52   /// <summary>
53   /// Sqlite exception class.
54   /// </summary>
55 #if !PLATFORM_COMPACTFRAMEWORK
56   [Serializable]
57   public class SqliteException : DbException
58 #else
59   public class SqliteException : Exception
60 #endif
61   {
62     private SqliteErrorCode _errorCode;
63
64 #if !PLATFORM_COMPACTFRAMEWORK
65     private SqliteException(SerializationInfo info, StreamingContext context)
66       : base(info, context)
67     {
68     }
69 #endif
70
71     /// <summary>
72     /// Public constructor for generating a Sqlite error given the base error code
73     /// </summary>
74     /// <param name="errorCode">The Sqlite error code to report</param>
75     /// <param name="extendedInformation">Extra text to go along with the error message text</param>
76     public SqliteException(int errorCode, string extendedInformation)
77       : base(GetStockErrorMessage(errorCode, extendedInformation))
78     {
79       _errorCode = (SqliteErrorCode)errorCode;
80     }
81
82     /// <summary>
83     /// Various public constructors that just pass along to the base Exception
84     /// </summary>
85     /// <param name="message">Passed verbatim to Exception</param>
86     public SqliteException(string message)
87       : base(message)
88     {
89     }
90
91     /// <summary>
92     /// Various public constructors that just pass along to the base Exception
93     /// </summary>
94     public SqliteException()
95     {
96     }
97
98     /// <summary>
99     /// Various public constructors that just pass along to the base Exception
100     /// <param name="message">Passed to Exception</param>
101     /// <param name="innerException">Passed to Exception</param>
102     /// </summary>
103     public SqliteException(string message, Exception innerException)
104       : base(message, innerException)
105     {
106     }
107
108     /// <summary>
109     /// Retrieves the underlying Sqlite error code for this exception
110     /// </summary>
111 #if !PLATFORM_COMPACTFRAMEWORK
112     public new SqliteErrorCode ErrorCode
113 #else
114     public SqliteErrorCode ErrorCode
115 #endif
116     {
117       get { return _errorCode; }
118     }
119
120     /// <summary>
121     /// Initializes the exception class with the Sqlite error code.
122     /// </summary>
123     /// <param name="errorCode">The Sqlite error code</param>
124     /// <param name="errorMessage">A detailed error message</param>
125     /// <returns>An error message string</returns>
126     private static string GetStockErrorMessage(int errorCode, string errorMessage)
127     {
128       if (errorMessage == null) errorMessage = "";
129
130       if (errorMessage.Length > 0)
131         errorMessage = "\r\n" + errorMessage;
132
133       if (errorCode < 0 || errorCode >= _errorMessages.Length)
134         errorCode = 1;
135
136       return _errorMessages[errorCode] + errorMessage;
137     }
138
139     private static string[] _errorMessages = {
140       "Sqlite OK",
141       "Sqlite error",
142       "An internal logic error in Sqlite",
143       "Access permission denied",
144       "Callback routine requested an abort",
145       "The database file is locked",
146       "A table in the database is locked",
147       "malloc() failed",
148       "Attempt to write a read-only database",
149       "Operation terminated by sqlite3_interrupt()",
150       "Some kind of disk I/O error occurred",
151       "The database disk image is malformed",
152       "Table or record not found",
153       "Insertion failed because the database is full",
154       "Unable to open the database file",
155       "Database lock protocol error",
156       "Database is empty",
157       "The database schema changed",
158       "Too much data for one row of a table",
159       "Abort due to constraint violation",
160       "Data type mismatch",
161       "Library used incorrectly",
162       "Uses OS features not supported on host",
163       "Authorization denied",
164       "Auxiliary database format error",
165       "2nd parameter to sqlite3_bind() out of range",
166       "File opened that is not a database file",
167     };
168   }
169
170   /// <summary>
171   /// Sqlite error codes
172   /// </summary>
173   public enum SqliteErrorCode
174   {
175     /// <summary>
176     /// Success
177     /// </summary>
178     Ok = 0,
179     /// <summary>
180     /// SQL error or missing database
181     /// </summary>
182     Error,
183     /// <summary>
184     /// Internal logic error in Sqlite
185     /// </summary>
186     Internal,
187     /// <summary>
188     /// Access permission denied
189     /// </summary>
190     Perm,
191     /// <summary>
192     /// Callback routine requested an abort
193     /// </summary>
194     Abort,
195     /// <summary>
196     /// The database file is locked
197     /// </summary>
198     Busy,
199     /// <summary>
200     /// A table in the database is locked
201     /// </summary>
202     Locked,
203     /// <summary>
204     /// malloc() failed
205     /// </summary>
206     NoMem,
207     /// <summary>
208     /// Attempt to write a read-only database
209     /// </summary>
210     ReadOnly,
211     /// <summary>
212     /// Operation terminated by sqlite3_interrupt()
213     /// </summary>
214     Interrupt,
215     /// <summary>
216     /// Some kind of disk I/O error occurred
217     /// </summary>
218     IOErr,
219     /// <summary>
220     /// The database disk image is malformed
221     /// </summary>
222     Corrupt,
223     /// <summary>
224     /// Table or record not found
225     /// </summary>
226     NotFound,
227     /// <summary>
228     /// Insertion failed because database is full
229     /// </summary>
230     Full,
231     /// <summary>
232     /// Unable to open the database file
233     /// </summary>
234     CantOpen,
235     /// <summary>
236     /// Database lock protocol error
237     /// </summary>
238     Protocol,
239     /// <summary>
240     /// Database is empty
241     /// </summary>
242     Empty,
243     /// <summary>
244     /// The database schema changed
245     /// </summary>
246     Schema,
247     /// <summary>
248     /// Too much data for one row of a table
249     /// </summary>
250     TooBig,
251     /// <summary>
252     /// Abort due to constraint violation
253     /// </summary>
254     Constraint,
255     /// <summary>
256     /// Data type mismatch
257     /// </summary>
258     Mismatch,
259     /// <summary>
260     /// Library used incorrectly
261     /// </summary>
262     Misuse,
263     /// <summary>
264     /// Uses OS features not supported on host
265     /// </summary>
266     NOLFS,
267     /// <summary>
268     /// Authorization denied
269     /// </summary>
270     Auth,
271     /// <summary>
272     /// Auxiliary database format error
273     /// </summary>
274     Format,
275     /// <summary>
276     /// 2nd parameter to sqlite3_bind out of range
277     /// </summary>
278     Range,
279     /// <summary>
280     /// File opened that is not a database file
281     /// </summary>
282     NotADatabase,
283     /// <summary>
284     /// sqlite3_step() has another row ready
285     /// </summary>
286     Row = 100,
287     /// <summary>
288     /// sqlite3_step() has finished executing
289     /// </summary>
290     Done = 101,
291   }
292 }
293 #endif