Example #1
0
        private static async Task DoWorkAsync()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                await session.StartAsync();

                await session.PrepareAsync("_p0", "select id, message from fortune");

                while (!_stopping)
                {
                    Interlocked.Increment(ref _counter);

                    void BindColumn(object _, ReadBuffer readBuffer, int index, int length)
                    {
                        switch (index)
                        {
                        case 0:
                            readBuffer.ReadInt();
                            break;

                        case 1:
                            readBuffer.ReadString(length);
                            break;
                        }
                    }

                    await session.ExecuteAsync <object>("_p0", null, BindColumn);
                }
            }
        }
        public async Task Prepare_success()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                await session.StartAsync();

                await session.PrepareAsync("_p0", "select id, message from fortune");
            }
        }
        public async Task Prepare_failure_invalid_sql()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                await session.StartAsync();

                Assert.Equal(
                    "syntax error at or near \"boom\"",
                    (await Assert.ThrowsAsync <InvalidOperationException>(
                         () => session.PrepareAsync("_p0", "boom!"))).Message);
            }
        }
        public async Task Execute_query_parameter_success()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                await session.StartAsync();

                await session.PrepareAsync("q", "select id, randomnumber from world where id = $1");

                World world = null;

                World CreateWorld()
                {
                    world = new World();

                    return(world);
                }

                void BindColumn(World w, ReadBuffer readBuffer, int index, int _)
                {
                    switch (index)
                    {
                    case 0:
                        w.Id = readBuffer.ReadInt();
                        break;

                    case 1:
                        w.RandomNumber = readBuffer.ReadInt();
                        break;
                    }
                }

                await session.ExecuteAsync("q", CreateWorld, BindColumn, 45);

                Assert.NotNull(world);
                Assert.Equal(45, world.Id);
                Assert.InRange(world.RandomNumber, 1, 10000);
            }
        }
        public async Task Execute_query_no_parameters_success()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                await session.StartAsync();

                await session.PrepareAsync("q", "select id, message from fortune");

                Fortune CreateFortune(List <Fortune> results)
                {
                    var fortune = new Fortune();

                    results.Add(fortune);

                    return(fortune);
                }

                void BindColumn(Fortune fortune, ReadBuffer readBuffer, int index, int length)
                {
                    switch (index)
                    {
                    case 0:
                        fortune.Id = readBuffer.ReadInt();
                        break;

                    case 1:
                        fortune.Message = readBuffer.ReadString(length);
                        break;
                    }
                }

                var fortunes = new List <Fortune>();

                await session.ExecuteAsync("q", fortunes, CreateFortune, BindColumn);

                Assert.Equal(12, fortunes.Count);
            }
        }
Example #6
0
 public Task PrepareAsync() => _session.PrepareAsync("p", CommandText);