Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Mono.Data.SqliteClient / SqliteCommand.cs
blob96a46fc2bbb86ff18f48dea1dec1130ad788f38a
1 //
2 // Mono.Data.SqliteClient.SqliteCommand.cs
3 //
4 // Represents a Transact-SQL statement or stored procedure to execute against
5 // a Sqlite database file.
6 //
7 // Author(s): Vladimir Vukicevic <vladimir@pobox.com>
8 // Everaldo Canuto <everaldo_canuto@yahoo.com.br>
9 //
10 // Copyright (C) 2002 Vladimir Vukicevic
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Text;
34 using System.Runtime.InteropServices;
35 using System.Data;
37 namespace Mono.Data.SqliteClient
39 public class SqliteCommand : IDbCommand
42 #region Fields
44 private SqliteConnection parent_conn;
45 //private SqliteTransaction transaction;
46 private IDbTransaction transaction;
47 private string sql;
48 private int timeout;
49 private CommandType type;
50 private UpdateRowSource upd_row_source;
51 private SqliteParameterCollection sql_params;
53 #endregion
55 #region Constructors and destructors
57 public SqliteCommand ()
59 sql = "";
60 sql_params = new SqliteParameterCollection ();
63 public SqliteCommand (string sqlText)
65 sql = sqlText;
66 sql_params = new SqliteParameterCollection ();
69 public SqliteCommand (string sqlText, SqliteConnection dbConn)
71 sql = sqlText;
72 parent_conn = dbConn;
73 sql_params = new SqliteParameterCollection ();
76 public SqliteCommand (string sqlText, SqliteConnection dbConn, IDbTransaction trans)
78 sql = sqlText;
79 parent_conn = dbConn;
80 transaction = trans;
81 sql_params = new SqliteParameterCollection ();
84 public void Dispose ()
88 #endregion
90 #region Properties
92 public string CommandText {
93 get { return sql; }
94 set { sql = value; }
97 public int CommandTimeout {
98 get { return timeout; }
99 set { timeout = value; }
102 public CommandType CommandType {
103 get { return type; }
104 set { type = value; }
107 IDbConnection IDbCommand.Connection {
108 get { return parent_conn; }
109 set {
110 if (!(value is SqliteConnection)) {
111 throw new InvalidOperationException ("Can't set Connection to something other than a SqliteConnection");
113 parent_conn = (SqliteConnection) value;
117 public SqliteConnection Connection {
118 get { return parent_conn; }
119 set { parent_conn = value; }
122 IDataParameterCollection IDbCommand.Parameters {
123 get { return Parameters; }
126 public SqliteParameterCollection Parameters {
127 get { return sql_params; }
130 public IDbTransaction Transaction {
131 get { return transaction; }
132 set { transaction = value; }
135 public UpdateRowSource UpdatedRowSource {
136 get { return upd_row_source; }
137 set { upd_row_source = value; }
140 #endregion
142 #region Internal Methods
144 internal int NumChanges ()
146 if (parent_conn.Version == 3)
147 return Sqlite.sqlite3_changes(parent_conn.Handle);
148 else
149 return Sqlite.sqlite_changes(parent_conn.Handle);
152 #endregion
154 #region Public Methods
156 public void Cancel ()
160 public void Prepare ()
164 IDbDataParameter IDbCommand.CreateParameter ()
166 return CreateParameter ();
169 public SqliteParameter CreateParameter ()
171 return new SqliteParameter ();
174 public int ExecuteNonQuery ()
176 int rows_affected;
177 // Since want_results is false, this always returns null
178 ExecuteReader (CommandBehavior.Default, false, out rows_affected);
179 return rows_affected;
182 public object ExecuteScalar ()
184 SqliteDataReader r = ExecuteReader ();
185 if (r == null || !r.Read ()) {
186 return null;
188 object o = r[0];
189 r.Close ();
190 return o;
193 IDataReader IDbCommand.ExecuteReader ()
195 return ExecuteReader ();
198 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
200 return ExecuteReader (behavior);
203 public SqliteDataReader ExecuteReader ()
205 return ExecuteReader (CommandBehavior.Default);
208 public SqliteDataReader ExecuteReader (CommandBehavior behavior)
210 int r;
211 return ExecuteReader (behavior, true, out r);
214 public SqliteDataReader ExecuteReader (CommandBehavior behavior, bool want_results, out int rows_affected)
216 SqliteDataReader reader = null;
217 SqliteError err = SqliteError.OK;
218 IntPtr errMsg = IntPtr.Zero;
220 parent_conn.StartExec ();
222 string msg = "";
224 try {
225 if (want_results) {
226 IntPtr pVm = IntPtr.Zero;
227 IntPtr pzTail = IntPtr.Zero;
228 if (parent_conn.Version == 3)
229 err = Sqlite.sqlite3_prepare (parent_conn.Handle, sql, sql.Length, out pVm, out pVm);
230 else
231 err = Sqlite.sqlite_compile (parent_conn.Handle, sql, out pzTail, out pVm, out errMsg);
232 if (err == SqliteError.OK)
233 reader = new SqliteDataReader (this, pVm, parent_conn.Version);
234 else
235 throw new SqliteException (err);
237 } else {
238 if (parent_conn.Version == 3)
239 err = Sqlite.sqlite3_exec (parent_conn.Handle, sql, IntPtr.Zero, IntPtr.Zero, out errMsg);
240 else
241 err = Sqlite.sqlite_exec (parent_conn.Handle, sql, IntPtr.Zero, IntPtr.Zero, out errMsg);
243 } finally {
244 parent_conn.EndExec ();
247 if (err != SqliteError.OK) {
248 if (errMsg != IntPtr.Zero) {
249 msg = Marshal.PtrToStringAnsi (errMsg);
250 if (parent_conn.Version != 3)
251 Sqlite.sqliteFree (errMsg);
253 throw new SqliteException (err, msg);
256 rows_affected = NumChanges ();
258 return reader;
261 public int LastInsertRowID ()
263 if (parent_conn.Version == 3)
264 return Sqlite.sqlite3_last_insert_rowid(parent_conn.Handle);
265 else
266 return Sqlite.sqlite_last_insert_rowid(parent_conn.Handle);
269 #endregion