2004-11-17 Geoff Norton <gnorton@customerdna.com>
[mono.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / Sqlite.cs
1 //
2 // Mono.Data.SqliteClient.Sqlite.cs
3 //
4 // Provides C# bindings to the library sqlite.dll
5 //
6 // Author(s): Everaldo Canuto  <everaldo_canuto@yahoo.com.br>
7 //
8 // Copyright (C) 2004  Everaldo Canuto
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Security;
32 using System.Runtime.InteropServices;
33
34 namespace Mono.Data.SqliteClient
35 {
36         /// <summary>
37         /// Represents the return values for sqlite_exec() and sqlite_step()
38         /// </summary>
39         internal enum SqliteError : int {
40                 /// <value>Successful result</value>
41                 OK        =  0,
42                 /// <value>SQL error or missing database</value>
43                 ERROR     =  1,
44                 /// <value>An internal logic error in SQLite</value>
45                 INTERNAL  =  2,
46                 /// <value>Access permission denied</value>
47                 PERM      =  3,
48                 /// <value>Callback routine requested an abort</value>
49                 ABORT     =  4,
50                 /// <value>The database file is locked</value>
51                 BUSY      =  5,
52                 /// <value>A table in the database is locked</value>
53                 LOCKED    =  6,
54                 /// <value>A malloc() failed</value>
55                 NOMEM     =  7,
56                 /// <value>Attempt to write a readonly database</value>
57                 READONLY  =  8,
58                 /// <value>Operation terminated by public const int interrupt()</value>
59                 INTERRUPT =  9,
60                 /// <value>Some kind of disk I/O error occurred</value>
61                 IOERR     = 10,
62                 /// <value>The database disk image is malformed</value>
63                 CORRUPT   = 11,
64                 /// <value>(Internal Only) Table or record not found</value>
65                 NOTFOUND  = 12,
66                 /// <value>Insertion failed because database is full</value>
67                 FULL      = 13,
68                 /// <value>Unable to open the database file</value>
69                 CANTOPEN  = 14,
70                 /// <value>Database lock protocol error</value>
71                 PROTOCOL  = 15,
72                 /// <value>(Internal Only) Database table is empty</value>
73                 EMPTY     = 16,
74                 /// <value>The database schema changed</value>
75                 SCHEMA    = 17,
76                 /// <value>Too much data for one row of a table</value>
77                 TOOBIG    = 18,
78                 /// <value>Abort due to contraint violation</value>
79                 CONSTRAINT= 19,
80                 /// <value>Data type mismatch</value>
81                 MISMATCH  = 20,
82                 /// <value>Library used incorrectly</value>
83                 MISUSE    = 21,
84                 /// <value>Uses OS features not supported on host</value>
85                 NOLFS     = 22,
86                 /// <value>Authorization denied</value>
87                 AUTH      = 23,
88                 /// <value>Auxiliary database format error</value>
89                 FORMAT    = 24,
90                 /// <value>2nd parameter to sqlite_bind out of range</value>
91                 RANGE     = 25,
92                 /// <value>File opened that is not a database file</value>
93                 NOTADB    = 26,
94                 /// <value>sqlite_step() has another row ready</value>
95                 ROW       = 100,
96                 /// <value>sqlite_step() has finished executing</value>
97                 DONE      = 101
98         }
99
100         /// <summary>
101         /// Provides the core of C# bindings to the library sqlite.dll
102         /// </summary>
103         internal sealed class Sqlite {
104
105                 #region PInvoke Functions
106                 
107                 [DllImport("sqlite")]
108                 internal static extern IntPtr sqlite_open (string dbname, int db_mode, out IntPtr errstr);
109
110                 [DllImport("sqlite")]
111                 internal static extern void sqlite_close (IntPtr sqlite_handle);
112
113                 [DllImport("sqlite")]
114                 internal static extern int sqlite_changes (IntPtr handle);
115
116                 [DllImport("sqlite")]
117                 internal static extern int sqlite_last_insert_rowid (IntPtr sqlite_handle);
118
119                 [DllImport ("sqlite")]
120                 internal static extern void sqliteFree (IntPtr ptr);
121                 
122                 [DllImport ("sqlite")]
123                 internal static extern SqliteError sqlite_compile (IntPtr sqlite_handle, string zSql, out IntPtr pzTail, out IntPtr pVm, out IntPtr errstr);
124
125                 [DllImport ("sqlite")]
126                 internal static extern SqliteError sqlite_step (IntPtr pVm, out int pN, out IntPtr pazValue, out IntPtr pazColName);
127
128                 [DllImport ("sqlite")]
129                 internal static extern SqliteError sqlite_finalize (IntPtr pVm, out IntPtr pzErrMsg);
130
131                 [DllImport ("sqlite")]
132                 internal static extern SqliteError sqlite_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
133                 
134                 [DllImport("sqlite3")]
135                 internal static extern int sqlite3_open (string dbname, out IntPtr handle);
136
137                 [DllImport("sqlite3")]
138                 internal static extern void sqlite3_close (IntPtr sqlite_handle);
139
140                 [DllImport("sqlite3")]
141                 internal static extern string sqlite3_errmsg (IntPtr sqlite_handle);
142
143                 [DllImport("sqlite3")]
144                 internal static extern int sqlite3_changes (IntPtr handle);
145
146                 [DllImport("sqlite3")]
147                 internal static extern int sqlite3_last_insert_rowid (IntPtr sqlite_handle);
148
149                 [DllImport ("sqlite3")]
150                 internal static extern SqliteError sqlite3_prepare (IntPtr sqlite_handle, string zSql, int zSqllen, out IntPtr pVm, out IntPtr pzTail);
151
152                 [DllImport ("sqlite3")]
153                 internal static extern SqliteError sqlite3_step (IntPtr pVm);
154
155                 [DllImport ("sqlite3")]
156                 internal static extern SqliteError sqlite3_finalize (IntPtr pVm, out IntPtr pzErrMsg);
157
158                 [DllImport ("sqlite3")]
159                 internal static extern SqliteError sqlite3_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
160         
161                 [DllImport ("sqlite3")]
162                 internal static extern IntPtr sqlite3_column_name (IntPtr pVm, int col);
163                 [DllImport ("sqlite3")]
164                 internal static extern IntPtr sqlite3_column_text (IntPtr pVm, int col);
165                 [DllImport ("sqlite3")]
166                 internal static extern IntPtr sqlite3_column_blob (IntPtr pVm, int col);
167                 [DllImport ("sqlite3")]
168                 internal static extern int sqlite3_column_bytes (IntPtr pVm, int col);
169                 [DllImport ("sqlite3")]
170                 internal static extern int sqlite3_column_count (IntPtr pVm);
171                 [DllImport ("sqlite3")]
172                 internal static extern int sqlite3_column_type (IntPtr pVm, int col);
173                 [DllImport ("sqlite3")]
174                 internal static extern Int64 sqlite3_column_int64 (IntPtr pVm, int col);
175                 [DllImport ("sqlite3")]
176                 internal static extern double sqlite3_column_double (IntPtr pVm, int col);
177                 #endregion
178         }
179 }