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