コード例 #1
0
        public void PqsqlTypeRegistryTest6()
        {
            Action[] actions = new Action[20];

            // stress test user-defined type setup
            for (int i = 0; i < 20; i++)
            {
                actions[i] = () =>
                {
                    using (PqsqlConnection conn = new PqsqlConnection(mConnection.ConnectionString))
                        using (PqsqlCommand cmd = new PqsqlCommand("select 'hello world'::citext", conn))
                            using (PqsqlDataReader reader = cmd.ExecuteReader())
                            {
                                bool read = reader.Read();
                                Assert.IsTrue(read);
                                object helloWorld = reader.GetValue(0);                 // must access by GetValue, GetString verifies typoid
                                Assert.AreEqual("hello world", helloWorld);
                                read = reader.Read();
                                Assert.IsFalse(read);
                            }
                };
            }

            using (PqsqlCommand check = new PqsqlCommand("select oid from pg_extension where extname='citext'", mConnection))
                using (PqsqlCommand create = new PqsqlCommand("create extension citext", mConnection))
                    using (PqsqlTransaction t = mConnection.BeginTransaction())
                    {
                        object o = null;

                        try
                        {
                            check.Transaction = t;
                            o = check.ExecuteScalar();

                            if (o == null)
                            {
                                create.Transaction = t;
                                int aff = create.ExecuteNonQuery();
                                Assert.AreEqual(0, aff);
                            }

                            t.Commit();

                            Parallel.Invoke(actions);
                        }
                        finally
                        {
                            if (o == null)
                            {
                                using (PqsqlCommand drop = new PqsqlCommand("drop extension if exists citext", mConnection))
                                {
                                    int aff = drop.ExecuteNonQuery();
                                    Assert.AreEqual(0, aff);
                                }
                            }
                        }
                    }
        }
コード例 #2
0
        public void PqsqlTypeRegistryTest4()
        {
            mCmd.CommandText = "select 'YES'::citext";
            mCmd.CommandType = CommandType.Text;

            using (PqsqlCommand check = new PqsqlCommand("select oid from pg_extension where extname='citext'", mConnection))
                using (PqsqlCommand create = new PqsqlCommand("create extension citext", mConnection))
                    using (PqsqlCommand drop = new PqsqlCommand("drop extension if exists citext", mConnection))
                    {
                        object o = null;

                        try
                        {
                            o = check.ExecuteScalar();

                            if (o == null)
                            {
                                int aff = create.ExecuteNonQuery();
                                Assert.AreEqual(0, aff);
                            }

                            PqsqlDataReader reader = mCmd.ExecuteReader();

                            bool read = reader.Read();
                            Assert.IsTrue(read);

                            object yes = reader.GetValue(0);             // must access by GetValue, GetString verifies typoid
                            Assert.AreEqual("YES", yes);

                            reader.Close();
                        }
                        finally
                        {
                            if (o == null)
                            {
                                int aff = drop.ExecuteNonQuery();
                                Assert.AreEqual(0, aff);
                            }
                        }
                    }
        }
コード例 #3
0
        public void PqsqlCommandTest9()
        {
            PqsqlTransaction t = mConnection.BeginTransaction();

            PqsqlCommand cmd = mConnection.CreateCommand();

            cmd.Transaction    = t;
            cmd.CommandText    = @"create or replace function ""pg_temp"".""test me""(i int) returns int as $code$ begin return $1 * i; end; $code$ language plpgsql;
								select ""pg_temp"".""test me""(:p1);
								select ""pg_temp"".""test me""($1)"                                ;
            cmd.CommandTimeout = 2;
            cmd.CommandType    = CommandType.Text;
            cmd.Parameters.AddWithValue("p1", 4711);

            using (PqsqlDataReader r = cmd.ExecuteReader())
            {
                bool good = r.Read();
                Assert.IsFalse(good);

                good = r.NextResult();
                Assert.IsTrue(good);

                good = r.Read();
                Assert.IsTrue(good);

                int n = r.GetInt32(0);
                Assert.AreEqual(4711 * 4711, n);

                good = r.Read();
                Assert.IsFalse(good);

                good = r.NextResult();
                Assert.IsTrue(good);

                good = r.Read();
                Assert.IsTrue(good);

                n = r.GetInt32(0);
                Assert.AreEqual(4711 * 4711, n);

                good = r.Read();
                Assert.IsFalse(good);
            }

            cmd.CommandText = "\"pg_temp\".\"test me\"";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("i", 4711);
            cmd.Parameters.Add(new PqsqlParameter
            {
                ParameterName = "\"pg_temp\".\"test me\"",
                DbType        = DbType.Int32,
                Direction     = ParameterDirection.Output
            });

            object x = cmd.ExecuteScalar();

            Assert.AreEqual(4711 * 4711, x);

            t.Rollback();
        }
コード例 #4
0
        public void PqsqlDataAdapterTest1()
        {
            // get current_database() name
            mCmd.CommandText = "current_database";
            mCmd.CommandType = CommandType.StoredProcedure;
            object curdb = mCmd.ExecuteScalar();

            // get dbname from connection
            object dbname = mConnection.Database;

            // fetch pg_database tuple
            mCmd.CommandText = "select datname,pg_encoding_to_char(encoding),datcollate,datctype,datallowconn from pg_database where datname=:dat";
            mCmd.CommandType = CommandType.Text;

            PqsqlParameter datpar = mCmd.CreateParameter();

            datpar.ParameterName = "dat";
            datpar.DbType        = DbType.String;
            datpar.Value         = curdb;
            mCmd.Parameters.Add(datpar);

            DataSet ds = new DataSet();

            using (PqsqlDataAdapter adapter = new PqsqlDataAdapter(mCmd))
            {
                adapter.Fill(ds);
            }

            Assert.AreEqual(1, ds.Tables.Count, "wrong table count");

            int tables  = 0;
            int rows    = 0;
            int columns = 0;

            foreach (DataTable table in ds.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    foreach (object item in row.ItemArray)
                    {
                        // read item
                        switch (columns)
                        {
                        case 0:                         // datname
                            Assert.AreEqual(dbname, item, "wrong database name");
                            break;

                        case 1:                         // encoding
                            Assert.AreEqual("UTF8", item, "wrong encoding id");
                            break;

                        case 2:                         // datcollate
                        case 3:                         // datctype
                            Assert.IsNotNull(item);
                            break;

                        case 4:                         // datallowconn
                            Assert.AreEqual(true, item, "we must be allowed to connect");
                            break;
                        }
                        columns++;
                    }
                    rows++;
                }
                tables++;
            }

            Assert.AreEqual(1, tables, "wrong table count");
            Assert.AreEqual(1, rows, "wrong row count");
            Assert.AreEqual(5, columns, "wrong column count");
        }