-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathnatives.cpp
More file actions
2116 lines (1794 loc) · 56.8 KB
/
natives.cpp
File metadata and controls
2116 lines (1794 loc) · 56.8 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "natives.hpp"
#include "CQuery.hpp"
#include "CHandle.hpp"
#include "CCallback.hpp"
#include "CResult.hpp"
#include "COptions.hpp"
#include "COrm.hpp"
#include "CLog.hpp"
#include "misc.hpp"
#include "sscanf.hpp"
#include <fstream>
#include <future>
#include <fmt/printf.h>
// native ORM:orm_create(const table[], MySQL:handle = MYSQL_DEFAULT_HANDLE);
AMX_DECLARE_NATIVE(Native::orm_create)
{
CScopedDebugInfo dbg_info(amx, "orm_create", params, "sd");
const char *tablename = nullptr;
amx_StrParam(amx, params[1], tablename);
const HandleId_t handleid = params[2];
CError<COrm> orm_error;
OrmId_t orm_id = COrmManager::Get()->Create(handleid, tablename, orm_error);
if (orm_error)
{
CLog::Get()->LogNative(orm_error);
return 0;
}
cell ret_val = orm_id;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native orm_destroy(ORM:id);
AMX_DECLARE_NATIVE(Native::orm_destroy)
{
CScopedDebugInfo dbg_info(amx, "orm_destroy", params, "d");
const OrmId_t ormid = params[1];
if (COrmManager::Get()->IsValid(ormid) == false)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
cell ret_val = COrmManager::Get()->Delete(ormid) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native E_ORM_ERROR:orm_errno(ORM:id);
AMX_DECLARE_NATIVE(Native::orm_errno)
{
CScopedDebugInfo dbg_info(amx, "orm_errno", params, "d");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return static_cast<cell>(COrm::PawnError::INVALID);
}
cell ret_val = static_cast<cell>(orm->GetError());
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native orm_apply_cache(ORM:id, row_idx, result_idx = 0);
AMX_DECLARE_NATIVE(Native::orm_apply_cache)
{
CScopedDebugInfo dbg_info(amx, "orm_apply_cache", params, "ddd");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
ResultSet_t active_resultset = CResultSetManager::Get()->GetActiveResultSet();
if (!active_resultset)
{
CLog::Get()->LogNative(LogLevel::ERROR, "no active resultset");
return 0;
}
auto const res = active_resultset->GetResultByIndex(params[3]);
if (res == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid resultset index '{}'", params[3]);
return 0;
}
if (orm->ApplyResultByName(res, params[2]) == false)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid row index index '{}'", params[2]);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
static bool FireOrmQueryWithCallback(AMX *amx, cell *params, COrm::QueryType type)
{
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return false;
}
Handle_t handle = CHandleManager::Get()->GetHandle(orm->GetHandleId());
if (!handle)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"handle id '{}' passed to orm instance is invalid",
orm->GetHandleId());
return false;
}
string
callback_str = amx_GetCppString(amx, params[2]),
format_str = amx_GetCppString(amx, params[3]);
CError<CCallback> callback_error;
Callback_t callback = CCallback::Create(
amx,
callback_str.c_str(),
format_str.c_str(),
params, 4,
callback_error);
if (callback_error && callback_error.type() != CCallback::Error::EMPTY_NAME)
{
CLog::Get()->LogNative(callback_error);
return false;
}
if (type == COrm::QueryType::SAVE)
type = orm->GetSaveQueryType();
string query_str;
CError<COrm> error;
if ((error = orm->GenerateQuery(type, query_str)))
{
CLog::Get()->LogNative(error);
return false;
}
CLog::Get()->LogNative(LogLevel::INFO, "generated query \"{}\"", query_str);
Query_t query = CQuery::Create(query_str);
query->OnExecutionFinished([orm, callback, type](ResultSet_t result)
{
switch (type)
{
case COrm::QueryType::SELECT:
orm->ApplyResult(result->GetActiveResult());
break;
case COrm::QueryType::INSERT:
orm->UpdateKeyValue(result->GetActiveResult());
break;
}
if (callback)
callback->Execute();
orm->ResetError();
});
return handle->Execute(CHandle::ExecutionType::THREADED, query);
}
// native orm_select(ORM:id, const callback[] = "", const format[] = "", {Float, _}:...);
AMX_DECLARE_NATIVE(Native::orm_select)
{
CScopedDebugInfo dbg_info(amx, "orm_select", params, "dss");
cell ret_val =
FireOrmQueryWithCallback(amx, params, COrm::QueryType::SELECT) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native orm_update(ORM:id, const callback[] = "", const format[] = "", {Float, _}:...);
AMX_DECLARE_NATIVE(Native::orm_update)
{
CScopedDebugInfo dbg_info(amx, "orm_update", params, "dss");
cell ret_val =
FireOrmQueryWithCallback(amx, params, COrm::QueryType::UPDATE) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native orm_insert(ORM:id, const callback[] = "", const format[] = "", {Float, _}:...);
AMX_DECLARE_NATIVE(Native::orm_insert)
{
CScopedDebugInfo dbg_info(amx, "orm_insert", params, "dss");
cell ret_val =
FireOrmQueryWithCallback(amx, params, COrm::QueryType::INSERT) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native orm_delete(ORM:id, const callback[] = "", const format[] = "", {Float, _}:...);
AMX_DECLARE_NATIVE(Native::orm_delete)
{
CScopedDebugInfo dbg_info(amx, "orm_delete", params, "dss");
cell ret_val =
FireOrmQueryWithCallback(amx, params, COrm::QueryType::DELETE) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native orm_save(ORM:id, const callback[] = "", const format[] = "", {Float, _}:...);
AMX_DECLARE_NATIVE(Native::orm_save)
{
CScopedDebugInfo dbg_info(amx, "orm_save", params, "dss");
cell ret_val =
FireOrmQueryWithCallback(amx, params, COrm::QueryType::SAVE) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native orm_addvar_int(ORM:id, &var, const columnname[]);
AMX_DECLARE_NATIVE(Native::orm_addvar_int)
{
CScopedDebugInfo dbg_info(amx, "orm_addvar_int", params, "drs");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
cell *var = nullptr;
amx_GetAddr(amx, params[2], &var);
const char *column_name = nullptr;
amx_StrParam(amx, params[3], column_name);
CError<COrm> error;
if ((error = orm->AddVariable(COrm::Variable::Type::INT, column_name, var)))
{
CLog::Get()->LogNative(error);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native orm_addvar_float(ORM:id, &Float:var, const columnname[]);
AMX_DECLARE_NATIVE(Native::orm_addvar_float)
{
CScopedDebugInfo dbg_info(amx, "orm_addvar_float", params, "drs");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
cell *var = nullptr;
amx_GetAddr(amx, params[2], &var);
const char *column_name = nullptr;
amx_StrParam(amx, params[3], column_name);
CError<COrm> error;
if ((error = orm->AddVariable(COrm::Variable::Type::FLOAT, column_name, var)))
{
CLog::Get()->LogNative(error);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native orm_addvar_string(ORM:id, var[], var_maxlen, const columnname[]);
AMX_DECLARE_NATIVE(Native::orm_addvar_string)
{
CScopedDebugInfo dbg_info(amx, "orm_addvar_string", params, "drds");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
cell *var = nullptr;
amx_GetAddr(amx, params[2], &var);
const char *column_name = nullptr;
amx_StrParam(amx, params[4], column_name);
CError<COrm> error;
if ((error = orm->AddVariable(COrm::Variable::Type::STRING,
column_name, var, params[3])))
{
CLog::Get()->LogNative(error);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native orm_clear_vars(ORM:id);
AMX_DECLARE_NATIVE(Native::orm_clear_vars)
{
CScopedDebugInfo dbg_info(amx, "orm_clear_vars", params, "d");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
orm->ClearAllVariables();
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native orm_delvar(ORM:id, const columnname[]);
AMX_DECLARE_NATIVE(Native::orm_delvar)
{
CScopedDebugInfo dbg_info(amx, "orm_delvar", params, "ds");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
const char *column_name = nullptr;
amx_StrParam(amx, params[2], column_name);
if (column_name == nullptr || strlen(column_name) == 0)
{
CLog::Get()->LogNative(LogLevel::ERROR, "empty column name");
return 0;
}
CError<COrm> error;
if ((error = orm->RemoveVariable(column_name)))
{
CLog::Get()->LogNative(error);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native orm_setkey(ORM:id, const columnname[]);
AMX_DECLARE_NATIVE(Native::orm_setkey)
{
CScopedDebugInfo dbg_info(amx, "orm_setkey", params, "ds");
const OrmId_t ormid = params[1];
Orm_t orm = COrmManager::Get()->Find(ormid);
if (!orm)
{
CLog::Get()->LogNative(LogLevel::ERROR, "invalid orm id '{}'", ormid);
return 0;
}
const char *column_name = nullptr;
amx_StrParam(amx, params[2], column_name);
if (column_name == nullptr || strlen(column_name) == 0)
{
CLog::Get()->LogNative(LogLevel::ERROR, "empty column name");
return 0;
}
CError<COrm> error;
if ((error = orm->SetKeyVariable(column_name)))
{
CLog::Get()->LogNative(error);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native MySQL:mysql_connect(const host[], const user[], const password[],
// const database[], MySQLOpt:option_id = MySQLOpt:0);
AMX_DECLARE_NATIVE(Native::mysql_connect)
{
CScopedDebugInfo dbg_info(amx, "mysql_connect", params, "ss*sd");
OptionsId_t options_id = static_cast<OptionsId_t>(params[5]);
auto *options = COptionManager::Get()->GetOptionHandle(options_id);
if (options == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid option id '{}'", options_id);
return 0;
}
const char
*host_str = nullptr,
*user_str = nullptr,
*passwd_str = nullptr,
*db_str = nullptr;
amx_StrParam(amx, params[1], host_str);
amx_StrParam(amx, params[2], user_str);
amx_StrParam(amx, params[3], passwd_str);
amx_StrParam(amx, params[4], db_str);
CError<CHandle> handle_error;
Handle_t handle = CHandleManager::Get()->Create(
host_str, user_str, passwd_str, db_str,
options, handle_error);
if (handle_error)
{
CLog::Get()->LogNative(handle_error);
return 0;
}
assert(handle != nullptr);
cell ret_val = handle->GetId();
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native MySQL:mysql_connect_file(const file_name[] = "mysql.ini");
AMX_DECLARE_NATIVE(Native::mysql_connect_file)
{
CScopedDebugInfo dbg_info(amx, "mysql_connect_file", params, "s");
string file_name = amx_GetCppString(amx, params[1]);
//no directory seperators allowed
if (file_name.find_first_of("/\\") != string::npos)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"file \"{}\" not in SA-MP root folder", file_name);
return 0;
}
CError<CHandle> handle_error;
Handle_t handle = CHandleManager::Get()->CreateFromFile(file_name,
handle_error);
if (handle_error)
{
CLog::Get()->LogNative(handle_error);
return 0;
}
assert(handle != nullptr);
cell ret_val = handle->GetId();
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native mysql_close(MySQL:handle = MYSQL_DEFAULT_HANDLE);
AMX_DECLARE_NATIVE(Native::mysql_close)
{
CScopedDebugInfo dbg_info(amx, "mysql_close", params, "d");
const HandleId_t handle_id = static_cast<HandleId_t>(params[1]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);
return 0;
}
cell ret_val = CHandleManager::Get()->Destroy(handle) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native mysql_unprocessed_queries(MySQL:handle = MYSQL_DEFAULT_HANDLE);
AMX_DECLARE_NATIVE(Native::mysql_unprocessed_queries)
{
CScopedDebugInfo dbg_info(amx, "mysql_unprocessed_queries", params, "d");
const HandleId_t handle_id = static_cast<HandleId_t>(params[1]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);
return -1;
}
cell ret_val = handle->GetUnprocessedQueryCount();
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native mysql_global_options(E_MYSQL_GLOBAL_OPTION:type, value);
AMX_DECLARE_NATIVE(Native::mysql_global_options)
{
CScopedDebugInfo dbg_info(amx, "mysql_global_options", params, "dd");
using OptEnum = COptionManager::GlobalOption;
OptEnum option = static_cast<OptEnum>(params[1]);
switch (option)
{
//boolean type
case OptEnum::DUPLICATE_CONNECTIONS:
case OptEnum::DUPLICATE_CONNECTION_WARNING:
COptionManager::Get()->SetGlobalOption(
option, static_cast<bool>(params[2] != 0));
break;
default:
CLog::Get()->LogNative(LogLevel::ERROR,
"unknown option type '{}'", params[1]);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native MySQLOpt:mysql_init_options();
AMX_DECLARE_NATIVE(Native::mysql_init_options)
{
CScopedDebugInfo dbg_info(amx, "mysql_init_options", params);
cell ret_val = COptionManager::Get()->Create();
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native mysql_set_option(MySQLOpt:option_id, E_MYSQL_OPTION:type, ...);
AMX_DECLARE_NATIVE(Native::mysql_set_option)
{
CScopedDebugInfo dbg_info(amx, "mysql_set_option", params, "dd");
OptionsId_t options_id = static_cast<OptionsId_t>(params[1]);
auto *options = COptionManager::Get()->GetOptionHandle(options_id);
if (options == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid option id '{}'", options_id);
return 0;
}
if (params[0] == sizeof(cell) * 2)
{
CLog::Get()->LogNative(LogLevel::ERROR, "no value specified");
return 0;
}
cell *param_addr = nullptr;
amx_GetAddr(amx, params[3], ¶m_addr);
cell value = *param_addr;
bool ret_val = false;
const COptions::Type option = static_cast<COptions::Type>(params[2]);
switch (option)
{
//boolean types
case COptions::Type::AUTO_RECONNECT:
case COptions::Type::MULTI_STATEMENTS:
case COptions::Type::SSL_ENABLE:
ret_val = options->SetOption(option, static_cast<bool>(value != 0));
break;
//string types
case COptions::Type::SSL_KEY_FILE:
case COptions::Type::SSL_CERT_FILE:
case COptions::Type::SSL_CA_FILE:
case COptions::Type::SSL_CA_PATH:
case COptions::Type::SSL_CIPHER:
ret_val = options->SetOption(option, amx_GetCppString(amx, value));
break;
//other types
case COptions::Type::POOL_SIZE:
if (value >= 0 && value <= 32)
ret_val = options->SetOption(option,
static_cast<unsigned int>(value));
else
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid pool size '{}'", value);
break;
case COptions::Type::SERVER_PORT:
if (value >= 0 && value <= std::numeric_limits<unsigned short>::max())
ret_val = options->SetOption(option,
static_cast<unsigned int>(value));
else
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid MySQL server port '{}'", value);
break;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val ? 1 : 0);
return ret_val ? 1 : 0;
}
static bool SendQueryWithCallback(AMX *amx, cell *params,
CHandle::ExecutionType query_type)
{
const HandleId_t handle_id = static_cast<HandleId_t>(params[1]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);
return false;
}
string
callback_str = amx_GetCppString(amx, params[3]),
format_str = amx_GetCppString(amx, params[4]);
CError<CCallback> callback_error;
Callback_t callback = CCallback::Create(
amx,
callback_str.c_str(),
format_str.c_str(),
params, 5,
callback_error);
if (callback_error && callback_error.type() != CCallback::Error::EMPTY_NAME)
{
CLog::Get()->LogNative(callback_error);
return false;
}
string query_str = amx_GetCppString(amx, params[2]);
Query_t query = CQuery::Create(query_str);
if (callback != nullptr)
{
query->OnExecutionFinished([callback](ResultSet_t resultset)
{
CResultSetManager::Get()->SetActiveResultSet(resultset);
callback->Execute();
//unset active result(cache) + delete result (done by shared_ptr)
CResultSetManager::Get()->SetActiveResultSet(nullptr);
});
}
query->OnError(
[amx, handle_id, callback_str, query_str](unsigned int errorid,
string error)
{
CError<CCallback> error_cb_error;
//forward OnQueryError(errorid, const error[], const callback[], const query[], MySQL:handle);
Callback_t error_cb = CCallback::Create(error_cb_error, amx,
"OnQueryError", "dsssd",
errorid, error.c_str(),
callback_str.c_str(),
query_str.c_str(), handle_id);
if (!error_cb_error)
error_cb->Execute();
});
return handle->Execute(query_type, query);
}
// native mysql_pquery(MySQL:handle, const query[], const callback[] = "",
// const format[] = "", {Float,_}:...);
AMX_DECLARE_NATIVE(Native::mysql_pquery)
{
CScopedDebugInfo dbg_info(amx, "mysql_pquery", params, "dsss");
cell ret_val = SendQueryWithCallback(
amx, params, CHandle::ExecutionType::PARALLEL) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native mysql_tquery(MySQL:handle, const query[], const callback[] = "",
// const format[] = "", {Float,_}:...);
AMX_DECLARE_NATIVE(Native::mysql_tquery)
{
CScopedDebugInfo dbg_info(amx, "mysql_tquery", params, "dsss");
cell ret_val = SendQueryWithCallback(
amx, params, CHandle::ExecutionType::THREADED) ? 1 : 0;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native Cache:mysql_query(MySQL:handle, const query[], bool:use_cache = true);
AMX_DECLARE_NATIVE(Native::mysql_query)
{
CScopedDebugInfo dbg_info(amx, "mysql_query", params, "dsd");
const HandleId_t handle_id = static_cast<HandleId_t>(params[1]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);
return 0;
}
Query_t query = CQuery::Create(amx_GetCppString(amx, params[2]));
cell ret_val = 0;
if (handle->Execute(CHandle::ExecutionType::UNTHREADED, query)
&& params[3] != 0)
{
CResultSetManager::Get()->SetActiveResultSet(query->GetResult());
ret_val = CResultSetManager::Get()->StoreActiveResultSet();
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
bool ParseQueriesFromFile(const string &filepath, vector<string> &queries)
{
std::ifstream file(filepath);
if (file.fail())
return false;
string query_str;
while (file.good())
{
string tmp_query_str;
std::getline(file, tmp_query_str);
if (tmp_query_str.empty())
continue;
/*
* check for comments (start with "-- " or "#")
* a query could look like this:
* "SELECT stuff FROM table; -- selects # records"
* that's why we search for both comment specifiers
* and check for which comes first
* NOTE: we don't process C-style multiple-line comments,
* because the MySQL server handles them in a special way
*/
size_t comment_pos = std::min(tmp_query_str.find("-- "),
tmp_query_str.find('#'));
if (comment_pos != string::npos)
tmp_query_str.erase(comment_pos);
// append space to make multi-line SQL queries work
tmp_query_str.push_back(' ');
size_t sem_pos;
while ((sem_pos = tmp_query_str.find(';')) != string::npos)
{
query_str.append(tmp_query_str.substr(0U, sem_pos + 1));
tmp_query_str.erase(0, sem_pos + 1);
queries.push_back(query_str);
query_str.clear();
}
query_str.append(tmp_query_str);
}
return true;
}
// native mysql_tquery_file(MySQL:handle,
// const file_path[], const callback[] = "",
// const format[] = "", {Float,_}:...);
AMX_DECLARE_NATIVE(Native::mysql_tquery_file)
{
CScopedDebugInfo dbg_info(amx, "mysql_tquery_file", params, "dsss");
const HandleId_t handle_id = static_cast<HandleId_t>(params[1]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);
return 0;
}
string filename = amx_GetCppString(amx, params[2]);
if (filename.find("..") != string::npos)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid file path '{}'", filename);
return 0;
}
string filepath = "scriptfiles/" + filename;
std::ifstream file(filepath);
if (file.fail())
{
CLog::Get()->LogNative(LogLevel::ERROR, "can't open file '{}'", filepath);
return 0;
}
string
callback_str = amx_GetCppString(amx, params[3]),
format_str = amx_GetCppString(amx, params[4]);
CError<CCallback> callback_error;
Callback_t callback = CCallback::Create(
amx,
callback_str.c_str(),
format_str.c_str(),
params, 5,
callback_error);
if (callback_error && callback_error.type() != CCallback::Error::EMPTY_NAME)
{
CLog::Get()->LogNative(callback_error);
return 0;
}
std::function<void(std::string, unsigned int, std::string)> query_error_func
= [amx, handle_id, callback_str]
(string query_str, unsigned int errorid, string error)
{
CError<CCallback> error_cb_error;
//forward OnQueryError(errorid, const error[], const callback[], const query[], MySQL:handle);
Callback_t error_cb = CCallback::Create(error_cb_error, amx,
"OnQueryError", "dsssd",
errorid, error.c_str(),
callback_str.c_str(),
query_str.c_str(), handle_id);
if (!error_cb_error)
error_cb->Execute();
};
auto func = [](Handle_t handle, string file_path, Callback_t callback,
decltype(query_error_func) query_error_func)
{
vector<string> queries;
if (ParseQueriesFromFile(file_path, queries) && !queries.empty())
{
CLog::Get()->Log(LogLevel::DEBUG, "parsed {} queries for file '{}'",
queries.size(), file_path);
auto results = std::make_shared<vector<ResultSet_t>>();
for (auto i = queries.begin(); i != queries.end(); i++)
{
string query_str = *i;
Query_t query = CQuery::Create(query_str);
if (callback != nullptr)
{
bool is_last_query = ((i + 1) == queries.end());
query->OnExecutionFinished([=](ResultSet_t result)
{
results->push_back(result);
if (is_last_query)
{
CResultSetManager::Get()->SetActiveResultSet(
CResultSet::Merge(*results));
callback->Execute();
CResultSetManager::Get()->SetActiveResultSet(nullptr);
}
});
}
query->OnError(std::bind(query_error_func,
query_str, std::placeholders::_1, std::placeholders::_2));
handle->Execute(CHandle::ExecutionType::THREADED, query);
}
}
};
std::async(std::launch::async, std::move(func),
handle, filepath, callback, query_error_func);
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '1'");
return 1;
}
// native Cache:mysql_query_file(MySQL:handle,
// const file_path[], bool:use_cache = false);
AMX_DECLARE_NATIVE(Native::mysql_query_file)
{
CScopedDebugInfo dbg_info(amx, "mysql_query_file", params, "dsd");
const HandleId_t handle_id = static_cast<HandleId_t>(params[1]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);
return 0;
}
string filename = amx_GetCppString(amx, params[2]);
if (filename.find("..") != string::npos)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid file path '{}'", filename);
return 0;
}
vector<string> queries;
const string filepath = "scriptfiles/" + filename;
if (!ParseQueriesFromFile(filepath, queries))
{
CLog::Get()->LogNative(LogLevel::ERROR, "can't open file '{}'", filepath);
return 0;
}
CLog::Get()->LogNative(LogLevel::DEBUG, "parsed {} queries for file '{}'",
queries.size(), filepath);
bool store_result = (params[3] != 0);
vector<ResultSet_t> results;
for (auto query_str : queries)
{
Query_t query = CQuery::Create(query_str);
CLog::Get()->LogNative(LogLevel::DEBUG, "executing query '{}'", query_str);
if (!handle->Execute(CHandle::ExecutionType::UNTHREADED, query))
{
CLog::Get()->LogNative(LogLevel::ERROR, "failed to execute query '{}'", query_str);
return 0;
}
if (store_result)
results.push_back(query->GetResult());
}
cell ret_val = 0;
if (store_result)
{
CResultSetManager::Get()->SetActiveResultSet(CResultSet::Merge(results));
ret_val = CResultSetManager::Get()->StoreActiveResultSet();
}
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
// native mysql_errno(MySQL:handle = MYSQL_DEFAULT_HANDLE);
AMX_DECLARE_NATIVE(Native::mysql_errno)
{
CScopedDebugInfo dbg_info(amx, "mysql_errno", params, "d");
const HandleId_t handle_id = static_cast<HandleId_t>(params[1]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);
return -1;
}
unsigned int errorid = 0;
if (handle->GetErrorId(errorid) == false)
return -1;
cell ret_val = errorid;
CLog::Get()->LogNative(LogLevel::DEBUG, "return value: '{}'", ret_val);
return ret_val;
}
//native mysql_error(destination[], max_len = sizeof(destination),
// MySQL:handle = MYSQL_DEFAULT_HANDLE);
AMX_DECLARE_NATIVE(Native::mysql_error)
{
CScopedDebugInfo dbg_info(amx, "mysql_error", params, "rdd");
const HandleId_t handle_id = static_cast<HandleId_t>(params[3]);
Handle_t handle = CHandleManager::Get()->GetHandle(handle_id);
if (handle == nullptr)
{
CLog::Get()->LogNative(LogLevel::ERROR,
"invalid connection handle '{}'", handle_id);