(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / SqliteCommand.cs
blobe790ebc70a723f8ab89114647c25daf64cef1af8
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 return Sqlite.sqlite_changes(parent_conn.Handle);
149 #endregion
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 ()
173 int rows_affected;
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 ()) {
182 return null;
184 object o = r[0];
185 r.Close ();
186 return o;
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)
206 int r;
207 return ExecuteReader (behavior, true, out r);
210 public SqliteDataReader ExecuteReader (CommandBehavior behavior, bool want_results, out int rows_affected)
212 SqliteDataReader reader = null;
213 SqliteError err;
215 parent_conn.StartExec ();
217 string msg = "";
218 unsafe {
219 byte *msg_result;
221 try {
222 if (want_results) {
223 reader = new SqliteDataReader (this);
225 err = Sqlite.sqlite_exec(parent_conn.Handle,
226 sql,
227 new Sqlite.SqliteCallbackFunction (reader.SqliteCallback),
228 IntPtr.Zero, &msg_result);
230 reader.ReadingDone ();
231 } else {
232 err = Sqlite.sqlite_exec(parent_conn.Handle,
233 sql,
234 null,
235 IntPtr.Zero, &msg_result);
237 } finally {
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 ();
257 return reader;
260 public int LastInsertRowID ()
262 return Sqlite.sqlite_last_insert_rowid(parent_conn.Handle);
265 #endregion