-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvdbemem_c.cs
More file actions
executable file
·1674 lines (1576 loc) · 47.4 KB
/
vdbemem_c.cs
File metadata and controls
executable file
·1674 lines (1576 loc) · 47.4 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
using System;
using System.Diagnostics;
using System.Text;
using i64 = System.Int64;
using u8 = System.Byte;
using u16 = System.UInt16;
using u32 = System.UInt32;
namespace System.Data.SQLite
{
using sqlite3_value = Sqlite3.Mem;
using System.Globalization;
public partial class Sqlite3
{
/*
** 2004 May 26
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains code use to manipulate "Mem" structure. A "Mem"
** stores a single value in the VDBE. Mem is an opaque structure visible
** only within the VDBE. Interface routines refer to a Mem using the
** name sqlite_value
*************************************************************************
** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
** C#-SQLite is an independent reimplementation of the SQLite software library
**
** SQLITE_SOURCE_ID: 2011-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908e
**
*************************************************************************
*/
//#include "sqliteInt.h"
//#include "vdbeInt.h"
/*
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
** P if required.
*/
//#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
static void expandBlob(Mem P)
{
if ((P.flags & MEM_Zero) != 0)
sqlite3VdbeMemExpandBlob(P);
} // TODO -- Convert to inline for speed
/// <summary>
/// If pMem is an object with a valid string representation, this routine
/// ensures the internal encoding for the string representation is
/// 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
///
/// If pMem is not a string object, or the encoding of the string
/// representation is already stored using the requested encoding, then this
/// routine is a no-op.
///
/// SQLITE_OK is returned if the conversion is successful (or not required).
/// SQLITE_NOMEM may be returned if a malloc() fails during conversion
/// between formats.
/// </summary>
static int sqlite3VdbeChangeEncoding(Mem pMem, int desiredEnc)
{
int rc;
Debug.Assert((pMem.flags & MEM_RowSet) == 0);
Debug.Assert(desiredEnc == SQLITE_UTF8 || desiredEnc == SQLITE_UTF16LE
|| desiredEnc == SQLITE_UTF16BE);
if ((pMem.flags & MEM_Str) == 0 || pMem.enc == desiredEnc)
{
if (String.IsNullOrEmpty(pMem.z) && pMem.zBLOB != null)
pMem.z = Encoding.UTF8.GetString(pMem.zBLOB, 0, pMem.zBLOB.Length);
return SQLITE_OK;
}
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
#if SQLITE_OMIT_UTF16
return SQLITE_ERROR;
#else
/* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
** then the encoding of the value may not have changed.
*/
rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
Debug.Assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
Debug.Assert(rc==SQLITE_OK || pMem.enc!=desiredEnc);
Debug.Assert(rc==SQLITE_NOMEM || pMem.enc==desiredEnc);
return rc;
#endif
}
/// <summary>
/// Make sure pMem.z points to a writable allocation of at least
/// n bytes.
///
/// If the memory cell currently contains string or blob data
/// and the third argument passed to this function is true, the
/// current content of the cell is preserved. Otherwise, it may
/// be discarded.
///
/// This function sets the MEM_Dyn flag and clears any xDel callback.
/// It also clears MEM_Ephem and MEM_Static. If the preserve flag is
/// not set, Mem.n is zeroed.
/// </summary>
static int sqlite3VdbeMemGrow(Mem pMem, int n, int preserve)
{
// TODO -- What do we want to do about this routine?
//Debug.Assert( 1 >=
// ((pMem.zMalloc !=null )? 1 : 0) + //&& pMem.zMalloc==pMem.z) ? 1 : 0) +
// (((pMem.flags & MEM_Dyn)!=0 && pMem.xDel!=null) ? 1 : 0) +
// ((pMem.flags & MEM_Ephem)!=0 ? 1 : 0) +
// ((pMem.flags & MEM_Static)!=0 ? 1 : 0)
//);
//assert( (pMem->flags&MEM_RowSet)==0 );
//if( n<32 ) n = 32;
//if( sqlite3DbMallocSize(pMem->db, pMem.zMalloc)<n ){
if (preserve != 0)
{//& pMem.z==pMem.zMalloc ){
if (pMem.z == null)
pMem.z = "";// sqlite3DbReallocOrFree( pMem.db, pMem.z, n );
else
if (n < pMem.z.Length)
pMem.z = pMem.z.Substring(0, n);
preserve = 0;
}
else
{
// sqlite3DbFree(pMem->db,ref pMem.zMalloc);
pMem.z = "";// sqlite3DbMallocRaw( pMem.db, n );
}
//}
// if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
// memcpy(pMem.zMalloc, pMem.z, pMem.n);
//}
if ((pMem.flags & MEM_Dyn) != 0 && pMem.xDel != null)
{
pMem.xDel(ref pMem.z);
}
// TODO --pMem.z = pMem.zMalloc;
if (pMem.z == null)
{
pMem.flags = MEM_Null;
}
else
{
pMem.flags = (u16)(pMem.flags & ~(MEM_Ephem | MEM_Static));
}
pMem.xDel = null;
return pMem.z != null ? SQLITE_OK : SQLITE_NOMEM;
}
/// <summary>
/// Make the given Mem object MEM_Dyn. In other words, make it so
/// that any TEXT or BLOB content is stored in memory obtained from
/// malloc(). In this way, we know that the memory is safe to be
/// overwritten or altered.
///
/// Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
/// </summary>
static int sqlite3VdbeMemMakeWriteable(Mem pMem)
{
int f;
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
Debug.Assert((pMem.flags & MEM_RowSet) == 0);
expandBlob(pMem);
f = pMem.flags;
if ((f & (MEM_Str | MEM_Blob)) != 0) // TODO -- && pMem.z != pMem.zMalloc )
{
if (sqlite3VdbeMemGrow(pMem, pMem.n + 2, 1) != 0)
//{
// return SQLITE_NOMEM;
//}
//pMem.z[pMem->n] = 0;
//pMem.z[pMem->n + 1] = 0;
pMem.flags |= MEM_Term;
#if SQLITE_DEBUG
pMem.pScopyFrom = null;
#endif
}
return SQLITE_OK;
}
/*
** If the given Mem* has a zero-filled tail, turn it into an ordinary
** blob stored in dynamically allocated space.
*/
#if !SQLITE_OMIT_INCRBLOB
static int sqlite3VdbeMemExpandBlob( Mem pMem )
{
if ( ( pMem.flags & MEM_Zero ) != 0 )
{
u32 nByte;
Debug.Assert( ( pMem.flags & MEM_Blob ) != 0 );
Debug.Assert( ( pMem.flags & MEM_RowSet ) == 0 );
Debug.Assert( pMem.db == null || sqlite3_mutex_held( pMem.db.mutex ) );
/* Set nByte to the number of bytes required to store the expanded blob. */
nByte = (u32)( pMem.n + pMem.u.nZero );
if ( nByte <= 0 )
{
nByte = 1;
}
if ( sqlite3VdbeMemGrow( pMem, (int)nByte, 1 ) != 0 )
{
return SQLITE_NOMEM;
} /* Set nByte to the number of bytes required to store the expanded blob. */
nByte = (u32)( pMem.n + pMem.u.nZero );
if ( nByte <= 0 )
{
nByte = 1;
}
if ( sqlite3VdbeMemGrow( pMem, (int)nByte, 1 ) != 0 )
{
return SQLITE_NOMEM;
}
//memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
pMem.zBLOB = Encoding.UTF8.GetBytes( pMem.z );
pMem.z = null;
pMem.n += (int)pMem.u.nZero;
pMem.u.i = 0;
pMem.flags = (u16)( pMem.flags & ~( MEM_Zero | MEM_Static | MEM_Ephem | MEM_Term ) );
pMem.flags |= MEM_Dyn;
}
return SQLITE_OK;
}
#endif
/// <summary>
/// Make sure the given Mem is \u0000 terminated.
/// </summary>
static int sqlite3VdbeMemNulTerminate(Mem pMem)
{
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
if ((pMem.flags & MEM_Term) != 0 || (pMem.flags & MEM_Str) == 0)
{
return SQLITE_OK; /* Nothing to do */
}
//if ( pMem.n != 0 && sqlite3VdbeMemGrow( pMem, pMem.n + 2, 1 ) != 0 )
//{
// return SQLITE_NOMEM;
//}
// pMem.z[pMem->n] = 0;
// pMem.z[pMem->n+1] = 0;
if (pMem.z != null && pMem.n < pMem.z.Length)
pMem.z = pMem.z.Substring(0, pMem.n);
pMem.flags |= MEM_Term;
return SQLITE_OK;
}
/// <summary>
/// Add MEM_Str to the set of representations for the given Mem. Numbers
/// are converted using sqlite3_snprintf(). Converting a BLOB to a string
/// is a no-op.
///
/// Existing representations MEM_Int and MEM_Real are *not* invalidated.
///
/// A MEM_Null value will never be passed to this function. This function is
/// used for converting values to text for returning to the user (i.e. via
/// sqlite3_value_text()), or for ensuring that values to be used as btree
/// keys are strings. In the former case a NULL pointer is returned the
/// user and the later is an internal programming error.
/// </summary>
static int sqlite3VdbeMemStringify(Mem pMem, int enc)
{
int rc = SQLITE_OK;
int fg = pMem.flags;
const int nByte = 32;
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
Debug.Assert((fg & MEM_Zero) == 0);
Debug.Assert((fg & (MEM_Str | MEM_Blob)) == 0);
Debug.Assert((fg & (MEM_Int | MEM_Real)) != 0);
Debug.Assert((pMem.flags & MEM_RowSet) == 0);
//assert( EIGHT_BYTE_ALIGNMENT(pMem) );
if (sqlite3VdbeMemGrow(pMem, nByte, 0) != 0)
{
return SQLITE_NOMEM;
}
/* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
** string representation of the value. Then, if the required encoding
** is UTF-16le or UTF-16be do a translation.
**
** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
*/
if ((fg & MEM_Int) != 0)
{
pMem.z = pMem.u.i.ToString(); //sqlite3_snprintf(nByte, pMem.z, "%lld", pMem->u.i);
}
else
{
Debug.Assert((fg & MEM_Real) != 0);
if (Double.IsNegativeInfinity(pMem.r))
pMem.z = "-Inf";
else if (Double.IsInfinity(pMem.r))
pMem.z = "Inf";
else if (Double.IsPositiveInfinity(pMem.r))
pMem.z = "+Inf";
else if (pMem.r.ToString(CultureInfo.InvariantCulture).Contains("."))
pMem.z = pMem.r.ToString(CultureInfo.InvariantCulture).ToLower();//sqlite3_snprintf(nByte, pMem.z, "%!.15g", pMem->r);
else
pMem.z = pMem.r.ToString(CultureInfo.InvariantCulture) + ".0";
}
pMem.n = sqlite3Strlen30(pMem.z);
pMem.enc = SQLITE_UTF8;
pMem.flags |= MEM_Str | MEM_Term;
sqlite3VdbeChangeEncoding(pMem, enc);
return rc;
}
/// <summary>
/// Memory cell pMem contains the context of an aggregate function.
/// This routine calls the finalize method for that function. The
/// result of the aggregate is stored back into pMem.
///
/// Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
/// otherwise.
/// </summary>
static int sqlite3VdbeMemFinalize(Mem pMem, FuncDef pFunc)
{
int rc = SQLITE_OK;
if (ALWAYS(pFunc != null && pFunc.xFinalize != null))
{
sqlite3_context ctx = new sqlite3_context();
Debug.Assert((pMem.flags & MEM_Null) != 0 || pFunc == pMem.u.pDef);
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
//memset(&ctx, 0, sizeof(ctx));
ctx.s.flags = MEM_Null;
ctx.s.db = pMem.db;
ctx.pMem = pMem;
ctx.pFunc = pFunc;
pFunc.xFinalize(ctx); /* IMP: R-24505-23230 */
Debug.Assert(0 == (pMem.flags & MEM_Dyn) && pMem.xDel == null);
sqlite3DbFree(pMem.db, ref pMem.zBLOB);//zMalloc );
ctx.s.CopyTo(ref pMem);//memcpy(pMem, &ctx.s, sizeof(ctx.s));
rc = ctx.isError;
}
return rc;
}
/// <summary>
/// If the memory cell contains a string value that must be freed by
/// invoking an external callback, free it now. Calling this function
/// does not free any Mem.zMalloc buffer.
/// </summary>
static void sqlite3VdbeMemReleaseExternal(Mem p)
{
Debug.Assert(p.db == null || sqlite3_mutex_held(p.db.mutex));
testcase(p.flags & MEM_Agg);
testcase(p.flags & MEM_Dyn);
testcase(p.flags & MEM_RowSet);
testcase(p.flags & MEM_Frame);
if ((p.flags & (MEM_Agg | MEM_Dyn | MEM_RowSet | MEM_Frame)) != 0)
{
if ((p.flags & MEM_Agg) != 0)
{
sqlite3VdbeMemFinalize(p, p.u.pDef);
Debug.Assert((p.flags & MEM_Agg) == 0);
sqlite3VdbeMemRelease(p);
}
else if ((p.flags & MEM_Dyn) != 0 && p.xDel != null)
{
Debug.Assert((p.flags & MEM_RowSet) == 0);
p.xDel(ref p.z);
p.xDel = null;
}
else if ((p.flags & MEM_RowSet) != 0)
{
sqlite3RowSetClear(p.u.pRowSet);
}
else if ((p.flags & MEM_Frame) != 0)
{
sqlite3VdbeMemSetNull(p);
}
}
p.n = 0;
p.z = null;
p.zBLOB = null;
}
/// <summary>
/// Release any memory held by the Mem. This may leave the Mem in an
/// inconsistent state, for example with (Mem.z==0) and
/// (Mem.type==SQLITE_TEXT).
/// </summary>
static void sqlite3VdbeMemRelease(Mem p)
{
sqlite3VdbeMemReleaseExternal(p);
sqlite3DbFree(p.db, ref p.zBLOB);//zMalloc );
p.z = null;
//p.zMalloc = 0;
p.xDel = null;
}
/// <summary>
/// Convert a 64-bit IEEE double into a 64-bit signed integer.
/// If the double is too large, return 0x8000000000000000.
///
/// Most systems appear to do this simply by assigning
/// variables and without the extra range tests. But
/// there are reports that windows throws an expection
/// if the floating point value is out of range. (See ticket #2880.)
/// Because we do not completely understand the problem, we will
/// take the conservative approach and always do range tests
/// before attempting the conversion.
/// </summary>
static i64 doubleToInt64(double r)
{
#if SQLITE_OMIT_FLOATING_POINT
/* When floating-point is omitted, double and int64 are the same thing */
return r;
#else
/*
** Many compilers we encounter do not define constants for the
** minimum and maximum 64-bit integers, or they define them
** inconsistently. And many do not understand the "LL" notation.
** So we define our own static constants here using nothing
** larger than a 32-bit integer constant.
*/
const i64 maxInt = LARGEST_INT64;
const i64 minInt = SMALLEST_INT64;
if (r < (double)minInt)
{
return minInt;
}
else if (r > (double)maxInt)
{
/* minInt is correct here - not maxInt. It turns out that assigning
** a very large positive number to an integer results in a very large
** negative integer. This makes no sense, but it is what x86 hardware
** does so for compatibility we will do the same in software. */
return minInt;
}
else
{
return (i64)r;
}
#endif
}
/// <summary>
/// Return some kind of integer value which is the best we can do
/// at representing the value that *pMem describes as an integer.
/// If pMem is an integer, then the value is exact. If pMem is
/// a floating-point then the value returned is the integer part.
/// If pMem is a string or blob, then we make an attempt to convert
/// it into a integer and return that. If pMem represents an
/// an SQL-NULL value, return 0.
///
/// If pMem represents a string value, its encoding might be changed.
/// </summary>
static i64 sqlite3VdbeIntValue(Mem pMem)
{
int flags;
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
// assert( EIGHT_BYTE_ALIGNMENT(pMem) );
flags = pMem.flags;
if ((flags & MEM_Int) != 0)
{
return pMem.u.i;
}
else if ((flags & MEM_Real) != 0)
{
return doubleToInt64(pMem.r);
}
else if ((flags & (MEM_Str)) != 0)
{
i64 value = 0;
Debug.Assert(pMem.z != null || pMem.n == 0);
testcase(pMem.z == null);
sqlite3Atoi64(pMem.z, ref value, pMem.n, pMem.enc);
return value;
}
else if ((flags & (MEM_Blob)) != 0)
{
i64 value = 0;
Debug.Assert(pMem.zBLOB != null || pMem.n == 0);
testcase(pMem.zBLOB == null);
sqlite3Atoi64(Encoding.UTF8.GetString(pMem.zBLOB, 0, pMem.n), ref value, pMem.n, pMem.enc);
return value;
}
else
{
return 0;
}
}
/// <summary>
/// Return the best representation of pMem that we can get into a
/// double. If pMem is already a double or an integer, return its
/// value. If it is a string or blob, try to convert it to a double.
/// If it is a NULL, return 0.0.
/// </summary>
static double sqlite3VdbeRealValue(Mem pMem)
{
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
//assert( EIGHT_BYTE_ALIGNMENT(pMem) );
if ((pMem.flags & MEM_Real) != 0)
{
return pMem.r;
}
else if ((pMem.flags & MEM_Int) != 0)
{
return (double)pMem.u.i;
}
else if ((pMem.flags & (MEM_Str)) != 0)
{
/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
double val = (double)0;
sqlite3AtoF(pMem.z, ref val, pMem.n, pMem.enc);
return val;
}
else if ((pMem.flags & (MEM_Blob)) != 0)
{
/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
double val = (double)0;
Debug.Assert(pMem.zBLOB != null || pMem.n == 0);
sqlite3AtoF(Encoding.UTF8.GetString(pMem.zBLOB, 0, pMem.n), ref val, pMem.n, pMem.enc);
return val;
}
else
{
/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
return (double)0;
}
}
/// <summary>
/// The MEM structure is already a MEM_Real. Try to also make it a
/// MEM_Int if we can.
/// </summary>
static void sqlite3VdbeIntegerAffinity(Mem pMem)
{
Debug.Assert((pMem.flags & MEM_Real) != 0);
Debug.Assert((pMem.flags & MEM_RowSet) == 0);
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
//assert( EIGHT_BYTE_ALIGNMENT(pMem) );
pMem.u.i = doubleToInt64(pMem.r);
/* Only mark the value as an integer if
**
** (1) the round-trip conversion real->int->real is a no-op, and
** (2) The integer is neither the largest nor the smallest
** possible integer (ticket #3922)
**
** The second and third terms in the following conditional enforces
** the second condition under the assumption that addition overflow causes
** values to wrap around. On x86 hardware, the third term is always
** true and could be omitted. But we leave it in because other
** architectures might behave differently.
*/
if (pMem.r == (double)pMem.u.i && pMem.u.i > SMALLEST_INT64
&& ALWAYS(pMem.u.i < LARGEST_INT64))
{
pMem.flags |= MEM_Int;
}
}
/// <summary>
/// Convert pMem to type integer. Invalidate any prior representations.
/// </summary>
static int sqlite3VdbeMemIntegerify(Mem pMem)
{
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
Debug.Assert((pMem.flags & MEM_RowSet) == 0);
//assert( EIGHT_BYTE_ALIGNMENT(pMem) );
pMem.u.i = sqlite3VdbeIntValue(pMem);
MemSetTypeFlag(pMem, MEM_Int);
return SQLITE_OK;
}
/// <summary>
/// Convert pMem so that it is of type MEM_Real.
/// Invalidate any prior representations.
/// </summary>
static int sqlite3VdbeMemRealify(Mem pMem)
{
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
//assert( EIGHT_BYTE_ALIGNMENT(pMem) );
pMem.r = sqlite3VdbeRealValue(pMem);
MemSetTypeFlag(pMem, MEM_Real);
return SQLITE_OK;
}
/// <summary>
/// Convert pMem so that it has types MEM_Real or MEM_Int or both.
/// Invalidate any prior representations.
///
/// Every effort is made to force the conversion, even if the input
/// is a string that does not look completely like a number. Convert
/// as much of the string as we can and ignore the rest.
/// </summary>
static int sqlite3VdbeMemNumerify(Mem pMem)
{
if ((pMem.flags & (MEM_Int | MEM_Real | MEM_Null)) == 0)
{
Debug.Assert((pMem.flags & (MEM_Blob | MEM_Str)) != 0);
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
if ((pMem.flags & MEM_Blob) != 0 && pMem.z == null)
{
if (0 == sqlite3Atoi64(Encoding.UTF8.GetString(pMem.zBLOB, 0, pMem.zBLOB.Length), ref pMem.u.i, pMem.n, pMem.enc))
MemSetTypeFlag(pMem, MEM_Int);
else
{
pMem.r = sqlite3VdbeRealValue(pMem);
MemSetTypeFlag(pMem, MEM_Real);
sqlite3VdbeIntegerAffinity(pMem);
}
}
else if (0 == sqlite3Atoi64(pMem.z, ref pMem.u.i, pMem.n, pMem.enc))
{
MemSetTypeFlag(pMem, MEM_Int);
}
else
{
pMem.r = sqlite3VdbeRealValue(pMem);
MemSetTypeFlag(pMem, MEM_Real);
sqlite3VdbeIntegerAffinity(pMem);
}
}
Debug.Assert((pMem.flags & (MEM_Int | MEM_Real | MEM_Null)) != 0);
pMem.flags = (ushort)(pMem.flags & ~(MEM_Str | MEM_Blob));
return SQLITE_OK;
}
#if !SQLITE_OMIT_FLOATING_POINT
/// <summary>
/// Delete any previous value and set the value stored in pMem to NULL.
/// </summary>
static void sqlite3VdbeMemSetNull(Mem pMem)
{
if ((pMem.flags & MEM_Frame) != 0)
{
VdbeFrame pFrame = pMem.u.pFrame;
pFrame.pParent = pFrame.v.pDelFrame;
pFrame.v.pDelFrame = pFrame;
}
if ((pMem.flags & MEM_RowSet) != 0)
{
sqlite3RowSetClear(pMem.u.pRowSet);
}
MemSetTypeFlag(pMem, MEM_Null);
sqlite3_free(ref pMem.zBLOB);
pMem.z = null;
pMem.type = SQLITE_NULL;
}
#endif
/// <summary>
/// Delete any previous value and set the value to be a BLOB of length
/// n containing all zeros.
/// </summary>
static void sqlite3VdbeMemSetZeroBlob(Mem pMem, int n)
{
sqlite3VdbeMemRelease(pMem);
pMem.flags = MEM_Blob | MEM_Zero;
pMem.type = SQLITE_BLOB;
pMem.n = 0;
if (n < 0)
n = 0;
pMem.u.nZero = n;
pMem.enc = SQLITE_UTF8;
#if SQLITE_OMIT_INCRBLOB
sqlite3VdbeMemGrow(pMem, n, 0);
//if( pMem.z!= null ){
pMem.n = n;
pMem.z = null;//memset(pMem.z, 0, n);
pMem.zBLOB = sqlite3Malloc(n);
//}
#endif
}
/// <summary>
/// Delete any previous value and set the value stored in pMem to val,
/// manifest type INTEGER.
/// </summary>
static void sqlite3VdbeMemSetInt64(Mem pMem, i64 val)
{
sqlite3VdbeMemRelease(pMem);
pMem.u.i = val;
pMem.flags = MEM_Int;
pMem.type = SQLITE_INTEGER;
}
/// <summary>
/// Delete any previous value and set the value stored in pMem to val,
/// manifest type REAL.
/// </summary>
static void sqlite3VdbeMemSetDouble(Mem pMem, double val)
{
if (sqlite3IsNaN(val))
{
sqlite3VdbeMemSetNull(pMem);
}
else
{
sqlite3VdbeMemRelease(pMem);
pMem.r = val;
pMem.flags = MEM_Real;
pMem.type = SQLITE_FLOAT;
}
}
/// <summary>
/// Delete any previous value and set the value of pMem to be an
/// empty boolean index.
/// </summary>
static void sqlite3VdbeMemSetRowSet(Mem pMem)
{
sqlite3 db = pMem.db;
Debug.Assert(db != null);
Debug.Assert((pMem.flags & MEM_RowSet) == 0);
sqlite3VdbeMemRelease(pMem);
//pMem.zMalloc = sqlite3DbMallocRaw( db, 64 );
//if ( db.mallocFailed != 0 )
//{
// pMem.flags = MEM_Null;
//}
//else
{
//Debug.Assert( pMem.zMalloc );
pMem.u.pRowSet = new RowSet(db, 5);// sqlite3RowSetInit( db, pMem.zMalloc,
// sqlite3DbMallocSize( db, pMem.zMalloc ) );
Debug.Assert(pMem.u.pRowSet != null);
pMem.flags = MEM_RowSet;
}
}
/// <summary>
/// Return true if the Mem object contains a TEXT or BLOB that is
/// too large - whose size exceeds p.db.aLimit[SQLITE_LIMIT_LENGTH].
/// </summary>
static bool sqlite3VdbeMemTooBig(Mem p)
{
//Debug.Assert( p.db != null );
if ((p.flags & (MEM_Str | MEM_Blob)) != 0)
{
int n = p.n;
if ((p.flags & MEM_Zero) != 0)
{
n += p.u.nZero;
}
return n > p.db.aLimit[SQLITE_LIMIT_LENGTH];
}
return false;
}
#if SQLITE_DEBUG
/*
** This routine prepares a memory cell for modication by breaking
** its link to a shallow copy and by marking any current shallow
** copies of this cell as invalid.
**
** This is used for testing and debugging only - to make sure shallow
** copies are not misused.
*/
static void sqlite3VdbeMemPrepareToChange( Vdbe pVdbe, Mem pMem )
{
int i;
Mem pX;
for ( i = 1; i <= pVdbe.nMem; i++ )
{
pX = pVdbe.aMem[i];
if ( pX.pScopyFrom == pMem )
{
pX.flags |= MEM_Invalid;
pX.pScopyFrom = null;
}
}
pMem.pScopyFrom = null;
}
#endif //* SQLITE_DEBUG */
/*
** Size of struct Mem not including the Mem.zMalloc member.
*/
//#define MEMCELLSIZE (size_t)(&(((Mem *)0).zMalloc))
/// <summary>
/// Make an shallow copy of pFrom into pTo. Prior contents of
/// pTo are freed. The pFrom.z field is not duplicated. If
/// pFrom.z is used, then pTo.z points to the same thing as pFrom.z
/// and flags gets srcType (either MEM_Ephem or MEM_Static).
/// </summary>
static void sqlite3VdbeMemShallowCopy(Mem pTo, Mem pFrom, int srcType)
{
Debug.Assert((pFrom.flags & MEM_RowSet) == 0);
sqlite3VdbeMemReleaseExternal(pTo);
pFrom.CopyTo(ref pTo);// memcpy(pTo, pFrom, MEMCELLSIZE);
pTo.xDel = null;
if ((pFrom.flags & MEM_Static) != 0)
{
pTo.flags = (u16)(pFrom.flags & ~(MEM_Dyn | MEM_Static | MEM_Ephem));
Debug.Assert(srcType == MEM_Ephem || srcType == MEM_Static);
pTo.flags |= (u16)srcType;
}
}
/// <summary>
/// Make a full copy of pFrom into pTo. Prior contents of pTo are
/// freed before the copy is made.
/// </summary>
static int sqlite3VdbeMemCopy(Mem pTo, Mem pFrom)
{
int rc = SQLITE_OK;
Debug.Assert((pFrom.flags & MEM_RowSet) == 0);
sqlite3VdbeMemReleaseExternal(pTo);
pFrom.CopyTo(ref pTo);// memcpy(pTo, pFrom, MEMCELLSIZE);
pTo.flags = (u16)(pTo.flags & ~MEM_Dyn);
if ((pTo.flags & (MEM_Str | MEM_Blob)) != 0)
{
if (0 == (pFrom.flags & MEM_Static))
{
pTo.flags |= MEM_Ephem;
rc = sqlite3VdbeMemMakeWriteable(pTo);
}
}
return rc;
}
/// <summary>
/// Transfer the contents of pFrom to pTo. Any existing value in pTo is
/// freed. If pFrom contains ephemeral data, a copy is made.
///
/// pFrom contains an SQL NULL when this routine returns.
/// </summary>
static void sqlite3VdbeMemMove(Mem pTo, Mem pFrom)
{
Debug.Assert(pFrom.db == null || sqlite3_mutex_held(pFrom.db.mutex));
Debug.Assert(pTo.db == null || sqlite3_mutex_held(pTo.db.mutex));
Debug.Assert(pFrom.db == null || pTo.db == null || pFrom.db == pTo.db);
sqlite3VdbeMemRelease(pTo);
pFrom.CopyTo(ref pTo);// memcpy(pTo, pFrom, Mem).Length;
pFrom.flags = MEM_Null;
pFrom.xDel = null;
pFrom.z = null;
sqlite3_free(ref pFrom.zBLOB); //pFrom.zMalloc=0;
}
/// <summary>
/// Change the value of a Mem to be a string or a BLOB.
///
/// The memory management strategy depends on the value of the xDel
/// parameter. If the value passed is SQLITE_TRANSIENT, then the
/// string is copied into a (possibly existing) buffer managed by the
/// Mem structure. Otherwise, any existing buffer is freed and the
/// pointer copied.
///
/// If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
/// size limit) then no memory allocation occurs. If the string can be
/// stored without allocating memory, then it is. If a memory allocation
/// is required to store the string, then value of pMem is unchanged. In
/// either case, SQLITE_TOOBIG is returned.
/// </summary>
/// <param name='pMem'>
/// Memory cell to set to string value.
/// </param>
/// <param name='zBlob'>
/// Blob pointer
/// </param>
/// <param name='n'>
/// Bytes in Blob
/// </param>
/// <param name='enc'>
/// 0 for BLOBs
/// </param>
/// <param name='xDel'>
/// Destructor function
/// </param>
static int sqlite3VdbeMemSetBlob(
Mem pMem,
byte[] zBlob,
int n,
u8 enc,
dxDel xDel
)
{
return sqlite3VdbeMemSetBlob(pMem, zBlob, 0, n >= 0 ? n : zBlob.Length, enc, xDel);
} // Call w/o offset
static int sqlite3VdbeMemSetBlob(
Mem pMem, /* Memory cell to set to string value */
byte[] zBlob, /* Blob pointer */
int offset, /* offset into string */
int n, /* Bytes in string, or negative */
u8 enc, /* Encoding of z. 0 for BLOBs */
dxDel xDel//)(void*)/* Destructor function */
)
{
int nByte = n; /* New value for pMem->n */
int iLimit; /* Maximum allowed string or blob size */
Debug.Assert(pMem.db == null || sqlite3_mutex_held(pMem.db.mutex));
Debug.Assert((pMem.flags & MEM_RowSet) == 0);
/* If zBlob is a NULL pointer, set pMem to contain an SQL NULL. */
if (zBlob == null || zBlob.Length < offset)
{
sqlite3VdbeMemSetNull(pMem);
return SQLITE_OK;
}
if (pMem.db != null)
{
iLimit = pMem.db.aLimit[SQLITE_LIMIT_LENGTH];
}
else
{
iLimit = SQLITE_MAX_LENGTH;
}
if (nByte < 0)
{
Debug.Assert(enc != 0);
if (enc == SQLITE_UTF8)
{
for (nByte = 0; nByte <= iLimit && nByte < zBlob.Length - offset && zBlob[offset + nByte] != 0; nByte++)
{
}
}
else
{
for (nByte = 0; nByte <= iLimit && zBlob[nByte + offset] != 0 || zBlob[offset + nByte + 1] != 0; nByte += 2)
{
}
}
}
/* The following block sets the new values of Mem.z and Mem.xDel. It
** also sets a flag in local variable "flags" to indicate the memory
** management (one of MEM_Dyn or MEM_Static).
*/
Debug.Assert(enc == 0);
{
pMem.z = null;
pMem.zBLOB = sqlite3Malloc(n);
Buffer.BlockCopy(zBlob, offset, pMem.zBLOB, 0, n);
}
pMem.n = nByte;
pMem.flags = MEM_Blob | MEM_Term;
pMem.enc = (enc == 0 ? SQLITE_UTF8 : enc);
pMem.type = (enc == 0 ? SQLITE_BLOB : SQLITE_TEXT);
if (nByte > iLimit)
{
return SQLITE_TOOBIG;
}
return SQLITE_OK;
}
/// <summary>
/// Sqlite3s the vdbe mem set string.
/// </summary>
/// <param name='pMem'>
/// Memory cell to set to string value
/// </param>
/// <param name='z'>
/// String pointer
/// </param>
/// <param name='n'>
/// Bytes in string, or negative
/// </param>
/// <param name='enc'>
/// Encoding of z. 0 for BLOBs.
/// </param>
/// <param name='xDel'>
/// Destructor function
/// </param>
static int sqlite3VdbeMemSetStr(
Mem pMem,
string z,
int n,
u8 enc,
dxDel xDel
)
{
return sqlite3VdbeMemSetStr(pMem, z, 0, n, enc, xDel);
} // Call w/o offset
/// <summary>
/// Sqlite3s the vdbe mem set string.
/// </summary>