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 return Sqlite
.sqlite_changes(parent_conn
.Handle
);
151 #region Public Methods
153 public void Cancel ()
157 public void Prepare ()
161 IDbDataParameter IDbCommand
.CreateParameter ()
163 return CreateParameter ();
166 public SqliteParameter
CreateParameter ()
168 return new SqliteParameter ();
171 public int ExecuteNonQuery ()
174 SqliteDataReader r
= ExecuteReader (CommandBehavior
.Default
, false, out rows_affected
);
175 return rows_affected
;
178 public object ExecuteScalar ()
180 SqliteDataReader r
= ExecuteReader ();
181 if (r
== null || !r
.Read ()) {
189 IDataReader IDbCommand
.ExecuteReader ()
191 return ExecuteReader ();
194 IDataReader IDbCommand
.ExecuteReader (CommandBehavior behavior
)
196 return ExecuteReader (behavior
);
199 public SqliteDataReader
ExecuteReader ()
201 return ExecuteReader (CommandBehavior
.Default
);
204 public SqliteDataReader
ExecuteReader (CommandBehavior behavior
)
207 return ExecuteReader (behavior
, true, out r
);
210 public SqliteDataReader
ExecuteReader (CommandBehavior behavior
, bool want_results
, out int rows_affected
)
212 SqliteDataReader reader
= null;
215 parent_conn
.StartExec ();
223 reader
= new SqliteDataReader (this);
225 err
= Sqlite
.sqlite_exec(parent_conn
.Handle
,
227 new Sqlite
.SqliteCallbackFunction (reader
.SqliteCallback
),
228 IntPtr
.Zero
, &msg_result
);
230 reader
.ReadingDone ();
232 err
= Sqlite
.sqlite_exec(parent_conn
.Handle
,
235 IntPtr
.Zero
, &msg_result
);
238 parent_conn
.EndExec ();
241 if (msg_result
!= null) {
242 StringBuilder sb
= new StringBuilder ();
244 for (byte *y
= msg_result
; *y
!= 0; y
++)
245 sb
.Append ((char) *y
);
246 msg
= sb
.ToString ();
248 Sqlite
.sqliteFree(msg_result
);
252 if (err
!= SqliteError
.OK
)
253 throw new ApplicationException ("Sqlite error " + msg
);
255 rows_affected
= NumChanges ();
260 public int LastInsertRowID ()
262 return Sqlite
.sqlite_last_insert_rowid(parent_conn
.Handle
);