/
sqlInt.h
4460 lines (4088 loc) · 158 KB
/
sqlInt.h
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
/*
* Copyright 2010-2017, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``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
* <COPYRIGHT HOLDER> 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.
*/
/*
* Internal interface definitions for sql.
*
*/
#ifndef sqlINT_H
#define sqlINT_H
#define IdChar(C) ((sqlCtypeMap[(unsigned char)C]&0x46)!=0)
/* Special Comments:
*
* Some comments have special meaning to the tools that measure test
* coverage:
*
* NO_TEST - The branches on this line are not
* measured by branch coverage. This is
* used on lines of code that actually
* implement parts of coverage testing.
*
* OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
* and the correct answer is still obtained,
* though perhaps more slowly.
*
* OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
* and the correct answer is still obtained,
* though perhaps more slowly.
*
* PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
* that would be harmless and undetectable
* if it did occur.
*
* In all cases, the special comment must be enclosed in the usual
* slash-asterisk...asterisk-slash comment marks, with no spaces between the
* asterisks and the comment text.
*/
#include <stdbool.h>
#include "box/column_mask.h"
#include "parse_def.h"
#include "box/field_def.h"
#include "box/func.h"
#include "box/func_def.h"
#include "box/trigger_def.h"
#include "box/sql.h"
#include "box/txn.h"
#include "trivia/util.h"
/*
* These #defines should enable >2GB file support on POSIX if the
* underlying operating system supports it. If the OS lacks
* large file support, these should be no-ops.
*
* Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
* system #includes. Hence, this block of code must be the very first
* code in all source files.
*
* Large file support can be disabled using the -Dsql_DISABLE_LFS switch
* on the compiler command line. This is necessary if you are compiling
* on a recent machine (ex: Red Hat 7.2) but you want your code to work
* on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
* without this option, LFS is enable. But LFS does not exist in the kernel
* in Red Hat 6.0, so the code won't work. Hence, for maximum binary
* portability you should omit LFS.
*
* The previous paragraph was written in 2005. (This paragraph is written
* on 2008-11-28.) These days, all Linux kernels support large files, so
* you should probably leave LFS enabled. But some embedded platforms might
* lack LFS in which case the sql_DISABLE_LFS macro might still be useful.
*
* Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
*/
#ifndef SQL_DISABLE_LFS
#define _LARGE_FILE 1
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#define _LARGEFILE_SOURCE 1
#endif
/* What version of GCC is being used. 0 means GCC is not being used */
#ifdef __GNUC__
#define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
#else
#define GCC_VERSION 0
#endif
/* Needed for various definitions... */
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include "sqlLimit.h"
/*
* Include standard header files as necessary
*/
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
/*
* The following macros are used to cast pointers to integers and
* integers to pointers. The way you do this varies from one compiler
* to the next, so we have developed the following set of #if statements
* to generate appropriate macros for a wide range of compilers.
*
* The correct "ANSI" way to do this is to use the intptr_t type.
* Unfortunately, that typedef is not available on all compilers, or
* if it is available, it requires an #include of specific headers
* that vary from one machine to the next.
*
* Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
* the ((void*)&((char*)0)[X]) construct.
*/
#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
#define SQL_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
#define SQL_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
#define SQL_INT_TO_PTR(X) ((void*)&((char*)0)[X])
#define SQL_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
#define SQL_INT_TO_PTR(X) ((void*)(intptr_t)(X))
#define SQL_PTR_TO_INT(X) ((int)(intptr_t)(X))
#else /* Generates a warning - but it always works */
#define SQL_INT_TO_PTR(X) ((void*)(X))
#define SQL_PTR_TO_INT(X) ((int)(X))
#endif
/*
* A macro to hint to the compiler that a function should not be
* inlined.
*/
#if defined(__GNUC__)
#define SQL_NOINLINE __attribute__((noinline))
#else
#define SQL_NOINLINE
#endif
#if defined(SQL_SYSTEM_MALLOC) \
+ defined(SQL_ZERO_MALLOC) > 1
#error "Two or more of the following compile-time configuration options\
are defined but at most one is allowed:\
SQL_SYSTEM_MALLOC, SQL_ZERO_MALLOC"
#endif
#if defined(SQL_SYSTEM_MALLOC) \
+ defined(SQL_ZERO_MALLOC) == 0
#define SQL_SYSTEM_MALLOC 1
#endif
/*
* If sql_MALLOC_SOFT_LIMIT is not zero, then try to keep the
* sizes of memory allocations below this value where possible.
*/
#if !defined(SQL_MALLOC_SOFT_LIMIT)
#define SQL_MALLOC_SOFT_LIMIT 1024
#endif
/*
* Enable sql_ENABLE_EXPLAIN_COMMENTS if sql_DEBUG is turned on.
*/
#if !defined(SQL_ENABLE_EXPLAIN_COMMENTS) && defined(SQL_DEBUG)
#define SQL_ENABLE_EXPLAIN_COMMENTS 1
#endif
/*
* The testcase() macro is used to aid in coverage testing. When
* doing coverage testing, the condition inside the argument to
* testcase() must be evaluated both true and false in order to
* get full branch coverage. The testcase() macro is inserted
* to help ensure adequate test coverage in places where simple
* condition/decision coverage is inadequate. For example, testcase()
* can be used to make sure boundary values are tested. For
* bitmask tests, testcase() can be used to make sure each bit
* is significant and used at least once. On switch statements
* where multiple cases go to the same block of code, testcase()
* can insure that all cases are evaluated.
*
*/
#define testcase(X)
/*
* The TESTONLY macro is used to enclose variable declarations or
* other bits of code that are needed to support the arguments
* within testcase() and assert() macros.
*/
#if !defined(NDEBUG)
#define TESTONLY(X) X
#else
#define TESTONLY(X)
#endif
/*
* Sometimes we need a small amount of code such as a variable initialization
* to setup for a later assert() statement. We do not want this code to
* appear when assert() is disabled. The following macro is therefore
* used to contain that setup code. The "VVA" acronym stands for
* "Verification, Validation, and Accreditation". In other words, the
* code within VVA_ONLY() will only run during verification processes.
*/
#ifndef NDEBUG
#define VVA_ONLY(X) X
#else
#define VVA_ONLY(X)
#endif
/*
* The ALWAYS and NEVER macros surround boolean expressions which
* are intended to always be true or false, respectively. Such
* expressions could be omitted from the code completely. But they
* are included in a few cases in order to enhance the resilience
* of sql to unexpected behavior - to make the code "self-healing"
* or "ductile" rather than being "brittle" and crashing at the first
* hint of unplanned behavior.
*
* In other words, ALWAYS and NEVER are added for defensive code.
*/
#if !defined(NDEBUG)
#define ALWAYS(X) ((X)?1:(assert(0),0))
#define NEVER(X) ((X)?(assert(0),1):0)
#else
#define ALWAYS(X) (X)
#define NEVER(X) (X)
#endif
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stddef.h>
typedef long long int sql_int64;
typedef unsigned long long int sql_uint64;
typedef sql_int64 sql_int64;
typedef sql_uint64 sql_uint64;
typedef struct sql_stmt sql_stmt;
typedef struct sql_context sql_context;
typedef struct sql sql;
typedef struct Mem sql_value;
typedef struct sql_file sql_file;
struct sql_file {
const struct sql_io_methods *pMethods; /* Methods for an open file */
};
typedef int (*sql_callback) (void *, int, char **, char **);
typedef struct sql_vfs sql_vfs;
struct sql_vfs {
int iVersion; /* Structure version number (currently 3) */
int szOsFile; /* Size of subclassed sql_file */
int mxPathname; /* Maximum file pathname length */
sql_vfs *pNext; /* Next registered VFS */
const char *zName; /* Name of this virtual file system */
void *pAppData; /* Pointer to application-specific data */
int (*xOpen) (sql_vfs *, const char *zName, sql_file *,
int flags, int *pOutFlags);
int (*xDelete) (sql_vfs *, const char *zName, int syncDir);
int (*xRandomness) (sql_vfs *, int nByte, char *zOut);
int (*xCurrentTime) (sql_vfs *, double *);
/*
** The methods above are in version 1 of the sql_vfs object
** definition. Those that follow are added in version 2 or later
*/
int (*xCurrentTimeInt64) (sql_vfs *, sql_int64 *);
/*
** The methods above are in versions 1 through 3 of the sql_vfs object.
** New fields may be appended in future versions. The iVersion
** value will increment whenever this happens.
*/
};
#define SQL_LIMIT_LENGTH 0
#define SQL_LIMIT_SQL_LENGTH 1
#define SQL_LIMIT_COLUMN 2
#define SQL_LIMIT_EXPR_DEPTH 3
#define SQL_LIMIT_COMPOUND_SELECT 4
#define SQL_LIMIT_VDBE_OP 5
#define SQL_LIMIT_FUNCTION_ARG 6
#define SQL_LIMIT_ATTACHED 7
#define SQL_LIMIT_LIKE_PATTERN_LENGTH 8
#define SQL_LIMIT_TRIGGER_DEPTH 9
enum sql_ret_code {
/** sql_step() has another row ready. */
SQL_ROW = 1,
/** sql_step() has finished executing. */
SQL_DONE = 2,
};
void *
sql_malloc(int);
void *
sql_malloc64(sql_uint64);
void *
sql_realloc64(void *, sql_uint64);
void
sql_free(void *);
int
sql_stricmp(const char *, const char *);
int
sql_strnicmp(const char *, const char *, int);
const void *
sql_value_blob(sql_value *);
int
sql_value_bytes(sql_value *);
double
sql_value_double(sql_value *);
bool
sql_value_boolean(sql_value *val);
int
sql_value_int(sql_value *);
/**
* Get row column subtype.
* @param stmt row data to process.
* @param i column index.
* @retval SQL subtype if any, 0 else.
*/
enum sql_subtype
sql_column_subtype(struct sql_stmt *stmt, int i);
sql_int64
sql_value_int64(sql_value *);
uint64_t
sql_value_uint64(sql_value *val);
const unsigned char *
sql_value_text(sql_value *);
/**
* Return pointer to a string with the data type in the case of
* binary data stored in @a value. Otherwise, return the result
* of sql_value_text(). It is used due to the fact that not all
* binary strings can be displayed correctly (e.g. contain
* unprintable symbols).
*/
const char *
sql_value_to_diag_str(sql_value *value);
enum mp_type
sql_value_type(sql_value *);
static inline bool
sql_value_is_null(sql_value *value)
{
return sql_value_type(value) == MP_NIL;
}
sql *
sql_context_db_handle(sql_context *);
void
sql_result_blob(sql_context *, const void *,
int, void (*)(void *));
void
sql_result_blob64(sql_context *, const void *,
sql_uint64, void (*)(void *));
void
sql_result_double(sql_context *, double);
void
sql_result_uint(sql_context *ctx, uint64_t u_val);
void
sql_result_int(sql_context *ctx, int64_t val);
void
sql_result_bool(struct sql_context *ctx, bool value);
void
sql_result_null(sql_context *);
void
sql_result_text(sql_context *, const char *,
int, void (*)(void *));
void
sql_result_text64(sql_context *, const char *,
sql_uint64, void (*)(void *));
void
sql_result_value(sql_context *,
sql_value *);
void
sql_result_zeroblob(sql_context *, int n);
int
sql_result_zeroblob64(sql_context *,
sql_uint64 n);
char *
sql_mprintf(const char *, ...);
char *
sql_vmprintf(const char *, va_list);
char *
sql_snprintf(int, char *, const char *, ...);
char *
sql_vsnprintf(int, char *, const char *, va_list);
/**
* Wildcard characters used in REGEXP-like operators.
*/
#define MATCH_ONE_WILDCARD '_'
#define MATCH_ALL_WILDCARD '%'
typedef void (*sql_destructor_type) (void *);
#define SQL_STATIC ((sql_destructor_type)0)
#define SQL_TRANSIENT ((sql_destructor_type)-1)
int
sql_prepare(sql * db, /* Database handle */
const char *zSql, /* SQL statement, UTF-8 encoded */
int nByte, /* Maximum length of zSql in bytes. */
sql_stmt ** ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zSql */
);
int
sql_prepare_v2(sql * db, /* Database handle */
const char *zSql, /* SQL statement, UTF-8 encoded */
int nByte, /* Maximum length of zSql in bytes. */
sql_stmt ** ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zSql */
);
int
sql_step(sql_stmt *);
const void *
sql_column_blob(sql_stmt *, int iCol);
int
sql_column_bytes(sql_stmt *, int iCol);
int
sql_column_bytes16(sql_stmt *, int iCol);
double
sql_column_double(sql_stmt *, int iCol);
int
sql_column_int(sql_stmt *, int iCol);
bool
sql_column_boolean(struct sql_stmt *stmt, int column);
sql_int64
sql_column_int64(sql_stmt *, int iCol);
uint64_t
sql_column_uint64(struct sql_stmt *stmt, int column);
const unsigned char *
sql_column_text(sql_stmt *,
int iCol);
enum mp_type
sql_column_type(sql_stmt *stmt, int field_no);
sql_value *
sql_column_value(sql_stmt *,
int iCol);
int
sql_finalize(sql_stmt * pStmt);
/*
* Terminate the current execution of an SQL statement and reset
* it back to its starting state so that it can be reused.
*
* @param stmt VDBE program.
* @retval 0 On success.
* @retval sql_ret_code Error code on error.
*/
int
sql_reset(struct sql_stmt *stmt);
int
sql_exec(sql *, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback) (void *, int, char **, char **), /* Callback function */
void * /* 1st argument to callback */
);
/**
* Subtype of a main type. Allows to do some subtype specific
* things: serialization, unpacking etc.
*/
enum sql_subtype {
SQL_SUBTYPE_NO = 0,
SQL_SUBTYPE_MSGPACK = 77,
};
void
sql_randomness(int N, void *P);
/**
* Return the number of affected rows in the last SQL statement.
*/
void
sql_row_count(struct sql_context *context, MAYBE_UNUSED int unused1,
MAYBE_UNUSED sql_value **unused2);
void *
sql_aggregate_context(sql_context *,
int nBytes);
int
sql_column_count(sql_stmt * pStmt);
const char *
sql_column_name(sql_stmt *, int N);
const char *
sql_column_datatype(sql_stmt *, int N);
int
sql_initialize(void);
#define SQL_TRACE_STMT 0x01
#define SQL_TRACE_PROFILE 0x02
#define SQL_TRACE_ROW 0x04
#define SQL_TRACE_CLOSE 0x08
#define SQL_OPEN_READONLY 0x00000001 /* Ok for sql_open_v2() */
#define SQL_OPEN_READWRITE 0x00000002 /* Ok for sql_open_v2() */
#define SQL_OPEN_CREATE 0x00000004 /* Ok for sql_open_v2() */
#define SQL_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
#define SQL_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
#define SQL_OPEN_URI 0x00000040 /* Ok for sql_open_v2() */
#define SQL_OPEN_MAIN_DB 0x00000100 /* VFS only */
sql_vfs *
sql_vfs_find(const char *zVfsName);
typedef struct sql_io_methods sql_io_methods;
struct sql_io_methods {
int iVersion;
int (*xClose) (sql_file *);
int (*xRead) (sql_file *, void *, int iAmt,
sql_int64 iOfst);
int (*xWrite) (sql_file *, const void *, int iAmt,
sql_int64 iOfst);
int (*xFileControl) (sql_file *, int op, void *pArg);
/* Methods above are valid for version 2 */
int (*xFetch) (sql_file *, sql_int64 iOfst, int iAmt,
void **pp);
int (*xUnfetch) (sql_file *, sql_int64 iOfst, void *p);
/* Methods above are valid for version 3 */
/* Additional methods may be added in future releases */
};
#define SQL_FCNTL_LOCKSTATE 1
#define SQL_FCNTL_LAST_ERRNO 4
#define SQL_FCNTL_SIZE_HINT 5
#define SQL_FCNTL_CHUNK_SIZE 6
#define SQL_FCNTL_VFSNAME 11
#define SQL_FCNTL_TEMPFILENAME 15
#define SQL_FCNTL_MMAP_SIZE 16
#define SQL_FCNTL_HAS_MOVED 18
void
sql_os_init(void);
int
sql_limit(sql *, int id, int newVal);
extern char *
sql_temp_directory;
const char *
sql_uri_parameter(const char *zFilename,
const char *zParam);
const char *
sql_sql(sql_stmt * pStmt);
int
sql_vfs_register(sql_vfs *, int makeDflt);
#define SQL_STMTSTATUS_FULLSCAN_STEP 1
#define SQL_STMTSTATUS_SORT 2
#define SQL_STMTSTATUS_AUTOINDEX 3
#define SQL_STMTSTATUS_VM_STEP 4
int
sql_bind_blob(sql_stmt *, int, const void *,
int n, void (*)(void *));
int
sql_bind_blob64(sql_stmt *, int, const void *,
sql_uint64, void (*)(void *));
int
sql_bind_double(sql_stmt *, int, double);
/**
* Perform boolean parameter binding for the prepared sql
* statement.
* @param stmt Prepared statement.
* @param i Index of the variable to be binded.
* @param value Boolean value to use.
* @retval 0 On Success, not 0 otherwise.
*/
int
sql_bind_boolean(struct sql_stmt *stmt, int i, bool value);
int
sql_bind_int(sql_stmt *, int, int);
int
sql_bind_int64(sql_stmt *, int, sql_int64);
int
sql_bind_uint64(struct sql_stmt *stmt, int i, uint64_t value);
int
sql_bind_null(sql_stmt *, int);
int
sql_bind_text(sql_stmt *, int, const char *, int,
void (*)(void *));
int
sql_bind_text64(sql_stmt *, int, const char *,
sql_uint64, void (*)(void *));
int
sql_bind_zeroblob(sql_stmt *, int, int n);
int
sql_bind_zeroblob64(sql_stmt *, int,
sql_uint64);
/**
* Perform pointer parameter binding for the prepared sql
* statement.
* @param stmt Prepared statement.
* @param i Index of the variable to be binded.
* @param ptr Pointer value to use.
* @retval 0 On Success.
* @retval Not 0 otherwise.
*/
int
sql_bind_ptr(struct sql_stmt *stmt, int i, void *ptr);
int
sql_stmt_busy(sql_stmt *);
int
sql_init_db(sql **db);
/**
* Get number of the named parameter in the prepared sql
* statement.
* @param pStmt Prepared statement.
* @param zName Parameter name.
* @param nName Parameter name length.
*
* @retval > 0 Number of the parameter.
* @retval 0 Parameter is not found.
*/
int
sql_bind_parameter_lindex(sql_stmt * pStmt, const char *zName,
int nName);
/*
* If compiling for a processor that lacks floating point support,
* substitute integer for floating-point
*/
#ifndef SQL_BIG_DBL
#define SQL_BIG_DBL (1e99)
#endif
/*
* OMIT_TEMPDB is set to 1 if sql_OMIT_TEMPDB is defined, or 0
* afterward. Having this macro allows us to cause the C compiler
* to omit code used by TEMP tables without messy #ifndef statements.
*/
#ifdef SQL_OMIT_TEMPDB
#define OMIT_TEMPDB 1
#else
#define OMIT_TEMPDB 0
#endif
/*
* Determine whether triggers are recursive by default. This can be
* changed at run-time using a pragma.
*/
#ifndef SQL_DEFAULT_RECURSIVE_TRIGGERS
#define SQL_DEFAULT_RECURSIVE_TRIGGERS 0
#endif
/**
* Default count of allowed compound selects.
*
* Tarantool: gh-2548, gh-3382: Fiber stack is 64KB by default,
* so maximum number of entities should be less than 30 or stack
* guard will be triggered (triggered with clang).
*/
#ifndef SQL_DEFAULT_COMPOUND_SELECT
#define SQL_DEFAULT_COMPOUND_SELECT 30
#endif
/*
* GCC does not define the offsetof() macro so we'll have to do it
* ourselves.
*/
#ifndef offsetof
#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
#endif
/*
* Macros to compute minimum and maximum of two numbers.
*/
#ifndef MIN
#define MIN(A,B) ((A)<(B)?(A):(B))
#endif
#ifndef MAX
#define MAX(A,B) ((A)>(B)?(A):(B))
#endif
/*
* Integers of known sizes. These typedefs might change for architectures
* where the sizes very. Preprocessor macros are available so that the
* types can be conveniently redefined at compile-type. Like this:
*
* cc '-DUINTPTR_TYPE=long long int' ...
*/
#ifndef UINT32_TYPE
#ifdef HAVE_UINT32_T
#define UINT32_TYPE uint32_t
#else
#define UINT32_TYPE unsigned int
#endif
#endif
#ifndef UINT16_TYPE
#ifdef HAVE_UINT16_T
#define UINT16_TYPE uint16_t
#else
#define UINT16_TYPE unsigned short int
#endif
#endif
#ifndef INT16_TYPE
#ifdef HAVE_INT16_T
#define INT16_TYPE int16_t
#else
#define INT16_TYPE short int
#endif
#endif
#ifndef UINT8_TYPE
#ifdef HAVE_UINT8_T
#define UINT8_TYPE uint8_t
#else
#define UINT8_TYPE unsigned char
#endif
#endif
#ifndef INT8_TYPE
#ifdef HAVE_INT8_T
#define INT8_TYPE int8_t
#else
#define INT8_TYPE signed char
#endif
#endif
#ifndef LONGDOUBLE_TYPE
#define LONGDOUBLE_TYPE long double
#endif
typedef sql_int64 i64; /* 8-byte signed integer */
typedef sql_uint64 u64; /* 8-byte unsigned integer */
typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
typedef INT16_TYPE i16; /* 2-byte signed integer */
typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
typedef INT8_TYPE i8; /* 1-byte signed integer */
/*
* sql_MAX_U32 is a u64 constant that is the maximum u64 value
* that can be stored in a u32 without loss of data. The value
* is 0x00000000ffffffff. But because of quirks of some compilers, we
* have to specify the value in the less intuitive manner shown:
*/
#define SQL_MAX_U32 ((((u64)1)<<32)-1)
/*
* The datatype used to store estimates of the number of rows in a
* table or index. This is an unsigned integer type. For 99.9% of
* the world, a 32-bit integer is sufficient. But a 64-bit integer
* can be used at compile-time if desired.
*/
#ifdef SQL_64BIT_STATS
typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
#else
typedef u32 tRowcnt; /* 32-bit is the default */
#endif
/*
* Estimated quantities used for query planning are stored as 16-bit
* logarithms. For quantity X, the value stored is 10*log2(X). This
* gives a possible range of values of approximately 1.0e986 to 1e-986.
* But the allowed values are "grainy". Not every value is representable.
* For example, quantities 16 and 17 are both represented by a LogEst
* of 40. However, since LogEst quantities are suppose to be estimates,
* not exact values, this imprecision is not a problem.
*
* "LogEst" is short for "Logarithmic Estimate".
*
* Examples:
* 1 -> 0 20 -> 43 10000 -> 132
* 2 -> 10 25 -> 46 25000 -> 146
* 3 -> 16 100 -> 66 1000000 -> 199
* 4 -> 20 1000 -> 99 1048576 -> 200
* 10 -> 33 1024 -> 100 4294967296 -> 320
*
* The LogEst can be negative to indicate fractional values.
* Examples:
*
* 0.5 -> -10 0.1 -> -33 0.0625 -> -40
*/
typedef INT16_TYPE LogEst;
/*
* Set the sql_PTRSIZE macro to the number of bytes in a pointer
*/
#ifndef SQL_PTRSIZE
#if defined(__SIZEOF_POINTER__)
#define SQL_PTRSIZE __SIZEOF_POINTER__
#elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
defined(_M_ARM) || defined(__arm__) || defined(__x86)
#define SQL_PTRSIZE 4
#else
#define SQL_PTRSIZE 8
#endif
#endif
/* The uptr type is an unsigned integer large enough to hold a pointer
*/
#if defined(HAVE_STDINT_H)
typedef uintptr_t uptr;
#elif SQL_PTRSIZE==4
typedef u32 uptr;
#else
typedef u64 uptr;
#endif
/*
* The sql_WITHIN(P,S,E) macro checks to see if pointer P points to
* something between S (inclusive) and E (exclusive).
*
* In other words, S is a buffer and E is a pointer to the first byte after
* the end of buffer S. This macro returns true if P points to something
* contained within the buffer S.
*/
#define SQL_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
/*
* Constants for the largest and smallest possible 64-bit signed integers.
* These macros are designed to work correctly on both 32-bit and 64-bit
* compilers.
*/
#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
/*
* Round up a number to the next larger multiple of 8. This is used
* to force 8-byte alignment on 64-bit architectures.
*/
#define ROUND8(x) (((x)+7)&~7)
/*
* Round down to the nearest multiple of 8
*/
#define ROUNDDOWN8(x) ((x)&~7)
/*
* Assert that the pointer X is aligned to an 8-byte boundary. This
* macro is used only within assert() to verify that the code gets
* all alignment restrictions correct.
*
* Except, if sql_4_BYTE_ALIGNED_MALLOC is defined, then the
* underlying malloc() implementation might return us 4-byte aligned
* pointers. In that case, only verify 4-byte alignment.
*/
#ifdef SQL_4_BYTE_ALIGNED_MALLOC
#define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
#else
#define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
#endif
/*
* Default maximum size of memory used by memory-mapped I/O in the VFS
*/
#ifdef __APPLE__
#include <TargetConditionals.h>
#endif
#ifndef SQL_MAX_MMAP_SIZE
#if defined(__linux__) \
|| (defined(__APPLE__) && defined(__MACH__))
#define SQL_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
#else
#define SQL_MAX_MMAP_SIZE 0
#endif
#define SQL_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
#endif
/*
* The default MMAP_SIZE is zero on all platforms. Or, even if a larger
* default MMAP_SIZE is specified at compile-time, make sure that it does
* not exceed the maximum mmap size.
*/
#ifndef SQL_DEFAULT_MMAP_SIZE
#define SQL_DEFAULT_MMAP_SIZE 0
#define SQL_DEFAULT_MMAP_SIZE_xc 1 /* Exclude from ctime.c */
#endif
#if SQL_DEFAULT_MMAP_SIZE>SQL_MAX_MMAP_SIZE
#undef SQL_DEFAULT_MMAP_SIZE
#define SQL_DEFAULT_MMAP_SIZE SQL_MAX_MMAP_SIZE
#endif
/*
* A convenience macro that returns the number of elements in
* an array.
*/
#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
/*
* Determine if the argument is a power of two
*/
#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
/*
* The following value as a destructor means to use sqlDbFree().
* The sqlDbFree() routine requires two parameters instead of the
* one parameter that destructors normally want. So we have to introduce
* this magic value that the code knows to handle differently. Any
* pointer will work here as long as it is distinct from sql_STATIC
* and sql_TRANSIENT.
*/
#define SQL_DYNAMIC ((sql_destructor_type)sqlMallocSize)
/*
* The usual case where Writable Static Data (WSD) is supported,
* the sql_WSD and GLOBAL macros become no-ops and have zero
* performance impact.
*/
#define SQL_WSD