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;
35 namespace Mono.Data.SqliteClient
37 public class SqliteConnection : IDbConnection
42 private string conn_str;
43 private string db_file;
45 private int db_version;
46 private IntPtr sqlite_handle;
47 private ConnectionState state;
51 #region Constructors and destructors
53 public SqliteConnection ()
58 state = ConnectionState.Closed;
59 sqlite_handle = IntPtr.Zero;
62 public SqliteConnection (string connstring) : this ()
64 ConnectionString = connstring;
67 public void Dispose ()
76 public string ConnectionString {
77 get { return conn_str; }
78 set { SetConnectionString(value); }
81 public int ConnectionTimeout {
85 public string Database {
86 get { return db_file; }
89 public ConnectionState State {
93 internal int Version {
94 get { return db_version; }
97 internal IntPtr Handle {
98 get { return sqlite_handle; }
101 public int LastInsertRowId {
104 return Sqlite.sqlite3_last_insert_rowid (Handle);
106 return Sqlite.sqlite_last_insert_rowid (Handle);
112 #region Private Methods
114 private void SetConnectionString(string connstring)
116 if (connstring == null) {
122 if (connstring != conn_str) {
124 conn_str = connstring;
129 string[] conn_pieces = connstring.Split (',');
130 foreach (string piece in conn_pieces) {
132 string[] arg_pieces = piece.Split ('=');
133 if (arg_pieces.Length != 2) {
134 throw new InvalidOperationException ("Invalid connection string");
136 string token = arg_pieces[0].ToLower ();
137 string tvalue = arg_pieces[1];
138 string tvalue_lc = arg_pieces[1].ToLower ();
139 if (token == "uri") {
140 if (tvalue_lc.StartsWith ("file://")) {
141 db_file = tvalue.Substring (6);
142 } else if (tvalue_lc.StartsWith ("file:")) {
143 db_file = tvalue.Substring (5);
144 } else if (tvalue_lc.StartsWith ("/")) {
147 throw new InvalidOperationException ("Invalid connection string: invalid URI");
149 } else if (token == "mode") {
150 db_mode = Convert.ToInt32 (tvalue);
151 } else if (token == "version") {
152 db_version = Convert.ToInt32 (tvalue);
156 if (db_file == null) {
157 throw new InvalidOperationException ("Invalid connection string: no URI");
164 #region Internal Methods
166 internal void StartExec ()
169 state = ConnectionState.Executing;
172 internal void EndExec ()
174 state = ConnectionState.Open;
179 #region Public Methods
181 public IDbTransaction BeginTransaction ()
183 if (state != ConnectionState.Open)
184 throw new InvalidOperationException("Invalid operation: The connection is close");
186 SqliteTransaction t = new SqliteTransaction();
188 SqliteCommand cmd = this.CreateCommand();
189 cmd.CommandText = "BEGIN";
190 cmd.ExecuteNonQuery();
194 public IDbTransaction BeginTransaction (IsolationLevel il)
201 if (state != ConnectionState.Open) {
205 state = ConnectionState.Closed;
208 Sqlite.sqlite3_close (sqlite_handle);
210 Sqlite.sqlite_close(sqlite_handle);
211 sqlite_handle = IntPtr.Zero;
214 public void ChangeDatabase (string databaseName)
216 throw new NotImplementedException ();
219 IDbCommand IDbConnection.CreateCommand ()
221 return CreateCommand ();
224 public SqliteCommand CreateCommand ()
226 return new SqliteCommand (null, this);
231 if (conn_str == null) {
232 throw new InvalidOperationException ("No database specified");
235 if (state != ConnectionState.Closed) {
239 IntPtr errmsg = IntPtr.Zero;
241 int err = Sqlite.sqlite3_open(db_file, out sqlite_handle);
242 if (err == (int)SqliteError.ERROR)
243 throw new ApplicationException (Sqlite.sqlite3_errmsg (sqlite_handle));
245 sqlite_handle = Sqlite.sqlite_open(db_file, db_mode, out errmsg);
247 if (errmsg != IntPtr.Zero) {
248 string msg = Marshal.PtrToStringAnsi (errmsg);
249 Sqlite.sqliteFree (errmsg);
250 throw new ApplicationException (msg);
253 state = ConnectionState.Open;