Skip to content

Commit 7876e3a

Browse files
committed
Removed lua table from PGconn and made on_notify just a lua reference
1 parent 2b07410 commit 7876e3a

File tree

5 files changed

+19
-58
lines changed

5 files changed

+19
-58
lines changed

source/async_postgres.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ namespace async_postgres {
8989

9090
struct Connection {
9191
pg::conn conn;
92-
GLua::AutoReference lua_table;
9392
std::queue<Query> queries;
9493
std::optional<ResetEvent> reset_event;
95-
bool receive_notifications =
96-
false; // enabled if on_notify lua field is set
94+
GLua::AutoReference on_notify;
9795

9896
Connection(GLua::ILuaInterface* lua, pg::conn&& conn);
9997
~Connection();

source/connection.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ std::vector<Connection*> async_postgres::connections = {};
66

77
Connection::Connection(GLua::ILuaInterface* lua, pg::conn&& conn)
88
: conn(std::move(conn)) {
9-
lua->CreateTable();
10-
this->lua_table = GLua::AutoReference(lua);
11-
129
// add connection to global list
1310
connections.push_back(this);
1411
}

source/main.cpp

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,6 @@ namespace async_postgres::lua {
1212
return 0;
1313
}
1414

15-
lua_protected_fn(__index) {
16-
auto state = lua_connection_state();
17-
18-
state->lua_table.Push();
19-
lua->Push(2);
20-
lua->GetTable(-2);
21-
if (!lua->IsType(-1, GLua::Type::Nil)) {
22-
return 1;
23-
}
24-
25-
// is it alright if I don't pop previous stack values?
26-
27-
lua->PushMetaTable(async_postgres::connection_meta);
28-
lua->Push(2);
29-
lua->GetTable(-2);
30-
31-
return 1;
32-
}
33-
34-
lua_protected_fn(__newindex) {
35-
auto state = lua_connection_state();
36-
37-
state->lua_table.Push();
38-
lua->Push(2);
39-
lua->Push(3);
40-
lua->SetTable(-3);
41-
42-
auto key = get_string(lua, 2);
43-
if (key == "on_notify") {
44-
state->receive_notifications = !lua->IsType(3, GLua::Type::Nil);
45-
}
46-
47-
return 1;
48-
}
49-
5015
lua_protected_fn(loop) {
5116
async_postgres::process_pending_connections(lua);
5217

@@ -126,6 +91,16 @@ namespace async_postgres::lua {
12691

12792
return 0;
12893
}
94+
95+
lua_protected_fn(setNotifyCallback) {
96+
lua->CheckType(1, async_postgres::connection_meta);
97+
lua->CheckType(2, GLua::Type::Function);
98+
99+
auto state = lua_connection_state();
100+
state->on_notify = GLua::AutoReference(lua, 2);
101+
102+
return 0;
103+
}
129104
} // namespace async_postgres::lua
130105

131106
#define register_lua_fn(name) \
@@ -135,12 +110,14 @@ namespace async_postgres::lua {
135110
void register_connection_mt(GLua::ILuaInterface* lua) {
136111
async_postgres::connection_meta = lua->CreateMetaTable("PGconn");
137112

138-
register_lua_fn(__index);
139-
register_lua_fn(__newindex);
113+
lua->Push(-1);
114+
lua->SetField(-2, "__index");
115+
140116
register_lua_fn(__gc);
141117
register_lua_fn(query);
142118
register_lua_fn(queryParams);
143119
register_lua_fn(reset);
120+
register_lua_fn(setNotifyCallback);
144121

145122
async_postgres::register_misc_connection_functions(lua);
146123

source/notifications.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,14 @@
22

33
using namespace async_postgres;
44

5-
bool push_on_notify(GLua::ILuaInterface* lua, Connection* state) {
6-
state->lua_table.Push();
7-
lua->GetField(-1, "on_notify");
8-
lua->Remove(-2); // remove the connection table
9-
10-
if (lua->IsType(-1, GLua::Type::Nil)) {
11-
lua->Pop();
12-
return false;
13-
}
14-
return true;
15-
}
16-
175
void async_postgres::process_notifications(GLua::ILuaInterface* lua,
186
Connection* state) {
197
if (state->reset_event) {
208
// don't process notifications while reconnecting
219
return;
2210
}
2311

24-
if (!state->receive_notifications) {
12+
if (!state->on_notify) {
2513
return;
2614
}
2715

@@ -34,7 +22,7 @@ void async_postgres::process_notifications(GLua::ILuaInterface* lua,
3422
}
3523

3624
while (auto notify = pg::getNotify(state->conn)) {
37-
if (push_on_notify(lua, state)) {
25+
if (state->on_notify.Push()) {
3826
lua->PushString(notify->relname); // arg 1 channel name
3927
lua->PushString(notify->extra); // arg 2 payload
4028
lua->PushNumber(notify->be_pid); // arg 3 backend pid

source/util.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ ParamValues async_postgres::array_to_params(GLua::ILuaInterface* lua,
7474
#define poll WSAPoll
7575
#else
7676
#include <poll.h>
77+
#define SOCKET int
7778
#endif
7879

7980
SocketStatus async_postgres::check_socket_status(PGconn* conn) {
8081
SocketStatus status = {};
81-
int fd = PQsocket(conn);
82+
SOCKET fd = PQsocket(conn);
8283
if (fd < 0) {
8384
status.failed = true;
8485
return status;

0 commit comments

Comments
 (0)