SqlCommand.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. // FIXME: temporarily commmented out, so I could get a simple working
  138. // SqlConnection and SqlCommand. I had to temporarily
  139. // comment it out the ExecuteReader in IDbCommand as well.
  140. /*
  141. [MonoTODO]
  142. IDataReader IDbCommand.ExecuteReader ()
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. [MonoTODO]
  147. SqlDataReader ExecuteReader ()
  148. {
  149. throw new NotImplementedException ();
  150. }
  151. [MonoTODO]
  152. IDataReader IDbCommand.ExecuteReader (
  153. CommandBehavior behavior)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. [MonoTODO]
  158. public SqlDataReader ExecuteReader (CommandBehavior behavior)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. */
  163. [MonoTODO]
  164. public object ExecuteScalar ()
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. [MonoTODO]
  169. public XmlReader ExecuteXmlReader ()
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. [MonoTODO]
  174. public void Prepare ()
  175. {
  176. // FIXME: parameters have to be implemented for this
  177. throw new NotImplementedException ();
  178. }
  179. [MonoTODO]
  180. public SqlCommand Clone ()
  181. {
  182. throw new NotImplementedException ();
  183. }
  184. #endregion // Methods
  185. #region Properties
  186. public string CommandText {
  187. get {
  188. return sql;
  189. }
  190. set {
  191. sql = value;
  192. }
  193. }
  194. public int CommandTimeout {
  195. get {
  196. return timeout;
  197. }
  198. set {
  199. // FIXME: if value < 0, throw
  200. // ArgumentException
  201. // if (value < 0)
  202. // throw ArgumentException;
  203. timeout = value;
  204. }
  205. }
  206. public CommandType CommandType {
  207. get {
  208. return cmdType;
  209. }
  210. set {
  211. cmdType = value;
  212. }
  213. }
  214. // FIXME: for property Connection, is this the correct
  215. // way to handle a return of a stronger type?
  216. IDbConnection IDbCommand.Connection {
  217. get {
  218. return Connection;
  219. }
  220. set {
  221. // FIXME: throw an InvalidOperationException
  222. // if the change was during a
  223. // transaction in progress
  224. // csc
  225. Connection = (SqlConnection) value;
  226. // mcs
  227. // Connection = value;
  228. // FIXME: set Transaction property to null
  229. }
  230. }
  231. public SqlConnection Connection {
  232. get {
  233. // conn defaults to null
  234. return conn;
  235. }
  236. set {
  237. // FIXME: throw an InvalidOperationException
  238. // if the change was during
  239. // a transaction in progress
  240. conn = value;
  241. // FIXME: set Transaction property to null
  242. }
  243. }
  244. public bool DesignTimeVisible {
  245. get {
  246. return designTime;
  247. }
  248. set{
  249. designTime = value;
  250. }
  251. }
  252. // FIXME; for property Parameters, is this the correct
  253. // way to handle a stronger return type?
  254. IDataParameterCollection IDbCommand.Parameters {
  255. get {
  256. return Parameters;
  257. }
  258. }
  259. SqlParameterCollection Parameters {
  260. get {
  261. return parmCollection;
  262. }
  263. }
  264. // FIXME: for property Transaction, is this the correct
  265. // way to handle a return of a stronger type?
  266. IDbTransaction IDbCommand.Transaction {
  267. get {
  268. return Transaction;
  269. }
  270. set {
  271. // FIXME: error handling - do not allow
  272. // setting of transaction if transaction
  273. // has already begun
  274. // csc
  275. Transaction = (SqlTransaction) value;
  276. // mcs
  277. // Transaction = value;
  278. }
  279. }
  280. public SqlTransaction Transaction {
  281. get {
  282. return trans;
  283. }
  284. set {
  285. // FIXME: error handling
  286. trans = value;
  287. }
  288. }
  289. [MonoTODO]
  290. public UpdateRowSource UpdatedRowSource {
  291. // FIXME: do this once DbDataAdaptor
  292. // and DataRow are done
  293. get {
  294. throw new NotImplementedException ();
  295. }
  296. set {
  297. throw new NotImplementedException ();
  298. }
  299. }
  300. #endregion // Properties
  301. #region Destructors
  302. [MonoTODO]
  303. public void Dispose() {
  304. // FIXME: need proper way to release resources
  305. // Dispose(true);
  306. }
  307. [MonoTODO]
  308. ~SqlCommand()
  309. {
  310. // FIXME: need proper way to release resources
  311. // Dispose(false);
  312. }
  313. #endregion //Destructors
  314. }
  315. }