Exemplo n.º 1
0
        public void PqsqlCommandTest1()
        {
            PqsqlCommand cmd = mConnection.CreateCommand();

            cmd.CommandText = "select pg_sleep(1);";
            PqsqlDataReader r = cmd.ExecuteReader();

            bool b = r.Read();

            Assert.AreEqual(true, b);

            object v = r.GetValue(0);

            Assert.AreEqual("", v);
        }
Exemplo n.º 2
0
        public void PqsqlConnectionTest9()
        {
            PqsqlConnection connection = new PqsqlConnection(connectionString);
            string          s          = connection.GetErrorMessage();

            Assert.AreEqual(string.Empty, s);

            PqsqlCommand cmd = connection.CreateCommand();

            cmd.CommandText = "foobar command";

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (PqsqlException exception)
            {
                // SYNTAX_ERROR = 16801924, // 42601 (syntax_error)
                //Assert.AreNotSame(string.Empty, exception.Hint);
                Assert.AreEqual("42601", exception.SqlState);
                Assert.AreEqual(16801924, exception.ErrorCode);
            }

            s = connection.GetErrorMessage();
            Assert.IsNotNull(s);
            Assert.AreNotSame(string.Empty, s);
        }
Exemplo n.º 3
0
        public void PqsqlConnectionTest6()
        {
            PqsqlConnection connection = new PqsqlConnection(connectionString);

            connection.Open();
            Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state");

            PqsqlCommand cmd = connection.CreateCommand();

            Assert.AreEqual(CommandType.Text, cmd.CommandType, "wrong command type");
            Assert.AreEqual(-1, cmd.CommandTimeout, "wrong command timeout");
            Assert.AreEqual(string.Empty, cmd.CommandText, "wrong command text");
            Assert.AreEqual(connection, cmd.Connection, "wrong command connection");
            Assert.AreEqual(null, cmd.Transaction, "wrong command transaction");

            cmd.CommandText = "select pg_terminate_backend(pg_backend_pid()); select pg_sleep(5);";

            cmd.ExecuteNonQuery();             // must execute both statements

            cmd.Cancel();

            cmd.CommandText = "";

            cmd.ExecuteNonQuery();
        }
        public void PqsqlCommandBuilderTest3()
        {
            using (PqsqlConnection connection = new PqsqlConnection(connectionString))
                using (PqsqlCommand command = connection.CreateCommand())
                {
                    PqsqlTransaction transaction = connection.BeginTransaction();
                    command.Transaction = transaction;
                    command.CommandText = "create temp table temptab (c0 int4 primary key, c1 float8)";
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();
                    transaction.Commit();             // temp table must be visible in the next transaction

                    transaction = connection.BeginTransaction();

                    PqsqlDataAdapter adapter = new PqsqlDataAdapter("select * from temptab", connection)
                    {
                        SelectCommand =
                        {
                            Transaction = transaction
                        }
                    };
                    PqsqlCommandBuilder builder = new PqsqlCommandBuilder(adapter);

                    // INSERT INTO "postgres"."pg_temp_2"."temptab" ("c0", "c1") VALUES (:p1, :p2)
                    PqsqlCommand inserter = builder.GetInsertCommand();
                    inserter.Parameters["p1"].Value = 1;
                    inserter.Parameters["p2"].Value = 2.1;
                    int inserted = inserter.ExecuteNonQuery();
                    Assert.AreEqual(1, inserted);

                    // UPDATE "postgres"."pg_temp_2"."temptab"
                    // SET "c0" = :p1, "c1" = :p2
                    // WHERE (("c0" = :p3) AND ((:p4 = 1 AND "c1" IS NULL) OR ("c1" = :p5)))
                    PqsqlCommand updater = builder.GetUpdateCommand();
                    updater.Parameters["p1"].Value = 2;
                    updater.Parameters["p2"].Value = 2.2;
                    updater.Parameters["p3"].Value = 1;
                    updater.Parameters["p4"].Value = 0;
                    updater.Parameters["p5"].Value = 2.1;
                    int updated = updater.ExecuteNonQuery();
                    Assert.AreEqual(1, updated);

                    // DELETE FROM "postgres"."pg_temp_2"."temptab"
                    // WHERE (("c0" = :p1) AND ((:p2 = 1 AND "c1" IS NULL) OR ("c1" = :p3)))
                    PqsqlCommand deleter = builder.GetDeleteCommand();
                    deleter.Parameters["p1"].Value = 2;
                    deleter.Parameters["p2"].Value = 0;
                    deleter.Parameters["p3"].Value = 2.2;
                    int deleted = deleter.ExecuteNonQuery();
                    Assert.AreEqual(1, deleted);

                    transaction.Rollback();
                }
        }
        public void PqsqlCommandBuilderTest2()
        {
            using (PqsqlConnection connection = new PqsqlConnection(connectionString))
                using (PqsqlCommand command = connection.CreateCommand())
                {
                    PqsqlTransaction transaction = connection.BeginTransaction();
                    command.Transaction = transaction;
                    command.CommandText = "create temp table temptab (c0 int4 primary key, c1 float8)";
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();
                    transaction.Commit();             // temp table must be visible in the next transaction

                    transaction = connection.BeginTransaction();

                    PqsqlDataAdapter adapter = new PqsqlDataAdapter("select * from temptab", connection)
                    {
                        SelectCommand =
                        {
                            Transaction = transaction
                        },
                    };

                    adapter.RowUpdated += Adapter_RowUpdated;

                    PqsqlCommandBuilder builder = new PqsqlCommandBuilder(adapter);

                    DataSet ds = new DataSet();
                    adapter.FillSchema(ds, SchemaType.Source);
                    adapter.Fill(ds, "temptab");

                    DataTable temptab = ds.Tables["temptab"];
                    DataRow   row     = temptab.NewRow();
                    row["c0"] = 123;
                    row["c1"] = 1.23;
                    temptab.Rows.Add(row);

                    adapter.Update(ds, "temptab");

                    command.CommandText = "select * from temptab";
                    command.CommandType = CommandType.Text;

                    using (PqsqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Assert.AreEqual(123, reader.GetInt32(0));
                            Assert.AreEqual(1.23, reader.GetDouble(1));
                        }
                    }

                    transaction.Rollback();
                }
        }
