**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / SqliteConnection.cs
blob54358896b9af9bc649ea733f7b934f0ed225a227
1 //
2 // Mono.Data.SqliteClient.SqliteConnection.cs
3 //
4 // Represents an open connection to a Sqlite database file.
5 //
6 // Author(s): Vladimir Vukicevic <vladimir@pobox.com>
7 // Everaldo Canuto <everaldo_canuto@yahoo.com.br>
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 using System;
32 using System.Runtime.InteropServices;
33 using System.Data;
35 namespace Mono.Data.SqliteClient
37 public class SqliteConnection : IDbConnection
40 #region Fields
42 private string conn_str;
43 private string db_file;
44 private int db_mode;
45 private IntPtr sqlite_handle;
46 private ConnectionState state;
48 #endregion
50 #region Constructors and destructors
52 public SqliteConnection ()
54 db_file = null;
55 db_mode = 0644;
56 state = ConnectionState.Closed;
57 sqlite_handle = IntPtr.Zero;
60 public SqliteConnection (string connstring) : this ()
62 ConnectionString = connstring;
65 public void Dispose ()
67 Close ();
70 #endregion
72 #region Properties
74 public string ConnectionString {
75 get { return conn_str; }
76 set { SetConnectionString(value); }
79 public int ConnectionTimeout {
80 get { return 0; }
83 public string Database {
84 get { return db_file; }
87 public ConnectionState State {
88 get { return state; }
91 internal IntPtr Handle {
92 get { return sqlite_handle; }
95 public int LastInsertRowId {
96 get {
97 return Sqlite.sqlite_last_insert_rowid (Handle);
101 #endregion
103 #region Private Methods
105 private void SetConnectionString(string connstring)
107 if (connstring == null) {
108 Close ();
109 conn_str = null;
110 return;
113 if (connstring != conn_str) {
114 Close ();
115 conn_str = connstring;
117 db_file = null;
118 db_mode = 0644;
120 string[] conn_pieces = connstring.Split (',');
121 foreach (string piece in conn_pieces) {
122 piece.Trim ();
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 ("/")) {
136 db_file = tvalue;
137 } else {
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");
151 #endregion
153 #region Internal Methods
155 internal void StartExec ()
157 // use a mutex here
158 state = ConnectionState.Executing;
161 internal void EndExec ()
163 state = ConnectionState.Open;
166 #endregion
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();
176 t.Connection = this;
177 SqliteCommand cmd = this.CreateCommand();
178 cmd.CommandText = "BEGIN";
179 cmd.ExecuteNonQuery();
180 return t;
183 public IDbTransaction BeginTransaction (IsolationLevel il)
185 return null;
188 public void Close ()
190 if (state != ConnectionState.Open) {
191 return;
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);
215 public void Open ()
217 if (conn_str == null) {
218 throw new InvalidOperationException ("No database specified");
221 if (state != ConnectionState.Closed) {
222 return;
225 string errmsg;
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;
234 #endregion