SqlCommand.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // System.Data.SqlClient.SqlCommand.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. //
  8. // (C) Ximian, Inc 2002
  9. //
  10. // use #define DEBUG_SqlCommand if you want to spew debug messages
  11. // #define DEBUG_SqlCommand
  12. using System;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Data.Common;
  16. using System.Runtime.InteropServices;
  17. using System.Xml;
  18. namespace System.Data.SqlClient
  19. {
  20. /// <summary>
  21. /// Represents a SQL statement that is executed
  22. /// while connected to a SQL database.
  23. /// </summary>
  24. // public sealed class SqlCommand : Component, IDbCommand, ICloneable
  25. public sealed class SqlCommand : IDbCommand
  26. {
  27. // FIXME: Console.WriteLine() is used for debugging throughout
  28. #region Fields
  29. string sql = "";
  30. int timeout = 30;
  31. // default is 30 seconds
  32. // for command execution
  33. SqlConnection conn = null;
  34. SqlTransaction trans = null;
  35. CommandType cmdType = CommandType.Text;
  36. bool designTime = false;
  37. SqlParameterCollection parmCollection = new
  38. SqlParameterCollection();
  39. #endregion // Fields
  40. #region Constructors
  41. public SqlCommand()
  42. {
  43. sql = "";
  44. }
  45. public SqlCommand (string cmdText)
  46. {
  47. sql = cmdText;
  48. }
  49. public SqlCommand (string cmdText, SqlConnection connection)
  50. {
  51. sql = cmdText;
  52. conn = connection;
  53. }
  54. public SqlCommand (string cmdText, SqlConnection connection,
  55. SqlTransaction transaction)
  56. {
  57. sql = cmdText;
  58. conn = connection;
  59. trans = transaction;
  60. }
  61. #endregion // Constructors
  62. #region Methods
  63. [MonoTODO]
  64. public void Cancel ()
  65. {
  66. // FIXME: use non-blocking Exec for this
  67. throw new NotImplementedException ();
  68. }
  69. // FIXME: is this the correct way to return a stronger type?
  70. [MonoTODO]
  71. IDbDataParameter IDbCommand.CreateParameter ()
  72. {
  73. return CreateParameter ();
  74. }
  75. [MonoTODO]
  76. public SqlParameter CreateParameter ()
  77. {
  78. return new SqlParameter ();
  79. }
  80. [MonoTODO]
  81. public int ExecuteNonQuery ()
  82. {
  83. IntPtr pgResult; // PGresult
  84. int rowsAffected = -1;
  85. ExecStatusType execStatus;
  86. String rowsAffectedString;
  87. if(conn.State != ConnectionState.Open)
  88. throw new InvalidOperationException(
  89. "ConnnectionState is not Open");
  90. // FIXME: PQexec blocks
  91. // while PQsendQuery is non-blocking
  92. // which is better to use?
  93. // int PQsendQuery(PGconn *conn,
  94. // const char *query);
  95. // execute SQL command
  96. // uses internal property to get the PGConn IntPtr
  97. pgResult = PostgresLibrary.
  98. PQexec (conn.PostgresConnection, sql);
  99. execStatus = PostgresLibrary.
  100. PQresultStatus (pgResult);
  101. if(execStatus == ExecStatusType.PGRES_COMMAND_OK)
  102. {
  103. rowsAffectedString = PostgresLibrary.
  104. PQcmdTuples (pgResult);
  105. #if DEBUG_SqlCommand
  106. Console.WriteLine("rowsAffectedString: " +
  107. rowsAffectedString);
  108. #endif // DEBUG_SqlCommand
  109. if(rowsAffectedString != null)
  110. if(rowsAffectedString.Equals("") == false)
  111. rowsAffected = int.Parse(rowsAffectedString);
  112. }
  113. else
  114. {
  115. String errorMessage;
  116. errorMessage = PostgresLibrary.
  117. PQresStatus(execStatus);
  118. errorMessage += " " + PostgresLibrary.
  119. PQresultErrorMessage(pgResult);
  120. throw new SqlException(0, 0,
  121. errorMessage, 0, "",
  122. conn.DataSource, "SqlCommand", 0);
  123. }
  124. #if DEBUG_SqlCommand
  125. String cmdStatus;
  126. cmdStatus = PostgresLibrary.
  127. PQcmdStatus(pgResult);
  128. Console.WriteLine("*** Command Status: " +
  129. cmdStatus);
  130. #endif // DEBUG_SqlCommand
  131. PostgresLibrary.PQclear (pgResult);
  132. // FIXME: get number of rows
  133. // affected for INSERT, UPDATE, or DELETE
  134. // any other, return -1 (such as, CREATE TABLE)
  135. return rowsAffected;
  136. }
  137. [MonoTODO]
  138. IDataReader IDbCommand.ExecuteReader ()
  139. {
  140. return ExecuteReader ();
  141. }
  142. [MonoTODO]
  143. SqlDataReader ExecuteReader ()
  144. {
  145. throw new NotImplementedException ();
  146. }
  147. [MonoTODO]
  148. IDataReader IDbCommand.ExecuteReader (
  149. CommandBehavior behavior)
  150. {
  151. return ExecuteReader (behavior);
  152. }
  153. [MonoTODO]
  154. public SqlDataReader ExecuteReader (CommandBehavior behavior)
  155. {
  156. throw new NotImplementedException ();
  157. }
  158. [MonoTODO]
  159. public object ExecuteScalar ()
  160. {
  161. throw new NotImplementedException ();
  162. }
  163. [MonoTODO]
  164. public XmlReader ExecuteXmlReader ()
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. [MonoTODO]
  169. public void Prepare ()
  170. {
  171. // FIXME: parameters have to be implemented for this
  172. throw new NotImplementedException ();
  173. }
  174. [MonoTODO]
  175. public SqlCommand Clone ()
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. #endregion // Methods
  180. #region Properties
  181. public string CommandText {
  182. get {
  183. return sql;
  184. }
  185. set {
  186. sql = value;
  187. }
  188. }
  189. public int CommandTimeout {
  190. get {
  191. return timeout;
  192. }
  193. set {
  194. // FIXME: if value < 0, throw
  195. // ArgumentException
  196. // if (value < 0)
  197. // throw ArgumentException;
  198. timeout = value;
  199. }
  200. }
  201. public CommandType CommandType {
  202. get {
  203. return cmdType;
  204. }
  205. set {
  206. cmdType = value;
  207. }
  208. }
  209. // FIXME: for property Connection, is this the correct
  210. // way to handle a return of a stronger type?
  211. IDbConnection IDbCommand.Connection {
  212. get {
  213. return Connection;
  214. }
  215. set {
  216. // FIXME: throw an InvalidOperationException
  217. // if the change was during a
  218. // transaction in progress
  219. // csc
  220. Connection = (SqlConnection) value;
  221. // mcs
  222. // Connection = value;
  223. // FIXME: set Transaction property to null
  224. }
  225. }
  226. public SqlConnection Connection {
  227. get {
  228. // conn defaults to null
  229. return conn;
  230. }
  231. set {
  232. // FIXME: throw an InvalidOperationException
  233. // if the change was during
  234. // a transaction in progress
  235. conn = value;
  236. // FIXME: set Transaction property to null
  237. }
  238. }
  239. public bool DesignTimeVisible {
  240. get {
  241. return designTime;
  242. }
  243. set{
  244. designTime = value;
  245. }
  246. }
  247. // FIXME; for property Parameters, is this the correct
  248. // way to handle a stronger return type?
  249. IDataParameterCollection IDbCommand.Parameters {
  250. get {
  251. return Parameters;
  252. }
  253. }
  254. SqlParameterCollection Parameters {
  255. get {
  256. return parmCollection;
  257. }
  258. }
  259. // FIXME: for property Transaction, is this the correct
  260. // way to handle a return of a stronger type?
  261. IDbTransaction IDbCommand.Transaction {
  262. get {
  263. return Transaction;
  264. }
  265. set {
  266. // FIXME: error handling - do not allow
  267. // setting of transaction if transaction
  268. // has already begun
  269. // csc
  270. Transaction = (SqlTransaction) value;
  271. // mcs
  272. // Transaction = value;
  273. }
  274. }
  275. public SqlTransaction Transaction {
  276. get {
  277. return trans;
  278. }
  279. set {
  280. // FIXME: error handling
  281. trans = value;
  282. }
  283. }
  284. [MonoTODO]
  285. public UpdateRowSource UpdatedRowSource {
  286. // FIXME: do this once DbDataAdaptor
  287. // and DataRow are done
  288. get {
  289. throw new NotImplementedException ();
  290. }
  291. set {
  292. throw new NotImplementedException ();
  293. }
  294. }
  295. #endregion // Properties
  296. #region Destructors
  297. [MonoTODO]
  298. public void Dispose() {
  299. // FIXME: need proper way to release resources
  300. // Dispose(true);
  301. }
  302. [MonoTODO]
  303. ~SqlCommand()
  304. {
  305. // FIXME: need proper way to release resources
  306. // Dispose(false);
  307. }
  308. #endregion //Destructors
  309. }
  310. }