CommandTests.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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 System.Globalization;
  29. using NpgsqlTypes;
  30. namespace NpgsqlTests
  31. {
  32. [TestFixture]
  33. public class CommandTests
  34. {
  35. NpgsqlConnection _conn = null;
  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 (TestConfiguration.NpgsqlConnectionString);
  43. }
  44. [TearDown]
  45. protected void TearDown()
  46. {
  47. if (_conn != null && _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. Assert.AreEqual(":Parameter1", command.Parameters[":Parameter1"].ParameterName);
  60. Assert.AreEqual(":Parameter2", command.Parameters[":Parameter2"].ParameterName);
  61. Assert.AreEqual(":Parameter3", command.Parameters[":Parameter3"].ParameterName);
  62. Assert.AreEqual(":Parameter1", command.Parameters[0].ParameterName);
  63. Assert.AreEqual(":Parameter2", command.Parameters[1].ParameterName);
  64. Assert.AreEqual(":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(ArgumentNullException))]
  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. Assert.AreEqual(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. Assert.AreEqual(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. Assert.AreEqual(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. Assert.AreEqual(1, result);
  129. }
  130. [Test]
  131. public void FunctionCallWithParametersReturnSingleValueNpgsqlDbType()
  132. {
  133. _conn.Open();
  134. NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
  135. command.CommandType = CommandType.StoredProcedure;
  136. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
  137. command.Parameters[0].Value = 4;
  138. Int64 result = (Int64) command.ExecuteScalar();
  139. Assert.AreEqual(1, result);
  140. }
  141. [Test]
  142. public void FunctionCallWithParametersPrepareReturnSingleValue()
  143. {
  144. _conn.Open();
  145. NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
  146. command.CommandType = CommandType.StoredProcedure;
  147. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  148. Assert.AreEqual(1, command.Parameters.Count);
  149. command.Prepare();
  150. command.Parameters[0].Value = 4;
  151. Int64 result = (Int64) command.ExecuteScalar();
  152. Assert.AreEqual(1, result);
  153. }
  154. [Test]
  155. public void FunctionCallWithParametersPrepareReturnSingleValueNpgsqlDbType()
  156. {
  157. _conn.Open();
  158. NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
  159. command.CommandType = CommandType.StoredProcedure;
  160. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
  161. Assert.AreEqual(1, command.Parameters.Count);
  162. command.Prepare();
  163. command.Parameters[0].Value = 4;
  164. Int64 result = (Int64) command.ExecuteScalar();
  165. Assert.AreEqual(1, result);
  166. }
  167. [Test]
  168. public void FunctionCallReturnResultSet()
  169. {
  170. _conn.Open();
  171. NpgsqlCommand command = new NpgsqlCommand("funcB()", _conn);
  172. command.CommandType = CommandType.StoredProcedure;
  173. NpgsqlDataReader dr = command.ExecuteReader();
  174. }
  175. [Test]
  176. public void CursorStatement()
  177. {
  178. _conn.Open();
  179. Int32 i = 0;
  180. NpgsqlTransaction t = _conn.BeginTransaction();
  181. NpgsqlCommand command = new NpgsqlCommand("declare te cursor for select * from tablea;", _conn);
  182. command.ExecuteNonQuery();
  183. command.CommandText = "fetch forward 3 in te;";
  184. NpgsqlDataReader dr = command.ExecuteReader();
  185. while (dr.Read())
  186. {
  187. i++;
  188. }
  189. Assert.AreEqual(3, i);
  190. i = 0;
  191. command.CommandText = "fetch backward 1 in te;";
  192. NpgsqlDataReader dr2 = command.ExecuteReader();
  193. while (dr2.Read())
  194. {
  195. i++;
  196. }
  197. Assert.AreEqual(1, i);
  198. command.CommandText = "close te;";
  199. command.ExecuteNonQuery();
  200. t.Commit();
  201. }
  202. [Test]
  203. public void PreparedStatementNoParameters()
  204. {
  205. _conn.Open();
  206. NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
  207. command.Prepare();
  208. command.Prepare();
  209. NpgsqlDataReader dr = command.ExecuteReader();
  210. }
  211. [Test]
  212. public void PreparedStatementWithParameters()
  213. {
  214. _conn.Open();
  215. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_int4 = :a and field_int8 = :b;", _conn);
  216. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  217. command.Parameters.Add(new NpgsqlParameter("b", DbType.Int64));
  218. Assert.AreEqual(2, command.Parameters.Count);
  219. Assert.AreEqual(DbType.Int32, command.Parameters[0].DbType);
  220. command.Prepare();
  221. command.Parameters[0].Value = 3;
  222. command.Parameters[1].Value = 5;
  223. NpgsqlDataReader dr = command.ExecuteReader();
  224. }
  225. [Test]
  226. public void PreparedStatementWithParametersNpgsqlDbType()
  227. {
  228. _conn.Open();
  229. NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_int4 = :a and field_int8 = :b;", _conn);
  230. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
  231. command.Parameters.Add(new NpgsqlParameter("b", NpgsqlDbType.Bigint));
  232. Assert.AreEqual(2, command.Parameters.Count);
  233. Assert.AreEqual(DbType.Int32, command.Parameters[0].DbType);
  234. command.Prepare();
  235. command.Parameters[0].Value = 3;
  236. command.Parameters[1].Value = 5;
  237. NpgsqlDataReader dr = command.ExecuteReader();
  238. }
  239. [Test]
  240. [ExpectedException(typeof(InvalidOperationException))]
  241. public void ListenNotifySupport()
  242. {
  243. _conn.Open();
  244. NpgsqlCommand command = new NpgsqlCommand("listen notifytest;", _conn);
  245. command.ExecuteNonQuery();
  246. _conn.Notification += new NotificationEventHandler(NotificationSupportHelper);
  247. command = new NpgsqlCommand("notify notifytest;", _conn);
  248. command.ExecuteNonQuery();
  249. }
  250. private void NotificationSupportHelper(Object sender, NpgsqlNotificationEventArgs args)
  251. {
  252. throw new InvalidOperationException();
  253. }
  254. [Test]
  255. public void DateTimeSupport()
  256. {
  257. _conn.Open();
  258. NpgsqlCommand command = new NpgsqlCommand("select field_timestamp from tableb where field_serial = 2;", _conn);
  259. DateTime d = (DateTime)command.ExecuteScalar();
  260. Assert.AreEqual("2002-02-02 09:00:23Z", d.ToString("u"));
  261. DateTimeFormatInfo culture = new DateTimeFormatInfo();
  262. culture.TimeSeparator = ":";
  263. DateTime dt = System.DateTime.Parse("2004-06-04 09:48:00", culture);
  264. command.CommandText = "insert into tableb(field_timestamp) values (:a);delete from tableb where field_serial > 4;";
  265. command.Parameters.Add(new NpgsqlParameter("a", DbType.DateTime));
  266. command.Parameters[0].Value = dt;
  267. command.ExecuteScalar();
  268. }
  269. [Test]
  270. public void DateTimeSupportNpgsqlDbType()
  271. {
  272. _conn.Open();
  273. NpgsqlCommand command = new NpgsqlCommand("select field_timestamp from tableb where field_serial = 2;", _conn);
  274. DateTime d = (DateTime)command.ExecuteScalar();
  275. Assert.AreEqual("2002-02-02 09:00:23Z", d.ToString("u"));
  276. DateTimeFormatInfo culture = new DateTimeFormatInfo();
  277. culture.TimeSeparator = ":";
  278. DateTime dt = System.DateTime.Parse("2004-06-04 09:48:00", culture);
  279. command.CommandText = "insert into tableb(field_timestamp) values (:a);delete from tableb where field_serial > 4;";
  280. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Timestamp));
  281. command.Parameters[0].Value = dt;
  282. command.ExecuteScalar();
  283. }
  284. [Test]
  285. public void DateSupport()
  286. {
  287. _conn.Open();
  288. NpgsqlCommand command = new NpgsqlCommand("select field_date from tablec where field_serial = 1;", _conn);
  289. DateTime d = (DateTime)command.ExecuteScalar();
  290. Assert.AreEqual("2002-03-04", d.ToString("yyyy-MM-dd"));
  291. }
  292. [Test]
  293. public void TimeSupport()
  294. {
  295. _conn.Open();
  296. NpgsqlCommand command = new NpgsqlCommand("select field_time from tablec where field_serial = 2;", _conn);
  297. DateTime d = (DateTime)command.ExecuteScalar();
  298. Assert.AreEqual("10:03:45.345", d.ToString("HH:mm:ss.fff"));
  299. }
  300. [Test]
  301. public void NumericSupport()
  302. {
  303. _conn.Open();
  304. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
  305. command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
  306. command.Parameters[0].Value = 7.4M;
  307. Int32 rowsAdded = command.ExecuteNonQuery();
  308. Assert.AreEqual(1, rowsAdded);
  309. command.CommandText = "select * from tableb where field_numeric = :a";
  310. NpgsqlDataReader dr = command.ExecuteReader();
  311. dr.Read();
  312. Decimal result = dr.GetDecimal(3);
  313. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
  314. command.Parameters.Clear();
  315. command.ExecuteNonQuery();
  316. Assert.AreEqual(7.4000000M, result);
  317. }
  318. [Test]
  319. public void NumericSupportNpgsqlDbType()
  320. {
  321. _conn.Open();
  322. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
  323. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Numeric));
  324. command.Parameters[0].Value = 7.4M;
  325. Int32 rowsAdded = command.ExecuteNonQuery();
  326. Assert.AreEqual(1, rowsAdded);
  327. command.CommandText = "select * from tableb where field_numeric = :a";
  328. NpgsqlDataReader dr = command.ExecuteReader();
  329. dr.Read();
  330. Decimal result = dr.GetDecimal(3);
  331. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
  332. command.Parameters.Clear();
  333. command.ExecuteNonQuery();
  334. Assert.AreEqual(7.4000000M, result);
  335. }
  336. [Test]
  337. public void InsertSingleValue()
  338. {
  339. _conn.Open();
  340. NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float4) values (:a)", _conn);
  341. command.Parameters.Add(new NpgsqlParameter(":a", DbType.Single));
  342. command.Parameters[0].Value = 7.4F;
  343. Int32 rowsAdded = command.ExecuteNonQuery();
  344. Assert.AreEqual(1, rowsAdded);
  345. command.CommandText = "select * from tabled where field_float4 = :a";
  346. NpgsqlDataReader dr = command.ExecuteReader();
  347. dr.Read();
  348. Single result = dr.GetFloat(1);
  349. command.CommandText = "delete from tabled where field_serial > 2;";
  350. command.Parameters.Clear();
  351. command.ExecuteNonQuery();
  352. Assert.AreEqual(7.4F, result);
  353. }
  354. [Test]
  355. public void InsertSingleValueNpgsqlDbType()
  356. {
  357. _conn.Open();
  358. NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float4) values (:a)", _conn);
  359. command.Parameters.Add(new NpgsqlParameter(":a", NpgsqlDbType.Real));
  360. command.Parameters[0].Value = 7.4F;
  361. Int32 rowsAdded = command.ExecuteNonQuery();
  362. Assert.AreEqual(1, rowsAdded);
  363. command.CommandText = "select * from tabled where field_float4 = :a";
  364. NpgsqlDataReader dr = command.ExecuteReader();
  365. dr.Read();
  366. Single result = dr.GetFloat(1);
  367. command.CommandText = "delete from tabled where field_serial > 2;";
  368. command.Parameters.Clear();
  369. command.ExecuteNonQuery();
  370. Assert.AreEqual(7.4F, result);
  371. }
  372. [Test]
  373. public void InsertDoubleValue()
  374. {
  375. _conn.Open();
  376. NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float8) values (:a)", _conn);
  377. command.Parameters.Add(new NpgsqlParameter(":a", DbType.Double));
  378. command.Parameters[0].Value = 7.4D;
  379. Int32 rowsAdded = command.ExecuteNonQuery();
  380. Assert.AreEqual(1, rowsAdded);
  381. command.CommandText = "select * from tabled where field_float8 = :a";
  382. NpgsqlDataReader dr = command.ExecuteReader();
  383. dr.Read();
  384. Double result = dr.GetDouble(2);
  385. command.CommandText = "delete from tabled where field_serial > 2;";
  386. command.Parameters.Clear();
  387. //command.ExecuteNonQuery();
  388. Assert.AreEqual(7.4D, result);
  389. }
  390. [Test]
  391. public void InsertDoubleValueNpgsqlDbType()
  392. {
  393. _conn.Open();
  394. NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float8) values (:a)", _conn);
  395. command.Parameters.Add(new NpgsqlParameter(":a", NpgsqlDbType.Double));
  396. command.Parameters[0].Value = 7.4D;
  397. Int32 rowsAdded = command.ExecuteNonQuery();
  398. Assert.AreEqual(1, rowsAdded);
  399. command.CommandText = "select * from tabled where field_float8 = :a";
  400. NpgsqlDataReader dr = command.ExecuteReader();
  401. dr.Read();
  402. Double result = dr.GetDouble(2);
  403. command.CommandText = "delete from tabled where field_serial > 2;";
  404. command.Parameters.Clear();
  405. //command.ExecuteNonQuery();
  406. Assert.AreEqual(7.4D, result);
  407. }
  408. [Test]
  409. public void NegativeNumericSupport()
  410. {
  411. _conn.Open();
  412. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
  413. NpgsqlDataReader dr = command.ExecuteReader();
  414. dr.Read();
  415. Decimal result = dr.GetDecimal(3);
  416. Assert.AreEqual(-4.3000000M, result);
  417. }
  418. [Test]
  419. public void PrecisionScaleNumericSupport()
  420. {
  421. _conn.Open();
  422. NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
  423. NpgsqlDataReader dr = command.ExecuteReader();
  424. dr.Read();
  425. Decimal result = dr.GetDecimal(3);
  426. Assert.AreEqual(-4.3000000M, (Decimal)result);
  427. //Assert.AreEqual(11, result.Precision);
  428. //Assert.AreEqual(7, result.Scale);
  429. }
  430. [Test]
  431. public void InsertNullString()
  432. {
  433. _conn.Open();
  434. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
  435. command.Parameters.Add(new NpgsqlParameter("a", DbType.String));
  436. command.Parameters[0].Value = DBNull.Value;
  437. Int32 rowsAdded = command.ExecuteNonQuery();
  438. Assert.AreEqual(1, rowsAdded);
  439. command.CommandText = "select count(*) from tablea where field_text is null";
  440. command.Parameters.Clear();
  441. Int64 result = (Int64)command.ExecuteScalar();
  442. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea) and field_serial != 4;";
  443. command.ExecuteNonQuery();
  444. Assert.AreEqual(4, result);
  445. }
  446. [Test]
  447. public void InsertNullStringNpgsqlDbType()
  448. {
  449. _conn.Open();
  450. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
  451. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Text));
  452. command.Parameters[0].Value = DBNull.Value;
  453. Int32 rowsAdded = command.ExecuteNonQuery();
  454. Assert.AreEqual(1, rowsAdded);
  455. command.CommandText = "select count(*) from tablea where field_text is null";
  456. command.Parameters.Clear();
  457. Int64 result = (Int64)command.ExecuteScalar();
  458. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea) and field_serial != 4;";
  459. command.ExecuteNonQuery();
  460. Assert.AreEqual(4, result);
  461. }
  462. [Test]
  463. public void InsertNullDateTime()
  464. {
  465. _conn.Open();
  466. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_timestamp) values (:a)", _conn);
  467. command.Parameters.Add(new NpgsqlParameter("a", DbType.DateTime));
  468. command.Parameters[0].Value = DBNull.Value;
  469. Int32 rowsAdded = command.ExecuteNonQuery();
  470. Assert.AreEqual(1, rowsAdded);
  471. command.CommandText = "select count(*) from tableb where field_timestamp is null";
  472. command.Parameters.Clear();
  473. Object result = command.ExecuteScalar();
  474. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
  475. command.ExecuteNonQuery();
  476. Assert.AreEqual(4, result);
  477. }
  478. [Test]
  479. public void InsertNullDateTimeNpgsqlDbType()
  480. {
  481. _conn.Open();
  482. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_timestamp) values (:a)", _conn);
  483. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Timestamp));
  484. command.Parameters[0].Value = DBNull.Value;
  485. Int32 rowsAdded = command.ExecuteNonQuery();
  486. Assert.AreEqual(1, rowsAdded);
  487. command.CommandText = "select count(*) from tableb where field_timestamp is null";
  488. command.Parameters.Clear();
  489. Object result = command.ExecuteScalar();
  490. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
  491. command.ExecuteNonQuery();
  492. Assert.AreEqual(4, result);
  493. }
  494. [Test]
  495. public void InsertNullInt16()
  496. {
  497. _conn.Open();
  498. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_int2) values (:a)", _conn);
  499. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
  500. command.Parameters[0].Value = DBNull.Value;
  501. Int32 rowsAdded = command.ExecuteNonQuery();
  502. Assert.AreEqual(1, rowsAdded);
  503. command.CommandText = "select count(*) from tableb where field_int2 is null";
  504. command.Parameters.Clear();
  505. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  506. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
  507. command.ExecuteNonQuery();
  508. Assert.AreEqual(4, result);
  509. }
  510. [Test]
  511. public void InsertNullInt16NpgsqlDbType()
  512. {
  513. _conn.Open();
  514. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_int2) values (:a)", _conn);
  515. command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Smallint));
  516. command.Parameters[0].Value = DBNull.Value;
  517. Int32 rowsAdded = command.ExecuteNonQuery();
  518. Assert.AreEqual(1, rowsAdded);
  519. command.CommandText = "select count(*) from tableb where field_int2 is null";
  520. command.Parameters.Clear();
  521. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  522. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
  523. command.ExecuteNonQuery();
  524. Assert.AreEqual(4, result);
  525. }
  526. [Test]
  527. public void InsertNullInt32()
  528. {
  529. _conn.Open();
  530. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_int4) values (:a)", _conn);
  531. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  532. command.Parameters[0].Value = DBNull.Value;
  533. Int32 rowsAdded = command.ExecuteNonQuery();
  534. Assert.AreEqual(1, rowsAdded);
  535. command.CommandText = "select count(*) from tablea where field_int4 is null";
  536. command.Parameters.Clear();
  537. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  538. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
  539. command.ExecuteNonQuery();
  540. Assert.AreEqual(5, result);
  541. }
  542. [Test]
  543. public void InsertNullNumeric()
  544. {
  545. _conn.Open();
  546. NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
  547. command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
  548. command.Parameters[0].Value = DBNull.Value;
  549. Int32 rowsAdded = command.ExecuteNonQuery();
  550. Assert.AreEqual(1, rowsAdded);
  551. command.CommandText = "select count(*) from tableb where field_numeric is null";
  552. command.Parameters.Clear();
  553. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  554. command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
  555. command.ExecuteNonQuery();
  556. Assert.AreEqual(3, result);
  557. }
  558. [Test]
  559. public void InsertNullBoolean()
  560. {
  561. _conn.Open();
  562. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_bool) values (:a)", _conn);
  563. command.Parameters.Add(new NpgsqlParameter("a", DbType.Boolean));
  564. command.Parameters[0].Value = DBNull.Value;
  565. Int32 rowsAdded = command.ExecuteNonQuery();
  566. Assert.AreEqual(1, rowsAdded);
  567. command.CommandText = "select count(*) from tablea where field_bool is null";
  568. command.Parameters.Clear();
  569. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  570. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
  571. command.ExecuteNonQuery();
  572. Assert.AreEqual(5, result);
  573. }
  574. [Test]
  575. public void AnsiStringSupport()
  576. {
  577. _conn.Open();
  578. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
  579. command.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiString));
  580. command.Parameters[0].Value = "TesteAnsiString";
  581. Int32 rowsAdded = command.ExecuteNonQuery();
  582. Assert.AreEqual(1, rowsAdded);
  583. command.CommandText = String.Format("select count(*) from tablea where field_text = '{0}'", command.Parameters[0].Value);
  584. command.Parameters.Clear();
  585. Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
  586. command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
  587. command.ExecuteNonQuery();
  588. Assert.AreEqual(1, result);
  589. }
  590. [Test]
  591. public void MultipleQueriesFirstResultsetEmpty()
  592. {
  593. _conn.Open();
  594. NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values ('a'); select count(*) from tablea;", _conn);
  595. Object result = command.ExecuteScalar();
  596. command.CommandText = "delete from tablea where field_serial > 5";
  597. command.ExecuteNonQuery();
  598. command.CommandText = "select * from tablea where field_serial = 0";
  599. command.ExecuteScalar();
  600. Assert.AreEqual(6, result);
  601. }
  602. [Test]
  603. [ExpectedException(typeof(NpgsqlException))]
  604. public void ConnectionStringWithInvalidParameters()
  605. {
  606. NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests;Password=j");
  607. NpgsqlCommand command = new NpgsqlCommand("select * from tablea", conn);
  608. command.Connection.Open();
  609. command.ExecuteReader();
  610. command.Connection.Close();
  611. }
  612. [Test]
  613. [ExpectedException(typeof(NpgsqlException))]
  614. public void InvalidConnectionString()
  615. {
  616. NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests");
  617. NpgsqlCommand command = new NpgsqlCommand("select * from tablea", conn);
  618. command.Connection.Open();
  619. command.ExecuteReader();
  620. command.Connection.Close();
  621. }
  622. [Test]
  623. public void AmbiguousFunctionParameterType()
  624. {
  625. NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests;Password=npgsql_tests");
  626. NpgsqlCommand command = new NpgsqlCommand("ambiguousParameterType(:a, :b, :c, :d, :e, :f)", conn);
  627. command.CommandType = CommandType.StoredProcedure;
  628. NpgsqlParameter p = new NpgsqlParameter("a", DbType.Int16);
  629. p.Value = 2;
  630. command.Parameters.Add(p);
  631. p = new NpgsqlParameter("b", DbType.Int32);
  632. p.Value = 2;
  633. command.Parameters.Add(p);
  634. p = new NpgsqlParameter("c", DbType.Int64);
  635. p.Value = 2;
  636. command.Parameters.Add(p);
  637. p = new NpgsqlParameter("d", DbType.String);
  638. p.Value = "a";
  639. command.Parameters.Add(p);
  640. p = new NpgsqlParameter("e", DbType.String);
  641. p.Value = "a";
  642. command.Parameters.Add(p);
  643. p = new NpgsqlParameter("f", DbType.String);
  644. p.Value = "a";
  645. command.Parameters.Add(p);
  646. command.Connection.Open();
  647. command.Prepare();
  648. command.ExecuteScalar();
  649. command.Connection.Close();
  650. }
  651. [Test]
  652. public void TestParameterReplace()
  653. {
  654. _conn.Open();
  655. String sql = @"select * from tablea where
  656. field_serial = :a
  657. ";
  658. NpgsqlCommand command = new NpgsqlCommand(sql, _conn);
  659. command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
  660. command.Parameters[0].Value = 2;
  661. Int32 rowsAdded = command.ExecuteNonQuery();
  662. }
  663. [Test]
  664. public void TestPointSupport()
  665. {
  666. _conn.Open();
  667. NpgsqlCommand command = new NpgsqlCommand("select field_point from tablee where field_serial = 1", _conn);
  668. NpgsqlPoint p = (NpgsqlPoint) command.ExecuteScalar();
  669. Assert.AreEqual(4, p.X);
  670. Assert.AreEqual(3, p.Y);
  671. }
  672. [Test]
  673. public void TestBoxSupport()
  674. {
  675. _conn.Open();
  676. NpgsqlCommand command = new NpgsqlCommand("select field_box from tablee where field_serial = 2", _conn);
  677. NpgsqlBox box = (NpgsqlBox) command.ExecuteScalar();
  678. Assert.AreEqual(5, box.UpperRight.X);
  679. Assert.AreEqual(4, box.UpperRight.Y);
  680. Assert.AreEqual(4, box.LowerLeft.X);
  681. Assert.AreEqual(3, box.LowerLeft.Y);
  682. }
  683. [Test]
  684. public void TestLSegSupport()
  685. {
  686. _conn.Open();
  687. NpgsqlCommand command = new NpgsqlCommand("select field_lseg from tablee where field_serial = 3", _conn);
  688. NpgsqlLSeg lseg = (NpgsqlLSeg) command.ExecuteScalar();
  689. Assert.AreEqual(4, lseg.Start.X);
  690. Assert.AreEqual(3, lseg.Start.Y);
  691. Assert.AreEqual(5, lseg.End.X);
  692. Assert.AreEqual(4, lseg.End.Y);
  693. }
  694. [Test]
  695. public void TestClosedPathSupport()
  696. {
  697. _conn.Open();
  698. NpgsqlCommand command = new NpgsqlCommand("select field_path from tablee where field_serial = 4", _conn);
  699. NpgsqlPath path = (NpgsqlPath) command.ExecuteScalar();
  700. Assert.AreEqual(false, path.Open);
  701. Assert.AreEqual(2, path.Count);
  702. Assert.AreEqual(4, path[0].X);
  703. Assert.AreEqual(3, path[0].Y);
  704. Assert.AreEqual(5, path[1].X);
  705. Assert.AreEqual(4, path[1].Y);
  706. }
  707. [Test]
  708. public void TestOpenPathSupport()
  709. {
  710. _conn.Open();
  711. NpgsqlCommand command = new NpgsqlCommand("select field_path from tablee where field_serial = 5", _conn);
  712. NpgsqlPath path = (NpgsqlPath) command.ExecuteScalar();
  713. Assert.AreEqual(true, path.Open);
  714. Assert.AreEqual(2, path.Count);
  715. Assert.AreEqual(4, path[0].X);
  716. Assert.AreEqual(3, path[0].Y);
  717. Assert.AreEqual(5, path[1].X);
  718. Assert.AreEqual(4, path[1].Y);
  719. }
  720. [Test]
  721. public void TestPolygonSupport()
  722. {
  723. _conn.Open();
  724. NpgsqlCommand command = new NpgsqlCommand("select field_polygon from tablee where field_serial = 6", _conn);
  725. NpgsqlPolygon polygon = (NpgsqlPolygon) command.ExecuteScalar();
  726. Assert.AreEqual(2, polygon.Count);
  727. Assert.AreEqual(4, polygon[0].X);
  728. Assert.AreEqual(3, polygon[0].Y);
  729. Assert.AreEqual(5, polygon[1].X);
  730. Assert.AreEqual(4, polygon[1].Y);
  731. }
  732. [Test]
  733. public void TestCircleSupport()
  734. {
  735. _conn.Open();
  736. NpgsqlCommand command = new NpgsqlCommand("select field_circle from tablee where field_serial = 7", _conn);
  737. NpgsqlCircle circle = (NpgsqlCircle) command.ExecuteScalar();
  738. Assert.AreEqual(4, circle.Center.X);
  739. Assert.AreEqual(3, circle.Center.Y);
  740. Assert.AreEqual(5, circle.Radius);
  741. }
  742. }
  743. }