2 // Mono.Data.SqliteClient.SqliteCommand.cs
4 // Represents a Transact-SQL statement or stored procedure to execute against
5 // a Sqlite database file.
7 // Author(s): Vladimir Vukicevic <vladimir@pobox.com>
8 // Everaldo Canuto <everaldo_canuto@yahoo.com.br>
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 using System.Runtime.InteropServices;
37 namespace Mono.Data.SqliteClient
39 public class SqliteCommand : IDbCommand
44 private SqliteConnection parent_conn;
45 //private SqliteTransaction transaction;
46 private IDbTransaction transaction;
49 private CommandType type;
50 private UpdateRowSource upd_row_source;
51 private SqliteParameterCollection sql_params;
55 #region Constructors and destructors
57 public SqliteCommand ()
60 sql_params = new SqliteParameterCollection ();
63 public SqliteCommand (string sqlText)
66 sql_params = new SqliteParameterCollection ();
69 public SqliteCommand (string sqlText, SqliteConnection dbConn)
73 sql_params = new SqliteParameterCollection ();
76 public SqliteCommand (string sqlText, SqliteConnection dbConn, IDbTransaction trans)
81 sql_params = new SqliteParameterCollection ();
84 public void Dispose ()
92 public string CommandText {
97 public int CommandTimeout {
98 get { return timeout; }
99 set { timeout = value; }
102 public CommandType CommandType {
104 set { type = value; }
107 IDbConnection IDbCommand.Connection {
108 get { return parent_conn; }
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; }
142 #region Internal Methods
144 internal int NumChanges ()
146 if (parent_conn.Version == 3)
147 return Sqlite.sqlite3_changes(parent_conn.Handle);
149 return Sqlite.sqlite_changes(parent_conn.Handle);
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 ()
177 SqliteDataReader r = ExecuteReader (CommandBehavior.Default, false, out rows_affected);
178 return rows_affected;
181 public object ExecuteScalar ()
183 SqliteDataReader r = ExecuteReader ();
184 if (r == null || !r.Read ()) {
192 IDataReader IDbCommand.ExecuteReader ()
194 return ExecuteReader ();
197 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
199 return ExecuteReader (behavior);
202 public SqliteDataReader ExecuteReader ()
204 return ExecuteReader (CommandBehavior.Default);
207 public SqliteDataReader ExecuteReader (CommandBehavior behavior)
210 return ExecuteReader (behavior, true, out r);
213 public SqliteDataReader ExecuteReader (CommandBehavior behavior, bool want_results, out int rows_affected)
215 SqliteDataReader reader = null;
216 SqliteError err = SqliteError.OK;
217 IntPtr errMsg = IntPtr.Zero;
219 parent_conn.StartExec ();
225 IntPtr pVm = IntPtr.Zero;
226 IntPtr pzTail = IntPtr.Zero;
227 if (parent_conn.Version == 3)
228 err = Sqlite.sqlite3_prepare (parent_conn.Handle, sql, sql.Length, out pVm, out pVm);
230 err = Sqlite.sqlite_compile (parent_conn.Handle, sql, out pzTail, out pVm, out errMsg);
231 if (err == SqliteError.OK)
232 reader = new SqliteDataReader (this, pVm, parent_conn.Version);
233 if (parent_conn.Version == 3)
234 err = Sqlite.sqlite3_finalize (pVm, out errMsg);
236 err = Sqlite.sqlite_finalize (pVm, out errMsg);
238 if (parent_conn.Version == 3)
239 err = Sqlite.sqlite3_exec (parent_conn.Handle, sql, IntPtr.Zero, IntPtr.Zero, out errMsg);
241 err = Sqlite.sqlite_exec (parent_conn.Handle, sql, IntPtr.Zero, IntPtr.Zero, out errMsg);
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 ApplicationException ("Sqlite error " + msg);
256 rows_affected = NumChanges ();
261 public int LastInsertRowID ()
263 if (parent_conn.Version == 3)
264 return Sqlite.sqlite3_last_insert_rowid(parent_conn.Handle);
266 return Sqlite.sqlite_last_insert_rowid(parent_conn.Handle);