This repository has been archived by the owner on Jun 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScriptFunctions.cpp
309 lines (275 loc) · 7.47 KB
/
ScriptFunctions.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (c) 2010-2011, mabako
*
* Based on mta-mysql module for Multi Theft Auto: San Andreas
* Copyright (c) 2008, Alberto Alonso
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the mta-mysql nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*************************************************************
*
* Solution : Mafia2-Online
* Project : M2Online-MySQL
* File : ScriptFunctions.cpp
*
***************************************************************/
#include "Global.h"
using namespace std;
list< MySQL* > handlers;
list< MySQL_Result* > results;
/* Checks if the parameter at index idx is a valid MySQL Handler, if so stores it in the 'handler' variable, otherwise returns an error and exits. */
#define CHECK_HANDLER(idx) \
MySQL* handler = 0; \
{ \
SQUserPointer sqTemp; \
sq_getuserpointer( vm, idx+1, &sqTemp ); \
for( list< MySQL* >::iterator iter = handlers.begin( ); iter != handlers.end( ); ++ iter ) \
{ \
if( *iter == sqTemp ) \
{ \
handler = (MySQL*)sqTemp; \
break; \
} \
} \
if( handler == 0 ) \
{ \
Log( "Invalid MySQL-Handler" ); \
sq_pushbool( vm, false ); \
return 1; \
} \
}
/* Checks if the parameter at index idx is a valid MySQL Result, if so stores it in the 'result' variable, otherwise returns an error and exits. */
#define CHECK_RESULT(idx) \
MySQL_Result* result = 0; \
{ \
SQUserPointer sqTemp; \
sq_getuserpointer( vm, idx+1, &sqTemp ); \
for( list< MySQL_Result* >::iterator iter = results.begin( ); iter != results.end( ); ++ iter ) \
{ \
if( *iter == sqTemp ) \
{ \
result = (MySQL_Result*)sqTemp; \
break; \
} \
} \
if( result == 0 ) \
{ \
Log( "Invalid MySQL-Result" ); \
sq_pushbool( vm, false ); \
return 1; \
} \
}
/* Checks if the function has AT LEAST the given number of parameters */
#define CHECK_PARAMS(count) \
int numparams = sq_gettop(vm); \
if( numparams <= count ) \
{ \
Log( "Invalid Parameter count, need %d, got %d", count, numparams - 1 ); \
sq_pushbool( vm, false ); \
return 1; \
}
/* Obtains a string from the parameters */
#define GET_STRING(idx, to) \
sq_getstring( vm, idx+1, &to )
int ScriptFunctions::sq_mysql_connect( HSQUIRRELVM vm )
{
const char* hostname;
const char* username;
const char* password;
const char* database;
GET_STRING(1, hostname);
GET_STRING(2, username);
GET_STRING(3, password);
GET_STRING(4, database);
MySQL* handler = new MySQL(hostname, username, password, database);
if (handler->OK())
{
handlers.push_back(handler);
sq_pushuserpointer(vm, handler);
}
else
{
Log("MySQL Connection failed (#%d): %s", handler->Errno(), handler->Error());
delete handler;
sq_pushbool(vm, false);
}
return 1;
}
int ScriptFunctions::sq_mysql_close(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_HANDLER(1);
handler->Close();
handlers.remove(handler);
delete handler;
sq_pushbool(vm, true);
return 1;
}
int ScriptFunctions::sq_mysql_escape_string(HSQUIRRELVM vm)
{
CHECK_PARAMS(2);
CHECK_HANDLER(1);
const char* string;
GET_STRING(2, string);
size_t len = strlen(string);
char* result = new char[(len << 1) + 1];
size_t newlen = handler->EscapeString(string, result, len);
sq_pushstring(vm, result, newlen);
return 1;
}
int ScriptFunctions::sq_mysql_ping(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_HANDLER(1);
sq_pushbool(vm, handler->Ping());
return 1;
}
int ScriptFunctions::sq_mysql_errno(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_HANDLER(1);
sq_pushinteger(vm, handler->Errno());
return 1;
}
int ScriptFunctions::sq_mysql_error(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_HANDLER(1);
const char* error = handler->Error();
sq_pushstring(vm, error, strlen(error));
return 1;
}
int ScriptFunctions::sq_mysql_query(HSQUIRRELVM vm)
{
CHECK_PARAMS(2);
CHECK_HANDLER(1);
const char* query;
GET_STRING(2, query);
MySQL_Result* result = handler->Query(query);
if (result){
results.push_back(result);
sq_pushuserpointer(vm, result);
}
else {
sq_pushbool(vm, false);
}
return 1;
}
int ScriptFunctions::sq_mysql_free_result(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_RESULT(1);
results.remove(result);
delete result;
sq_pushbool(vm, true);
return 1;
}
int ScriptFunctions::sq_mysql_fetch_assoc(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_RESULT(1);
if (!result->Empty())
{
MYSQL_ROW row = result->FetchRow();
if (row)
{
/* Build our table */
sq_newtable(vm);
result->FieldSeek(0);
MYSQL_FIELD* field;
int i = 0;
for (field = result->FetchField(), i = 0; field != 0; field = result->FetchField(), i++)
{
sq_pushstring(vm, field->name, field->name_length);
if (row[i])
{
switch (field->type)
{
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
sq_pushfloat(vm, static_cast < float > (atof(row[i])));
break;
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_BIT:
sq_pushinteger(vm, atoi(row[i]));
break;
case MYSQL_TYPE_NULL:
sq_pushnull(vm);
break;
default:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_SET:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_NEWDATE:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
sq_pushstring(vm, row[i], strlen(row[i]));
break;
}
}
else
{
sq_pushnull(vm);
}
sq_rawset(vm, -3);
}
}
else
sq_pushbool(vm, false);
}
else
sq_pushbool(vm, false);
return 1;
}
int ScriptFunctions::sq_mysql_insert_id(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_HANDLER(1);
sq_pushinteger(vm, handler->InsertID());
return 1;
}
int ScriptFunctions::sq_mysql_affected_rows(HSQUIRRELVM vm)
{
CHECK_PARAMS(1);
CHECK_HANDLER(1);
sq_pushinteger(vm, handler->AffectedRows());
return 1;
}