2 // Mono.Data.SqliteClient.Sqlite.cs
4 // Provides C# bindings to the library sqlite.dll
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>
11 // Copyright (C) 2004 Everaldo Canuto
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Security;
35 using System.Runtime.InteropServices;
37 namespace Mono.Data.SqliteClient
40 /// Represents the return values for sqlite_exec() and sqlite_step()
42 internal enum SqliteError : int {
43 /// <value>Successful result</value>
45 /// <value>SQL error or missing database</value>
47 /// <value>An internal logic error in SQLite</value>
49 /// <value>Access permission denied</value>
51 /// <value>Callback routine requested an abort</value>
53 /// <value>The database file is locked</value>
55 /// <value>A table in the database is locked</value>
57 /// <value>A malloc() failed</value>
59 /// <value>Attempt to write a readonly database</value>
61 /// <value>Operation terminated by public const int interrupt()</value>
63 /// <value>Some kind of disk I/O error occurred</value>
65 /// <value>The database disk image is malformed</value>
67 /// <value>(Internal Only) Table or record not found</value>
69 /// <value>Insertion failed because database is full</value>
71 /// <value>Unable to open the database file</value>
73 /// <value>Database lock protocol error</value>
75 /// <value>(Internal Only) Database table is empty</value>
77 /// <value>The database schema changed</value>
79 /// <value>Too much data for one row of a table</value>
81 /// <value>Abort due to contraint violation</value>
83 /// <value>Data type mismatch</value>
85 /// <value>Library used incorrectly</value>
87 /// <value>Uses OS features not supported on host</value>
89 /// <value>Authorization denied</value>
91 /// <value>Auxiliary database format error</value>
93 /// <value>2nd parameter to sqlite_bind out of range</value>
95 /// <value>File opened that is not a database file</value>
97 /// <value>sqlite_step() has another row ready</value>
99 /// <value>sqlite_step() has finished executing</value>
104 /// Provides the core of C# bindings to the library sqlite.dll
106 internal sealed class Sqlite {
108 #region PInvoke Functions
110 [DllImport("sqlite")]
111 internal static extern IntPtr sqlite_open (string dbname, int db_mode, out IntPtr errstr);
113 [DllImport("sqlite")]
114 internal static extern void sqlite_close (IntPtr sqlite_handle);
116 [DllImport("sqlite")]
117 internal static extern int sqlite_changes (IntPtr handle);
119 [DllImport("sqlite")]
120 internal static extern int sqlite_last_insert_rowid (IntPtr sqlite_handle);
122 [DllImport ("sqlite")]
123 internal static extern void sqliteFree (IntPtr ptr);
125 [DllImport ("sqlite")]
126 internal static extern SqliteError sqlite_compile (IntPtr sqlite_handle, string zSql, out IntPtr pzTail, out IntPtr pVm, out IntPtr errstr);
128 [DllImport ("sqlite")]
129 internal static extern SqliteError sqlite_step (IntPtr pVm, out int pN, out IntPtr pazValue, out IntPtr pazColName);
131 [DllImport ("sqlite")]
132 internal static extern SqliteError sqlite_finalize (IntPtr pVm, out IntPtr pzErrMsg);
134 [DllImport ("sqlite")]
135 internal static extern SqliteError sqlite_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
137 [DllImport("sqlite3")]
138 internal static extern int sqlite3_open (string dbname, out IntPtr handle);
140 [DllImport("sqlite3")]
141 internal static extern void sqlite3_close (IntPtr sqlite_handle);
143 [DllImport("sqlite3")]
144 internal static extern string sqlite3_errmsg (IntPtr sqlite_handle);
146 [DllImport("sqlite3")]
147 internal static extern int sqlite3_changes (IntPtr handle);
149 [DllImport("sqlite3")]
150 internal static extern int sqlite3_last_insert_rowid (IntPtr sqlite_handle);
152 [DllImport ("sqlite3")]
153 internal static extern SqliteError sqlite3_prepare (IntPtr sqlite_handle, string zSql, int zSqllen, out IntPtr pVm, out IntPtr pzTail);
155 [DllImport ("sqlite3")]
156 internal static extern SqliteError sqlite3_step (IntPtr pVm);
158 [DllImport ("sqlite3")]
159 internal static extern SqliteError sqlite3_finalize (IntPtr pVm, out IntPtr pzErrMsg);
161 [DllImport ("sqlite3")]
162 internal static extern SqliteError sqlite3_exec (IntPtr handle, string sql, IntPtr callback, IntPtr user_data, out IntPtr errstr_ptr);
164 [DllImport ("sqlite3")]
165 internal static extern IntPtr sqlite3_column_name (IntPtr pVm, int col);
167 [DllImport ("sqlite3")]
168 internal static extern IntPtr sqlite3_column_text (IntPtr pVm, int col);
170 [DllImport ("sqlite3")]
171 internal static extern IntPtr sqlite3_column_blob (IntPtr pVm, int col);
173 [DllImport ("sqlite3")]
174 internal static extern int sqlite3_column_bytes (IntPtr pVm, int col);
176 [DllImport ("sqlite3")]
177 internal static extern int sqlite3_column_count (IntPtr pVm);
179 [DllImport ("sqlite3")]
180 internal static extern int sqlite3_column_type (IntPtr pVm, int col);
182 [DllImport ("sqlite3")]
183 internal static extern Int64 sqlite3_column_int64 (IntPtr pVm, int col);
185 [DllImport ("sqlite3")]
186 internal static extern double sqlite3_column_double (IntPtr pVm, int col);
188 [DllImport ("sqlite3")]
189 internal static extern int sqlite3_bind_parameter_count (IntPtr pStmt);
191 [DllImport ("sqlite3")]
192 internal static extern String sqlite3_bind_parameter_name (IntPtr pStmt, int n);
194 [DllImport ("sqlite3")]
195 internal static extern SqliteError sqlite3_bind_blob (IntPtr pStmt, int n, byte[] blob, int length, IntPtr freetype);
197 [DllImport ("sqlite3")]
198 internal static extern SqliteError sqlite3_bind_double (IntPtr pStmt, int n, double value);
200 [DllImport ("sqlite3")]
201 internal static extern SqliteError sqlite3_bind_int (IntPtr pStmt, int n, int value);
203 [DllImport ("sqlite3")]
204 internal static extern SqliteError sqlite3_bind_int64 (IntPtr pStmt, Int64 n, long value);
206 [DllImport ("sqlite3")]
207 internal static extern SqliteError sqlite3_bind_null (IntPtr pStmt, int n);
209 [DllImport ("sqlite3")]
210 internal static extern SqliteError sqlite3_bind_text (IntPtr pStmt, int n, string value, int length, IntPtr freetype);
212 [DllImport ("sqlite3")]
213 internal static extern SqliteError sqlite3_bind_text16 (IntPtr pStmt, int n, byte[] value, int length, IntPtr freetype);