Skip to content

Commit f0e39d4

Browse files
committed
Added enums and querying + resetting status functions
1 parent 47b989f commit f0e39d4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

source/async_postgres.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ namespace async_postgres {
145145

146146
// misc.cpp
147147
void register_misc_connection_functions(GLua::ILuaInterface* lua);
148+
void register_enums(GLua::ILuaInterface* lua);
148149

149150
// util.cpp
150151
std::string_view get_string(GLua::ILuaInterface* lua, int index = -1);

source/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ namespace async_postgres::lua {
249249
state->query); // busy if reset or query are in progress
250250
return 1;
251251
}
252+
253+
lua_protected_fn(querying) {
254+
lua->CheckType(1, async_postgres::connection_meta);
255+
auto state = lua_connection_state();
256+
lua->PushBool(state->query.has_value());
257+
return 1;
258+
}
259+
260+
lua_protected_fn(resetting) {
261+
lua->CheckType(1, async_postgres::connection_meta);
262+
auto state = lua_connection_state();
263+
lua->PushBool(state->reset_event.has_value());
264+
return 1;
265+
}
252266
} // namespace async_postgres::lua
253267

254268
#define register_lua_fn(name) \
@@ -272,6 +286,8 @@ void register_connection_mt(GLua::ILuaInterface* lua) {
272286
register_lua_fn(setNotifyCallback);
273287
register_lua_fn(wait);
274288
register_lua_fn(isBusy);
289+
register_lua_fn(querying);
290+
register_lua_fn(resetting);
275291

276292
async_postgres::register_misc_connection_functions(lua);
277293

@@ -283,6 +299,8 @@ void make_global_table(GLua::ILuaInterface* lua) {
283299

284300
register_lua_fn(connect);
285301

302+
async_postgres::register_enums(lua);
303+
286304
lua->SetField(GLua::INDEX_GLOBAL, "async_postgres");
287305
}
288306

source/misc.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
#include "async_postgres.hpp"
24

35
using namespace async_postgres;
@@ -195,3 +197,41 @@ void async_postgres::register_misc_connection_functions(
195197
register_lua_fn(escapeBytea);
196198
register_lua_fn(unescapeBytea);
197199
}
200+
201+
#define enum_value(name) {#name, name}
202+
203+
void async_postgres::register_enums(GLua::ILuaInterface* lua) {
204+
using enum_array = std::vector<std::pair<const char*, int>>;
205+
206+
std::vector<enum_array> enums = {
207+
{
208+
enum_value(CONNECTION_OK),
209+
enum_value(CONNECTION_BAD),
210+
},
211+
{
212+
enum_value(PQTRANS_IDLE),
213+
enum_value(PQTRANS_ACTIVE),
214+
enum_value(PQTRANS_INTRANS),
215+
enum_value(PQTRANS_INERROR),
216+
enum_value(PQTRANS_UNKNOWN),
217+
},
218+
{
219+
enum_value(PQERRORS_TERSE),
220+
enum_value(PQERRORS_DEFAULT),
221+
enum_value(PQERRORS_VERBOSE),
222+
enum_value(PQERRORS_SQLSTATE),
223+
},
224+
{
225+
enum_value(PQSHOW_CONTEXT_NEVER),
226+
enum_value(PQSHOW_CONTEXT_ERRORS),
227+
enum_value(PQSHOW_CONTEXT_ALWAYS),
228+
},
229+
};
230+
231+
for (const auto& e : enums) {
232+
for (const auto& [name, value] : e) {
233+
lua->PushNumber(value);
234+
lua->SetField(-2, name);
235+
}
236+
}
237+
}

0 commit comments

Comments
 (0)