-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCPostgresConnection.cpp
More file actions
80 lines (62 loc) · 2.02 KB
/
CPostgresConnection.cpp
File metadata and controls
80 lines (62 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*********************************************************
*
* Multi Theft Auto: San Andreas - Deathmatch
*
* ml_base, External lua add-on module
*
* Copyright ? 2003-2008 MTA. All Rights Reserved.
*
* Grand Theft Auto is (c) 2002-2003 Rockstar North
*
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
* PROVIDED WITH THIS PACKAGE.
*
*********************************************************/
#include "CPostgresConnection.hpp"
CPostgresConnection::CPostgresConnection(lua_State* pLuaVM, const char* szConnectionInfo) : m_pLuaVM{pLuaVM}
{
m_pConnection = PQconnectdb(szConnectionInfo);
}
CPostgresConnection::~CPostgresConnection()
{
if (m_pConnection)
PQfinish(m_pConnection);
}
PGresult* CPostgresConnection::Query(lua_State* luaVM)
{
libpq_query_t query_str = luaL_checkstring(luaVM, 2);
lua_remove(luaVM, 1);
lua_remove(luaVM, 1);
int args_count = lua_gettop(luaVM);
std::vector<const char*> args{};
for (int i = args_count; i > 0; --i) {
args.push_back(luaL_checkstring(luaVM, i));
}
PGresult* pResult = PQexecParams(m_pConnection, query_str.c_str(), args.size(), NULL, (char**)args.data(), NULL, NULL, 0);
if (pResult && PQresultStatus(pResult) == PGRES_TUPLES_OK)
{
return pResult;
}
return nullptr;
}
bool CPostgresConnection::Exec(lua_State* luaVM)
{
libpq_query_t query_str = luaL_checkstring(luaVM, 2);
lua_remove(luaVM, 1);
lua_remove(luaVM, 1);
int args_count = lua_gettop(luaVM);
std::vector<const char*> args{};
for (int i = args_count; i > 0; --i) {
args.push_back(luaL_checkstring(luaVM, i));
}
PGresult* pResult = PQexecParams(m_pConnection, query_str.c_str(), args.size(), NULL, (char**)args.data(), NULL, NULL, 0);
if (PQresultStatus(pResult) == PGRES_TUPLES_OK)
{
return false;
}
PQclear(pResult);
return true;
}