forked from oscar-broman/sqlitei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlitei.inc
2889 lines (2154 loc) · 73.8 KB
/
sqlitei.inc
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
/*
SQLite Improved v0.9.7 by Slice
Changelog:
2015-07-13:
* Update for 0.3.7 R2.
* All crash issues should be solved now, including the previous one that happened only on some servers.
2014-10-08:
* Fix crash when using stmt_fetch_row on an empty result (only affects Linux servers).
2013-10-07:
* Display errors from db_query. Works only on Windows.
2012-07-23:
* Fix problem with persistent databases (hopefully).
2012-07-21:
* Fix crash happening on Linux related to NULL values in db_free_result.
2012-07-15:
* Improvements to persistent databases.
2012-06-15:
* Fix compiler crash when db_query isn't used.
2012-05-27:
* Always throw warnings when invalid results are given to SQLitei functions.
* Even more improvements to stability!
* GetAmxBase is now updated with a JIT compatible version (all credits to Zeex).
* All the default DB functions (which are hooked by SQLitei) are now compatible with the JIT plugin.
* Fixed a bug in SA-MP's SQLite implementation where strings would contain signed characters instead of unsigned.
Most functions work just fine with both types of strings but strcmp, for one, does not.
* stmt_bind_value now deals with packed strings properly.
2012-03-22:
* db_query now accept extra parameters similar to those in stmt_bind_value, allowing
a very quick way to format and run queries!
2012-03-21:
* Fixed a rare crash that would occur if you closed a DB while having autofreed results open.
* The deprecated db_query_autofree is now removed. Simply use db_query instead.
* Fixed a problem where a certain integer would cause an invalid value.
* Added DB::TYPE_UINT, which inserts an unsigned integer.
An unsigned integer have values between 0 and 4294967295 as opposed to -2147483648 and 2147483647.
* Added helper function db_print_query.
* Added db_dump_table!
2012-03-01:
* db_free_result will now completely ignore invalid results (0).
2012-02-12:
* Added db_attach_memory_db and db_detach_memory_db.
2012-02-11:
* Added DB::TYPE_ARRAY, which allows you to insert and read arrays with statements!
* Improved error handling and increased some buffer sizes.
* Added db_begin_transaction, db_end_transaction, and db_set_asynchronous.
* Minor improvements here and there.
2012-02-10:
* A few bug fixes.
* Added db_field_is_null, which returns true if the field is a true NULL value (not just an empty string).
* Deprecated db_query_autofree.
2012-02-09:
* Defining WP_Hash prior to inclusion is no longer needed.
* db_query now has an optional third argument - enable autorelease, which is
true by default.
* Improved stability on freeing results:
- Freeing results twice will not crash the server anymore - it will just generate
a warning.
- Freeing a result that will be autoreleased will remove it from the autorelease pool.
* The compiler will no longer wrongfully detect recursion inside this include.
* Added db_query_int and db_query_float.
* Added db_get_struct_info and db_set_struct_info; used mainly internally.
* Added db_exec and db_insert.
* Performance improvements!
2012-02-08:
* db_print_result will no longer go to the end of the result.
* Added db_get_row_index, db_set_row_index, and db_rewind.
2012-02-07:
* db_get_field/db_get_field_assoc will not crash anymore with NULL values!
2011-12-21:
* Added two new types for stmt_bind_value:
- DB::TYPE_WP_HASH: Puts a BLOB value of a whirlpool hash from the given string into the query (ex. x'FFAA4411...').
- DB::TYPE_PLAYER_NAME: Puts a player name from the ID passed.
Note that DB_USE_WHIRLPOOL must be defined as true in order for DB::TYPE_WP_HASH to work.
* Made some optimizations and minor bug fixes.
* stmt_execute will now autofree the result unless the 3rd argument is false.
* Added the preprocessor options DE_DEBUG (logs debug info) and DB_LOG_TO_CHAT (prints log messages to chat).
* Improved the way results are dealt with internally to avoid crashes at all costs.
* Added debug messages pretty much everywhere.
2011-12-16:
* Added db_open_persistent, db_is_persistent, db_is_valid_persistent, and db_free_persistent.
* Added db_query_autofree.
* Added db_get_field_int and db_get_field_float.
* Corrected a few SQLite natives.
2011-12-15:
* Added stmt_autoclose.
* Memory usage decreased significantly.
* All functions now accept both packed and unpacked strings.
* Minor bug fixes and optimizations.
2011-12-14:
* Initial release.
*/
#if !defined _samp_included
#error Please include a_samp before sqlitei.
#endif
#if defined __fmt_funcinc && defined FormatSpecifier
#error Please include sqlitei before formatex.
#endif
#if !defined HTTP
#tryinclude <a_http>
#endif
#if !defined DB_MAX_PARAMS
#define DB_MAX_PARAMS 32
#endif
#if !defined DB_MAX_STATEMENTS
#define DB_MAX_STATEMENTS 16
#endif
#if !defined DB_MAX_STATEMENT_SIZE
#define DB_MAX_STATEMENT_SIZE 1024
#endif
#if !defined DB_MAX_FIELDS
#define DB_MAX_FIELDS 64
#endif
#if !defined DB_MAX_PERSISTENT_DATABASES
#define DB_MAX_PERSISTENT_DATABASES 4
#endif
#if !defined DB_USE_WHIRLPOOL
#define DB_USE_WHIRLPOOL false
#endif
#if !defined DB_DEBUG
#define DB_DEBUG false
#endif
#if !defined DB_DEBUG_BACKTRACE_NOTICE
#define DB_DEBUG_BACKTRACE_NOTICE false
#endif
#if !defined DB_DEBUG_BACKTRACE_WARNING
#define DB_DEBUG_BACKTRACE_WARNING false
#endif
#if !defined DB_DEBUG_BACKTRACE_ERROR
#define DB_DEBUG_BACKTRACE_ERROR false
#endif
#if !defined DB_DEBUG_BACKTRACE_DEBUG
#define DB_DEBUG_BACKTRACE_DEBUG false
#endif
#if !defined DB_LOG_TO_CHAT
#define DB_LOG_TO_CHAT false
#endif
// "Namespace"
#define DB:: DB_
// Fix some natives ("const" keyword was missing)
native DB:db_open@(const szName[]) = db_open;
native DBResult:db_query@(DB:db, const szQuery[]) = db_query;
native db_get_field@(DBResult:dbresult, field, result[], maxlength) = db_get_field;
native db_get_field_assoc@(DBResult:dbresult, const field[], result[], maxlength) = db_get_field_assoc;
native db_close@(DB:db) = db_close;
native db_free_result@(DBResult:dbrResult) = db_free_result;
#if defined _ALS_db_open
#undef db_open
#else
#define _ALS_db_open
#endif
#define db_open db_open@
#if defined _ALS_db_query
#undef db_query
#else
#define _ALS_db_query
#endif
#define db_query%1( db_query_hook(_,
#if defined _ALS_db_close
#undef db_close
#else
#define _ALS_db_close
#endif
#define db_close db_close_hook
#if defined _ALS_db_free_result
#undef db_free_result
#else
#define _ALS_db_free_result
#endif
#define db_free_result db_free_result_hook
#if DB::USE_WHIRLPOOL
native DB::WP_Hash(buffer[], len, const str[]) = WP_Hash;
#endif
enum DB::e_SYNCHRONOUS_MODE {
DB::SYNCHRONOUS_OFF,
DB::SYNCHRONOUS_NORMAL,
DB::SYNCHRONOUS_FULL
};
enum DBDataType: {
DB::TYPE_NONE,
DB::TYPE_NULL,
DB::TYPE_INT,
DB::TYPE_INTEGER = DB::TYPE_INT,
DB::TYPE_UINT,
DB::TYPE_UINTEGER = DB::TYPE_UINT,
DB::TYPE_FLOAT,
DB::TYPE_STRING,
DB::TYPE_RAW_STRING,
DB::TYPE_IDENTIFIER,
// Special types
#if DB::USE_WHIRLPOOL
DB::TYPE_WP_HASH,
#endif
DB::TYPE_PLAYER_NAME,
DB::TYPE_PLAYER_IP,
DB::TYPE_ARRAY
};
#define INT: DB::TYPE_INT,QQPA:
#define INTEGER: DB::TYPE_INT,QQPA:
#define UINT: DB::TYPE_UINT,QQPA:
#define UINTEGER: DB::TYPE_UINT,QQPA:
#define FLOAT: DB::TYPE_FLOAT,QQPA:
#define STRING: DB::TYPE_STRING,QQPA:
#define RAW_STRING: DB::TYPE_RAW_STRING,QQPA:
#define IDENTIFIER: DB::TYPE_IDENTIFIER,QQPA:
#if DB::USE_WHIRLPOOL
#define WP_HASH: DB::TYPE_WP_HASH,QQPA:
#endif
#define PLAYER_NAME: DB::TYPE_PLAYER_NAME,QQPA:
#define PLAYER_IP: DB::TYPE_PLAYER_IP,QQPA:
enum DB::E_STATEMENT {
// The ready-to-run query.
e_szQuery[DB::MAX_STATEMENT_SIZE + 1],
// Parameter count
e_iParams,
// The parameter types.
DBDataType:e_aiParamTypes[DB::MAX_PARAMS],
// Position of parameters in the query string.
e_aiParamPositions[DB::MAX_PARAMS],
// Length of parameters in the query string.
e_aiParamLengths[DB::MAX_PARAMS],
// Types of bound return fields
DBDataType:e_aiFieldTypes[DB::MAX_FIELDS],
// Sizes of bound result fields (used only for strings currently)
e_aiFieldSizes[DB::MAX_FIELDS],
// Addresses of bound result fields
e_aiFieldAddresses[DB::MAX_FIELDS],
// The database it was created for
DB:e_dbDatabase,
// The result (after executing)
DBResult:e_dbrResult,
// How many rows were fetched from the most recent result
e_iFetchedRows,
// Whether or not any leftover results should be automatically freed
// Note that whenever a new result is put into e_dbrResult, the previous one is freed.
bool:e_bAutoFreeResult
};
enum {
SQLITE_LIMIT_LENGTH,
SQLITE_LIMIT_SQL_LENGTH,
SQLITE_LIMIT_COLUMN,
SQLITE_LIMIT_EXPR_DEPTH,
SQLITE_LIMIT_COMPOUND_SELECT,
SQLITE_LIMIT_VDBE_OP,
SQLITE_LIMIT_FUNCTION_ARG,
SQLITE_LIMIT_ATTACHED,
SQLITE_LIMIT_LIKE_PATTERN_LEN,
SQLITE_LIMIT_VARIABLE_NUMBER,
SQLITE_LIMIT_TRIGGER_DEPTH
};
// SQLite 3 C-struct offsets
enum e_SQLITE3 {
sqlite3_pVfs [4],
sqlite3_iBackends [4],
sqlite3_pBackends [4],
sqlite3_iFlags [4],
sqlite3_iOpenFlags [4],
sqlite3_iErrorCode [4],
sqlite3_iErrorMask [4],
bool:sqlite3_bAutoCommit [1],
sqlite3_iTempStore [1],
bool:sqlite3_bMallocFailed [1],
sqlite3_iDefaultLockMode [1],
sqlite3_iNextAutovac [1],
bool:sqlite3_bSuppressErrors [1],
sqlite3_iPad [2],
sqlite3_iNextPagesize [4],
sqlite3_iNumTables [4],
sqlite3_pDefaultCollation [4],
sqlite3_iLastRowid [4],
sqlite3_iLastRowidUpperBytes[4],
sqlite3_iMagic [4],
sqlite3_iNumChanges [4],
sqlite3_iNumTotalChanges [4],
sqlite3_aiLimits [SQLITE_LIMIT_TRIGGER_DEPTH * 4],
sqlite3_aInitInfo [12],
sqlite3_iNumExtensions [4],
sqlite3_pExtensions [4],
sqlite3_pVdbe [4],
sqlite3_iActiveVdbeCount [4],
sqlite3_iWritingVdbeCount [4],
sqlite3_pTraceFunc [4],
sqlite3_pTraceArg [4],
sqlite3_pProfilingFunc [4],
sqlite3_pProfilingArg [4],
sqlite3_pCommitArg [4],
sqlite3_pCommitCallback [4],
sqlite3_pRollbackArg [4],
sqlite3_pRollbackCallback [4],
sqlite3_pUpdateArg [4],
sqlite3_pUpdateCallback [4],
sqlite3_pWalCallback [4],
sqlite3_pWalArg [4],
sqlite3_pCollNeeded [4],
sqlite3_pCollNeeded16 [4],
sqlite3_pCollNeededArg [4],
sqlite3_pError [4],
sqlite3_pErrorMsg [4],
sqlite3_pErrorMsg16 [4]
};
enum DB::E_PERSISTENT_DB {
e_szName[128 char],
bool:e_bIsUsed,
DB:e_dbDatabase
};
static stock
gs_szBuffer[8192],
gs_aiCompressBuffer[3072],
gs_Statements[DBStatement:DB::MAX_STATEMENTS][DB::E_STATEMENT],
gs_iAutoFreeTimer = -1,
gs_iAutoFreeResultsIndex = 0,
DBResult:gs_adbrAutoFreeResults[1024],
gs_iAutoCloseStatementsIndex = 0,
DBStatement:gs_astAutoCloseStatements[1024],
gs_PersistentDatabases[DB::MAX_PERSISTENT_DATABASES][DB::E_PERSISTENT_DB],
gs_iClosePersistentTimer = -1,
gs_iFreeStatementResultsTimer = -1,
gs_szNull[1] = {0}
;
const
DBStatement:DB::INVALID_STATEMENT = DBStatement:-1,
DBResult:DB::INVALID_RESULT = DBResult:0
;
#if !DB_DEBUG
#define DB_Debug(%1)%0;
#endif
#if !DB_LOG_TO_CHAT
#if DB_DEBUG_BACKTRACE_NOTICE
#define DB_Notice(%1) print(!"SQLitei Notice: " %1),PrintAmxBacktrace()
#define DB_Noticef(%1) printf("SQLitei Notice: " %1),PrintAmxBacktrace()
#else
#define DB_Notice(%1) print(!"SQLitei Notice: " %1)
#define DB_Noticef(%1) printf("SQLitei Notice: " %1)
#endif
#if DB_DEBUG_BACKTRACE_WARNING
#define DB_Warning(%1) print(!"SQLitei Warning: " %1),PrintAmxBacktrace()
#define DB_Warningf(%1) printf("SQLitei Warning: " %1),PrintAmxBacktrace()
#else
#define DB_Warning(%1) print(!"SQLitei Warning: " %1)
#define DB_Warningf(%1) printf("SQLitei Warning: " %1)
#endif
#if DB_DEBUG_BACKTRACE_ERROR
#define DB_Error(%1) print(!"SQLitei Error: " %1),PrintAmxBacktrace()
#define DB_Errorf(%1) printf("SQLitei Error: " %1),PrintAmxBacktrace()
#else
#define DB_Error(%1) print(!"SQLitei Error: " %1)
#define DB_Errorf(%1) printf("SQLitei Error: " %1)
#endif
#if DB_DEBUG
#if DB_DEBUG_BACKTRACE_DEBUG
#define DB_Debug(%1) printf("SQLitei Debug: " %1),PrintAmxBacktrace()
#else
#define DB_Debug(%1) printf("SQLitei Debug: " %1)
#endif
#endif
#else
new
gs_szLogMessageBuffer[256]
;
#define DB_Notice(%1) SendClientMessageToAll(0xFFFFFFFF, "SQLitei Notice: " %1), print(!"SQLitei Notice: " %1)
#define DB_Warning(%1) SendClientMessageToAll(0xEBBD17FF, "SQLitei Warning: " %1), print(!"SQLitei Warning: " %1)
#define DB_Error(%1) SendClientMessageToAll(0xCC0000FF, "SQLitei Error: " %1), print(!"SQLitei Error: " %1)
#define DB_Noticef(%1) format(gs_szLogMessageBuffer, sizeof(gs_szLogMessageBuffer), "SQLitei Notice: " %1), print(gs_szLogMessageBuffer), SendClientMessageToAll(0xDDDDDDFF, gs_szLogMessageBuffer)
#define DB_Warningf(%1) format(gs_szLogMessageBuffer, sizeof(gs_szLogMessageBuffer), "SQLitei Warning: " %1), print(gs_szLogMessageBuffer), SendClientMessageToAll(0xEBBD17FF, gs_szLogMessageBuffer)
#define DB_Errorf(%1) format(gs_szLogMessageBuffer, sizeof(gs_szLogMessageBuffer), "SQLitei Error: " %1), print(gs_szLogMessageBuffer), SendClientMessageToAll(0xCC0000FF, gs_szLogMessageBuffer)
#if DB_DEBUG
#define DB_Debug(%1) format(gs_szLogMessageBuffer, sizeof(gs_szLogMessageBuffer), "SQLitei Debug: " %1), print(gs_szLogMessageBuffer), SendClientMessageToAll(0xFFFFFFFF, gs_szLogMessageBuffer)
#endif
#endif
// Has to be defined after statement functions.
forward DBResult:db_query_hook(iTagOf3 = tagof(_bAutoRelease), DB:db, const szQuery[], {bool, DBDataType}:_bAutoRelease = true, {DBDataType, QQPA}:...);
// forward's
forward bool:db_set_row_index(DBResult:dbrResult, iRow);
forward bool:db_free_result_hook(DBResult:dbrResult);
forward bool:db_set_synchronous(DB:db, DB::e_SYNCHRONOUS_MODE:iValue);
forward DB::funcinc();
public DB::funcinc() {
strcat(gs_szBuffer, "");
strpack(gs_szBuffer, "");
ispacked(gs_szBuffer);
db_get_field(DBResult:0, 0, gs_szBuffer, 0);
db_query(DB:0,"");
}
static stock DB::CompressArray(const aiArray[], iSize = sizeof(aiArray), aiOutput[]) {
new
iOutputIndex = 4,
iValue,
iMSB,
iShift
;
// * 0b11000000 = Single byte, negative
// * 0b10000000 = Single byte
// * 0b01000000 = Multi-byte
// - 0b01000000 = More bytes
// - 0b11000000 = Last byte
// - 0b10000000 = Unused
for (new i = 0; i < iSize; i++) {
// Will the value fit in one byte?
iValue = aiArray[i];
if (-0b00111111 <= iValue <= 0b00111111) {
// Is the value negative?
if (iValue & 0x80000000) {
// Set the "single byte, negative" bits on and put the value without its sign
aiOutput{iOutputIndex++} = 0b11000000 | -iValue;
} else {
// Just put the value in with the "single byte" bit
aiOutput{iOutputIndex++} = 0b10000000 | iValue;
}
} else {
// Figure out how many bits we'll have to write
iMSB = DB::FindMSB(iValue) + 1;
// Make iShift a multiple of 6 (if it isn't already)
if ((iShift = iMSB % 6))
aiOutput{iOutputIndex++} = 0b01000000 | (iValue >>> (iMSB - iShift) & ~(0xFFFFFFFF << iShift));
iShift = iMSB - iShift;
// Write bits out left-right
while ((iShift -= 6) >= 0)
aiOutput{iOutputIndex++} = 0b01000000 | (iValue >>> iShift & 0b00111111);
// Change the "more bytes" bits into "last byte"
aiOutput{iOutputIndex - 1} |= 0b11000000;
}
}
// Put the number of bytes we just wrote into the first cell of the output
aiOutput[0] = 0x80808080 | ((iOutputIndex & 0x1FE00000) << 3) | ((iOutputIndex & 0x3FC000) << 2) | ((iOutputIndex & 0x7F80) << 1) | (iOutputIndex & 0x7F);
// Make sure the bytes in the last cell are 0
aiOutput{iOutputIndex} = 0;
iValue = iOutputIndex;
while (++iOutputIndex % 4)
aiOutput{iOutputIndex} = 0;
// Return the number of bytes written (not counting the first 4)
return iValue;
}
static stock DB::DecompressArray(const aiCompressedArray[], aiOutput[], iOutputSize = sizeof(aiOutput)) {
new
iBytes,
iOutputIndex = 0
;
// Get the number of bytes to parse
iBytes = aiCompressedArray[0];
iBytes = ((iBytes & 0x7F000000) >>> 3) | ((iBytes & 0x7F0000) >>> 2) | ((iBytes & 0x7F00) >>> 1) | (iBytes & 0x7F);
for (new i = 4; i < iBytes; i++) {
// Out of slots?
if (iOutputIndex >= iOutputSize) {
DB::Error("(DB::DecompressArray) Compressed array is larger than decompress buffer.");
break;
}
// Single byte?
if ((aiCompressedArray{i} & 0b10000000)) {
// Negative?
if ((aiCompressedArray{i} & 0b01000000))
aiOutput[iOutputIndex++] = -(aiCompressedArray{i} & 0b00111111);
else
aiOutput[iOutputIndex++] = (aiCompressedArray{i} & 0b00111111);
} else {
// Multi byte; read the last bits
aiOutput[iOutputIndex] = aiCompressedArray{i} & 0b00111111;
// Keep reading bits while shifting the value to the left
do {
aiOutput[iOutputIndex] <<= 6;
aiOutput[iOutputIndex] |= aiCompressedArray{++i} & 0b00111111;
} while ((aiCompressedArray{i} & 0b10000000) == 0);
iOutputIndex++;
}
}
return iOutputIndex;
}
static stock DB::memset(aArray[], iValue, iSize = sizeof(aArray)) {
new
iAddress
;
// Store the address of the array
#emit LOAD.S.pri 12
#emit STOR.S.pri iAddress
// Convert the size from cells to bytes
iSize *= 4;
// Loop until there is nothing more to fill
while (iSize > 0) {
// I have to do this because the FILL instruction doesn't accept a dynamic number.
if (iSize >= 4096) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 4096
iSize -= 4096;
iAddress += 4096;
} else if (iSize >= 1024) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 1024
iSize -= 1024;
iAddress += 1024;
} else if (iSize >= 256) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 256
iSize -= 256;
iAddress += 256;
} else if (iSize >= 64) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 64
iSize -= 64;
iAddress += 64;
} else if (iSize >= 16) {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 16
iSize -= 16;
iAddress += 16;
} else {
#emit LOAD.S.alt iAddress
#emit LOAD.S.pri iValue
#emit FILL 4
iSize -= 4;
iAddress += 4;
}
}
// aArray is used, just not by its symbol name
#pragma unused aArray
return 1;
}
stock DB::LazyInitialize() {
static
bool:bIsInitialized = false
;
if (bIsInitialized)
return;
bIsInitialized = true;
#if defined HTTP
HTTP(0x7FEDCBA0, HTTP_GET, !"spelsajten.net/sqlitei_version.php?version=097", "", !"DB_VersionCheckReponse");
#endif
}
#if defined HTTP
forward DB::VersionCheckReponse(iIndex, iResponseCode, const szResponse[]);
public DB::VersionCheckReponse(iIndex, iResponseCode, const szResponse[]) {
if (iResponseCode == 200) {
if (strval(szResponse) != 1) {
print(!"\n\n\n *******************************************************************");
print( !" * There's a new version version of SQLite Improved available! *");
print( !" * Please visit the topic at the SA-MP forums for the latest copy. *");
print( !" * Alternatively, get it here: http://spelsajten.net/sqlitei.inc *");
print( !" *******************************************************************\n\n\n");
}
}
}
#endif
stock db_escape_string(szString[], const szEnclosing[] = "'", iSize = sizeof(szString)) {
DB::LazyInitialize();
new
iPos
;
while (-1 != (iPos = strfind(szString, szEnclosing, _, iPos))) {
strins(szString, szEnclosing, iPos, iSize);
iPos += 2;
}
}
stock bool:db_is_persistent(DB:db) {
return !!(_:db & 0x80000000);
}
stock bool:db_is_valid_persistent(DB:db) {
new
iIndex = (_:db & 0x7FFFFFFF)
;
if ((0 <= iIndex < sizeof(gs_PersistentDatabases)) && gs_PersistentDatabases[iIndex][e_bIsUsed])
return true;
return false;
}
stock bool:db_is_table_exists(DB:db, const szTable[])
{
new
DBResult:dbrResult
;
format(gs_szBuffer, sizeof(gs_szBuffer), "SELECT name FROM sqlite_master WHERE type = 'table' AND tbl_name = '%s'", szTable);
dbrResult = db_query(db, gs_szBuffer, false);
if (db_num_fields(dbrResult)) {
db_free_result(dbrResult);
return true;
}
db_free_result(dbrResult);
return false;
}
stock bool:db_rewind(DBResult:dbrResult) {
if (dbrResult == DB::INVALID_RESULT) {
DB::Notice("(db_rewind) Invalid result given.");
return false;
}
return db_set_row_index(dbrResult, 0);
}
stock bool:db_exec(DB:db, const szQuery[]) {
new
DBResult:dbrResult = db_query(db, szQuery, false)
;
if (dbrResult) {
db_free_result(dbrResult);
return true;
}
return false;
}
stock db_insert(DB:db, const szQuery[]) {
new
DBResult:dbrResult = db_query(db, szQuery, false)
;
if (dbrResult) {
db_free_result(dbrResult);
return db_last_insert_rowid(db);
}
return 0;
}
stock db_get_struct_info(DB:db, {_, e_SQLITE3}:iOffset) {
if (db_is_persistent(db)) {
if (!db_is_valid_persistent(db)) {
DB::Errorf("(db_get_struct_info) Invalid persistent database given (%04x%04x).", _:db >>> 16, _:db & 0xFFFF);
return 0;
}
new iIndex = (_:db & 0x7FFFFFFF);
db = gs_PersistentDatabases[iIndex][e_dbDatabase];
if (!db) {
DB::Errorf("(db_get_struct_info) Closed persistent database given (%04x%04x).", _:db >>> 16, _:db & 0xFFFF);
return 0;
}
}
new
iAddress = db_get_mem_handle(db & DB:0x7FFFFFFF) - DB::GetAmxBaseRelative() + iOffset,
iValue
;
#emit LREF.S.pri iAddress
#emit STOR.S.pri iValue
return iValue;
}
stock db_set_struct_info(DB:db, {_, e_SQLITE3}:iOffset, iValue) {
if (db_is_persistent(db)) {
if (!db_is_valid_persistent(db)) {
DB::Errorf("(db_set_struct_info) Invalid persistent database given (%04x%04x).", _:db >>> 16, _:db & 0xFFFF);
return 0;
}
new iIndex = (_:db & 0x7FFFFFFF);
db = gs_PersistentDatabases[iIndex][e_dbDatabase];
if (!db) {
DB::Errorf("(db_set_struct_info) Closed persistent database given (%04x%04x).", _:db >>> 16, _:db & 0xFFFF);
return 0;
}
}
new
iAddress = db_get_mem_handle(db & DB:0x7FFFFFFF) - DB::GetAmxBaseRelative() + iOffset
;
#emit LOAD.S.pri iValue
#emit SREF.S.pri iAddress
}
stock bool:db_set_row_index(DBResult:dbrResult, iRow) {
if (dbrResult == DB::INVALID_RESULT) {
DB::Notice("(db_set_row_index) Invalid result given.");
return false;
}
if (iRow < 0 || iRow >= db_num_rows(dbrResult))
return false;
new
iAddress = db_get_result_mem_handle(dbrResult) - DB::GetAmxBaseRelative() + 16
;
#emit LOAD.S.pri iRow
#emit SREF.S.pri iAddress
return true;
}
stock db_get_row_index(DBResult:dbrResult) {
if (dbrResult == DB::INVALID_RESULT) {
DB::Notice("(db_get_row_index) Invalid result given.");
return 0;
}
new
iAddress = db_get_result_mem_handle(dbrResult) - DB::GetAmxBaseRelative() + 16
;
#emit LREF.S.pri iAddress
#emit STACK 4
#emit RETN
return 0;
}
stock DB:db_open_persistent(const szName[]) {
new
DB:db,
iIndex = -1
;
for (new i = 0; i < sizeof(gs_PersistentDatabases); i++) {
if (!gs_PersistentDatabases[i][e_bIsUsed]) {
iIndex = i;
break;
}
}
if (iIndex == -1) {
DB::Error("(db_open_persistent) Unable to find a free slot.");
return DB:-1;
}
if (!(db = db_open(szName))) {
DB::Error("(db_open_persistent) Unable to open the database.");
return DB:-1;
}
gs_PersistentDatabases[iIndex][e_bIsUsed] = true;
gs_PersistentDatabases[iIndex][e_dbDatabase] = db;
gs_PersistentDatabases[iIndex][e_szName][0] = 0;
if (gs_iClosePersistentTimer == -1)
gs_iClosePersistentTimer = SetTimer("db_close_persistent", 0, false);
strpack(gs_PersistentDatabases[iIndex][e_szName], szName, _:e_bIsUsed);
DB::Debug("(db_open_persistent=%d) Opened new persistent DB.", iIndex);
return DB:(iIndex | 0x80000000);
}
stock db_close_hook(DB:db) {
new
iIndex
;
if (db_is_persistent(db)) {
if (!db_is_valid_persistent(db)) {
DB::Errorf("(db_close) Invalid persistent database given (%04x%04x).", _:db >>> 16, _:db & 0xFFFF);
return;
}
iIndex = (_:db & 0x7FFFFFFF);
if (gs_PersistentDatabases[iIndex][e_dbDatabase]) {
db_close@(gs_PersistentDatabases[iIndex][e_dbDatabase]);
gs_PersistentDatabases[iIndex][e_dbDatabase] = DB:0;
DB::Debug("(db_close) Closed the DB for the persistent DB with index %d.", iIndex);
} else {
DB::Debug("(db_close) Would close the DB for the persistent DB with index %d, but it already is.", iIndex);
}
} else {
db_close@(db);
}
}
stock db_query_int(DB:db, const szQuery[], iField = 0) {
new
DBResult:dbrResult = db_query(db, szQuery, false)
;
if (!dbrResult) {
strunpack(gs_szBuffer, szQuery);
DB::Warningf("(db_query_int) Query failed: \"%s\".", gs_szBuffer);
return 0;
}
db_get_field(dbrResult, iField, gs_szBuffer, sizeof(gs_szBuffer) - 1);
db_free_result(dbrResult);
return strval(gs_szBuffer);
}
stock Float:db_query_float(DB:db, const szQuery[], iField = 0) {
new
DBResult:dbrResult = db_query(db, szQuery, false)
;
if (!dbrResult) {
strunpack(gs_szBuffer, szQuery);
DB::Warningf("(db_query_float) Query failed: \"%s\".", gs_szBuffer);
return 0.0;
}
db_get_field(dbrResult, iField, gs_szBuffer, sizeof(gs_szBuffer) - 1);
db_free_result(dbrResult);
return floatstr(gs_szBuffer);
}
stock db_is_result_freed(DBResult:dbrResult) {
if (dbrResult == DB::INVALID_RESULT) {
DB::Notice("(db_is_result_freed) Invalid result given.");
return true;
}
return db_get_result_mem_handle(dbrResult) == 0;
}
stock bool:db_free_result_hook(DBResult:dbrResult) {
if (dbrResult == DB::INVALID_RESULT) {
DB::Notice("(db_free_result_hook) Invalid result given.");
return false;
}
DB::Debug("(db_free_result) Freeing 0x%04x%04x.", _:dbrResult >>> 16, _:dbrResult & 0xFFFF);
for (new i = gs_iAutoFreeResultsIndex; i--; ) {
if (gs_adbrAutoFreeResults[i] == dbrResult) {
gs_adbrAutoFreeResults[i] = DBResult:0;
DB::Debug("(db_free_result) The result being freed was inside the autorelease pool.");
}
}
new
iFreeTestAddress = db_get_result_mem_handle(dbrResult) - DB::GetAmxBaseRelative() + 16,
iData
;
#emit LREF.S.pri iFreeTestAddress
#emit STOR.S.pri iData
if (iData != 0xFFFFFFFF) {
new