Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 2
0
 public async Task Start_timeout_on_bad_host()
 {
     using (var session = new PGSession("1.2.3.4", Port, Database, User, Password))
     {
         await Assert.ThrowsAsync <SocketException>(() => session.StartAsync());
     }
 }
Exemplo n.º 3
0
 public async Task Start_fail_on_bad_port()
 {
     using (var session = new PGSession(Host, 2345, Database, User, Password))
     {
         await Assert.ThrowsAnyAsync <SocketException>(() => session.StartAsync());
     }
 }
Exemplo n.º 4
0
        public override Task OpenAsync(CancellationToken cancellationToken)
        {
            ThrowIfDisposed();

            if (_session != null)
            {
                return(Task.CompletedTask);
            }

            _session = _pool.Rent();

            if (_session == null)
            {
                var parts
                    = (from s in ConnectionString.Split(';')
                       let kv = s.Split('=')
                                select kv)
                      .ToDictionary(p => p[0], p => p[1]);

                _session = new PGSession(
                    parts["host"],
                    port: 5432,
                    database: parts["database"],
                    user: parts["username"],
                    password: parts["password"]);
            }

            return(_session.IsConnected ? Task.CompletedTask : _session.StartAsync());
        }
Exemplo n.º 5
0
        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");
            }
        }
Exemplo n.º 6
0
 public async Task Start_fail_bad_password()
 {
     using (var session = new PGSession(Host, Port, Database, User, "wrong"))
     {
         Assert.Equal(
             "password authentication failed for user \"postgres\"",
             (await Assert.ThrowsAsync <InvalidOperationException>(
                  () => session.StartAsync())).Message);
     }
 }
Exemplo n.º 7
0
        public async Task Start_success()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                Assert.False(session.IsConnected);

                await session.StartAsync();

                Assert.True(session.IsConnected);
            }
        }
Exemplo n.º 8
0
        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);
            }
        }
Exemplo n.º 9
0
            public void Return(PGSession session)
            {
                for (var i = 0; i < _sessions.Length; i++)
                {
                    if (Interlocked.CompareExchange(ref _sessions[i], session, null) == null)
                    {
                        return;
                    }
                }

                session.Dispose();
            }
Exemplo n.º 10
0
        public async Task Dispose_when_open()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                await session.StartAsync();

                Assert.True(session.IsConnected);

                session.Dispose();

                Assert.Throws <ObjectDisposedException>(() => session.IsConnected);
            }
        }
Exemplo n.º 11
0
        public async Task Terminate_when_open_reopen()
        {
            using (var session = new PGSession(Host, Port, Database, User, Password))
            {
                await session.StartAsync();

                Assert.True(session.IsConnected);

                session.Terminate();

                Assert.False(session.IsConnected);

                await session.StartAsync();

                Assert.True(session.IsConnected);
            }
        }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        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);
            }
        }
        public override Task OpenAsync(CancellationToken cancellationToken)
        {
            if (_session == null)
            {
                var connectionInfo
                    = (from s in ConnectionString.Split(';')
                       let kv = s.Split('=')
                                select kv)
                      .ToDictionary(p => p[0], p => p[1]);

                _session
                    = new PGSession(
                          connectionInfo["host"],
                          5432,
                          connectionInfo["database"],
                          connectionInfo["username"],
                          connectionInfo["password"]);

                return(_session.StartAsync());
            }

            return(Task.CompletedTask);
        }
 public override void Close()
 {
     _session?.Dispose();
     _session = null;
 }
Exemplo n.º 16
0
 public PeregrineCommand(PGSession session)
 {
     _session    = session;
     _readBuffer = session.ReadBuffer;
     _reader     = new PeregrineDataReader(session.ReadBuffer);
 }
Exemplo n.º 17
0
 public PeregrineCommand(PGSession session) => _session = session;