**** Merged from MCS ****
[mono-project.git] / mcs / class / Npgsql / Test / CommandTests.cs
blob771d7255a5368f0ee85d05afc8ab10bafeb529dd
1 // created on 30/11/2002 at 22:35
2 //
3 // Author:
4 // Francisco Figueiredo Jr. <fxjrlists@yahoo.com>
5 //
6 // Copyright (C) 2002 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
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
24 using System;
25 using Npgsql;
26 using NUnit.Framework;
27 using NUnit.Core;
28 using System.Data;
29 using System.Globalization;
30 using NpgsqlTypes;
33 namespace NpgsqlTests
36 [TestFixture]
37 public class CommandTests
39 private NpgsqlConnection _conn = null;
40 private String _connString = "Server=localhost;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests;maxpoolsize=2";
42 [SetUp]
43 protected void SetUp()
45 //NpgsqlEventLog.Level = LogLevel.None;
46 //NpgsqlEventLog.Level = LogLevel.Debug;
47 //NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
48 _conn = new NpgsqlConnection(_connString);
51 [TearDown]
52 protected void TearDown()
54 if (_conn.State != ConnectionState.Closed)
55 _conn.Close();
59 [Test]
60 public void ParametersGetName()
62 NpgsqlCommand command = new NpgsqlCommand();
64 // Add parameters.
65 command.Parameters.Add(new NpgsqlParameter(":Parameter1", DbType.Boolean));
66 command.Parameters.Add(new NpgsqlParameter(":Parameter2", DbType.Int32));
67 command.Parameters.Add(new NpgsqlParameter(":Parameter3", DbType.DateTime));
70 // Get by indexers.
72 Assert.AreEqual(":Parameter1", command.Parameters[":Parameter1"].ParameterName);
73 Assert.AreEqual(":Parameter2", command.Parameters[":Parameter2"].ParameterName);
74 Assert.AreEqual(":Parameter3", command.Parameters[":Parameter3"].ParameterName);
77 Assert.AreEqual(":Parameter1", command.Parameters[0].ParameterName);
78 Assert.AreEqual(":Parameter2", command.Parameters[1].ParameterName);
79 Assert.AreEqual(":Parameter3", command.Parameters[2].ParameterName);
85 [Test]
86 public void EmptyQuery()
88 _conn.Open();
90 NpgsqlCommand command = new NpgsqlCommand(";", _conn);
91 command.ExecuteNonQuery();
94 [Test]
95 [ExpectedException(typeof(ArgumentNullException))]
96 public void NoNameParameterAdd()
98 NpgsqlCommand command = new NpgsqlCommand();
100 command.Parameters.Add(new NpgsqlParameter());
104 [Test]
105 public void FunctionCallFromSelect()
107 _conn.Open();
109 NpgsqlCommand command = new NpgsqlCommand("select * from funcB()", _conn);
111 NpgsqlDataReader reader = command.ExecuteReader();
113 Assertion.AssertNotNull(reader);
114 //reader.FieldCount
118 [Test]
119 public void ExecuteScalar()
121 _conn.Open();
123 NpgsqlCommand command = new NpgsqlCommand("select count(*) from tablea", _conn);
125 Object result = command.ExecuteScalar();
127 Assert.AreEqual(5, result);
128 //reader.FieldCount
132 [Test]
133 public void FunctionCallReturnSingleValue()
135 _conn.Open();
137 NpgsqlCommand command = new NpgsqlCommand("funcC()", _conn);
138 command.CommandType = CommandType.StoredProcedure;
140 Object result = command.ExecuteScalar();
142 Assert.AreEqual(5, result);
143 //reader.FieldCount
148 [Test]
149 public void FunctionCallReturnSingleValueWithPrepare()
151 _conn.Open();
153 NpgsqlCommand command = new NpgsqlCommand("funcC()", _conn);
154 command.CommandType = CommandType.StoredProcedure;
156 command.Prepare();
157 Object result = command.ExecuteScalar();
159 Assert.AreEqual(5, result);
160 //reader.FieldCount
164 [Test]
165 public void FunctionCallWithParametersReturnSingleValue()
167 _conn.Open();
169 NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
170 command.CommandType = CommandType.StoredProcedure;
172 command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
174 command.Parameters[0].Value = 4;
176 Int64 result = (Int64) command.ExecuteScalar();
178 Assert.AreEqual(1, result);
183 [Test]
184 public void FunctionCallWithParametersReturnSingleValueNpgsqlDbType()
186 _conn.Open();
188 NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
189 command.CommandType = CommandType.StoredProcedure;
191 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
193 command.Parameters[0].Value = 4;
195 Int64 result = (Int64) command.ExecuteScalar();
197 Assert.AreEqual(1, result);
204 [Test]
205 public void FunctionCallWithParametersPrepareReturnSingleValue()
207 _conn.Open();
209 NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
210 command.CommandType = CommandType.StoredProcedure;
213 command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
215 Assert.AreEqual(1, command.Parameters.Count);
216 command.Prepare();
219 command.Parameters[0].Value = 4;
221 Int64 result = (Int64) command.ExecuteScalar();
223 Assert.AreEqual(1, result);
228 [Test]
229 public void FunctionCallWithParametersPrepareReturnSingleValueNpgsqlDbType()
231 _conn.Open();
233 NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
234 command.CommandType = CommandType.StoredProcedure;
237 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
239 Assert.AreEqual(1, command.Parameters.Count);
240 command.Prepare();
243 command.Parameters[0].Value = 4;
245 Int64 result = (Int64) command.ExecuteScalar();
247 Assert.AreEqual(1, result);
253 [Test]
254 public void FunctionCallReturnResultSet()
256 _conn.Open();
258 NpgsqlCommand command = new NpgsqlCommand("funcB()", _conn);
259 command.CommandType = CommandType.StoredProcedure;
261 NpgsqlDataReader dr = command.ExecuteReader();
269 [Test]
270 public void CursorStatement()
273 _conn.Open();
275 Int32 i = 0;
277 NpgsqlTransaction t = _conn.BeginTransaction();
279 NpgsqlCommand command = new NpgsqlCommand("declare te cursor for select * from tablea;", _conn);
281 command.ExecuteNonQuery();
283 command.CommandText = "fetch forward 3 in te;";
285 NpgsqlDataReader dr = command.ExecuteReader();
288 while (dr.Read())
290 i++;
293 Assert.AreEqual(3, i);
296 i = 0;
298 command.CommandText = "fetch backward 1 in te;";
300 NpgsqlDataReader dr2 = command.ExecuteReader();
302 while (dr2.Read())
304 i++;
307 Assert.AreEqual(1, i);
309 command.CommandText = "close te;";
311 command.ExecuteNonQuery();
313 t.Commit();
319 [Test]
320 public void PreparedStatementNoParameters()
322 _conn.Open();
324 NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
326 command.Prepare();
328 command.Prepare();
330 NpgsqlDataReader dr = command.ExecuteReader();
335 [Test]
336 public void PreparedStatementWithParameters()
338 _conn.Open();
340 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_int4 = :a and field_int8 = :b;", _conn);
342 command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
343 command.Parameters.Add(new NpgsqlParameter("b", DbType.Int64));
345 Assert.AreEqual(2, command.Parameters.Count);
347 Assert.AreEqual(DbType.Int32, command.Parameters[0].DbType);
349 command.Prepare();
351 command.Parameters[0].Value = 3;
352 command.Parameters[1].Value = 5;
354 NpgsqlDataReader dr = command.ExecuteReader();
361 [Test]
362 public void PreparedStatementWithParametersNpgsqlDbType()
364 _conn.Open();
366 NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_int4 = :a and field_int8 = :b;", _conn);
368 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
369 command.Parameters.Add(new NpgsqlParameter("b", NpgsqlDbType.Bigint));
371 Assert.AreEqual(2, command.Parameters.Count);
373 Assert.AreEqual(DbType.Int32, command.Parameters[0].DbType);
375 command.Prepare();
377 command.Parameters[0].Value = 3;
378 command.Parameters[1].Value = 5;
380 NpgsqlDataReader dr = command.ExecuteReader();
388 [Test]
389 [ExpectedException(typeof(InvalidOperationException))]
390 public void ListenNotifySupport()
393 _conn.Open();
395 NpgsqlCommand command = new NpgsqlCommand("listen notifytest;", _conn);
396 command.ExecuteNonQuery();
398 _conn.Notification += new NotificationEventHandler(NotificationSupportHelper);
401 command = new NpgsqlCommand("notify notifytest;", _conn);
402 command.ExecuteNonQuery();
408 private void NotificationSupportHelper(Object sender, NpgsqlNotificationEventArgs args)
410 throw new InvalidOperationException();
413 [Test]
414 public void DateTimeSupport()
416 _conn.Open();
419 NpgsqlCommand command = new NpgsqlCommand("select field_timestamp from tableb where field_serial = 2;", _conn);
421 DateTime d = (DateTime)command.ExecuteScalar();
424 Assert.AreEqual("2002-02-02 09:00:23Z", d.ToString("u"));
426 DateTimeFormatInfo culture = new DateTimeFormatInfo();
427 culture.TimeSeparator = ":";
428 DateTime dt = System.DateTime.Parse("2004-06-04 09:48:00", culture);
430 command.CommandText = "insert into tableb(field_timestamp) values (:a);delete from tableb where field_serial > 4;";
431 command.Parameters.Add(new NpgsqlParameter("a", DbType.DateTime));
432 command.Parameters[0].Value = dt;
434 command.ExecuteScalar();
439 [Test]
440 public void DateTimeSupportNpgsqlDbType()
442 _conn.Open();
445 NpgsqlCommand command = new NpgsqlCommand("select field_timestamp from tableb where field_serial = 2;", _conn);
447 DateTime d = (DateTime)command.ExecuteScalar();
450 Assert.AreEqual("2002-02-02 09:00:23Z", d.ToString("u"));
452 DateTimeFormatInfo culture = new DateTimeFormatInfo();
453 culture.TimeSeparator = ":";
454 DateTime dt = System.DateTime.Parse("2004-06-04 09:48:00", culture);
456 command.CommandText = "insert into tableb(field_timestamp) values (:a);delete from tableb where field_serial > 4;";
457 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Timestamp));
458 command.Parameters[0].Value = dt;
460 command.ExecuteScalar();
464 [Test]
465 public void DateSupport()
467 _conn.Open();
469 NpgsqlCommand command = new NpgsqlCommand("select field_date from tablec where field_serial = 1;", _conn);
471 DateTime d = (DateTime)command.ExecuteScalar();
474 Assert.AreEqual("2002-03-04", d.ToString("yyyy-MM-dd"));
478 [Test]
479 public void TimeSupport()
481 _conn.Open();
483 NpgsqlCommand command = new NpgsqlCommand("select field_time from tablec where field_serial = 2;", _conn);
485 DateTime d = (DateTime)command.ExecuteScalar();
488 Assert.AreEqual("10:03:45.345", d.ToString("HH:mm:ss.fff"));
492 [Test]
493 public void NumericSupport()
495 _conn.Open();
498 NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
499 command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
501 command.Parameters[0].Value = 7.4M;
503 Int32 rowsAdded = command.ExecuteNonQuery();
505 Assert.AreEqual(1, rowsAdded);
507 command.CommandText = "select * from tableb where field_numeric = :a";
510 NpgsqlDataReader dr = command.ExecuteReader();
511 dr.Read();
513 Decimal result = dr.GetDecimal(3);
516 command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
517 command.Parameters.Clear();
518 command.ExecuteNonQuery();
521 Assert.AreEqual(7.4000000M, result);
528 [Test]
529 public void NumericSupportNpgsqlDbType()
531 _conn.Open();
534 NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
535 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Numeric));
537 command.Parameters[0].Value = 7.4M;
539 Int32 rowsAdded = command.ExecuteNonQuery();
541 Assert.AreEqual(1, rowsAdded);
543 command.CommandText = "select * from tableb where field_numeric = :a";
546 NpgsqlDataReader dr = command.ExecuteReader();
547 dr.Read();
549 Decimal result = dr.GetDecimal(3);
552 command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
553 command.Parameters.Clear();
554 command.ExecuteNonQuery();
557 Assert.AreEqual(7.4000000M, result);
565 [Test]
566 public void InsertSingleValue()
568 _conn.Open();
571 NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float4) values (:a)", _conn);
572 command.Parameters.Add(new NpgsqlParameter(":a", DbType.Single));
574 command.Parameters[0].Value = 7.4F;
576 Int32 rowsAdded = command.ExecuteNonQuery();
578 Assert.AreEqual(1, rowsAdded);
580 command.CommandText = "select * from tabled where field_float4 = :a";
583 NpgsqlDataReader dr = command.ExecuteReader();
584 dr.Read();
586 Single result = dr.GetFloat(1);
589 command.CommandText = "delete from tabled where field_serial > 2;";
590 command.Parameters.Clear();
591 command.ExecuteNonQuery();
594 Assert.AreEqual(7.4F, result);
599 [Test]
600 public void InsertSingleValueNpgsqlDbType()
602 _conn.Open();
605 NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float4) values (:a)", _conn);
606 command.Parameters.Add(new NpgsqlParameter(":a", NpgsqlDbType.Real));
608 command.Parameters[0].Value = 7.4F;
610 Int32 rowsAdded = command.ExecuteNonQuery();
612 Assert.AreEqual(1, rowsAdded);
614 command.CommandText = "select * from tabled where field_float4 = :a";
617 NpgsqlDataReader dr = command.ExecuteReader();
618 dr.Read();
620 Single result = dr.GetFloat(1);
623 command.CommandText = "delete from tabled where field_serial > 2;";
624 command.Parameters.Clear();
625 command.ExecuteNonQuery();
628 Assert.AreEqual(7.4F, result);
632 [Test]
633 public void InsertDoubleValue()
635 _conn.Open();
638 NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float8) values (:a)", _conn);
639 command.Parameters.Add(new NpgsqlParameter(":a", DbType.Double));
641 command.Parameters[0].Value = 7.4D;
643 Int32 rowsAdded = command.ExecuteNonQuery();
645 Assert.AreEqual(1, rowsAdded);
647 command.CommandText = "select * from tabled where field_float8 = :a";
650 NpgsqlDataReader dr = command.ExecuteReader();
651 dr.Read();
653 Double result = dr.GetDouble(2);
656 command.CommandText = "delete from tabled where field_serial > 2;";
657 command.Parameters.Clear();
658 //command.ExecuteNonQuery();
661 Assert.AreEqual(7.4D, result);
666 [Test]
667 public void InsertDoubleValueNpgsqlDbType()
669 _conn.Open();
672 NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float8) values (:a)", _conn);
673 command.Parameters.Add(new NpgsqlParameter(":a", NpgsqlDbType.Double));
675 command.Parameters[0].Value = 7.4D;
677 Int32 rowsAdded = command.ExecuteNonQuery();
679 Assert.AreEqual(1, rowsAdded);
681 command.CommandText = "select * from tabled where field_float8 = :a";
684 NpgsqlDataReader dr = command.ExecuteReader();
685 dr.Read();
687 Double result = dr.GetDouble(2);
690 command.CommandText = "delete from tabled where field_serial > 2;";
691 command.Parameters.Clear();
692 //command.ExecuteNonQuery();
695 Assert.AreEqual(7.4D, result);
700 [Test]
701 public void NegativeNumericSupport()
703 _conn.Open();
706 NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
709 NpgsqlDataReader dr = command.ExecuteReader();
710 dr.Read();
712 Decimal result = dr.GetDecimal(3);
714 Assert.AreEqual(-4.3000000M, result);
719 [Test]
720 public void PrecisionScaleNumericSupport()
722 _conn.Open();
725 NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
728 NpgsqlDataReader dr = command.ExecuteReader();
729 dr.Read();
731 Decimal result = dr.GetDecimal(3);
733 Assert.AreEqual(-4.3000000M, (Decimal)result);
734 //Assert.AreEqual(11, result.Precision);
735 //Assert.AreEqual(7, result.Scale);
739 [Test]
740 public void InsertNullString()
742 _conn.Open();
744 NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
746 command.Parameters.Add(new NpgsqlParameter("a", DbType.String));
748 command.Parameters[0].Value = DBNull.Value;
750 Int32 rowsAdded = command.ExecuteNonQuery();
752 Assert.AreEqual(1, rowsAdded);
754 command.CommandText = "select count(*) from tablea where field_text is null";
755 command.Parameters.Clear();
757 Int64 result = (Int64)command.ExecuteScalar();
759 command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea) and field_serial != 4;";
760 command.ExecuteNonQuery();
762 Assert.AreEqual(4, result);
768 [Test]
769 public void InsertNullStringNpgsqlDbType()
771 _conn.Open();
773 NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
775 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Text));
777 command.Parameters[0].Value = DBNull.Value;
779 Int32 rowsAdded = command.ExecuteNonQuery();
781 Assert.AreEqual(1, rowsAdded);
783 command.CommandText = "select count(*) from tablea where field_text is null";
784 command.Parameters.Clear();
786 Int64 result = (Int64)command.ExecuteScalar();
788 command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea) and field_serial != 4;";
789 command.ExecuteNonQuery();
791 Assert.AreEqual(4, result);
799 [Test]
800 public void InsertNullDateTime()
802 _conn.Open();
804 NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_timestamp) values (:a)", _conn);
806 command.Parameters.Add(new NpgsqlParameter("a", DbType.DateTime));
808 command.Parameters[0].Value = DBNull.Value;
810 Int32 rowsAdded = command.ExecuteNonQuery();
812 Assert.AreEqual(1, rowsAdded);
814 command.CommandText = "select count(*) from tableb where field_timestamp is null";
815 command.Parameters.Clear();
817 Object result = command.ExecuteScalar();
819 command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
820 command.ExecuteNonQuery();
822 Assert.AreEqual(4, result);
829 [Test]
830 public void InsertNullDateTimeNpgsqlDbType()
832 _conn.Open();
834 NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_timestamp) values (:a)", _conn);
836 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Timestamp));
838 command.Parameters[0].Value = DBNull.Value;
840 Int32 rowsAdded = command.ExecuteNonQuery();
842 Assert.AreEqual(1, rowsAdded);
844 command.CommandText = "select count(*) from tableb where field_timestamp is null";
845 command.Parameters.Clear();
847 Object result = command.ExecuteScalar();
849 command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
850 command.ExecuteNonQuery();
852 Assert.AreEqual(4, result);
860 [Test]
861 public void InsertNullInt16()
863 _conn.Open();
866 NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_int2) values (:a)", _conn);
868 command.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
870 command.Parameters[0].Value = DBNull.Value;
872 Int32 rowsAdded = command.ExecuteNonQuery();
874 Assert.AreEqual(1, rowsAdded);
876 command.CommandText = "select count(*) from tableb where field_int2 is null";
877 command.Parameters.Clear();
879 Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
881 command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
882 command.ExecuteNonQuery();
884 Assert.AreEqual(4, result);
890 [Test]
891 public void InsertNullInt16NpgsqlDbType()
893 _conn.Open();
896 NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_int2) values (:a)", _conn);
898 command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Smallint));
900 command.Parameters[0].Value = DBNull.Value;
902 Int32 rowsAdded = command.ExecuteNonQuery();
904 Assert.AreEqual(1, rowsAdded);
906 command.CommandText = "select count(*) from tableb where field_int2 is null";
907 command.Parameters.Clear();
909 Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
911 command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
912 command.ExecuteNonQuery();
914 Assert.AreEqual(4, result);
920 [Test]
921 public void InsertNullInt32()
923 _conn.Open();
926 NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_int4) values (:a)", _conn);
928 command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
930 command.Parameters[0].Value = DBNull.Value;
932 Int32 rowsAdded = command.ExecuteNonQuery();
934 Assert.AreEqual(1, rowsAdded);
936 command.CommandText = "select count(*) from tablea where field_int4 is null";
937 command.Parameters.Clear();
939 Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
941 command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
942 command.ExecuteNonQuery();
944 Assert.AreEqual(5, result);
949 [Test]
950 public void InsertNullNumeric()
952 _conn.Open();
955 NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
957 command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
959 command.Parameters[0].Value = DBNull.Value;
961 Int32 rowsAdded = command.ExecuteNonQuery();
963 Assert.AreEqual(1, rowsAdded);
965 command.CommandText = "select count(*) from tableb where field_numeric is null";
966 command.Parameters.Clear();
968 Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
970 command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
971 command.ExecuteNonQuery();
973 Assert.AreEqual(3, result);
977 [Test]
978 public void InsertNullBoolean()
980 _conn.Open();
983 NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_bool) values (:a)", _conn);
985 command.Parameters.Add(new NpgsqlParameter("a", DbType.Boolean));
987 command.Parameters[0].Value = DBNull.Value;
989 Int32 rowsAdded = command.ExecuteNonQuery();
991 Assert.AreEqual(1, rowsAdded);
993 command.CommandText = "select count(*) from tablea where field_bool is null";
994 command.Parameters.Clear();
996 Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
998 command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
999 command.ExecuteNonQuery();
1001 Assert.AreEqual(5, result);
1005 [Test]
1006 public void AnsiStringSupport()
1008 _conn.Open();
1010 NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
1012 command.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiString));
1014 command.Parameters[0].Value = "TesteAnsiString";
1016 Int32 rowsAdded = command.ExecuteNonQuery();
1018 Assert.AreEqual(1, rowsAdded);
1020 command.CommandText = String.Format("select count(*) from tablea where field_text = '{0}'", command.Parameters[0].Value);
1021 command.Parameters.Clear();
1023 Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
1025 command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
1026 command.ExecuteNonQuery();
1028 Assert.AreEqual(1, result);
1033 [Test]
1034 public void MultipleQueriesFirstResultsetEmpty()
1036 _conn.Open();
1038 NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values ('a'); select count(*) from tablea;", _conn);
1040 Object result = command.ExecuteScalar();
1043 command.CommandText = "delete from tablea where field_serial > 5";
1044 command.ExecuteNonQuery();
1046 command.CommandText = "select * from tablea where field_serial = 0";
1047 command.ExecuteScalar();
1050 Assert.AreEqual(6, result);
1055 [Test]
1056 [ExpectedException(typeof(NpgsqlException))]
1057 public void ConnectionStringWithInvalidParameters()
1059 NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests;Password=j");
1061 NpgsqlCommand command = new NpgsqlCommand("select * from tablea", conn);
1063 command.Connection.Open();
1064 command.ExecuteReader();
1065 command.Connection.Close();
1070 [Test]
1071 [ExpectedException(typeof(NpgsqlException))]
1072 public void InvalidConnectionString()
1074 NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests");
1076 NpgsqlCommand command = new NpgsqlCommand("select * from tablea", conn);
1078 command.Connection.Open();
1079 command.ExecuteReader();
1080 command.Connection.Close();
1086 [Test]
1087 public void AmbiguousFunctionParameterType()
1089 NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests;Password=npgsql_tests");
1092 NpgsqlCommand command = new NpgsqlCommand("ambiguousParameterType(:a, :b, :c, :d, :e, :f)", conn);
1093 command.CommandType = CommandType.StoredProcedure;
1094 NpgsqlParameter p = new NpgsqlParameter("a", DbType.Int16);
1095 p.Value = 2;
1096 command.Parameters.Add(p);
1097 p = new NpgsqlParameter("b", DbType.Int32);
1098 p.Value = 2;
1099 command.Parameters.Add(p);
1100 p = new NpgsqlParameter("c", DbType.Int64);
1101 p.Value = 2;
1102 command.Parameters.Add(p);
1103 p = new NpgsqlParameter("d", DbType.String);
1104 p.Value = "a";
1105 command.Parameters.Add(p);
1106 p = new NpgsqlParameter("e", DbType.String);
1107 p.Value = "a";
1108 command.Parameters.Add(p);
1109 p = new NpgsqlParameter("f", DbType.String);
1110 p.Value = "a";
1111 command.Parameters.Add(p);
1114 command.Connection.Open();
1115 command.Prepare();
1116 command.ExecuteScalar();
1117 command.Connection.Close();
1123 [Test]
1124 public void TestParameterReplace()
1126 _conn.Open();
1128 String sql = @"select * from tablea where
1129 field_serial = :a
1133 NpgsqlCommand command = new NpgsqlCommand(sql, _conn);
1135 command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
1137 command.Parameters[0].Value = 2;
1139 Int32 rowsAdded = command.ExecuteNonQuery();
1143 [Test]
1144 public void TestPointSupport()
1147 _conn.Open();
1149 NpgsqlCommand command = new NpgsqlCommand("select field_point from tablee where field_serial = 1", _conn);
1151 NpgsqlPoint p = (NpgsqlPoint) command.ExecuteScalar();
1153 Assert.AreEqual(4, p.X);
1154 Assert.AreEqual(3, p.Y);
1158 [Test]
1159 public void TestBoxSupport()
1162 _conn.Open();
1164 NpgsqlCommand command = new NpgsqlCommand("select field_box from tablee where field_serial = 2", _conn);
1166 NpgsqlBox box = (NpgsqlBox) command.ExecuteScalar();
1168 Assert.AreEqual(5, box.UpperRight.X);
1169 Assert.AreEqual(4, box.UpperRight.Y);
1170 Assert.AreEqual(4, box.LowerLeft.X);
1171 Assert.AreEqual(3, box.LowerLeft.Y);
1176 [Test]
1177 public void TestLSegSupport()
1180 _conn.Open();
1182 NpgsqlCommand command = new NpgsqlCommand("select field_lseg from tablee where field_serial = 3", _conn);
1184 NpgsqlLSeg lseg = (NpgsqlLSeg) command.ExecuteScalar();
1186 Assert.AreEqual(4, lseg.Start.X);
1187 Assert.AreEqual(3, lseg.Start.Y);
1188 Assert.AreEqual(5, lseg.End.X);
1189 Assert.AreEqual(4, lseg.End.Y);
1194 [Test]
1195 public void TestClosedPathSupport()
1198 _conn.Open();
1200 NpgsqlCommand command = new NpgsqlCommand("select field_path from tablee where field_serial = 4", _conn);
1202 NpgsqlPath path = (NpgsqlPath) command.ExecuteScalar();
1204 Assert.AreEqual(false, path.Open);
1205 Assert.AreEqual(2, path.Count);
1206 Assert.AreEqual(4, path[0].X);
1207 Assert.AreEqual(3, path[0].Y);
1208 Assert.AreEqual(5, path[1].X);
1209 Assert.AreEqual(4, path[1].Y);
1214 [Test]
1215 public void TestOpenPathSupport()
1218 _conn.Open();
1220 NpgsqlCommand command = new NpgsqlCommand("select field_path from tablee where field_serial = 5", _conn);
1222 NpgsqlPath path = (NpgsqlPath) command.ExecuteScalar();
1224 Assert.AreEqual(true, path.Open);
1225 Assert.AreEqual(2, path.Count);
1226 Assert.AreEqual(4, path[0].X);
1227 Assert.AreEqual(3, path[0].Y);
1228 Assert.AreEqual(5, path[1].X);
1229 Assert.AreEqual(4, path[1].Y);
1236 [Test]
1237 public void TestPolygonSupport()
1240 _conn.Open();
1242 NpgsqlCommand command = new NpgsqlCommand("select field_polygon from tablee where field_serial = 6", _conn);
1244 NpgsqlPolygon polygon = (NpgsqlPolygon) command.ExecuteScalar();
1246 Assert.AreEqual(2, polygon.Count);
1247 Assert.AreEqual(4, polygon[0].X);
1248 Assert.AreEqual(3, polygon[0].Y);
1249 Assert.AreEqual(5, polygon[1].X);
1250 Assert.AreEqual(4, polygon[1].Y);
1256 [Test]
1257 public void TestCircleSupport()
1260 _conn.Open();
1262 NpgsqlCommand command = new NpgsqlCommand("select field_circle from tablee where field_serial = 7", _conn);
1264 NpgsqlCircle circle = (NpgsqlCircle) command.ExecuteScalar();
1266 Assert.AreEqual(4, circle.Center.X);
1267 Assert.AreEqual(3, circle.Center.Y);
1268 Assert.AreEqual(5, circle.Radius);