| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278 |
- // created on 30/11/2002 at 22:35
- //
- // Author:
- // Francisco Figueiredo Jr. <[email protected]>
- //
- // Copyright (C) 2002 The Npgsql Development Team
- // [email protected]
- // http://gborg.postgresql.org/project/npgsql/projdisplay.php
- //
- // This library is free software; you can redistribute it and/or
- // modify it under the terms of the GNU Lesser General Public
- // License as published by the Free Software Foundation; either
- // version 2.1 of the License, or (at your option) any later version.
- //
- // This library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public
- // License along with this library; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- using System;
- using Npgsql;
- using NUnit.Framework;
- using NUnit.Core;
- using System.Data;
- using System.Globalization;
- using NpgsqlTypes;
- namespace NpgsqlTests
- {
- [TestFixture]
- public class CommandTests
- {
- NpgsqlConnection _conn = null;
-
- [SetUp]
- protected void SetUp()
- {
- //NpgsqlEventLog.Level = LogLevel.None;
- //NpgsqlEventLog.Level = LogLevel.Debug;
- //NpgsqlEventLog.LogName = "NpgsqlTests.LogFile";
- _conn = new NpgsqlConnection (TestConfiguration.NpgsqlConnectionString);
- }
-
- [TearDown]
- protected void TearDown()
- {
- if (_conn != null && _conn.State != ConnectionState.Closed)
- _conn.Close();
- }
-
-
- [Test]
- public void ParametersGetName()
- {
- NpgsqlCommand command = new NpgsqlCommand();
-
- // Add parameters.
- command.Parameters.Add(new NpgsqlParameter(":Parameter1", DbType.Boolean));
- command.Parameters.Add(new NpgsqlParameter(":Parameter2", DbType.Int32));
- command.Parameters.Add(new NpgsqlParameter(":Parameter3", DbType.DateTime));
-
-
- // Get by indexers.
-
- Assert.AreEqual(":Parameter1", command.Parameters[":Parameter1"].ParameterName);
- Assert.AreEqual(":Parameter2", command.Parameters[":Parameter2"].ParameterName);
- Assert.AreEqual(":Parameter3", command.Parameters[":Parameter3"].ParameterName);
-
- Assert.AreEqual(":Parameter1", command.Parameters[0].ParameterName);
- Assert.AreEqual(":Parameter2", command.Parameters[1].ParameterName);
- Assert.AreEqual(":Parameter3", command.Parameters[2].ParameterName);
-
-
-
- }
-
- [Test]
- public void EmptyQuery()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand(";", _conn);
- command.ExecuteNonQuery();
-
- }
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void NoNameParameterAdd()
- {
- NpgsqlCommand command = new NpgsqlCommand();
-
- command.Parameters.Add(new NpgsqlParameter());
-
- }
-
- [Test]
- public void FunctionCallFromSelect()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select * from funcB()", _conn);
-
- NpgsqlDataReader reader = command.ExecuteReader();
-
- Assertion.AssertNotNull(reader);
- //reader.FieldCount
-
- }
-
- [Test]
- public void ExecuteScalar()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select count(*) from tablea", _conn);
-
- Object result = command.ExecuteScalar();
-
- Assert.AreEqual(5, result);
- //reader.FieldCount
-
- }
-
- [Test]
- public void FunctionCallReturnSingleValue()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("funcC()", _conn);
- command.CommandType = CommandType.StoredProcedure;
-
- Object result = command.ExecuteScalar();
-
- Assert.AreEqual(5, result);
- //reader.FieldCount
-
- }
-
-
- [Test]
- public void FunctionCallReturnSingleValueWithPrepare()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("funcC()", _conn);
- command.CommandType = CommandType.StoredProcedure;
-
- command.Prepare();
- Object result = command.ExecuteScalar();
-
- Assert.AreEqual(5, result);
- //reader.FieldCount
-
- }
-
- [Test]
- public void FunctionCallWithParametersReturnSingleValue()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
- command.CommandType = CommandType.StoredProcedure;
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
-
- command.Parameters[0].Value = 4;
-
- Int64 result = (Int64) command.ExecuteScalar();
-
- Assert.AreEqual(1, result);
-
-
- }
-
- [Test]
- public void FunctionCallWithParametersReturnSingleValueNpgsqlDbType()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
- command.CommandType = CommandType.StoredProcedure;
-
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
-
- command.Parameters[0].Value = 4;
-
- Int64 result = (Int64) command.ExecuteScalar();
-
- Assert.AreEqual(1, result);
-
- }
-
-
-
-
- [Test]
- public void FunctionCallWithParametersPrepareReturnSingleValue()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
- command.CommandType = CommandType.StoredProcedure;
-
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
-
- Assert.AreEqual(1, command.Parameters.Count);
- command.Prepare();
-
-
- command.Parameters[0].Value = 4;
-
- Int64 result = (Int64) command.ExecuteScalar();
-
- Assert.AreEqual(1, result);
-
-
- }
-
- [Test]
- public void FunctionCallWithParametersPrepareReturnSingleValueNpgsqlDbType()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("funcC(:a)", _conn);
- command.CommandType = CommandType.StoredProcedure;
-
-
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
-
- Assert.AreEqual(1, command.Parameters.Count);
- command.Prepare();
-
-
- command.Parameters[0].Value = 4;
-
- Int64 result = (Int64) command.ExecuteScalar();
-
- Assert.AreEqual(1, result);
-
-
- }
-
-
- [Test]
- public void FunctionCallReturnResultSet()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("funcB()", _conn);
- command.CommandType = CommandType.StoredProcedure;
-
- NpgsqlDataReader dr = command.ExecuteReader();
-
-
-
-
- }
-
-
- [Test]
- public void CursorStatement()
- {
-
- _conn.Open();
-
- Int32 i = 0;
-
- NpgsqlTransaction t = _conn.BeginTransaction();
-
- NpgsqlCommand command = new NpgsqlCommand("declare te cursor for select * from tablea;", _conn);
-
- command.ExecuteNonQuery();
-
- command.CommandText = "fetch forward 3 in te;";
-
- NpgsqlDataReader dr = command.ExecuteReader();
-
-
- while (dr.Read())
- {
- i++;
- }
-
- Assert.AreEqual(3, i);
-
-
- i = 0;
-
- command.CommandText = "fetch backward 1 in te;";
-
- NpgsqlDataReader dr2 = command.ExecuteReader();
-
- while (dr2.Read())
- {
- i++;
- }
-
- Assert.AreEqual(1, i);
-
- command.CommandText = "close te;";
-
- command.ExecuteNonQuery();
-
- t.Commit();
-
-
-
- }
-
- [Test]
- public void PreparedStatementNoParameters()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select * from tablea;", _conn);
-
- command.Prepare();
-
- command.Prepare();
-
- NpgsqlDataReader dr = command.ExecuteReader();
-
-
- }
-
- [Test]
- public void PreparedStatementWithParameters()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_int4 = :a and field_int8 = :b;", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
- command.Parameters.Add(new NpgsqlParameter("b", DbType.Int64));
-
- Assert.AreEqual(2, command.Parameters.Count);
-
- Assert.AreEqual(DbType.Int32, command.Parameters[0].DbType);
-
- command.Prepare();
-
- command.Parameters[0].Value = 3;
- command.Parameters[1].Value = 5;
-
- NpgsqlDataReader dr = command.ExecuteReader();
-
-
-
-
- }
-
- [Test]
- public void PreparedStatementWithParametersNpgsqlDbType()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select * from tablea where field_int4 = :a and field_int8 = :b;", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Integer));
- command.Parameters.Add(new NpgsqlParameter("b", NpgsqlDbType.Bigint));
-
- Assert.AreEqual(2, command.Parameters.Count);
-
- Assert.AreEqual(DbType.Int32, command.Parameters[0].DbType);
-
- command.Prepare();
-
- command.Parameters[0].Value = 3;
- command.Parameters[1].Value = 5;
-
- NpgsqlDataReader dr = command.ExecuteReader();
-
-
-
-
- }
-
-
- [Test]
- [ExpectedException(typeof(InvalidOperationException))]
- public void ListenNotifySupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("listen notifytest;", _conn);
- command.ExecuteNonQuery();
-
- _conn.Notification += new NotificationEventHandler(NotificationSupportHelper);
-
-
- command = new NpgsqlCommand("notify notifytest;", _conn);
- command.ExecuteNonQuery();
-
-
-
- }
-
- private void NotificationSupportHelper(Object sender, NpgsqlNotificationEventArgs args)
- {
- throw new InvalidOperationException();
- }
-
- [Test]
- public void DateTimeSupport()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("select field_timestamp from tableb where field_serial = 2;", _conn);
-
- DateTime d = (DateTime)command.ExecuteScalar();
-
-
- Assert.AreEqual("2002-02-02 09:00:23Z", d.ToString("u"));
-
- DateTimeFormatInfo culture = new DateTimeFormatInfo();
- culture.TimeSeparator = ":";
- DateTime dt = System.DateTime.Parse("2004-06-04 09:48:00", culture);
- command.CommandText = "insert into tableb(field_timestamp) values (:a);delete from tableb where field_serial > 4;";
- command.Parameters.Add(new NpgsqlParameter("a", DbType.DateTime));
- command.Parameters[0].Value = dt;
-
- command.ExecuteScalar();
-
- }
-
-
- [Test]
- public void DateTimeSupportNpgsqlDbType()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("select field_timestamp from tableb where field_serial = 2;", _conn);
-
- DateTime d = (DateTime)command.ExecuteScalar();
-
-
- Assert.AreEqual("2002-02-02 09:00:23Z", d.ToString("u"));
-
- DateTimeFormatInfo culture = new DateTimeFormatInfo();
- culture.TimeSeparator = ":";
- DateTime dt = System.DateTime.Parse("2004-06-04 09:48:00", culture);
- command.CommandText = "insert into tableb(field_timestamp) values (:a);delete from tableb where field_serial > 4;";
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Timestamp));
- command.Parameters[0].Value = dt;
-
- command.ExecuteScalar();
-
- }
-
- [Test]
- public void DateSupport()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_date from tablec where field_serial = 1;", _conn);
-
- DateTime d = (DateTime)command.ExecuteScalar();
-
-
- Assert.AreEqual("2002-03-04", d.ToString("yyyy-MM-dd"));
-
- }
-
- [Test]
- public void TimeSupport()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_time from tablec where field_serial = 2;", _conn);
-
- DateTime d = (DateTime)command.ExecuteScalar();
-
-
- Assert.AreEqual("10:03:45.345", d.ToString("HH:mm:ss.fff"));
-
- }
-
- [Test]
- public void NumericSupport()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
-
- command.Parameters[0].Value = 7.4M;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select * from tableb where field_numeric = :a";
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Decimal result = dr.GetDecimal(3);
-
-
- command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
- command.Parameters.Clear();
- command.ExecuteNonQuery();
-
-
- Assert.AreEqual(7.4000000M, result);
-
-
-
-
- }
-
- [Test]
- public void NumericSupportNpgsqlDbType()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Numeric));
-
- command.Parameters[0].Value = 7.4M;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select * from tableb where field_numeric = :a";
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Decimal result = dr.GetDecimal(3);
-
-
- command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
- command.Parameters.Clear();
- command.ExecuteNonQuery();
-
-
- Assert.AreEqual(7.4000000M, result);
-
-
-
-
- }
-
-
- [Test]
- public void InsertSingleValue()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float4) values (:a)", _conn);
- command.Parameters.Add(new NpgsqlParameter(":a", DbType.Single));
-
- command.Parameters[0].Value = 7.4F;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select * from tabled where field_float4 = :a";
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Single result = dr.GetFloat(1);
-
-
- command.CommandText = "delete from tabled where field_serial > 2;";
- command.Parameters.Clear();
- command.ExecuteNonQuery();
-
-
- Assert.AreEqual(7.4F, result);
-
- }
-
-
- [Test]
- public void InsertSingleValueNpgsqlDbType()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float4) values (:a)", _conn);
- command.Parameters.Add(new NpgsqlParameter(":a", NpgsqlDbType.Real));
-
- command.Parameters[0].Value = 7.4F;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select * from tabled where field_float4 = :a";
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Single result = dr.GetFloat(1);
-
-
- command.CommandText = "delete from tabled where field_serial > 2;";
- command.Parameters.Clear();
- command.ExecuteNonQuery();
-
-
- Assert.AreEqual(7.4F, result);
-
- }
-
- [Test]
- public void InsertDoubleValue()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float8) values (:a)", _conn);
- command.Parameters.Add(new NpgsqlParameter(":a", DbType.Double));
-
- command.Parameters[0].Value = 7.4D;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select * from tabled where field_float8 = :a";
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Double result = dr.GetDouble(2);
-
-
- command.CommandText = "delete from tabled where field_serial > 2;";
- command.Parameters.Clear();
- //command.ExecuteNonQuery();
-
-
- Assert.AreEqual(7.4D, result);
-
- }
-
-
- [Test]
- public void InsertDoubleValueNpgsqlDbType()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tabled(field_float8) values (:a)", _conn);
- command.Parameters.Add(new NpgsqlParameter(":a", NpgsqlDbType.Double));
-
- command.Parameters[0].Value = 7.4D;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select * from tabled where field_float8 = :a";
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Double result = dr.GetDouble(2);
-
-
- command.CommandText = "delete from tabled where field_serial > 2;";
- command.Parameters.Clear();
- //command.ExecuteNonQuery();
-
-
- Assert.AreEqual(7.4D, result);
-
- }
-
-
- [Test]
- public void NegativeNumericSupport()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Decimal result = dr.GetDecimal(3);
-
- Assert.AreEqual(-4.3000000M, result);
-
- }
-
-
- [Test]
- public void PrecisionScaleNumericSupport()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("select * from tableb where field_serial = 4", _conn);
-
-
- NpgsqlDataReader dr = command.ExecuteReader();
- dr.Read();
-
- Decimal result = dr.GetDecimal(3);
-
- Assert.AreEqual(-4.3000000M, (Decimal)result);
- //Assert.AreEqual(11, result.Precision);
- //Assert.AreEqual(7, result.Scale);
-
- }
-
- [Test]
- public void InsertNullString()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.String));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tablea where field_text is null";
- command.Parameters.Clear();
-
- Int64 result = (Int64)command.ExecuteScalar();
-
- command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea) and field_serial != 4;";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(4, result);
-
-
-
- }
-
- [Test]
- public void InsertNullStringNpgsqlDbType()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Text));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tablea where field_text is null";
- command.Parameters.Clear();
-
- Int64 result = (Int64)command.ExecuteScalar();
-
- command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea) and field_serial != 4;";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(4, result);
-
-
-
- }
-
-
-
- [Test]
- public void InsertNullDateTime()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_timestamp) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.DateTime));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tableb where field_timestamp is null";
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar();
-
- command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(4, result);
-
-
-
- }
-
-
- [Test]
- public void InsertNullDateTimeNpgsqlDbType()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_timestamp) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Timestamp));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tableb where field_timestamp is null";
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar();
-
- command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb) and field_serial != 3;";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(4, result);
-
-
-
- }
-
-
-
- [Test]
- public void InsertNullInt16()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_int2) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Int16));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tableb where field_int2 is null";
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
-
- command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(4, result);
-
-
- }
-
-
- [Test]
- public void InsertNullInt16NpgsqlDbType()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_int2) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", NpgsqlDbType.Smallint));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tableb where field_int2 is null";
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
-
- command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(4, result);
-
-
- }
-
-
- [Test]
- public void InsertNullInt32()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_int4) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tablea where field_int4 is null";
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
-
- command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(5, result);
-
- }
-
-
- [Test]
- public void InsertNullNumeric()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tableb(field_numeric) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Decimal));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tableb where field_numeric is null";
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
-
- command.CommandText = "delete from tableb where field_serial = (select max(field_serial) from tableb);";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(3, result);
-
- }
-
- [Test]
- public void InsertNullBoolean()
- {
- _conn.Open();
-
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_bool) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Boolean));
-
- command.Parameters[0].Value = DBNull.Value;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = "select count(*) from tablea where field_bool is null";
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
-
- command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(5, result);
-
- }
-
- [Test]
- public void AnsiStringSupport()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values (:a)", _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiString));
-
- command.Parameters[0].Value = "TesteAnsiString";
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- Assert.AreEqual(1, rowsAdded);
-
- command.CommandText = String.Format("select count(*) from tablea where field_text = '{0}'", command.Parameters[0].Value);
- command.Parameters.Clear();
-
- Object result = command.ExecuteScalar(); // The missed cast is needed as Server7.2 returns Int32 and Server7.3+ returns Int64
-
- command.CommandText = "delete from tablea where field_serial = (select max(field_serial) from tablea);";
- command.ExecuteNonQuery();
-
- Assert.AreEqual(1, result);
-
- }
-
-
- [Test]
- public void MultipleQueriesFirstResultsetEmpty()
- {
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("insert into tablea(field_text) values ('a'); select count(*) from tablea;", _conn);
-
- Object result = command.ExecuteScalar();
-
-
- command.CommandText = "delete from tablea where field_serial > 5";
- command.ExecuteNonQuery();
-
- command.CommandText = "select * from tablea where field_serial = 0";
- command.ExecuteScalar();
-
-
- Assert.AreEqual(6, result);
-
-
- }
-
- [Test]
- [ExpectedException(typeof(NpgsqlException))]
- public void ConnectionStringWithInvalidParameters()
- {
- NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests;Password=j");
-
- NpgsqlCommand command = new NpgsqlCommand("select * from tablea", conn);
-
- command.Connection.Open();
- command.ExecuteReader();
- command.Connection.Close();
-
-
- }
-
- [Test]
- [ExpectedException(typeof(NpgsqlException))]
- public void InvalidConnectionString()
- {
- NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests");
-
- NpgsqlCommand command = new NpgsqlCommand("select * from tablea", conn);
-
- command.Connection.Open();
- command.ExecuteReader();
- command.Connection.Close();
-
-
- }
-
-
- [Test]
- public void AmbiguousFunctionParameterType()
- {
- NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=npgsql_tests;Password=npgsql_tests");
-
-
- NpgsqlCommand command = new NpgsqlCommand("ambiguousParameterType(:a, :b, :c, :d, :e, :f)", conn);
- command.CommandType = CommandType.StoredProcedure;
- NpgsqlParameter p = new NpgsqlParameter("a", DbType.Int16);
- p.Value = 2;
- command.Parameters.Add(p);
- p = new NpgsqlParameter("b", DbType.Int32);
- p.Value = 2;
- command.Parameters.Add(p);
- p = new NpgsqlParameter("c", DbType.Int64);
- p.Value = 2;
- command.Parameters.Add(p);
- p = new NpgsqlParameter("d", DbType.String);
- p.Value = "a";
- command.Parameters.Add(p);
- p = new NpgsqlParameter("e", DbType.String);
- p.Value = "a";
- command.Parameters.Add(p);
- p = new NpgsqlParameter("f", DbType.String);
- p.Value = "a";
- command.Parameters.Add(p);
-
-
- command.Connection.Open();
- command.Prepare();
- command.ExecuteScalar();
- command.Connection.Close();
-
-
- }
-
-
- [Test]
- public void TestParameterReplace()
- {
- _conn.Open();
-
- String sql = @"select * from tablea where
- field_serial = :a
- ";
-
-
- NpgsqlCommand command = new NpgsqlCommand(sql, _conn);
-
- command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
-
- command.Parameters[0].Value = 2;
-
- Int32 rowsAdded = command.ExecuteNonQuery();
-
- }
-
- [Test]
- public void TestPointSupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_point from tablee where field_serial = 1", _conn);
-
- NpgsqlPoint p = (NpgsqlPoint) command.ExecuteScalar();
-
- Assert.AreEqual(4, p.X);
- Assert.AreEqual(3, p.Y);
- }
-
-
- [Test]
- public void TestBoxSupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_box from tablee where field_serial = 2", _conn);
-
- NpgsqlBox box = (NpgsqlBox) command.ExecuteScalar();
-
- Assert.AreEqual(5, box.UpperRight.X);
- Assert.AreEqual(4, box.UpperRight.Y);
- Assert.AreEqual(4, box.LowerLeft.X);
- Assert.AreEqual(3, box.LowerLeft.Y);
-
-
- }
-
- [Test]
- public void TestLSegSupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_lseg from tablee where field_serial = 3", _conn);
-
- NpgsqlLSeg lseg = (NpgsqlLSeg) command.ExecuteScalar();
-
- Assert.AreEqual(4, lseg.Start.X);
- Assert.AreEqual(3, lseg.Start.Y);
- Assert.AreEqual(5, lseg.End.X);
- Assert.AreEqual(4, lseg.End.Y);
-
-
- }
-
- [Test]
- public void TestClosedPathSupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_path from tablee where field_serial = 4", _conn);
-
- NpgsqlPath path = (NpgsqlPath) command.ExecuteScalar();
-
- Assert.AreEqual(false, path.Open);
- Assert.AreEqual(2, path.Count);
- Assert.AreEqual(4, path[0].X);
- Assert.AreEqual(3, path[0].Y);
- Assert.AreEqual(5, path[1].X);
- Assert.AreEqual(4, path[1].Y);
-
-
- }
-
- [Test]
- public void TestOpenPathSupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_path from tablee where field_serial = 5", _conn);
-
- NpgsqlPath path = (NpgsqlPath) command.ExecuteScalar();
-
- Assert.AreEqual(true, path.Open);
- Assert.AreEqual(2, path.Count);
- Assert.AreEqual(4, path[0].X);
- Assert.AreEqual(3, path[0].Y);
- Assert.AreEqual(5, path[1].X);
- Assert.AreEqual(4, path[1].Y);
-
-
- }
-
-
-
- [Test]
- public void TestPolygonSupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_polygon from tablee where field_serial = 6", _conn);
-
- NpgsqlPolygon polygon = (NpgsqlPolygon) command.ExecuteScalar();
-
- Assert.AreEqual(2, polygon.Count);
- Assert.AreEqual(4, polygon[0].X);
- Assert.AreEqual(3, polygon[0].Y);
- Assert.AreEqual(5, polygon[1].X);
- Assert.AreEqual(4, polygon[1].Y);
-
-
- }
-
-
- [Test]
- public void TestCircleSupport()
- {
-
- _conn.Open();
-
- NpgsqlCommand command = new NpgsqlCommand("select field_circle from tablee where field_serial = 7", _conn);
-
- NpgsqlCircle circle = (NpgsqlCircle) command.ExecuteScalar();
-
- Assert.AreEqual(4, circle.Center.X);
- Assert.AreEqual(3, circle.Center.Y);
- Assert.AreEqual(5, circle.Radius);
-
-
-
- }
-
-
-
-
-
-
- }
- }
|