CommandTests.cs 31 KB

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