This repository was archived by the owner on Aug 29, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathLuaMysql.h
More file actions
144 lines (124 loc) · 3.57 KB
/
Copy pathLuaMysql.h
File metadata and controls
144 lines (124 loc) · 3.57 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef __LUAMYSQL_H__
#define __LUAMYSQL_H__
class Mysql {
public:
static int Connect(lua_State* L)
{
const int args = lua_gettop(L);
if (args >= 4)
{
std::string ip = lua_tostring(L, 1);
std::string username = lua_tostring(L, 2);
std::string password = lua_tostring(L, 3);
std::string database = lua_tostring(L, 4);
int port = 3306;
if (args == 5)
{
port = lua_tointeger(L, 5);
}
lua_pop(L, args);
if (mysql != nullptr) delete mysql;
mysql = new MysqlConnection(ip, username, password, database, port);
lua_pushboolean(L, mysql->WasConnectionSuccesful());
}
else
{
lua_pop(L, args);
API::Log::Warning("Mysql:Connect requires arg (string ip, string username, string password, string database, optional[ int port ]).");
lua_pushnil(L);
}
return 1;
}
static int Disconnect(lua_State* L)
{
const int args = lua_gettop(L);
if (args >= 0)
{
delete mysql; //Destructor closes the connection.
}
else
{
API::Log::Warning("Mysql:Disconnect requires arg ().");
}
lua_pop(L, args);
return 0;
}
static int Query(lua_State* L)
{
const int args = lua_gettop(L);
if (args == 1)
{
std::string query = lua_tostring(L, 1);
lua_pop(L, args);
std::vector<std::map<std::string, MysqlConnection::QueryResult>> ret = mysql->Call(query);
//Fething Rows and Collumns size.
int rows = ret.size();
//Calculating the size of the table
int initialSize = 0;
if (rows != 0)
{
initialSize = rows * ret[0].size();
}
//Creating table
lua_createtable(L, 0, initialSize);
//Iterating through rows
int i = 0;
//for each(std::map<std::string, MysqlConnection::QueryResult> row in ret)
for (std::map<std::string, MysqlConnection::QueryResult>& row : ret)
{
i++;
//Creating another table
lua_newtable(L);
//for each(std::pair<std::string, MysqlConnection::QueryResult> collumn in row)
for (std::pair<std::string, MysqlConnection::QueryResult> collumn : row)
{
//Pushing key and value and making table
lua_pushstring(L, collumn.first.c_str());
if (collumn.second.datatype == enum_field_types::MYSQL_TYPE_NULL)
{
lua_pushnil(L);
}
else
{
if (collumn.second.datatype == enum_field_types::MYSQL_TYPE_FLOAT ||
collumn.second.datatype == enum_field_types::MYSQL_TYPE_DOUBLE)
{
lua_pushnumber(L, std::stod(collumn.second.data));
}
else if (collumn.second.datatype == enum_field_types::MYSQL_TYPE_TINY ||
collumn.second.datatype == enum_field_types::MYSQL_TYPE_SHORT ||
collumn.second.datatype == enum_field_types::MYSQL_TYPE_DECIMAL ||
collumn.second.datatype == enum_field_types::MYSQL_TYPE_NEWDECIMAL ||
collumn.second.datatype == enum_field_types::MYSQL_TYPE_LONG ||
collumn.second.datatype == enum_field_types::MYSQL_TYPE_LONGLONG)
{
lua_pushinteger(L, std::stoll(collumn.second.data)); //stoll because lua_Integer = long long
}
else if (collumn.second.datatype == enum_field_types::MYSQL_TYPE_BIT)
{
lua_pushboolean(L, std::stoi(collumn.second.data));
}
else
{
lua_pushstring(L, collumn.second.data.c_str());
}
// look up if MYSQL_TYPE_TINY is just 1 & 0, if it is then should out to a boolean
}
lua_settable(L, -3);
}
//Pushing table to table
lua_pushinteger(L, i);
lua_insert(L, -2);
lua_settable(L, -3);
}
}
else
{
lua_pop(L, args);
API::Log::Warning("Mysql:Query requires arg (string query).");
lua_pushnil(L);
}
return 1;
}
};
#endif