Exemplo n.º 6
0
        public void TestInitialize()
        {
            mConnection = new PqsqlConnection(connectionString);

            // force UTC
            using (var cmd = new PqsqlCommand("set timezone to 'UTC';", mConnection))
            {
                cmd.ExecuteNonQuery();
            }

            mCmd = mConnection.CreateCommand();
        }
        public void PqsqlCommandBuilderTest4()
        {
            using (PqsqlConnection connection = new PqsqlConnection(connectionString))
                using (PqsqlCommand command = connection.CreateCommand())
                {
                    PqsqlTransaction transaction = connection.BeginTransaction();
                    command.Transaction = transaction;
                    command.CommandText = "create temp table temptab (c0 int4 primary key, c1 float8, c2 timestamp);";
                    command.CommandType = CommandType.Text;
                    command.ExecuteNonQuery();
                    transaction.Commit();             // temp table must be visible in the next transaction

                    transaction = connection.BeginTransaction();

                    PqsqlDataAdapter adapter = new PqsqlDataAdapter("select * from temptab", connection)
                    {
                        SelectCommand =
                        {
                            Transaction = transaction
                        },
                    };

                    adapter.RowUpdated += Adapter_RowUpdated;

                    PqsqlCommandBuilder builder = new PqsqlCommandBuilder(adapter);

                    DataTableMapping mapping = adapter.TableMappings.Add("Table", "temptab");
                    mapping.ColumnMappings.Add("c0", "id");
                    mapping.ColumnMappings.Add("c2", "time");

                    DataSet ds = new DataSet();
                    adapter.FillSchema(ds, SchemaType.Mapped);
                    adapter.Fill(ds);

                    DataTable tab = ds.Tables[0];

                    Assert.AreEqual("id", tab.Columns[0].ColumnName);
                    Assert.AreEqual("c1", tab.Columns[1].ColumnName);
                    Assert.AreEqual("time", tab.Columns[2].ColumnName);

                    transaction.Rollback();
                }
        }
Exemplo n.º 8
0
        public void PqsqlConnectionTest7()
        {
            PqsqlConnection connection = new PqsqlConnection(connectionString);

            bool opened = false;
            bool closed = true;

            connection.StateChange += (sender, args) =>
            {
                if (args.CurrentState == ConnectionState.Closed)
                {
                    opened = false;
                    closed = true;
                }

                if (args.CurrentState == ConnectionState.Open)
                {
                    opened = true;
                    closed = false;
                }
            };

            connection.Open();
            Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state");

            Assert.AreEqual(true, opened);
            Assert.AreEqual(false, closed);

            try
            {
                PqsqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "select pg_terminate_backend(pg_backend_pid()); select pg_sleep(5);";
                cmd.ExecuteNonQuery();                 // must execute both statements
            }
            catch (Exception)
            {
                // ignored
                connection.Close();
            }

            Assert.AreEqual(false, opened);
            Assert.AreEqual(true, closed);
        }
