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 IntPtr sqlite_handle
;
46 private ConnectionState state
;
50 #region Constructors and destructors
52 public SqliteConnection ()
56 state
= ConnectionState
.Closed
;
57 sqlite_handle
= IntPtr
.Zero
;
60 public SqliteConnection (string connstring
) : this ()
62 ConnectionString
= connstring
;
65 public void Dispose ()
74 public string ConnectionString
{
75 get { return conn_str; }
76 set { SetConnectionString(value); }
79 public int ConnectionTimeout
{
83 public string Database
{
84 get { return db_file; }
87 public ConnectionState State
{
91 internal IntPtr Handle
{
92 get { return sqlite_handle; }
95 public int LastInsertRowId
{
97 return Sqlite
.sqlite_last_insert_rowid (Handle
);
103 #region Private Methods
105 private void SetConnectionString(string connstring
)
107 if (connstring
== null) {
113 if (connstring
!= conn_str
) {
115 conn_str
= connstring
;
120 string[] conn_pieces
= connstring
.Split (',');
121 foreach (string piece
in conn_pieces
) {
123 string[] arg_pieces
= piece
.Split ('=');
124 if (arg_pieces
.Length
!= 2) {
125 throw new InvalidOperationException ("Invalid connection string");
127 string token
= arg_pieces
[0].ToLower ();
128 string tvalue
= arg_pieces
[1];
129 string tvalue_lc
= arg_pieces
[1].ToLower ();
130 if (token
== "uri") {
131 if (tvalue_lc
.StartsWith ("file://")) {
132 db_file
= tvalue
.Substring (6);
133 } else if (tvalue_lc
.StartsWith ("file:")) {
134 db_file
= tvalue
.Substring (5);
135 } else if (tvalue_lc
.StartsWith ("/")) {
138 throw new InvalidOperationException ("Invalid connection string: invalid URI");
140 } else if (token
== "mode") {
141 db_mode
= Convert
.ToInt32 (tvalue
);
145 if (db_file
== null) {
146 throw new InvalidOperationException ("Invalid connection string: no URI");
153 #region Internal Methods
155 internal void StartExec ()
158 state
= ConnectionState
.Executing
;
161 internal void EndExec ()
163 state
= ConnectionState
.Open
;
168 #region Public Methods
170 public IDbTransaction
BeginTransaction ()
172 if (state
!= ConnectionState
.Open
)
173 throw new InvalidOperationException("Invalid operation: The connection is close");
175 SqliteTransaction t
= new SqliteTransaction();
177 SqliteCommand cmd
= this.CreateCommand();
178 cmd
.CommandText
= "BEGIN";
179 cmd
.ExecuteNonQuery();
183 public IDbTransaction
BeginTransaction (IsolationLevel il
)
190 if (state
!= ConnectionState
.Open
) {
194 state
= ConnectionState
.Closed
;
196 Sqlite
.sqlite_close(sqlite_handle
);
197 sqlite_handle
= IntPtr
.Zero
;
200 public void ChangeDatabase (string databaseName
)
202 throw new NotImplementedException ();
205 IDbCommand IDbConnection
.CreateCommand ()
207 return CreateCommand ();
210 public SqliteCommand
CreateCommand ()
212 return new SqliteCommand (null, this);
217 if (conn_str
== null) {
218 throw new InvalidOperationException ("No database specified");
221 if (state
!= ConnectionState
.Closed
) {
226 sqlite_handle
= Sqlite
.sqlite_open(db_file
, db_mode
, out errmsg
);
228 if (errmsg
!= null) {
229 throw new ApplicationException (errmsg
);
231 state
= ConnectionState
.Open
;