-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathqrf.c
More file actions
3007 lines (2901 loc) · 91.1 KB
/
Copy pathqrf.c
File metadata and controls
3007 lines (2901 loc) · 91.1 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
/*
** 2025-10-20
**
** 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.
**
*************************************************************************
** Implementation of the Query Result-Format or "qrf" utility library for
** SQLite. See the README.md documentation for additional information.
*/
#ifndef SQLITE_QRF_H
#include "qrf.h"
#endif
#include <string.h>
#include <assert.h>
#include <stdint.h>
#ifndef SQLITE_AMALGAMATION
typedef sqlite3_int64 i64;
#endif
/* A single line in the EQP output */
typedef struct qrfEQPGraphRow qrfEQPGraphRow;
struct qrfEQPGraphRow {
int iEqpId; /* ID for this row */
int iParentId; /* ID of the parent row */
qrfEQPGraphRow *pNext; /* Next row in sequence */
char zText[1]; /* Text to display for this row */
};
/* All EQP output is collected into an instance of the following */
typedef struct qrfEQPGraph qrfEQPGraph;
struct qrfEQPGraph {
qrfEQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
qrfEQPGraphRow *pLast; /* Last element of the pRow list */
int nWidth; /* Width of the graph */
char zPrefix[400]; /* Graph prefix */
};
/*
** Private state information. Subject to change from one release to the
** next.
*/
typedef struct Qrf Qrf;
struct Qrf {
sqlite3_stmt *pStmt; /* The statement whose output is to be rendered */
sqlite3 *db; /* The corresponding database connection */
sqlite3_stmt *pJTrans; /* JSONB to JSON translator statement */
char **pzErr; /* Write error message here, if not NULL */
sqlite3_str *pOut; /* Accumulated output */
int iErr; /* Error code */
int nCol; /* Number of output columns */
int expMode; /* Original sqlite3_stmt_isexplain() plus 1 */
int mxWidth; /* Screen width */
int mxHeight; /* nLineLimit */
union {
struct { /* Content for QRF_STYLE_Line */
int mxColWth; /* Maximum display width of any column */
char **azCol; /* Names of output columns (MODE_Line) */
} sLine;
qrfEQPGraph *pGraph; /* EQP graph (Eqp, Stats, and StatsEst) */
struct { /* Content for QRF_STYLE_Explain */
int nIndent; /* Slots allocated for aiIndent */
int iIndent; /* Current slot */
int *aiIndent; /* Indentation for each opcode */
} sExpln;
unsigned int nIns; /* Bytes used for current INSERT stmt */
} u;
sqlite3_int64 nRow; /* Number of rows handled so far */
int *actualWidth; /* Actual width of each column */
sqlite3_qrf_spec spec; /* Copy of the original spec */
};
/*
** Data for substitute ctype.h functions. Used for x-platform
** consistency and so that '_' is counted as an alphabetic
** character.
**
** 0x01 - space
** 0x02 - digit
** 0x04 - alphabetic, including '_'
*/
static const char qrfCType[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0,
0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 4,
0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#define qrfSpace(x) ((qrfCType[(unsigned char)x]&1)!=0)
#define qrfDigit(x) ((qrfCType[(unsigned char)x]&2)!=0)
#define qrfAlpha(x) ((qrfCType[(unsigned char)x]&4)!=0)
#define qrfAlnum(x) ((qrfCType[(unsigned char)x]&6)!=0)
#ifndef deliberate_fall_through
/* Quiet some compilers about some of our intentional code. */
# if defined(GCC_VERSION) && GCC_VERSION>=7000000
# define deliberate_fall_through __attribute__((fallthrough));
# else
# define deliberate_fall_through
# endif
#endif
/*
** Set an error code and error message.
*/
static void qrfError(
Qrf *p, /* Query result state */
int iCode, /* Error code */
const char *zFormat, /* Message format (or NULL) */
...
){
p->iErr = iCode;
if( p->pzErr!=0 ){
sqlite3_free(*p->pzErr);
*p->pzErr = 0;
if( zFormat ){
va_list ap;
va_start(ap, zFormat);
*p->pzErr = sqlite3_vmprintf(zFormat, ap);
va_end(ap);
}
}
}
/*
** Out-of-memory error.
*/
static void qrfOom(Qrf *p){
qrfError(p, SQLITE_NOMEM, "out of memory");
}
/*
** Transfer any error in pStr over into p.
*/
static void qrfStrErr(Qrf *p, sqlite3_str *pStr){
int rc = pStr ? sqlite3_str_errcode(pStr) : 0;
if( rc ){
qrfError(p, rc, sqlite3_errstr(rc));
}
}
/*
** Add a new entry to the EXPLAIN QUERY PLAN data
*/
static void qrfEqpAppend(Qrf *p, int iEqpId, int p2, const char *zText){
qrfEQPGraphRow *pNew;
sqlite3_int64 nText;
if( zText==0 ) return;
if( p->u.pGraph==0 ){
p->u.pGraph = sqlite3_malloc64( sizeof(qrfEQPGraph) );
if( p->u.pGraph==0 ){
qrfOom(p);
return;
}
memset(p->u.pGraph, 0, sizeof(qrfEQPGraph) );
}
nText = strlen(zText);
pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
if( pNew==0 ){
qrfOom(p);
return;
}
pNew->iEqpId = iEqpId;
pNew->iParentId = p2;
memcpy(pNew->zText, zText, nText+1);
pNew->pNext = 0;
if( p->u.pGraph->pLast ){
p->u.pGraph->pLast->pNext = pNew;
}else{
p->u.pGraph->pRow = pNew;
}
p->u.pGraph->pLast = pNew;
}
/*
** Free and reset the EXPLAIN QUERY PLAN data that has been collected
** in p->u.pGraph.
*/
static void qrfEqpReset(Qrf *p){
qrfEQPGraphRow *pRow, *pNext;
if( p->u.pGraph ){
for(pRow = p->u.pGraph->pRow; pRow; pRow = pNext){
pNext = pRow->pNext;
sqlite3_free(pRow);
}
sqlite3_free(p->u.pGraph);
p->u.pGraph = 0;
}
}
/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
** pOld, or return the first such line if pOld is NULL
*/
static qrfEQPGraphRow *qrfEqpNextRow(Qrf *p, int iEqpId, qrfEQPGraphRow *pOld){
qrfEQPGraphRow *pRow = pOld ? pOld->pNext : p->u.pGraph->pRow;
while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
return pRow;
}
/* Render a single level of the graph that has iEqpId as its parent. Called
** recursively to render sublevels.
*/
static void qrfEqpRenderLevel(Qrf *p, int iEqpId){
qrfEQPGraphRow *pRow, *pNext;
i64 n = strlen(p->u.pGraph->zPrefix);
char *z;
for(pRow = qrfEqpNextRow(p, iEqpId, 0); pRow; pRow = pNext){
pNext = qrfEqpNextRow(p, iEqpId, pRow);
z = pRow->zText;
sqlite3_str_appendf(p->pOut, "%s%s%s\n", p->u.pGraph->zPrefix,
pNext ? "|--" : "`--", z);
if( n<(i64)sizeof(p->u.pGraph->zPrefix)-7 ){
memcpy(&p->u.pGraph->zPrefix[n], pNext ? "| " : " ", 4);
qrfEqpRenderLevel(p, pRow->iEqpId);
p->u.pGraph->zPrefix[n] = 0;
}
}
}
/*
** Render the 64-bit value N in a more human-readable format into
** pOut.
**
** + Only show the first three significant digits.
** + Append suffixes K, M, G, T, P, and E for 1e3, 1e6, ... 1e18
*/
static void qrfApproxInt64(sqlite3_str *pOut, i64 N){
static const char aSuffix[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
int i;
if( N<0 ){
N = N==INT64_MIN ? INT64_MAX : -N;
sqlite3_str_append(pOut, "-", 1);
}
if( N<10000 ){
sqlite3_str_appendf(pOut, "%4lld ", N);
return;
}
for(i=1; i<=18; i++){
N = (N+5)/10;
if( N<10000 ){
int n = (int)N;
switch( i%3 ){
case 0:
sqlite3_str_appendf(pOut, "%d.%02d", n/1000, (n%1000)/10);
break;
case 1:
sqlite3_str_appendf(pOut, "%2d.%d", n/100, (n%100)/10);
break;
case 2:
sqlite3_str_appendf(pOut, "%4d", n/10);
break;
}
sqlite3_str_append(pOut, &aSuffix[i/3], 1);
break;
}
}
}
/*
** Display and reset the EXPLAIN QUERY PLAN data
*/
static void qrfEqpRender(Qrf *p, i64 nCycle){
qrfEQPGraphRow *pRow;
if( p->u.pGraph!=0 && (pRow = p->u.pGraph->pRow)!=0 ){
if( pRow->zText[0]=='-' ){
if( pRow->pNext==0 ){
qrfEqpReset(p);
return;
}
sqlite3_str_appendf(p->pOut, "%s\n", pRow->zText+3);
p->u.pGraph->pRow = pRow->pNext;
sqlite3_free(pRow);
}else if( nCycle>0 ){
int nSp = p->u.pGraph->nWidth - 2;
if( p->spec.eStyle==QRF_STYLE_StatsEst ){
sqlite3_str_appendchar(p->pOut, nSp, ' ');
sqlite3_str_appendall(p->pOut,
"Cycles Loops (est) Rows (est)\n");
sqlite3_str_appendchar(p->pOut, nSp, ' ');
sqlite3_str_appendall(p->pOut,
"---------- ------------ ------------\n");
}else{
sqlite3_str_appendchar(p->pOut, nSp, ' ');
sqlite3_str_appendall(p->pOut,
"Cycles Loops Rows \n");
sqlite3_str_appendchar(p->pOut, nSp, ' ');
sqlite3_str_appendall(p->pOut,
"---------- ----- -----\n");
}
sqlite3_str_appendall(p->pOut, "QUERY PLAN");
sqlite3_str_appendchar(p->pOut, nSp - 10, ' ');
qrfApproxInt64(p->pOut, nCycle);
sqlite3_str_appendall(p->pOut, " 100%\n");
}else{
sqlite3_str_appendall(p->pOut, "QUERY PLAN\n");
}
p->u.pGraph->zPrefix[0] = 0;
qrfEqpRenderLevel(p, 0);
qrfEqpReset(p);
}
}
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
/*
** Helper function for qrfExpStats().
**
*/
static int qrfStatsHeight(sqlite3_stmt *p, int iEntry){
int iPid = 0;
int ret = 1;
sqlite3_stmt_scanstatus_v2(p, iEntry,
SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
);
while( iPid!=0 ){
int ii;
for(ii=0; 1; ii++){
int iId;
int res;
res = sqlite3_stmt_scanstatus_v2(p, ii,
SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId
);
if( res ) break;
if( iId==iPid ){
sqlite3_stmt_scanstatus_v2(p, ii,
SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
);
}
}
ret++;
}
return ret;
}
#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
/*
** Generate ".scanstatus est" style of EQP output.
*/
static void qrfEqpStats(Qrf *p){
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
qrfError(p, SQLITE_ERROR, "not available in this build");
#else
static const int f = SQLITE_SCANSTAT_COMPLEX;
sqlite3_stmt *pS = p->pStmt;
int i = 0;
i64 nTotal = 0;
int nWidth = 0;
int prevPid = -1; /* Previous iPid */
double rEstCum = 1.0; /* Cumulative row estimate */
sqlite3_str *pLine = sqlite3_str_new(p->db);
sqlite3_str *pStats = sqlite3_str_new(p->db);
qrfEqpReset(p);
for(i=0; 1; i++){
const char *z = 0;
int n = 0;
if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
break;
}
n = (int)strlen(z) + qrfStatsHeight(pS,i)*3;
if( n>nWidth ) nWidth = n;
}
nWidth += 2;
sqlite3_stmt_scanstatus_v2(pS,-1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
for(i=0; 1; i++){
i64 nLoop = 0;
i64 nRow = 0;
i64 nCycle = 0;
int iId = 0;
int iPid = 0;
const char *zo = 0;
const char *zName = 0;
double rEst = 0.0;
if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
break;
}
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
if( iPid!=prevPid ){
prevPid = iPid;
rEstCum = 1.0;
}
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
rEstCum *= rEst;
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
int nSp = 0;
sqlite3_str_reset(pStats);
if( nCycle>=0 && nTotal>0 ){
qrfApproxInt64(pStats, nCycle);
sqlite3_str_appendf(pStats, " %3d%%",
((nCycle*100)+nTotal/2) / nTotal
);
nSp = 2;
}
if( nLoop>=0 ){
if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
qrfApproxInt64(pStats, nLoop);
nSp = 2;
if( p->spec.eStyle==QRF_STYLE_StatsEst ){
sqlite3_str_appendf(pStats, " ");
qrfApproxInt64(pStats, (i64)(rEstCum/rEst));
}
}
if( nRow>=0 ){
if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
qrfApproxInt64(pStats, nRow);
nSp = 2;
if( p->spec.eStyle==QRF_STYLE_StatsEst ){
sqlite3_str_appendf(pStats, " ");
qrfApproxInt64(pStats, (i64)rEstCum);
}
}
sqlite3_str_appendf(pLine,
"% *s %s", -1*(nWidth-qrfStatsHeight(pS,i)*3), zo,
sqlite3_str_value(pStats)
);
sqlite3_str_reset(pStats);
qrfEqpAppend(p, iId, iPid, sqlite3_str_value(pLine));
sqlite3_str_reset(pLine);
}else{
qrfEqpAppend(p, iId, iPid, zo);
}
}
if( p->u.pGraph ) p->u.pGraph->nWidth = nWidth;
qrfStrErr(p, pLine);
sqlite3_free(sqlite3_str_finish(pLine));
qrfStrErr(p, pStats);
sqlite3_free(sqlite3_str_finish(pStats));
#endif
}
/*
** Reset the prepared statement.
*/
static void qrfResetStmt(Qrf *p){
int rc = sqlite3_reset(p->pStmt);
if( rc!=SQLITE_OK && p->iErr==SQLITE_OK ){
qrfError(p, rc, "%s", sqlite3_errmsg(p->db));
}
}
/*
** If xWrite is defined, send all content of pOut to xWrite and
** reset pOut.
*/
static void qrfWrite(Qrf *p){
int n;
if( p->spec.xWrite && (n = sqlite3_str_length(p->pOut))>0 ){
int rc = p->spec.xWrite(p->spec.pWriteArg,
sqlite3_str_value(p->pOut),
(sqlite3_int64)n);
sqlite3_str_reset(p->pOut);
if( rc ){
qrfError(p, rc, "Failed to write %d bytes of output", n);
}
}
}
/* Lookup table to estimate the number of columns consumed by a Unicode
** character.
*/
static const struct {
unsigned char w; /* Width of the character in columns */
int iFirst; /* First character in a span having this width */
} aQrfUWidth[] = {
/* {1, 0x00000}, */
{0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488},
{1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0},
{0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7},
{1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616},
{0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6},
{1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee},
{0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730},
{1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4},
{0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941},
{1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955},
{0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc},
{1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce},
{0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c},
{1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49},
{0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81},
{1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6},
{0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2},
{1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d},
{0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d},
{1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83},
{0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e},
{1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e},
{0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf},
{1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce},
{0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d},
{1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5},
{0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34},
{1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2},
{0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8},
{1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36},
{0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71},
{1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88},
{0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6},
{1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033},
{0, 0x01036}, {1, 0x0103b}, {0, 0x01058},
{1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f},
{1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735},
{0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4},
{1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7},
{0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b},
{1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923},
{0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939},
{1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04},
{0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c},
{1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74},
{0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b},
{1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064},
{0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329},
{1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f},
{2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806},
{1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827},
{2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e},
{1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20},
{1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00},
{1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc},
{0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c},
{1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40},
{0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185},
{1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245},
{2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001},
{1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0}
};
/*
** Return an estimate of the width, in columns, for the single Unicode
** character c. For normal characters, the answer is always 1. But the
** estimate might be 0 or 2 for zero-width and double-width characters.
**
** Different display devices display unicode using different widths. So
** it is impossible to know that true display width with 100% accuracy.
** Inaccuracies in the width estimates might cause columns to be misaligned.
** Unfortunately, there is nothing we can do about that.
*/
int sqlite3_qrf_wcwidth(int c){
int iFirst, iLast;
/* Fast path for common characters */
if( c<0x300 ) return 1;
/* The general case */
iFirst = 0;
iLast = sizeof(aQrfUWidth)/sizeof(aQrfUWidth[0]) - 1;
while( iFirst<iLast-1 ){
int iMid = (iFirst+iLast)/2;
int cMid = aQrfUWidth[iMid].iFirst;
if( cMid < c ){
iFirst = iMid;
}else if( cMid > c ){
iLast = iMid - 1;
}else{
return aQrfUWidth[iMid].w;
}
}
if( aQrfUWidth[iLast].iFirst > c ) return aQrfUWidth[iFirst].w;
return aQrfUWidth[iLast].w;
}
/*
** Compute the value and length of a multi-byte UTF-8 character that
** begins at z[0]. Return the length. Write the Unicode value into *pU.
**
** This routine only works for *multi-byte* UTF-8 characters. It does
** not attempt to detect illegal characters.
*/
int sqlite3_qrf_decode_utf8(const unsigned char *z, int *pU){
if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){
*pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f);
return 2;
}
if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){
*pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f);
return 3;
}
if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
&& (z[3] & 0xc0)==0x80
){
*pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
| (z[3] & 0x3f);
return 4;
}
*pU = 0;
return 1;
}
/*
** Check to see if z[] is a valid VT100 escape. If it is, then
** return the number of bytes in the escape sequence. Return 0 if
** z[] is not a VT100 escape.
**
** This routine assumes that z[0] is \033 (ESC).
*/
static int qrfIsVt100(const unsigned char *z){
int i;
if( z[1]!='[' ) return 0;
i = 2;
while( z[i]>=0x30 && z[i]<=0x3f ){ i++; }
while( z[i]>=0x20 && z[i]<=0x2f ){ i++; }
if( z[i]<0x40 || z[i]>0x7e ) return 0;
return i+1;
}
/*
** Return the length of a string in display characters.
**
** Most characters of the input string count as 1, including
** multi-byte UTF8 characters. However, zero-width unicode
** characters and VT100 escape sequences count as zero, and
** double-width characters count as two.
**
** The definition of "zero-width" and "double-width" characters
** is not precise. It depends on the output device, to some extent,
** and it varies according to the Unicode version. This routine
** makes the best guess that it can.
*/
size_t sqlite3_qrf_wcswidth(const char *zIn){
const unsigned char *z = (const unsigned char*)zIn;
size_t n = 0;
while( *z ){
if( z[0]<' ' ){
int k;
if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){
z += k;
}else{
z++;
}
}else if( (0x80&z[0])==0 ){
n++;
z++;
}else{
int u = 0;
int len = sqlite3_qrf_decode_utf8(z, &u);
z += len;
n += sqlite3_qrf_wcwidth(u);
}
}
return n;
}
/*
** Return the display width of the longest line of text
** in the (possibly) multi-line input string zIn[0..nByte].
** zIn[] is not necessarily zero-terminated. Take
** into account tab characters, zero- and double-width
** characters, CR and NL, and VT100 escape codes.
**
** Write the number of newlines into *pnNL. So, *pnNL will
** return 0 if everything fits on one line, or positive it
** it will need to be split.
*/
static int qrfDisplayWidth(const char *zIn, sqlite3_int64 nByte, int *pnNL){
const unsigned char *z;
const unsigned char *zEnd;
int mx = 0;
int n = 0;
int nNL = 0;
if( zIn==0 ) zIn = "";
z = (const unsigned char*)zIn;
zEnd = &z[nByte];
while( z<zEnd ){
if( z[0]<' ' ){
int k;
if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){
z += k;
}else{
if( z[0]=='\t' ){
n = (n+8)&~7;
}else if( z[0]=='\n' || z[0]=='\r' ){
nNL++;
if( n>mx ) mx = n;
n = 0;
}
z++;
}
}else if( (0x80&z[0])==0 ){
n++;
z++;
}else{
int u = 0;
int len = sqlite3_qrf_decode_utf8(z, &u);
z += len;
n += sqlite3_qrf_wcwidth(u);
}
}
if( mx>n ) n = mx;
if( pnNL ) *pnNL = nNL;
return n;
}
/*
** Escape the input string if it is needed and in accordance with
** eEsc, which is either QRF_ESC_Ascii or QRF_ESC_Symbol.
**
** Escaping is needed if the string contains any control characters
** other than \t, \n, and \r\n
**
** If no escaping is needed (the common case) then set *ppOut to NULL
** and return 0. If escaping is needed, write the escaped string into
** memory obtained from sqlite3_malloc64() and make *ppOut point to that
** memory and return 0. If an error occurs, return non-zero.
**
** The caller is responsible for freeing *ppFree if it is non-NULL in order
** to reclaim memory.
*/
static void qrfEscape(
int eEsc, /* QRF_ESC_Ascii or QRF_ESC_Symbol */
sqlite3_str *pStr, /* String to be escaped */
int iStart /* Begin escapding on this byte of pStr */
){
sqlite3_int64 i, j; /* Loop counters */
sqlite3_int64 sz; /* Size of the string prior to escaping */
sqlite3_int64 nCtrl = 0;/* Number of control characters to escape */
unsigned char *zIn; /* Text to be escaped */
unsigned char c; /* A single character of the text */
unsigned char *zOut; /* Where to write the results */
/* Find the text to be escaped */
zIn = (unsigned char*)sqlite3_str_value(pStr);
if( zIn==0 ) return;
zIn += iStart;
/* Count the control characters */
for(i=0; (c = zIn[i])!=0; i++){
if( c<=0x1f
&& c!='\t'
&& c!='\n'
&& (c!='\r' || zIn[i+1]!='\n')
){
nCtrl++;
}
}
if( nCtrl==0 ) return; /* Early out if no control characters */
/* Make space to hold the escapes. Copy the original text to the end
** of the available space. */
sz = sqlite3_str_length(pStr) - iStart;
if( eEsc==QRF_ESC_Symbol ) nCtrl *= 2;
sqlite3_str_appendchar(pStr, nCtrl, ' ');
zOut = (unsigned char*)sqlite3_str_value(pStr);
if( zOut==0 ) return;
zOut += iStart;
zIn = zOut + nCtrl;
memmove(zIn,zOut,sz);
/* Convert the control characters */
for(i=j=0; (c = zIn[i])!=0; i++){
if( c>0x1f
|| c=='\t'
|| c=='\n'
|| (c=='\r' && zIn[i+1]=='\n')
){
continue;
}
if( i>0 ){
memmove(&zOut[j], zIn, i);
j += i;
}
zIn += i+1;
i = -1;
if( eEsc==QRF_ESC_Symbol ){
zOut[j++] = 0xe2;
zOut[j++] = 0x90;
zOut[j++] = 0x80+c;
}else{
zOut[j++] = '^';
zOut[j++] = 0x40+c;
}
}
}
/*
** Determine if the string z[] can be shown as plain text. Return true
** if z[] is unambiguously text. Return false if z[] needs to be
** quoted.
**
** All of the following must be true in order for z[] to be relaxable:
**
** (1) z[] does not begin or end with ' or whitespace
** (2) z[] is not the same as the NULL rendering
** (3) z[] does not looks like a numeric literal
*/
static int qrfRelaxable(Qrf *p, const char *z){
size_t i, n;
if( z[0]=='\'' || qrfSpace(z[0]) ) return 0;
if( z[0]==0 ){
return (p->spec.zNull!=0 && p->spec.zNull[0]!=0);
}
n = strlen(z);
if( n==0 || z[n-1]=='\'' || qrfSpace(z[n-1]) ) return 0;
if( p->spec.zNull && strcmp(p->spec.zNull,z)==0 ) return 0;
i = (z[0]=='-' || z[0]=='+');
if( strcmp(z+i,"Inf")==0 ) return 0;
if( !qrfDigit(z[i]) ) return 1;
i++;
while( qrfDigit(z[i]) ){ i++; }
if( z[i]==0 ) return 0;
if( z[i]=='.' ){
i++;
while( qrfDigit(z[i]) ){ i++; }
if( z[i]==0 ) return 0;
}
if( z[i]=='e' || z[i]=='E' ){
i++;
if( z[i]=='+' || z[i]=='-' ){ i++; }
if( !qrfDigit(z[i]) ) return 1;
i++;
while( qrfDigit(z[i]) ){ i++; }
}
return z[i]!=0;
}
/*
** If a field contains any character identified by a 1 in the following
** array, then the string must be quoted for CSV.
*/
static const char qrfCsvQuote[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
/*
** Encode text appropriately and append it to pOut.
*/
static void qrfEncodeText(Qrf *p, sqlite3_str *pOut, const char *zTxt){
int iStart = sqlite3_str_length(pOut);
switch( p->spec.eText ){
case QRF_TEXT_Relaxed:
if( qrfRelaxable(p, zTxt) ){
sqlite3_str_appendall(pOut, zTxt);
break;
}
deliberate_fall_through; /* FALLTHRU */
case QRF_TEXT_Sql: {
if( p->spec.eEsc==QRF_ESC_Off ){
sqlite3_str_appendf(pOut, "%Q", zTxt);
}else{
sqlite3_str_appendf(pOut, "%#Q", zTxt);
}
break;
}
case QRF_TEXT_Csv: {
unsigned int i;
for(i=0; zTxt[i]; i++){
if( qrfCsvQuote[((const unsigned char*)zTxt)[i]] ){
i = 0;
break;
}
}
if( i==0 || strstr(zTxt, p->spec.zColumnSep)!=0 ){
sqlite3_str_appendf(pOut, "\"%w\"", zTxt);
}else{
sqlite3_str_appendall(pOut, zTxt);
}
break;
}
case QRF_TEXT_Html: {
const unsigned char *z = (const unsigned char*)zTxt;
while( *z ){
unsigned int i = 0;
unsigned char c;
while( (c=z[i])>'>'
|| (c && c!='<' && c!='>' && c!='&' && c!='\"' && c!='\'')
){
i++;
}
if( i>0 ){
sqlite3_str_append(pOut, (const char*)z, i);
}
switch( z[i] ){
case '>': sqlite3_str_append(pOut, ">", 4); break;
case '&': sqlite3_str_append(pOut, "&", 5); break;
case '<': sqlite3_str_append(pOut, "<", 4); break;
case '"': sqlite3_str_append(pOut, """, 6); break;
case '\'': sqlite3_str_append(pOut, "'", 5); break;
default: i--;
}
z += i + 1;
}
break;
}
case QRF_TEXT_Tcl:
case QRF_TEXT_Json: {
const unsigned char *z = (const unsigned char*)zTxt;
sqlite3_str_append(pOut, "\"", 1);
while( *z ){
unsigned int i;
for(i=0; z[i]>=0x20 && z[i]!='\\' && z[i]!='"'; i++){}
if( i>0 ){
sqlite3_str_append(pOut, (const char*)z, i);
}
if( z[i]==0 ) break;
switch( z[i] ){
case '"': sqlite3_str_append(pOut, "\\\"", 2); break;
case '\\': sqlite3_str_append(pOut, "\\\\", 2); break;
case '\b': sqlite3_str_append(pOut, "\\b", 2); break;
case '\f': sqlite3_str_append(pOut, "\\f", 2); break;
case '\n': sqlite3_str_append(pOut, "\\n", 2); break;
case '\r': sqlite3_str_append(pOut, "\\r", 2); break;
case '\t': sqlite3_str_append(pOut, "\\t", 2); break;
default: {
if( p->spec.eText==QRF_TEXT_Json ){
sqlite3_str_appendf(pOut, "\\u%04x", z[i]);
}else{
sqlite3_str_appendf(pOut, "\\%03o", z[i]);
}
break;
}
}
z += i + 1;
}
sqlite3_str_append(pOut, "\"", 1);
break;
}
default: {
sqlite3_str_appendall(pOut, zTxt);
break;
}
}
if( p->spec.eEsc!=QRF_ESC_Off ){
qrfEscape(p->spec.eEsc, pOut, iStart);
}
}
/*
** Do a quick sanity check to see aBlob[0..nBlob-1] is valid JSONB
** return true if it is and false if it is not.
**
** False positives are possible, but not false negatives.
*/
static int qrfJsonbQuickCheck(unsigned char *aBlob, int nBlob){
unsigned char x; /* Payload size half-byte */
int i; /* Loop counter */
int n; /* Bytes in the payload size integer */
sqlite3_uint64 sz; /* value of the payload size integer */
if( nBlob==0 ) return 0;
x = aBlob[0]>>4;
if( x<=11 ) return nBlob==(1+x);
n = x<14 ? x-11 : 4*(x-13);
if( nBlob<1+n ) return 0;
sz = aBlob[1];
for(i=1; i<n; i++) sz = (sz<<8) + aBlob[i+1];
return sz+n+1==(sqlite3_uint64)nBlob;
}
/*
** The current iCol-th column of p->pStmt is known to be a BLOB. Check
** to see if that BLOB is really a JSONB blob. If it is, then translate
** it into a text JSON representation and return a pointer to that text JSON.
** If the BLOB is not JSONB, then return a NULL pointer.
**
** The memory used to hold the JSON text is managed internally by the
** "p" object and is overwritten and/or deallocated upon the next call
** to this routine (with the same p argument) or when the p object is
** finailized.
*/