Skip to content

Commit 7a192be

Browse files
committed
Added prepare and queryPrepared functions
1 parent 73233ce commit 7a192be

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

source/async_postgres.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,22 @@ namespace async_postgres {
7575
ParamValues param;
7676
};
7777

78+
struct CreatePreparedCommand {
79+
std::string name;
80+
std::string command;
81+
};
82+
83+
struct PreparedCommand {
84+
std::string name;
85+
ParamValues param;
86+
};
87+
7888
struct Query {
79-
std::variant<SimpleCommand, ParameterizedCommand> command;
89+
using CommandVariant =
90+
std::variant<SimpleCommand, ParameterizedCommand,
91+
CreatePreparedCommand, PreparedCommand>;
92+
93+
CommandVariant command;
8094
GLua::AutoReference callback;
8195
bool sent = false;
8296
bool flushed = false;

source/main.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,54 @@ namespace async_postgres::lua {
8484
}
8585

8686
state->query = std::move(query);
87+
return 0;
88+
}
89+
90+
lua_protected_fn(prepare) {
91+
lua->CheckType(1, async_postgres::connection_meta);
92+
lua->CheckType(2, GLua::Type::String);
93+
lua->CheckType(3, GLua::Type::String);
94+
lua->CheckType(4, GLua::Type::Function);
95+
96+
auto state = lua_connection_state();
97+
if (state->query) {
98+
throw std::runtime_error("query already in progress");
99+
}
100+
101+
async_postgres::CreatePreparedCommand command = {lua->GetString(2),
102+
lua->GetString(3)};
103+
async_postgres::Query query = {std::move(command)};
104+
105+
if (lua->IsType(4, GLua::Type::Function)) {
106+
query.callback = GLua::AutoReference(lua, 4);
107+
}
108+
109+
state->query = std::move(query);
110+
return 0;
111+
}
87112

113+
lua_protected_fn(queryPrepared) {
114+
lua->CheckType(1, async_postgres::connection_meta);
115+
lua->CheckType(2, GLua::Type::String);
116+
lua->CheckType(3, GLua::Type::Table);
117+
lua->CheckType(4, GLua::Type::Function);
118+
119+
auto state = lua_connection_state();
120+
if (state->query) {
121+
throw std::runtime_error("query already in progress");
122+
}
123+
124+
async_postgres::PreparedCommand command = {
125+
lua->GetString(2),
126+
async_postgres::array_to_params(lua, 3),
127+
};
128+
async_postgres::Query query = {std::move(command)};
129+
130+
if (lua->IsType(4, GLua::Type::Function)) {
131+
query.callback = GLua::AutoReference(lua, 4);
132+
}
133+
134+
state->query = std::move(query);
88135
return 0;
89136
}
90137

@@ -126,6 +173,8 @@ void register_connection_mt(GLua::ILuaInterface* lua) {
126173
register_lua_fn(__gc);
127174
register_lua_fn(query);
128175
register_lua_fn(queryParams);
176+
register_lua_fn(prepare);
177+
register_lua_fn(queryPrepared);
129178
register_lua_fn(reset);
130179
register_lua_fn(setNotifyCallback);
131180

source/query.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22

33
using namespace async_postgres;
44

5+
#define get_if_command(type) \
6+
const auto* command = std::get_if<type>(&query.command)
7+
58
// returns true if query was sent
69
// returns false on error
710
inline bool send_query(PGconn* conn, Query& query) {
8-
if (const auto* command = std::get_if<SimpleCommand>(&query.command)) {
11+
if (get_if_command(SimpleCommand)) {
912
return PQsendQuery(conn, command->command.c_str()) == 1;
10-
} else if (const auto* command =
11-
std::get_if<ParameterizedCommand>(&query.command)) {
13+
} else if (get_if_command(ParameterizedCommand)) {
1214
return PQsendQueryParams(conn, command->command.c_str(),
1315
command->param.length(), nullptr,
1416
command->param.values.data(),
1517
command->param.lengths.data(),
1618
command->param.formats.data(), 0) == 1;
19+
} else if (get_if_command(CreatePreparedCommand)) {
20+
return PQsendPrepare(conn, command->name.c_str(),
21+
command->command.c_str(), 0, nullptr) == 1;
22+
} else if (get_if_command(PreparedCommand)) {
23+
return PQsendQueryPrepared(
24+
conn, command->name.c_str(), command->param.length(),
25+
command->param.values.data(), command->param.lengths.data(),
26+
command->param.formats.data(), 0) == 1;
1727
}
1828
return false;
1929
}

0 commit comments

Comments
 (0)