Skip to content

Commit 0fc28a7

Browse files
committed
wop
1 parent 4a2c2ba commit 0fc28a7

File tree

4 files changed

+66
-34
lines changed

4 files changed

+66
-34
lines changed

source/async_postgres.hpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ namespace GLua = GarrysMod::Lua;
4242
int name##__Imp([[maybe_unused]] GarrysMod::Lua::ILuaInterface* lua)
4343

4444
namespace async_postgres {
45+
typedef std::variant<std::nullptr_t, std::string, double, bool> ParamValue;
46+
47+
struct ParamValues {
48+
ParamValues(int n_params)
49+
: strings(n_params),
50+
values(n_params),
51+
lengths(n_params, 0),
52+
formats(n_params, 0) {}
53+
54+
inline int length() const { return strings.size(); }
55+
56+
std::vector<std::string> strings;
57+
// vectors below are used in PQexecParams-like functions
58+
std::vector<const char*> values;
59+
std::vector<int> lengths;
60+
std::vector<int> formats;
61+
};
62+
4563
struct SocketStatus {
4664
bool read_ready = false;
4765
bool write_ready = false;
@@ -54,7 +72,7 @@ namespace async_postgres {
5472

5573
struct ParameterizedCommand {
5674
std::string command;
57-
std::vector<std::string> values;
75+
ParamValues param;
5876
};
5977

6078
struct Query {
@@ -104,10 +122,11 @@ namespace async_postgres {
104122

105123
// misc.cpp
106124
void register_misc_connection_functions(GLua::ILuaInterface* lua);
107-
std::vector<std::optional<std::string>> lua_array_to_params();
108125

109126
// util.cpp
110127
std::string_view get_string(GLua::ILuaInterface* lua, int index = -1);
111128
void pcall(GLua::ILuaInterface* lua, int nargs, int nresults);
129+
// Converts a lua array at given index to a ParamValues
130+
ParamValues array_to_params(GLua::ILuaInterface* lua, int index);
112131
SocketStatus check_socket_status(PGconn* conn);
113132
}; // namespace async_postgres

source/main.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,10 @@ namespace async_postgres::lua {
101101

102102
auto state = lua_connection_state();
103103

104-
async_postgres::ParameterizedCommand command = {lua->GetString(2)};
105-
106-
lua->Push(3);
107-
for (int i = 1;; i++) {
108-
lua->PushNumber(i);
109-
lua->GetTable(-2);
110-
if (lua->IsType(-1, GLua::Type::Nil)) {
111-
lua->Pop(2);
112-
break;
113-
}
114-
115-
auto str = get_string(lua, -1);
116-
command.values.push_back({str.data(), str.size()});
117-
lua->Pop(1);
118-
}
104+
async_postgres::ParameterizedCommand command = {
105+
lua->GetString(2),
106+
async_postgres::array_to_params(lua, 3),
107+
};
119108

120109
async_postgres::Query query = {std::move(command)};
121110
query.callback = GLua::AutoReference(lua, 4);

source/query.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,11 @@ inline bool send_query(PGconn* conn, Query& query) {
99
return PQsendQuery(conn, command->command.c_str()) == 1;
1010
} else if (const auto* command =
1111
std::get_if<ParameterizedCommand>(&query.command)) {
12-
// TODO: support binary format
13-
size_t nParams = command->values.size();
14-
std::vector<const char*> paramValues(nParams);
15-
std::vector<int> paramLengths(nParams);
16-
std::vector<int> paramFormats(nParams, 1);
17-
for (size_t i = 0; i < nParams; i++) {
18-
const auto& value = command->values[i];
19-
paramValues[i] = value.c_str();
20-
paramLengths[i] = value.size();
21-
}
22-
23-
bool success =
24-
PQsendQueryParams(conn, command->command.c_str(), nParams, nullptr,
25-
paramValues.data(), paramLengths.data(),
26-
paramFormats.data(), 1) == 1;
27-
28-
return success;
12+
return PQsendQueryParams(conn, command->command.c_str(),
13+
command->param.length(), nullptr,
14+
command->param.values.data(),
15+
command->param.lengths.data(),
16+
command->param.formats.data(), 0) == 1;
2917
}
3018
return false;
3119
}

source/util.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,42 @@ void async_postgres::pcall(GLua::ILuaInterface* lua, int nargs, int nresults) {
3333
lua->Pop(); // ErrorNoHaltWithStack
3434
}
3535

36+
ParamValues async_postgres::array_to_params(GLua::ILuaInterface* lua,
37+
int index) {
38+
lua->Push(index);
39+
int len = lua->ObjLen(-1);
40+
41+
ParamValues param(len);
42+
for (int i = 0; i < len; i++) {
43+
lua->PushNumber(i + 1);
44+
lua->GetTable(-2);
45+
46+
auto type = lua->GetType(-1);
47+
if (type == GLua::Type::String) {
48+
auto value = get_string(lua, -1);
49+
param.strings[i] = std::string(value.data(), value.size());
50+
param.values[i] = param.strings[i].c_str();
51+
param.lengths[i] = value.length();
52+
param.formats[i] = 1;
53+
} else if (type == GLua::Type::Number) {
54+
param.strings[i] = std::to_string(lua->GetNumber(-1));
55+
param.values[i] = param.strings[i].c_str();
56+
} else if (type == GLua::Type::Bool) {
57+
param.values[i] = lua->GetBool(-1) ? "true" : "false";
58+
} else if (type == GLua::Type::Nil) {
59+
param.values[i] = nullptr;
60+
} else {
61+
throw std::runtime_error(
62+
"unsupported type given into params array");
63+
}
64+
65+
lua->Pop(1);
66+
}
67+
68+
lua->Pop(1);
69+
return param;
70+
}
71+
3672
#if SYSTEM_IS_WINDOWS
3773
#include <WinSock2.h>
3874
#define poll WSAPoll

0 commit comments

Comments
 (0)