-
Notifications
You must be signed in to change notification settings - Fork 376
/
vdbeapi.c
1251 lines (1154 loc) · 29 KB
/
vdbeapi.c
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.
*/
/*
*
* This file contains code use to implement APIs that are part of the
* VDBE.
*/
#include "sqlInt.h"
#include "vdbeInt.h"
/*
* Invoke the profile callback. This routine is only called if we already
* know that the profile callback is defined and needs to be invoked.
*/
static SQL_NOINLINE void
invokeProfileCallback(sql * db, Vdbe * p)
{
sql_int64 iNow;
sql_int64 iElapse;
assert(p->startTime > 0);
assert(db->xProfile != 0 || (db->mTrace & SQL_TRACE_PROFILE) != 0);
assert(db->init.busy == 0);
assert(p->zSql != 0);
sqlOsCurrentTimeInt64(db->pVfs, &iNow);
iElapse = (iNow - p->startTime) * 1000000;
if (db->xProfile) {
db->xProfile(db->pProfileArg, p->zSql, iElapse);
}
if (db->mTrace & SQL_TRACE_PROFILE) {
db->xTrace(SQL_TRACE_PROFILE, db->pTraceArg, p,
(void *)&iElapse);
}
p->startTime = 0;
}
/*
* The checkProfileCallback(DB,P) macro checks to see if a profile callback
* is needed, and it invokes the callback if it is needed.
*/
#define checkProfileCallback(DB,P) \
if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
/*
* The following routine destroys a virtual machine that is created by
* the sql_compile() routine. The integer returned is an SQL_
* success/failure code that describes the result of executing the virtual
* machine.
*/
int
sql_finalize(sql_stmt * pStmt)
{
if (pStmt == NULL)
return 0;
Vdbe *v = (Vdbe *) pStmt;
sql *db = v->db;
assert(db != NULL);
checkProfileCallback(db, v);
return sqlVdbeFinalize(v);
}
int
sql_reset(sql_stmt * pStmt)
{
assert(pStmt != NULL);
struct Vdbe *v = (Vdbe *) pStmt;
struct sql *db = v->db;
checkProfileCallback(db, v);
int rc = sqlVdbeReset(v);
sqlVdbeRewind(v);
return rc;
}
/*
* Set all the parameters in the compiled SQL statement to NULL.
*/
int
sql_clear_bindings(sql_stmt * pStmt)
{
int i;
int rc = 0;
Vdbe *p = (Vdbe *) pStmt;
for (i = 0; i < p->nVar; i++) {
sqlVdbeMemRelease(&p->aVar[i]);
p->aVar[i].flags = MEM_Null;
}
if (p->isPrepareV2 && p->expmask) {
p->expired = 1;
}
return rc;
}
/**************************** sql_value_ ******************************
* The following routines extract information from a Mem or sql_value
* structure.
*/
const void *
sql_value_blob(sql_value * pVal)
{
Mem *p = (Mem *) pVal;
if (p->flags & (MEM_Blob | MEM_Str)) {
if (ExpandBlob(p) != 0) {
assert(p->flags == MEM_Null && p->z == 0);
return 0;
}
p->flags |= MEM_Blob;
return p->n ? p->z : 0;
} else {
return sql_value_text(pVal);
}
}
int
sql_value_bytes(sql_value * pVal)
{
return sqlValueBytes(pVal);
}
double
sql_value_double(sql_value * pVal)
{
double v = 0.0;
sqlVdbeRealValue((Mem *) pVal, &v);
return v;
}
bool
sql_value_boolean(sql_value *val)
{
bool b = false;
int rc = mem_value_bool((struct Mem *) val, &b);
assert(rc == 0);
(void) rc;
return b;
}
int
sql_value_int(sql_value * pVal)
{
int64_t i = 0;
bool is_neg;
sqlVdbeIntValue((Mem *) pVal, &i, &is_neg);
return (int)i;
}
sql_int64
sql_value_int64(sql_value * pVal)
{
int64_t i = 0;
bool unused;
sqlVdbeIntValue((Mem *) pVal, &i, &unused);
return i;
}
uint64_t
sql_value_uint64(sql_value *val)
{
int64_t i = 0;
bool is_neg;
sqlVdbeIntValue((struct Mem *) val, &i, &is_neg);
assert(!is_neg);
return i;
}
enum sql_subtype
sql_value_subtype(sql_value * pVal)
{
return (pVal->flags & MEM_Subtype) != 0 ? pVal->subtype : SQL_SUBTYPE_NO;
}
const unsigned char *
sql_value_text(sql_value * pVal)
{
return (const unsigned char *)sqlValueText(pVal);
}
/* EVIDENCE-OF: R-12793-43283 Every value in sql has one of five
* fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
* point number string BLOB NULL
*/
enum mp_type
sql_value_type(sql_value *pVal)
{
switch (pVal->flags & MEM_PURE_TYPE_MASK) {
case MEM_Int: return MP_INT;
case MEM_UInt: return MP_UINT;
case MEM_Real: return MP_DOUBLE;
case MEM_Str: return MP_STR;
case MEM_Blob: return MP_BIN;
case MEM_Bool: return MP_BOOL;
case MEM_Null: return MP_NIL;
default: unreachable();
}
}
/* Make a copy of an sql_value object
*/
sql_value *
sql_value_dup(const sql_value * pOrig)
{
sql_value *pNew;
if (pOrig == 0)
return 0;
pNew = sql_malloc(sizeof(*pNew));
if (pNew == 0)
return 0;
memset(pNew, 0, sizeof(*pNew));
memcpy(pNew, pOrig, MEMCELLSIZE);
pNew->flags &= ~MEM_Dyn;
pNew->db = 0;
if (pNew->flags & (MEM_Str | MEM_Blob)) {
pNew->flags &= ~(MEM_Static | MEM_Dyn);
pNew->flags |= MEM_Ephem;
if (sqlVdbeMemMakeWriteable(pNew) != 0) {
sqlValueFree(pNew);
pNew = 0;
}
}
return pNew;
}
/* Destroy an sql_value object previously obtained from
* sql_value_dup().
*/
void
sql_value_free(sql_value * pOld)
{
sqlValueFree(pOld);
}
/**************************** sql_result_ ******************************
* The following routines are used by user-defined functions to specify
* the function result.
*
* The setStrOrError() function calls sqlVdbeMemSetStr() to store the
* result as a string or blob but if the string or blob is too large, it
* then sets the error code.
*
* The invokeValueDestructor(P,X) routine invokes destructor function X()
* on value P is not going to be used and need to be destroyed.
*/
static void
setResultStrOrError(sql_context * pCtx, /* Function context */
const char *z, /* String pointer */
int n, /* Bytes in string, or negative */
void (*xDel) (void *) /* Destructor function */
)
{
if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 1, xDel) != 0)
pCtx->is_aborted = true;
}
static int
invokeValueDestructor(const void *p, /* Value to destroy */
void (*xDel) (void *), /* The destructor */
sql_context *pCtx /* Set an error if no NULL */
)
{
assert(xDel != SQL_DYNAMIC);
if (xDel == 0) {
/* noop */
} else if (xDel == SQL_TRANSIENT) {
/* noop */
} else {
xDel((void *)p);
}
if (pCtx) {
diag_set(ClientError, ER_SQL_EXECUTE, "string or blob is too "\
"big");
pCtx->is_aborted = true;
}
return -1;
}
void
sql_result_blob(sql_context * pCtx,
const void *z, int n, void (*xDel) (void *)
)
{
assert(n >= 0);
if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 0, xDel) != 0)
pCtx->is_aborted = true;
}
void
sql_result_blob64(sql_context * pCtx,
const void *z, sql_uint64 n, void (*xDel) (void *)
)
{
assert(xDel != SQL_DYNAMIC);
if (n > 0x7fffffff) {
(void)invokeValueDestructor(z, xDel, pCtx);
} else {
setResultStrOrError(pCtx, z, (int)n, xDel);
}
}
void
sql_result_double(sql_context * pCtx, double rVal)
{
sqlVdbeMemSetDouble(pCtx->pOut, rVal);
}
void
sql_result_uint(sql_context *ctx, uint64_t u_val)
{
mem_set_u64(ctx->pOut, u_val);
}
void
sql_result_int(sql_context * pCtx, int64_t iVal)
{
mem_set_i64(pCtx->pOut, iVal);
}
void
sql_result_bool(struct sql_context *ctx, bool value)
{
mem_set_bool(ctx->pOut, value);
}
void
sql_result_null(sql_context * pCtx)
{
sqlVdbeMemSetNull(pCtx->pOut);
}
void
sql_result_text(sql_context * pCtx,
const char *z, int n, void (*xDel) (void *)
)
{
setResultStrOrError(pCtx, z, n, xDel);
}
void
sql_result_text64(sql_context * pCtx,
const char *z,
sql_uint64 n,
void (*xDel) (void *))
{
assert(xDel != SQL_DYNAMIC);
if (n > 0x7fffffff) {
(void)invokeValueDestructor(z, xDel, pCtx);
} else {
setResultStrOrError(pCtx, z, (int)n, xDel);
}
}
void
sql_result_value(sql_context * pCtx, sql_value * pValue)
{
sqlVdbeMemCopy(pCtx->pOut, pValue);
}
void
sql_result_zeroblob(sql_context * pCtx, int n)
{
sqlVdbeMemSetZeroBlob(pCtx->pOut, n);
}
int
sql_result_zeroblob64(sql_context * pCtx, u64 n)
{
Mem *pOut = pCtx->pOut;
if (n > (u64) pOut->db->aLimit[SQL_LIMIT_LENGTH]) {
diag_set(ClientError, ER_SQL_EXECUTE, "string or blob is too "\
"big");
return -1;
}
sqlVdbeMemSetZeroBlob(pCtx->pOut, (int)n);
return 0;
}
/*
* Execute the statement pStmt, either until a row of data is ready, the
* statement is completely executed or an error occurs.
*
* This routine implements the bulk of the logic behind the sql_step()
* API. The only thing omitted is the automatic recompile if a
* schema change has occurred. That detail is handled by the
* outer sql_step() wrapper procedure.
*/
static int
sqlStep(Vdbe * p)
{
sql *db;
int rc;
assert(p);
if (p->magic != VDBE_MAGIC_RUN)
sql_reset((sql_stmt *) p);
/* Check that malloc() has not failed. If it has, return early. */
db = p->db;
if (db->mallocFailed) {
p->is_aborted = true;
return -1;
}
if (p->pc <= 0 && p->expired) {
p->is_aborted = true;
return -1;
}
if (p->pc < 0) {
if ((db->xProfile || (db->mTrace & SQL_TRACE_PROFILE) != 0)
&& !db->init.busy && p->zSql) {
sqlOsCurrentTimeInt64(db->pVfs, &p->startTime);
} else {
assert(p->startTime == 0);
}
db->nVdbeActive++;
p->pc = 0;
}
if (p->explain) {
rc = sqlVdbeList(p);
} else {
db->nVdbeExec++;
rc = sqlVdbeExec(p);
db->nVdbeExec--;
}
/* If the statement completed successfully, invoke the profile callback */
if (rc != SQL_ROW)
checkProfileCallback(db, p);
if (p->isPrepareV2 && rc != SQL_ROW && rc != SQL_DONE) {
/* If this statement was prepared using sql_prepare_v2(), and an
* error has occurred, then return an error.
*/
if (p->is_aborted)
rc = -1;
}
return rc;
}
/*
* This is the top-level implementation of sql_step(). Call
* sqlStep() to do most of the work. If a schema error occurs,
* call sqlReprepare() and try again.
*/
int
sql_step(sql_stmt * pStmt)
{
Vdbe *v = (Vdbe *) pStmt; /* the prepared statement */
assert(v != NULL);
return sqlStep(v);
}
/*
* Extract the user data from a sql_context structure and return a
* pointer to it.
*/
void *
sql_user_data(sql_context * p)
{
assert(p && p->pFunc);
return p->pFunc->pUserData;
}
/*
* Extract the user data from a sql_context structure and return a
* pointer to it.
*
* IMPLEMENTATION-OF: R-46798-50301 The sql_context_db_handle() interface
* returns a copy of the pointer to the database connection (the 1st
* parameter) of the sql_create_function() and
* sql_create_function16() routines that originally registered the
* application defined function.
*/
sql *
sql_context_db_handle(sql_context * p)
{
assert(p && p->pOut);
return p->pOut->db;
}
/*
* Return the current time for a statement. If the current time
* is requested more than once within the same run of a single prepared
* statement, the exact same time is returned for each invocation regardless
* of the amount of time that elapses between invocations. In other words,
* the time returned is always the time of the first call.
*/
sql_int64
sqlStmtCurrentTime(sql_context * p)
{
int rc;
#ifndef SQL_ENABLE_OR_STAT4
sql_int64 *piTime = &p->pVdbe->iCurrentTime;
assert(p->pVdbe != 0);
#else
sql_int64 iTime = 0;
sql_int64 *piTime =
p->pVdbe != 0 ? &p->pVdbe->iCurrentTime : &iTime;
#endif
if (*piTime == 0) {
rc = sqlOsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
if (rc)
*piTime = 0;
}
return *piTime;
}
/*
* The following is the implementation of an SQL function that always
* fails with an error message stating that the function is used in the
* wrong context. The sql_overload_function() API might construct
* SQL function that use this routine so that the functions will exist
* for name resolution.
*/
void
sqlInvalidFunction(sql_context * context, /* The function calling context */
int NotUsed, /* Number of arguments to the function */
sql_value ** NotUsed2 /* Value of each argument */
)
{
const char *zName = context->pFunc->zName;
UNUSED_PARAMETER2(NotUsed, NotUsed2);
const char *err = "unable to use function %s in the requested context";
diag_set(ClientError, ER_SQL_EXECUTE, tt_sprintf(err, zName));
context->is_aborted = true;
}
/*
* Create a new aggregate context for p and return a pointer to
* its pMem->z element.
*/
static SQL_NOINLINE void *
createAggContext(sql_context * p, int nByte)
{
Mem *pMem = p->pMem;
assert((pMem->flags & MEM_Agg) == 0);
if (nByte <= 0) {
sqlVdbeMemSetNull(pMem);
pMem->z = 0;
} else {
sqlVdbeMemClearAndResize(pMem, nByte);
pMem->flags = MEM_Agg;
pMem->u.pDef = p->pFunc;
if (pMem->z) {
memset(pMem->z, 0, nByte);
}
}
return (void *)pMem->z;
}
/*
* Allocate or return the aggregate context for a user function. A new
* context is allocated on the first call. Subsequent calls return the
* same context that was returned on prior calls.
*/
void *
sql_aggregate_context(sql_context * p, int nByte)
{
assert(p && p->pFunc && p->pFunc->xFinalize);
testcase(nByte < 0);
if ((p->pMem->flags & MEM_Agg) == 0) {
return createAggContext(p, nByte);
} else {
return (void *)p->pMem->z;
}
}
/*
* Return the number of columns in the result set for the statement pStmt.
*/
int
sql_column_count(sql_stmt * pStmt)
{
Vdbe *pVm = (Vdbe *) pStmt;
return pVm ? pVm->nResColumn : 0;
}
/*
* Return the number of values available from the current row of the
* currently executing statement pStmt.
*/
int
sql_data_count(sql_stmt * pStmt)
{
Vdbe *pVm = (Vdbe *) pStmt;
if (pVm == 0 || pVm->pResultSet == 0)
return 0;
return pVm->nResColumn;
}
/*
* Return a pointer to static memory containing an SQL NULL value.
*/
static const Mem *
columnNullValue(void)
{
/* Even though the Mem structure contains an element
* of type i64, on certain architectures (x86) with certain compiler
* switches (-Os), gcc may align this Mem object on a 4-byte boundary
* instead of an 8-byte one. This all works fine, except that when
* running with SQL_DEBUG defined the sql code sometimes assert()s
* that a Mem structure is located on an 8-byte boundary. To prevent
* these assert()s from failing, when building with SQL_DEBUG defined
* using gcc, we force nullMem to be 8-byte aligned using the magical
* __attribute__((aligned(8))) macro.
*/
static const Mem nullMem
#if defined(SQL_DEBUG) && defined(__GNUC__)
__attribute__ ((aligned(8)))
#endif
= {
/* .u = */ {
0},
/* .flags = */ (u16) MEM_Null,
/* .eSubtype = */ (u8) 0,
/* .n = */ (int)0,
/* .z = */ (char *)0,
/* .zMalloc = */ (char *)0,
/* .szMalloc = */ (int)0,
/* .uTemp = */ (u32) 0,
/* .db = */ (sql *) 0,
/* .xDel = */ (void (*)(void *))0,
#ifdef SQL_DEBUG
/* .pScopyFrom = */ (Mem *) 0,
/* .pFiller = */ (void *)0,
#endif
};
return &nullMem;
}
/*
* Check to see if column iCol of the given statement is valid. If
* it is, return a pointer to the Mem for the value of that column.
* If iCol is not valid, return a pointer to a Mem which has a value
* of NULL.
*/
static Mem *
columnMem(sql_stmt * pStmt, int i)
{
Vdbe *pVm;
Mem *pOut;
pVm = (Vdbe *) pStmt;
if (pVm == 0)
return (Mem *) columnNullValue();
assert(pVm->db);
if (pVm->pResultSet != 0 && i < pVm->nResColumn && i >= 0) {
pOut = &pVm->pResultSet[i];
} else {
pOut = (Mem *) columnNullValue();
}
return pOut;
}
/**************************** sql_column_ ******************************
* The following routines are used to access elements of the current row
* in the result set.
*/
const void *
sql_column_blob(sql_stmt * pStmt, int i)
{
const void *val;
val = sql_value_blob(columnMem(pStmt, i));
return val;
}
int
sql_column_bytes(sql_stmt * pStmt, int i)
{
return sql_value_bytes(columnMem(pStmt, i));
}
double
sql_column_double(sql_stmt * pStmt, int i)
{
return sql_value_double(columnMem(pStmt, i));
}
int
sql_column_int(sql_stmt * pStmt, int i)
{
return sql_value_int(columnMem(pStmt, i));
}
bool
sql_column_boolean(struct sql_stmt *stmt, int i)
{
return sql_value_boolean(columnMem(stmt, i));
}
sql_int64
sql_column_int64(sql_stmt * pStmt, int i)
{
return sql_value_int64(columnMem(pStmt, i));
}
uint64_t
sql_column_uint64(sql_stmt * pStmt, int i)
{
return sql_value_uint64(columnMem(pStmt, i));
}
const unsigned char *
sql_column_text(sql_stmt * pStmt, int i)
{
return sql_value_text(columnMem(pStmt, i));
}
sql_value *
sql_column_value(sql_stmt * pStmt, int i)
{
Mem *pOut = columnMem(pStmt, i);
if (pOut->flags & MEM_Static) {
pOut->flags &= ~MEM_Static;
pOut->flags |= MEM_Ephem;
}
return (sql_value *) pOut;
}
enum mp_type
sql_column_type(sql_stmt * pStmt, int i)
{
return sql_value_type(columnMem(pStmt, i));
}
enum sql_subtype
sql_column_subtype(struct sql_stmt *stmt, int i)
{
return sql_value_subtype(columnMem(stmt, i));
}
/*
* Convert the N-th element of pStmt->pColName[] into a string using
* xFunc() then return that string. If N is out of range, return 0.
*
* There are up to 5 names for each column. useType determines which
* name is returned. Here are the names:
*
* 0 The column name as it should be displayed for output
* 1 The datatype name for the column
* 2 The name of the database that the column derives from
* 3 The name of the table that the column derives from
* 4 The name of the table column that the result column derives from
*
* If the result is not a simple column reference (if it is an expression
* or a constant) then useTypes 2, 3, and 4 return NULL.
*/
static const void *
columnName(sql_stmt * pStmt,
int N, const void *(*xFunc) (Mem *), int useType)
{
const void *ret;
Vdbe *p;
int n;
sql *db;
ret = 0;
p = (Vdbe *) pStmt;
db = p->db;
assert(db != 0);
n = sql_column_count(pStmt);
if (N < n && N >= 0) {
N += useType * n;
assert(db->mallocFailed == 0);
ret = xFunc(&p->aColName[N]);
/* A malloc may have failed inside of the xFunc() call. If this
* is the case, clear the mallocFailed flag and return NULL.
*/
if (db->mallocFailed) {
sqlOomClear(db);
ret = 0;
}
}
return ret;
}
/*
* Return the name of the Nth column of the result set returned by SQL
* statement pStmt.
*/
const char *
sql_column_name(sql_stmt * pStmt, int N)
{
return columnName(pStmt, N, (const void *(*)(Mem *))sql_value_text,
COLNAME_NAME);
}
const char *
sql_column_datatype(sql_stmt *pStmt, int N)
{
return columnName(pStmt, N, (const void *(*)(Mem *))sql_value_text,
COLNAME_DECLTYPE);
}
/*
* Return the column declaration type (if applicable) of the 'i'th column
* of the result set of SQL statement pStmt.
*/
const char *
sql_column_decltype(sql_stmt * pStmt, int N)
{
return columnName(pStmt, N, (const void *(*)(Mem *))sql_value_text,
COLNAME_DECLTYPE);
}
/******************************* sql_bind_ **************************
*
* Routines used to attach values to wildcards in a compiled SQL statement.
*/
/*
* Unbind the value bound to variable i in virtual machine p. This is the
* the same as binding a NULL value to the column.
*/
static int
vdbeUnbind(Vdbe * p, int i)
{
Mem *pVar;
assert(p != NULL);
assert(p->magic == VDBE_MAGIC_RUN && p->pc < 0);
assert(i > 0);
if(i > p->nVar) {
diag_set(ClientError, ER_SQL_EXECUTE, "The number of "\
"parameters is too large");
return -1;
}
i--;
pVar = &p->aVar[i];
sqlVdbeMemRelease(pVar);
pVar->flags = MEM_Null;
/* If the bit corresponding to this variable in Vdbe.expmask is set, then
* binding a new value to this variable invalidates the current query plan.
*
* IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
* parameter in the WHERE clause might influence the choice of query plan
* for a statement, then the statement will be automatically recompiled,
* as if there had been a schema change, on the first sql_step() call
* following any change to the bindings of that parameter.
*/
if (p->isPrepareV2 &&
((i < 32 && p->expmask & ((u32) 1 << i))
|| p->expmask == 0xffffffff)
) {
p->expired = 1;
}
return 0;
}
/**
* This function sets type for bound variable.
* We should bind types only for variables which occur in
* result set of SELECT query. For example:
*
* SELECT id, ?, ?, a WHERE id = ?;
*
* In this case we should set types only for two variables.
* That one which is situated under WHERE condition - is out
* of our interest.
*
* For named binding parameters we should propagate type
* for all occurrences of this parameter - since binding
* routine takes place only once for each DISTINCT parameter
* from list.
*
* @param v Current VDBE.
* @param position Ordinal position of binding parameter.
* @param type String literal representing type of binding param.
* @retval 0 on success.
*/
static int
sql_bind_type(struct Vdbe *v, uint32_t position, const char *type)
{
if (v->res_var_count < position)
return 0;
int rc = 0;
if (sqlVdbeSetColName(v, v->var_pos[position - 1], COLNAME_DECLTYPE,
type, SQL_TRANSIENT) != 0)
rc = -1;
const char *bind_name = v->aColName[position - 1].z;
if (strcmp(bind_name, "?") == 0)
return rc;
for (uint32_t i = position; i < v->res_var_count; ++i) {
if (strcmp(bind_name, v->aColName[i].z) == 0) {
if (sqlVdbeSetColName(v, v->var_pos[i],
COLNAME_DECLTYPE, type,
SQL_TRANSIENT) != 0)
return -1;
}
}
return 0;
}
/*
* Bind a text or BLOB value.
*/
static int
bindText(sql_stmt * pStmt, /* The statement to bind against */
int i, /* Index of the parameter to bind */
const void *zData, /* Pointer to the data to be bound */
int nData, /* Number of bytes of data to be bound */
void (*xDel) (void *) /* Destructor for the data */
)
{
Vdbe *p = (Vdbe *) pStmt;
Mem *pVar;
if (vdbeUnbind(p, i) != 0) {
if (xDel != SQL_STATIC && xDel != SQL_TRANSIENT)
xDel((void *)zData);
return -1;
}
if (zData == NULL)
return 0;
pVar = &p->aVar[i - 1];
if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0)
return -1;
return sql_bind_type(p, i, "TEXT");
}
/*
* Bind a blob value to an SQL statement variable.
*/
int
sql_bind_blob(sql_stmt * pStmt,
int i, const void *zData, int nData, void (*xDel) (void *)
)
{
struct Vdbe *p = (Vdbe *) pStmt;
if (vdbeUnbind(p, i) != 0) {
if (xDel != SQL_STATIC && xDel != SQL_TRANSIENT)
xDel((void *)zData);
return -1;
}
if (zData == NULL)
return 0;
struct Mem *var = &p->aVar[i - 1];
if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0)
return -1;
return sql_bind_type(p, i, "BLOB");
}
int
sql_bind_blob64(sql_stmt * pStmt,
int i,
const void *zData,
sql_uint64 nData, void (*xDel) (void *)
)
{
assert(xDel != SQL_DYNAMIC);
if (nData > 0x7fffffff) {
return invokeValueDestructor(zData, xDel, 0);
} else {
return sql_bind_blob(pStmt, i, zData, (int)nData, xDel);
}
}
int
sql_bind_double(sql_stmt * pStmt, int i, double rValue)
{
Vdbe *p = (Vdbe *) pStmt;
if (vdbeUnbind(p, i) != 0)
return -1;
int rc = sql_bind_type(p, i, "NUMERIC");
sqlVdbeMemSetDouble(&p->aVar[i - 1], rValue);
return rc;
}
int
sql_bind_boolean(struct sql_stmt *stmt, int i, bool value)
{
struct Vdbe *p = (struct Vdbe *) stmt;
if (vdbeUnbind(p, i) != 0)
return -1;
int rc = sql_bind_type(p, i, "BOOLEAN");
mem_set_bool(&p->aVar[i - 1], value);