2 // Mono.Data.SqliteClient.SqliteConnection.cs
4 // Represents an open connection to a Sqlite database file.
6 // Author(s): Vladimir Vukicevic <vladimir@pobox.com>
7 // Everaldo Canuto <everaldo_canuto@yahoo.com.br>
9 // Copyright (C) 2002 Vladimir Vukicevic
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
36 namespace Mono.Data.SqliteClient
38 public class SqliteConnection : IDbConnection
43 private string conn_str;
44 private string db_file;
46 private int db_version;
47 private IntPtr sqlite_handle;
48 private ConnectionState state;
49 private Encoding encoding;
50 private int busy_timeout;
54 #region Constructors and destructors
56 public SqliteConnection ()
61 state = ConnectionState.Closed;
62 sqlite_handle = IntPtr.Zero;
67 public SqliteConnection (string connstring) : this ()
69 ConnectionString = connstring;
72 public void Dispose ()
81 public string ConnectionString {
82 get { return conn_str; }
83 set { SetConnectionString(value); }
86 public int ConnectionTimeout {
90 public string Database {
91 get { return db_file; }
94 public ConnectionState State {
98 public Encoding Encoding {
99 get { return encoding; }
103 get { return db_version; }
106 internal IntPtr Handle {
107 get { return sqlite_handle; }
110 public int LastInsertRowId {
113 return (int)Sqlite.sqlite3_last_insert_rowid (Handle);
115 return Sqlite.sqlite_last_insert_rowid (Handle);
119 public int BusyTimeout {
124 busy_timeout = value < 0 ? 0 : value;
130 #region Private Methods
132 private void SetConnectionString(string connstring)
134 if (connstring == null) {
140 if (connstring != conn_str) {
142 conn_str = connstring;
147 string[] conn_pieces = connstring.Split (',');
148 for (int i = 0; i < conn_pieces.Length; i++) {
149 string piece = conn_pieces [i].Trim ();
150 if (piece.Length == 0) { // ignore empty elements
153 string[] arg_pieces = piece.Split ('=');
154 if (arg_pieces.Length != 2) {
155 throw new InvalidOperationException ("Invalid connection string");
157 string token = arg_pieces[0].ToLower (System.Globalization.CultureInfo.InvariantCulture).Trim ();
158 string tvalue = arg_pieces[1].Trim ();
159 string tvalue_lc = arg_pieces[1].ToLower (System.Globalization.CultureInfo.InvariantCulture).Trim ();
162 if (tvalue_lc.StartsWith ("file://")) {
163 db_file = tvalue.Substring (7);
164 } else if (tvalue_lc.StartsWith ("file:")) {
165 db_file = tvalue.Substring (5);
166 } else if (tvalue_lc.StartsWith ("/")) {
169 throw new InvalidOperationException ("Invalid connection string: invalid URI");
174 db_mode = Convert.ToInt32 (tvalue);
178 db_version = Convert.ToInt32 (tvalue);
181 case "encoding": // only for sqlite2
182 encoding = Encoding.GetEncoding (tvalue);
186 busy_timeout = Convert.ToInt32 (tvalue);
191 if (db_file == null) {
192 throw new InvalidOperationException ("Invalid connection string: no URI");
199 #region Internal Methods
201 internal void StartExec ()
204 state = ConnectionState.Executing;
207 internal void EndExec ()
209 state = ConnectionState.Open;
214 #region Public Methods
216 public IDbTransaction BeginTransaction ()
218 if (state != ConnectionState.Open)
219 throw new InvalidOperationException("Invalid operation: The connection is closed");
221 SqliteTransaction t = new SqliteTransaction();
223 SqliteCommand cmd = this.CreateCommand();
224 cmd.CommandText = "BEGIN";
225 cmd.ExecuteNonQuery();
229 public IDbTransaction BeginTransaction (IsolationLevel il)
231 throw new InvalidOperationException();
236 if (state != ConnectionState.Open) {
240 state = ConnectionState.Closed;
243 Sqlite.sqlite3_close (sqlite_handle);
245 Sqlite.sqlite_close(sqlite_handle);
246 sqlite_handle = IntPtr.Zero;
249 public void ChangeDatabase (string databaseName)
251 throw new NotImplementedException ();
254 IDbCommand IDbConnection.CreateCommand ()
256 return CreateCommand ();
259 public SqliteCommand CreateCommand ()
261 return new SqliteCommand (null, this);
266 if (conn_str == null) {
267 throw new InvalidOperationException ("No database specified");
270 if (state != ConnectionState.Closed) {
274 IntPtr errmsg = IntPtr.Zero;
278 sqlite_handle = Sqlite.sqlite_open(db_file, db_mode, out errmsg);
279 if (errmsg != IntPtr.Zero) {
280 string msg = Marshal.PtrToStringAnsi (errmsg);
281 Sqlite.sqliteFree (errmsg);
282 throw new ApplicationException (msg);
284 } catch (DllNotFoundException dll) {
287 if (busy_timeout != 0)
288 Sqlite.sqlite_busy_timeout (sqlite_handle, busy_timeout);
291 int err = Sqlite.sqlite3_open16(db_file, out sqlite_handle);
292 if (err == (int)SqliteError.ERROR)
293 throw new ApplicationException (Marshal.PtrToStringUni( Sqlite.sqlite3_errmsg16 (sqlite_handle)));
294 if (busy_timeout != 0)
295 Sqlite.sqlite3_busy_timeout (sqlite_handle, busy_timeout);
298 state = ConnectionState.Open;