@@ -4,7 +4,7 @@ using namespace async_postgres;
4
4
5
5
std::vector<Connection*> async_postgres::connections = {};
6
6
7
- Connection::Connection (GLua::ILuaInterface* lua, PGconnPtr && conn)
7
+ Connection::Connection (GLua::ILuaInterface* lua, pg::conn && conn)
8
8
: conn(std::move(conn)) {
9
9
lua->CreateTable ();
10
10
this ->lua_table = GLua::AutoReference (lua);
@@ -20,7 +20,7 @@ Connection::~Connection() {
20
20
}
21
21
22
22
struct ConnectionEvent {
23
- PGconnPtr conn;
23
+ pg::conn conn;
24
24
GLua::AutoReference callback;
25
25
PostgresPollingStatusType status = PGRES_POLLING_WRITING;
26
26
bool is_reset = false ;
@@ -40,17 +40,16 @@ inline bool socket_is_ready(PGconn* conn, PostgresPollingStatusType status) {
40
40
41
41
void async_postgres::connect (GLua::ILuaInterface* lua, std::string_view url,
42
42
GLua::AutoReference&& callback) {
43
- auto conn = PGconnPtr (PQconnectStart (url.data ()), &PQfinish);
44
- auto conn_ptr = conn.get ();
43
+ auto conn = pg::connectStart (url);
45
44
46
45
if (!conn) {
47
46
// funnily enough, this probably will instead throw a std::bad_alloc
48
47
throw std::runtime_error (" failed to allocate connection" );
49
48
}
50
49
51
- if (PQstatus (conn_ptr ) == CONNECTION_BAD ||
52
- PQsetnonblocking (conn_ptr , 1 ) != 0 ) {
53
- throw std::runtime_error (PQerrorMessage (conn_ptr ));
50
+ if (PQstatus (conn. get () ) == CONNECTION_BAD ||
51
+ PQsetnonblocking (conn. get () , 1 ) != 0 ) {
52
+ throw std::runtime_error (PQerrorMessage (conn. get () ));
54
53
}
55
54
56
55
auto event = ConnectionEvent{std::move (conn), std::move (callback)};
@@ -65,8 +64,6 @@ inline bool poll_pending_connection(GLua::ILuaInterface* lua,
65
64
return false ;
66
65
}
67
66
68
- // TODO: handle reset
69
-
70
67
event.status = PQconnectPoll (event.conn .get ());
71
68
if (event.status == PGRES_POLLING_OK) {
72
69
auto state = new Connection (lua, std::move (event.conn ));
0 commit comments