Skip to content

Commit 0e94bed

Browse files
committed
Added describePrepared and describePortal functions
1 parent 121cc0a commit 0e94bed

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

source/async_postgres.hpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,19 @@ namespace async_postgres {
8585
ParamValues param;
8686
};
8787

88+
struct DescribePreparedCommand {
89+
std::string name;
90+
};
91+
92+
struct DescribePortalCommand {
93+
std::string name;
94+
};
95+
8896
struct Query {
8997
using CommandVariant =
9098
std::variant<SimpleCommand, ParameterizedCommand,
91-
CreatePreparedCommand, PreparedCommand>;
99+
CreatePreparedCommand, PreparedCommand,
100+
DescribePreparedCommand, DescribePortalCommand>;
92101

93102
CommandVariant command;
94103
GLua::AutoReference callback;

source/main.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,48 @@ namespace async_postgres::lua {
135135
return 0;
136136
}
137137

138+
lua_protected_fn(describePrepared) {
139+
lua->CheckType(1, async_postgres::connection_meta);
140+
lua->CheckType(2, GLua::Type::String);
141+
lua->CheckType(3, GLua::Type::Function);
142+
143+
auto state = lua_connection_state();
144+
if (state->query) {
145+
throw std::runtime_error("query already in progress");
146+
}
147+
148+
async_postgres::DescribePreparedCommand command = {lua->GetString(2)};
149+
async_postgres::Query query = {std::move(command)};
150+
151+
if (lua->IsType(3, GLua::Type::Function)) {
152+
query.callback = GLua::AutoReference(lua, 3);
153+
}
154+
155+
state->query = std::move(query);
156+
return 0;
157+
}
158+
159+
lua_protected_fn(describePortal) {
160+
lua->CheckType(1, async_postgres::connection_meta);
161+
lua->CheckType(2, GLua::Type::String);
162+
lua->CheckType(3, GLua::Type::Function);
163+
164+
auto state = lua_connection_state();
165+
if (state->query) {
166+
throw std::runtime_error("query already in progress");
167+
}
168+
169+
async_postgres::DescribePortalCommand command = {lua->GetString(2)};
170+
async_postgres::Query query = {std::move(command)};
171+
172+
if (lua->IsType(3, GLua::Type::Function)) {
173+
query.callback = GLua::AutoReference(lua, 3);
174+
}
175+
176+
state->query = std::move(query);
177+
return 0;
178+
}
179+
138180
lua_protected_fn(reset) {
139181
lua->CheckType(1, async_postgres::connection_meta);
140182

@@ -175,6 +217,8 @@ void register_connection_mt(GLua::ILuaInterface* lua) {
175217
register_lua_fn(queryParams);
176218
register_lua_fn(prepare);
177219
register_lua_fn(queryPrepared);
220+
register_lua_fn(describePrepared);
221+
register_lua_fn(describePortal);
178222
register_lua_fn(reset);
179223
register_lua_fn(setNotifyCallback);
180224

source/query.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ inline bool send_query(PGconn* conn, Query& query) {
2424
conn, command->name.c_str(), command->param.length(),
2525
command->param.values.data(), command->param.lengths.data(),
2626
command->param.formats.data(), 0) == 1;
27+
} else if (get_if_command(DescribePreparedCommand)) {
28+
return PQsendDescribePrepared(conn, command->name.c_str()) == 1;
29+
} else if (get_if_command(DescribePortalCommand)) {
30+
return PQsendDescribePortal(conn, command->name.c_str()) == 1;
2731
}
2832
return false;
2933
}

0 commit comments

Comments
 (0)