Exemplo n.º 9
0
 public void TestInitialize()
 {
     mConnection = new PqsqlConnection(connectionString);
     mCmd        = mConnection.CreateCommand();
 }
Exemplo n.º 10
0
        public void PqsqlCopyFromTest3()
        {
            PqsqlTransaction t = mConnection.BeginTransaction();

            PqsqlCommand cmd = mConnection.CreateCommand();

            cmd.Transaction    = t;
            cmd.CommandText    = "CREATE TEMP TABLE testcopy (c0 int2, c1 int4, c2 int8, c3 bool, c4 text, c5 float4, c6 float8, c7 timestamp, c8 interval, c9 numeric, c10 date, c11 time, c12 timetz);";
            cmd.CommandTimeout = 100;
            cmd.CommandType    = CommandType.Text;

            cmd.ExecuteNonQuery();

            PqsqlCopyFrom copy = new PqsqlCopyFrom(mConnection)
            {
                Table       = "testcopy",
                ColumnList  = "c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12",
                CopyTimeout = 5
            };

            copy.Start();

            DateTime now  = new DateTime(2001, 1, 1, 1, 2, 3, DateTimeKind.Utc);
            DateTime date = DateTime.UtcNow.Date;

            TimeSpan time      = DateTime.UtcNow.TimeOfDay;
            long     timeTicks = time.Ticks;

            timeTicks = timeTicks - timeTicks % TimeSpan.TicksPerMillisecond;

            TimeSpan timetz      = DateTime.Now.TimeOfDay;
            long     timetzTicks = timetz.Ticks;

            timetzTicks = timetzTicks - timetzTicks % TimeSpan.TicksPerMillisecond;

            for (int i = 0; i < 4; i++)
            {
                copy.WriteInt2((short)i);
                copy.WriteInt4(i);
                copy.WriteInt8(i);
                copy.WriteBool(i > 0);
                copy.WriteText(Convert.ToString(i));
                copy.WriteFloat4((float)(i + 0.123));
                copy.WriteFloat8(i + 0.123);
                copy.WriteTimestamp(now.AddSeconds(i));
                copy.WriteInterval(TimeSpan.FromHours(24) + TimeSpan.FromDays(7) + TimeSpan.FromMinutes(i));
                copy.WriteNumeric((decimal)i / 10);
                copy.WriteDate(date);
                copy.WriteTime(time);
                copy.WriteTimeTZ(timetz);
            }

            copy.End();
            copy.Close();

            cmd.Transaction = t;

            cmd.CommandText = "testcopy";
            cmd.CommandType = CommandType.TableDirect;

            PqsqlDataReader r = cmd.ExecuteReader();

            Assert.AreEqual(-1, r.RecordsAffected);

            int j = 0;

            foreach (IDataRecord row in r)
            {
                Assert.AreEqual((short)j, row.GetInt16(0));
                Assert.AreEqual(j, row.GetInt32(1));
                Assert.AreEqual(j, row.GetInt64(2));
                Assert.AreEqual(j > 0, row.GetBoolean(3));
                Assert.AreEqual(Convert.ToString(j), row.GetString(4));
                Assert.AreEqual((float)(j + 0.123), row.GetFloat(5));
                Assert.AreEqual(j + 0.123, row.GetDouble(6));
                Assert.AreEqual(now.AddSeconds(j), row.GetDateTime(7));
                Assert.AreEqual(TimeSpan.FromHours(24) + TimeSpan.FromDays(7) + TimeSpan.FromMinutes(j), row.GetValue(8));
                Assert.AreEqual((double)j / 10, row.GetValue(9));
                Assert.AreEqual(date, row.GetValue(10));

                TimeSpan c11      = (TimeSpan)row.GetValue(11);
                long     c11Ticks = c11.Ticks;
                c11Ticks = c11Ticks - c11Ticks % TimeSpan.TicksPerMillisecond;

                Assert.AreEqual(timeTicks, c11Ticks);

                TimeSpan c12      = (TimeSpan)row.GetValue(12);
                long     c12Ticks = c12.Ticks;
                c12Ticks = c12Ticks - c12Ticks % TimeSpan.TicksPerMillisecond;

                Assert.AreEqual(timetzTicks, c12Ticks);

                j++;
            }

            t.Rollback();
        }