CommandTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. // created on 30/11/2002 at 22:35
  2. //
  3. // Author:
  4. // Francisco Figueiredo Jr. <[email protected]>
  5. //
  6. // Copyright (C) 2002 The Npgsql Development Team
  7. // [email protected]
  8. // http://gborg.postgresql.org/project/npgsql/projdisplay.php
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2.1 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. using System;
  24. using Npgsql;
  25. using NUnit.Framework;
  26. using NUnit.Core;
  27. using System.Data;
  28. using NpgsqlTypes;
  29. namespace NpgsqlTests
  30. {
  31. [TestFixture]
  32. public class CommandTests
  33. {
  34. private NpgsqlConnection _conn = null;
  35. private String _connString = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests;SSL=yes";
  36. [SetUp]
  37. protected void SetUp()
  38. {
  39. //NpgsqlEventLog.Level = LogLevel.None;
  40. NpgsqlEventLog.Level = LogLevel.Debug;
  41. NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
  42. _conn = new NpgsqlConnection(_connString);
  43. }
  44. [TearDown]
  45. protected void TearDown()
  46. {
  47. if (_conn.State != ConnectionState.Closed)
  48. _conn.Close();
  49. }
  50. [Test]
  51. public void ParametersGetName()
  52. {
  53. NpgsqlCommand command = new NpgsqlCommand();
  54. // Add parameters.
  55. command.Parameters.Add(new NpgsqlParameter(":Parameter1", DbType.Boolean));
  56. command.Parameters.Add(new NpgsqlParameter(":Parameter2", DbType.Int32));
  57. command.Parameters.Add(new NpgsqlParameter(":Parameter3", DbType.DateTime));
  58. // Get by indexers.
  59. Assertion.AssertEquals("ParametersGetName", ":Parameter1", command.Parameters[":Parameter1"].ParameterName);
  60. Assertion.AssertEquals("ParametersGetName", ":Parameter2", command.Parameters[":Parameter2"].ParameterName);
  61. Assertion.AssertEquals("ParametersGetName", ":Parameter3", command.Parameters[":Parameter3"].ParameterName);
  62. Assertion.AssertEquals("ParametersGetName", ":Parameter1", command.Parameters[0].ParameterName);
  63. Assertion.AssertEquals("ParametersGetName", ":Parameter2", command.Parameters[1].ParameterName);
  64. Assertion.AssertEquals("ParametersGetName", ":Parameter3", command.Parameters[2].ParameterName);
  65. }
  66. [Test]
  67. public void EmptyQuery()
  68. {
  69. _conn.Open();
  70. NpgsqlCommand command = new NpgsqlCommand(";", _conn);
  71. command.ExecuteNonQuery();
  72. }
  73. [Test]
  74. [ExpectedException(typeof(NpgsqlException))]
  75. public void NoNameParameterAdd()
  76. {
  77. NpgsqlCommand command = new NpgsqlCommand();
  78. command.Parameters.Add(new NpgsqlParameter());
  79. }
  80. [Test]
  81. public void FunctionCallFromSelect()
  82. {
  83. _conn.Open();
  84. NpgsqlCommand command = new NpgsqlCommand("select * from funcB()", _conn);
  85. NpgsqlDataReader reader = command.ExecuteReader();
  86. Assertion.AssertNotNull(reader);
  87. //reader.FieldCount
  88. }
  89. [Test]
  90. public void ExecuteScalar()
  91. {
  92. _conn.Open();
  93. NpgsqlCommand command = new NpgsqlCommand("select count(*) from tablea", _conn);
  94. Object result = command.ExecuteScalar();
  95. Assertion.AssertEquals(5, result);
  96. //reader.FieldCount
  97. }
  98. [Test]
  99. public void FunctionCallReturnSingleValue()
  100. {
  101. _conn.Open();
  102. NpgsqlCommand command = new NpgsqlCommand("funcC()", _conn);
  103. command.CommandType = CommandType.StoredProcedure;
  104. Object result = command.ExecuteScalar();
  105. Assertion.AssertEquals(5, result);
  106. //reader.FieldCount
  107. }
  108. [Test]
  109. public void FunctionCallReturnSingleValueWithPrepare()
  110. {
  111. _conn.Open();
  112. NpgsqlCommand command = new NpgsqlCommand("funcC()", _conn);
  113. command.CommandType = CommandType.StoredProcedure;
  114. command.Prepare();
  115. Object result = command.ExecuteScalar();
  116. Assertion.AssertEquals(5, result);
  117. //reader.FieldCount
  118. }
  119. [Test]
  120. public void FunctionCallWithParametersReturnSingleValue()
  121. {
  122. _conn.Open();
  123. NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
  124. command.CommandType = CommandType.StoredProcedure;
  125. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  126. command.Parameters[0].Value = 4;
  127. Int64 result = (Int64) command.ExecuteScalar();
  128. Assertion.AssertEquals(1, result);
  129. }
  130. [Test]
  131. public void FunctionCallWithParametersPrepareReturnSingleValue()
  132. {
  133. _conn.Open();
  134. NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
  135. command.CommandType = CommandType.StoredProcedure;
  136. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  137. Assertion.AssertEquals(1, command.Parameters.Count);
  138. command.Prepare();
  139. command.Parameters[0].Value = 4;
  140. Int64 result = (Int64) command.ExecuteScalar();
  141. Assertion.AssertEquals(1, result);
  142. }
  143. [Test]
  144. public void FunctionCallReturnResultSet()
  145. {
  146. _conn.Open();
  147. NpgsqlCommand command = new NpgsqlCommand("funcB()", _conn);
  148. command.CommandType = CommandType.StoredProcedure;
  149. NpgsqlDataReader dr = command.ExecuteReader();
  150. }
  151. [Test]
  152. public void CursorStatement()
  153. {
  154. _conn.Open();
  155. Int32 i = 0;
  156. NpgsqlTransaction t = _conn.BeginTransaction();
  157. NpgsqlCommand command = new NpgsqlCommand("declare te cursor for select * from tablea;", _conn);
  158. command.ExecuteNonQuery();
  159. command.CommandText = "fetch forward 3 in te;";
  160. NpgsqlDataReader dr = command.ExecuteReader();
  161. while (dr.Read())
  162. {
  163. i++;
  164. }
  165. Assertion.AssertEquals(3, i);
  166. i = 0;
  167. command.CommandText = "fetch backward 1 in te;";
  168. NpgsqlDataReader dr2 = command.ExecuteReader();
  169. while (dr2.Read())
  170. {
  171. i++;
  172. }
  173. Assertion.AssertEquals(1, i);
  174. command.CommandText = "close te;";
  175. command.ExecuteNonQuery();
  176. t.Commit();
  177. }
  178. [Test]
  179. public void PreparedStatementNoParameters()
  180. {
  181. _conn.Open();
  182. NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
  183. command.Prepare();
  184. command.Prepare();
  185. NpgsqlDataReader dr = command.ExecuteReader();
  186. }
  187. [Test]
  188. public void PreparedStatementWithParameters()
  189. {
  190. _conn.Open();
  191. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_int4 = :a and field_int8 = :b;", _conn);
  192. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  193. command.Parameters.Add(new NpgsqlParameter("b", DbType.Int64));
  194. Assertion.AssertEquals(2, command.Parameters.Count);
  195. Assertion.AssertEquals(DbType.Int32, command.Parameters[0].DbType);
  196. command.Prepare();
  197. command.Parameters[0].Value = 3;
  198. command.Parameters[1].Value = 5;
  199. NpgsqlDataReader dr = command.ExecuteReader();
  200. }
  201. [Test]
  202. [ExpectedException(typeof(InvalidOperationException))]
  203. public void ListenNotifySupport()
  204. {
  205. _conn.Open();
  206. NpgsqlCommand command = new NpgsqlCommand("listen notifytest;", _conn);
  207. command.ExecuteNonQuery();
  208. _conn.Notification += new NotificationEventHandler(NotificationSupportHelper);
  209. command = new NpgsqlCommand("notify notifytest;", _conn);
  210. command.ExecuteNonQuery();
  211. }
  212. private void NotificationSupportHelper(Object sender, NpgsqlNotificationEventArgs args)
  213. {
  214. throw new InvalidOperationException();
  215. }
  216. [Test]
  217. public void DateTimeSupport()
  218. {
  219. _conn.Open();
  220. NpgsqlCommand command = new NpgsqlCommand("select field_timestamp from tableb where field_serial = 2;", _conn);
  221. DateTime d = (DateTime)command.ExecuteScalar();
  222. Assertion.AssertEquals("2002-02-02 09:00:23Z", d.ToString("u"));
  223. }
  224. [Test]
  225. public void DateSupport()
  226. {
  227. _conn.Open();
  228. NpgsqlCommand command = new NpgsqlCommand("select field_date from tablec where field_serial = 1;", _conn);
  229. DateTime d = (DateTime)command.ExecuteScalar();
  230. Assertion.AssertEquals("2002-03-04", d.ToString("yyyy-MM-dd"));
  231. }
  232. [Test]
  233. public void TimeSupport()
  234. {
  235. _conn.Open();
  236. NpgsqlCommand command = new NpgsqlCommand("select field_time from tablec where field_serial = 2;", _conn);
  237. DateTime d = (DateTime)command.ExecuteScalar();
  238. Assertion.AssertEquals("10:03:45.345", d.ToString("HH:mm:ss.fff"));
  239. }
  240. [Test]
  241. public void NumericSupport()
  242. {
  243. _conn.Open();
  244. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
  245. command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
  246. command.Parameters[0].Value = 7.4M;
  247. Int32 rowsAdded = command.ExecuteNonQuery();
  248. Assertion.AssertEquals(1, rowsAdded);
  249. command.CommandText = "select * from tableb where field_numeric = :a";
  250. NpgsqlDataReader dr = command.ExecuteReader();
  251. dr.Read();
  252. Decimal result = dr.GetDecimal(3);
  253. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
  254. command.Parameters.Clear();
  255. command.ExecuteNonQuery();
  256. Assertion.AssertEquals(7.4M, result);
  257. }
  258. [Test]
  259. public void InsertSingleValue()
  260. {
  261. _conn.Open();
  262. NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float4) values (:a)", _conn);
  263. command.Parameters.Add(new NpgsqlParameter(":a", DbType.Single));
  264. command.Parameters[0].Value = 7.4F;
  265. Int32 rowsAdded = command.ExecuteNonQuery();
  266. Assertion.AssertEquals(1, rowsAdded);
  267. command.CommandText = "select * from tabled where field_float4 = :a";
  268. NpgsqlDataReader dr = command.ExecuteReader();
  269. dr.Read();
  270. Single result = dr.GetFloat(1);
  271. command.CommandText = "delete from tabled where field_serial > 2;";
  272. command.Parameters.Clear();
  273. command.ExecuteNonQuery();
  274. Assertion.AssertEquals(7.4F, result);
  275. }
  276. [Test]
  277. public void InsertDoubleValue()
  278. {
  279. _conn.Open();
  280. NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float8) values (:a)", _conn);
  281. command.Parameters.Add(new NpgsqlParameter(":a", DbType.Double));
  282. command.Parameters[0].Value = 7.4D;
  283. Int32 rowsAdded = command.ExecuteNonQuery();
  284. Assertion.AssertEquals(1, rowsAdded);
  285. command.CommandText = "select * from tabled where field_float8 = :a";
  286. NpgsqlDataReader dr = command.ExecuteReader();
  287. dr.Read();
  288. Double result = dr.GetDouble(2);
  289. command.CommandText = "delete from tabled where field_serial > 2;";
  290. command.Parameters.Clear();
  291. //command.ExecuteNonQuery();
  292. Assertion.AssertEquals(7.4D, result);
  293. }
  294. [Test]
  295. public void NegativeNumericSupport()
  296. {
  297. _conn.Open();
  298. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
  299. NpgsqlDataReader dr = command.ExecuteReader();
  300. dr.Read();
  301. Decimal result = dr.GetDecimal(3);
  302. Assertion.AssertEquals(-4.3M, result);
  303. }
  304. [Test]
  305. public void PrecisionScaleNumericSupport()
  306. {
  307. _conn.Open();
  308. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
  309. NpgsqlDataReader dr = command.ExecuteReader();
  310. dr.Read();
  311. Decimal result = dr.GetDecimal(3);
  312. Assertion.AssertEquals(-4.3M, (Decimal)result);
  313. //Assertion.AssertEquals(11, result.Precision);
  314. //Assertion.AssertEquals(7, result.Scale);
  315. }
  316. [Test]
  317. public void InsertNullString()
  318. {
  319. _conn.Open();
  320. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
  321. command.Parameters.Add(new NpgsqlParameter("a", DbType.String));
  322. command.Parameters[0].Value = DBNull.Value;
  323. Int32 rowsAdded = command.ExecuteNonQuery();
  324. Assertion.AssertEquals(1, rowsAdded);
  325. command.CommandText = "select count(*) from tablea where field_text is null";
  326. command.Parameters.Clear();
  327. Int64 result = (Int64)command.ExecuteScalar();
  328. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea) and field_serial != 4;";
  329. command.ExecuteNonQuery();
  330. Assertion.AssertEquals(4, result);
  331. }
  332. [Test]
  333. public void InsertNullDateTime()
  334. {
  335. _conn.Open();
  336. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_timestamp) values (:a)", _conn);
  337. command.Parameters.Add(new NpgsqlParameter("a", DbType.DateTime));
  338. command.Parameters[0].Value = DBNull.Value;
  339. Int32 rowsAdded = command.ExecuteNonQuery();
  340. Assertion.AssertEquals(1, rowsAdded);
  341. command.CommandText = "select count(*) from tableb where field_timestamp is null";
  342. command.Parameters.Clear();
  343. Object result = command.ExecuteScalar();
  344. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
  345. command.ExecuteNonQuery();
  346. Assertion.AssertEquals(4, result);
  347. }
  348. [Test]
  349. public void InsertNullInt16()
  350. {
  351. _conn.Open();
  352. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_int2) values (:a)", _conn);
  353. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
  354. command.Parameters[0].Value = DBNull.Value;
  355. Int32 rowsAdded = command.ExecuteNonQuery();
  356. Assertion.AssertEquals(1, rowsAdded);
  357. command.CommandText = "select count(*) from tableb where field_int2 is null";
  358. command.Parameters.Clear();
  359. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  360. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
  361. command.ExecuteNonQuery();
  362. Assertion.AssertEquals(4, result);
  363. }
  364. [Test]
  365. public void InsertNullInt32()
  366. {
  367. _conn.Open();
  368. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_int4) values (:a)", _conn);
  369. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  370. command.Parameters[0].Value = DBNull.Value;
  371. Int32 rowsAdded = command.ExecuteNonQuery();
  372. Assertion.AssertEquals(1, rowsAdded);
  373. command.CommandText = "select count(*) from tablea where field_int4 is null";
  374. command.Parameters.Clear();
  375. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  376. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
  377. command.ExecuteNonQuery();
  378. Assertion.AssertEquals(5, result);
  379. }
  380. [Test]
  381. public void InsertNullNumeric()
  382. {
  383. _conn.Open();
  384. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
  385. command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
  386. command.Parameters[0].Value = DBNull.Value;
  387. Int32 rowsAdded = command.ExecuteNonQuery();
  388. Assertion.AssertEquals(1, rowsAdded);
  389. command.CommandText = "select count(*) from tableb where field_numeric is null";
  390. command.Parameters.Clear();
  391. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  392. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
  393. command.ExecuteNonQuery();
  394. Assertion.AssertEquals(3, result);
  395. }
  396. [Test]
  397. public void InsertNullBoolean()
  398. {
  399. _conn.Open();
  400. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_bool) values (:a)", _conn);
  401. command.Parameters.Add(new NpgsqlParameter("a", DbType.Boolean));
  402. command.Parameters[0].Value = DBNull.Value;
  403. Int32 rowsAdded = command.ExecuteNonQuery();
  404. Assertion.AssertEquals(1, rowsAdded);
  405. command.CommandText = "select count(*) from tablea where field_bool is null";
  406. command.Parameters.Clear();
  407. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  408. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
  409. command.ExecuteNonQuery();
  410. Assertion.AssertEquals(5, result);
  411. }
  412. [Test]
  413. public void AnsiStringSupport()
  414. {
  415. _conn.Open();
  416. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
  417. command.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiString));
  418. command.Parameters[0].Value = "TesteAnsiString";
  419. Int32 rowsAdded = command.ExecuteNonQuery();
  420. Assertion.AssertEquals(1, rowsAdded);
  421. command.CommandText = String.Format("select count(*) from tablea where field_text = '{0}'", command.Parameters[0].Value);
  422. command.Parameters.Clear();
  423. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  424. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
  425. command.ExecuteNonQuery();
  426. Assertion.AssertEquals(1, result);
  427. }
  428. }
  429. }