SqlCommand.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. public int ExecuteNonQuery ()
  81. {
  82. IntPtr pgResult; // PGresult
  83. int rowsAffected = -1;
  84. ExecStatusType execStatus;
  85. String rowsAffectedString;
  86. if(conn.State != ConnectionState.Open)
  87. throw new InvalidOperationException(
  88. "ConnnectionState is not Open");
  89. // FIXME: PQexec blocks
  90. // while PQsendQuery is non-blocking
  91. // which is better to use?
  92. // int PQsendQuery(PGconn *conn,
  93. // const char *query);
  94. // execute SQL command
  95. // uses internal property to get the PGConn IntPtr
  96. pgResult = PostgresLibrary.
  97. PQexec (conn.PostgresConnection, sql);
  98. execStatus = PostgresLibrary.
  99. PQresultStatus (pgResult);
  100. if(execStatus == ExecStatusType.PGRES_COMMAND_OK)
  101. {
  102. rowsAffectedString = PostgresLibrary.
  103. PQcmdTuples (pgResult);
  104. if(rowsAffectedString != null)
  105. if(rowsAffectedString.Equals("") == false)
  106. rowsAffected = int.Parse(rowsAffectedString);
  107. PostgresLibrary.PQclear (pgResult);
  108. }
  109. else
  110. {
  111. String errorMessage;
  112. errorMessage = PostgresLibrary.
  113. PQresStatus(execStatus);
  114. errorMessage += " " + PostgresLibrary.
  115. PQresultErrorMessage(pgResult);
  116. throw new SqlException(0, 0,
  117. errorMessage, 0, "",
  118. conn.DataSource, "SqlCommand", 0);
  119. }
  120. return rowsAffected;
  121. }
  122. [MonoTODO]
  123. IDataReader IDbCommand.ExecuteReader ()
  124. {
  125. return ExecuteReader ();
  126. }
  127. [MonoTODO]
  128. SqlDataReader ExecuteReader ()
  129. {
  130. return ExecuteReader(CommandBehavior.Default);
  131. }
  132. [MonoTODO]
  133. IDataReader IDbCommand.ExecuteReader (
  134. CommandBehavior behavior)
  135. {
  136. return ExecuteReader (behavior);
  137. }
  138. [MonoTODO]
  139. public SqlDataReader ExecuteReader (CommandBehavior behavior)
  140. {
  141. // FIXME: currently only works for a
  142. // single result set
  143. // ExecuteReader can be used
  144. // for multiple result sets
  145. SqlDataReader dataReader = null;
  146. IntPtr pgResult; // PGresult
  147. ExecStatusType execStatus;
  148. if(conn.State != ConnectionState.Open)
  149. throw new InvalidOperationException(
  150. "ConnnectionState is not Open");
  151. // FIXME: PQexec blocks
  152. // while PQsendQuery is non-blocking
  153. // which is better to use?
  154. // int PQsendQuery(PGconn *conn,
  155. // const char *query);
  156. // execute SQL command
  157. // uses internal property to get the PGConn IntPtr
  158. pgResult = PostgresLibrary.
  159. PQexec (conn.PostgresConnection, sql);
  160. execStatus = PostgresLibrary.
  161. PQresultStatus (pgResult);
  162. if(execStatus == ExecStatusType.PGRES_TUPLES_OK) {
  163. DataTable dt = null;
  164. int rows, cols;
  165. int[] oids;
  166. // FIXME: maybe i should move the
  167. // BuildTableSchema code
  168. // to the SqlDataReader?
  169. dt = BuildTableSchema(pgResult,
  170. out rows, out cols, out oids);
  171. dataReader = new SqlDataReader(this, dt, pgResult,
  172. rows, cols, oids);
  173. }
  174. else {
  175. String errorMessage;
  176. errorMessage = PostgresLibrary.
  177. PQresStatus(execStatus);
  178. errorMessage += " " + PostgresLibrary.
  179. PQresultErrorMessage(pgResult);
  180. throw new SqlException(0, 0,
  181. errorMessage, 0, "",
  182. conn.DataSource, "SqlCommand", 0);
  183. }
  184. return dataReader;
  185. }
  186. internal DataTable BuildTableSchema (IntPtr pgResult,
  187. out int nRows,
  188. out int nFields,
  189. out int[] oids) {
  190. int nCol;
  191. DataTable dt = new DataTable();
  192. nRows = PostgresLibrary.
  193. PQntuples(pgResult);
  194. nFields = PostgresLibrary.
  195. PQnfields(pgResult);
  196. oids = new int[nFields];
  197. for(nCol = 0; nCol < nFields; nCol++) {
  198. // get column name
  199. String fieldName;
  200. fieldName = PostgresLibrary.
  201. PQfname(pgResult, nCol);
  202. // get PostgreSQL data type (OID)
  203. oids[nCol] = PostgresLibrary.
  204. PQftype(pgResult, nCol);
  205. int definedSize;
  206. // get defined size of column
  207. definedSize = PostgresLibrary.
  208. PQfsize(pgResult, nCol);
  209. // build the data column and add it the table
  210. DataColumn dc = new DataColumn(fieldName);
  211. dc.DataType = PostgresHelper.OidToType(oids[nCol]);
  212. dc.MaxLength = definedSize;
  213. dc.SetTable(dt);
  214. dt.Columns.Add(dc);
  215. }
  216. return dt;
  217. }
  218. [MonoTODO]
  219. public object ExecuteScalar ()
  220. {
  221. IntPtr pgResult; // PGresult
  222. ExecStatusType execStatus;
  223. object obj = null; // return
  224. int nRow = 0; // first row
  225. int nCol = 0; // first column
  226. String value;
  227. int nRows;
  228. int nFields;
  229. if(conn.State != ConnectionState.Open)
  230. throw new InvalidOperationException(
  231. "ConnnectionState is not Open");
  232. // FIXME: PQexec blocks
  233. // while PQsendQuery is non-blocking
  234. // which is better to use?
  235. // int PQsendQuery(PGconn *conn,
  236. // const char *query);
  237. // execute SQL command
  238. // uses internal property to get the PGConn IntPtr
  239. pgResult = PostgresLibrary.
  240. PQexec (conn.PostgresConnection, sql);
  241. execStatus = PostgresLibrary.
  242. PQresultStatus (pgResult);
  243. if(execStatus == ExecStatusType.PGRES_TUPLES_OK) {
  244. nRows = PostgresLibrary.
  245. PQntuples(pgResult);
  246. nFields = PostgresLibrary.
  247. PQnfields(pgResult);
  248. if(nRows > 0 && nFields > 0) {
  249. // get column name
  250. //String fieldName;
  251. //fieldName = PostgresLibrary.
  252. // PQfname(pgResult, nCol);
  253. int oid;
  254. // get PostgreSQL data type (OID)
  255. oid = PostgresLibrary.
  256. PQftype(pgResult, nCol);
  257. int definedSize;
  258. // get defined size of column
  259. definedSize = PostgresLibrary.
  260. PQfsize(pgResult, nCol);
  261. // get data value
  262. value = PostgresLibrary.
  263. PQgetvalue(
  264. pgResult,
  265. nRow, nCol);
  266. int columnIsNull;
  267. // is column NULL?
  268. columnIsNull = PostgresLibrary.
  269. PQgetisnull(pgResult,
  270. nRow, nCol);
  271. int actualLength;
  272. // get Actual Length
  273. actualLength = PostgresLibrary.
  274. PQgetlength(pgResult,
  275. nRow, nCol);
  276. obj = PostgresHelper.
  277. ConvertPgTypeToSystem (oid, value);
  278. }
  279. // close result set
  280. PostgresLibrary.PQclear (pgResult);
  281. }
  282. else {
  283. String errorMessage;
  284. errorMessage = PostgresLibrary.
  285. PQresStatus(execStatus);
  286. errorMessage += " " + PostgresLibrary.
  287. PQresultErrorMessage(pgResult);
  288. throw new SqlException(0, 0,
  289. errorMessage, 0, "",
  290. conn.DataSource, "SqlCommand", 0);
  291. }
  292. return obj;
  293. }
  294. [MonoTODO]
  295. public XmlReader ExecuteXmlReader ()
  296. {
  297. throw new NotImplementedException ();
  298. }
  299. [MonoTODO]
  300. public void Prepare ()
  301. {
  302. // FIXME: parameters have to be implemented for this
  303. throw new NotImplementedException ();
  304. }
  305. [MonoTODO]
  306. public SqlCommand Clone ()
  307. {
  308. throw new NotImplementedException ();
  309. }
  310. #endregion // Methods
  311. #region Properties
  312. public string CommandText {
  313. get {
  314. return sql;
  315. }
  316. set {
  317. sql = value;
  318. }
  319. }
  320. public int CommandTimeout {
  321. get {
  322. return timeout;
  323. }
  324. set {
  325. // FIXME: if value < 0, throw
  326. // ArgumentException
  327. // if (value < 0)
  328. // throw ArgumentException;
  329. timeout = value;
  330. }
  331. }
  332. public CommandType CommandType {
  333. get {
  334. return cmdType;
  335. }
  336. set {
  337. cmdType = value;
  338. }
  339. }
  340. // FIXME: for property Connection, is this the correct
  341. // way to handle a return of a stronger type?
  342. IDbConnection IDbCommand.Connection {
  343. get {
  344. return Connection;
  345. }
  346. set {
  347. // FIXME: throw an InvalidOperationException
  348. // if the change was during a
  349. // transaction in progress
  350. // csc
  351. Connection = (SqlConnection) value;
  352. // mcs
  353. // Connection = value;
  354. // FIXME: set Transaction property to null
  355. }
  356. }
  357. public SqlConnection Connection {
  358. get {
  359. // conn defaults to null
  360. return conn;
  361. }
  362. set {
  363. // FIXME: throw an InvalidOperationException
  364. // if the change was during
  365. // a transaction in progress
  366. conn = value;
  367. // FIXME: set Transaction property to null
  368. }
  369. }
  370. public bool DesignTimeVisible {
  371. get {
  372. return designTime;
  373. }
  374. set{
  375. designTime = value;
  376. }
  377. }
  378. // FIXME; for property Parameters, is this the correct
  379. // way to handle a stronger return type?
  380. IDataParameterCollection IDbCommand.Parameters {
  381. get {
  382. return Parameters;
  383. }
  384. }
  385. SqlParameterCollection Parameters {
  386. get {
  387. return parmCollection;
  388. }
  389. }
  390. // FIXME: for property Transaction, is this the correct
  391. // way to handle a return of a stronger type?
  392. IDbTransaction IDbCommand.Transaction {
  393. get {
  394. return Transaction;
  395. }
  396. set {
  397. // FIXME: error handling - do not allow
  398. // setting of transaction if transaction
  399. // has already begun
  400. // csc
  401. Transaction = (SqlTransaction) value;
  402. // mcs
  403. // Transaction = value;
  404. }
  405. }
  406. public SqlTransaction Transaction {
  407. get {
  408. return trans;
  409. }
  410. set {
  411. // FIXME: error handling
  412. trans = value;
  413. }
  414. }
  415. [MonoTODO]
  416. public UpdateRowSource UpdatedRowSource {
  417. // FIXME: do this once DbDataAdaptor
  418. // and DataRow are done
  419. get {
  420. throw new NotImplementedException ();
  421. }
  422. set {
  423. throw new NotImplementedException ();
  424. }
  425. }
  426. #endregion // Properties
  427. #region Destructors
  428. [MonoTODO]
  429. public void Dispose() {
  430. // FIXME: need proper way to release resources
  431. // Dispose(true);
  432. }
  433. [MonoTODO]
  434. ~SqlCommand()
  435. {
  436. // FIXME: need proper way to release resources
  437. // Dispose(false);
  438. }
  439. #endregion //Destructors
  440. }
  441. }