FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlcipher/src/tclsqlite.c at sqlite-release · securitykernel/sqlcipher · GitHub
securitykernel
/
sqlcipher
Public
forked from
sqlcipher/sqlcipher
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlcipher
/
src
/
tclsqlite.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
4032 lines (3793 loc) · 124 KB
Breadcrumbs
sqlcipher
/
src
/
tclsqlite.c
Copy path
File metadata and controls
4032 lines (3793 loc) · 124 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
** 2001 September 15
**
** 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.
**
*************************************************************************
** A TCL Interface to SQLite. Append this file to sqlite3.c and
** compile the whole thing to build a TCL-enabled version of SQLite.
**
** Compile-time options:
**
** -DTCLSH Add a "main()" routine that works as a tclsh.
**
** -DTCLSH_INIT_PROC=name
**
** Invoke name(interp) to initialize the Tcl interpreter.
** If name(interp) returns a non-NULL string, then run
** that string as a Tcl script to launch the application.
** If name(interp) returns NULL, then run the regular
** tclsh-emulator code.
*/
#ifdef
TCLSH_INIT_PROC
# define
TCLSH
1
#endif
/*
** If requested, include the SQLite compiler options file for MSVC.
*/
#if
defined(
INCLUDE_MSVC_H
)
# include
"msvc.h"
#endif
#if
defined(
INCLUDE_SQLITE_TCL_H
)
# include
"sqlite_tcl.h"
#else
# include
"tcl.h"
# ifndef
SQLITE_TCLAPI
# define
SQLITE_TCLAPI
# endif
#endif
#include
<errno.h>
/*
** Some additional include files are needed if this file is not
** appended to the amalgamation.
*/
#ifndef
SQLITE_AMALGAMATION
# include
"sqlite3.h"
# include
<stdlib.h>
# include
<string.h>
# include
<assert.h>
typedef
unsigned
char
u8
;
#endif
#include
<ctype.h>
/* Used to get the current process ID */
#if
!defined(
_WIN32
)
# include
<signal.h>
# include
<unistd.h>
# define
GETPID
getpid
#elif
!defined(
_WIN32_WCE
)
# ifndef
SQLITE_AMALGAMATION
# ifndef
WIN32_LEAN_AND_MEAN
# define
WIN32_LEAN_AND_MEAN
# endif
# include
<windows.h>
# endif
# include
<io.h>
# define
isatty
(
h
) _isatty(h)
# define
GETPID
(int)GetCurrentProcessId
#endif
/*
* Windows needs to know which symbols to export. Unix does not.
* BUILD_sqlite should be undefined for Unix.
*/
#ifdef
BUILD_sqlite
#undef
TCL_STORAGE_CLASS
#define
TCL_STORAGE_CLASS
DLLEXPORT
#endif
/* BUILD_sqlite */
#define
NUM_PREPARED_STMTS
10
#define
MAX_PREPARED_STMTS
100
/* Forward declaration */
typedef
struct
SqliteDb
SqliteDb
;
/*
** New SQL functions can be created as TCL scripts. Each such function
** is described by an instance of the following structure.
**
** Variable eType may be set to SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT,
** SQLITE_BLOB or SQLITE_NULL. If it is SQLITE_NULL, then the implementation
** attempts to determine the type of the result based on the Tcl object.
** If it is SQLITE_TEXT or SQLITE_BLOB, then a text (sqlite3_result_text())
** or blob (sqlite3_result_blob()) is returned. If it is SQLITE_INTEGER
** or SQLITE_FLOAT, then an attempt is made to return an integer or float
** value, falling back to float and then text if this is not possible.
*/
typedef
struct
SqlFunc
SqlFunc
;
struct
SqlFunc
{
Tcl_Interp
*
interp
;
/* The TCL interpret to execute the function */
Tcl_Obj
*
pScript
;
/* The Tcl_Obj representation of the script */
SqliteDb
*
pDb
;
/* Database connection that owns this function */
int
useEvalObjv
;
/* True if it is safe to use Tcl_EvalObjv */
int
eType
;
/* Type of value to return */
char
*
zName
;
/* Name of this function */
SqlFunc
*
pNext
;
/* Next function on the list of them all */
};
/*
** New collation sequences function can be created as TCL scripts. Each such
** function is described by an instance of the following structure.
*/
typedef
struct
SqlCollate
SqlCollate
;
struct
SqlCollate
{
Tcl_Interp
*
interp
;
/* The TCL interpret to execute the function */
char
*
zScript
;
/* The script to be run */
SqlCollate
*
pNext
;
/* Next function on the list of them all */
};
/*
** Prepared statements are cached for faster execution. Each prepared
** statement is described by an instance of the following structure.
*/
typedef
struct
SqlPreparedStmt
SqlPreparedStmt
;
struct
SqlPreparedStmt
{
SqlPreparedStmt
*
pNext
;
/* Next in linked list */
SqlPreparedStmt
*
pPrev
;
/* Previous on the list */
sqlite3_stmt
*
pStmt
;
/* The prepared statement */
int
nSql
;
/* chars in zSql[] */
const
char
*
zSql
;
/* Text of the SQL statement */
int
nParm
;
/* Size of apParm array */
Tcl_Obj
*
*
apParm
;
/* Array of referenced object pointers */
};
typedef
struct
IncrblobChannel
IncrblobChannel
;
/*
** There is one instance of this structure for each SQLite database
** that has been opened by the SQLite TCL interface.
**
** If this module is built with SQLITE_TEST defined (to create the SQLite
** testfixture executable), then it may be configured to use either
** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements.
** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used.
*/
struct
SqliteDb
{
sqlite3
*
db
;
/* The "real" database structure. MUST BE FIRST */
Tcl_Interp
*
interp
;
/* The interpreter used for this database */
char
*
zBusy
;
/* The busy callback routine */
char
*
zCommit
;
/* The commit hook callback routine */
char
*
zTrace
;
/* The trace callback routine */
char
*
zTraceV2
;
/* The trace_v2 callback routine */
char
*
zProfile
;
/* The profile callback routine */
char
*
zProgress
;
/* The progress callback routine */
char
*
zBindFallback
;
/* Callback to invoke on a binding miss */
char
*
zAuth
;
/* The authorization callback routine */
int
disableAuth
;
/* Disable the authorizer if it exists */
char
*
zNull
;
/* Text to substitute for an SQL NULL value */
SqlFunc
*
pFunc
;
/* List of SQL functions */
Tcl_Obj
*
pUpdateHook
;
/* Update hook script (if any) */
Tcl_Obj
*
pPreUpdateHook
;
/* Pre-update hook script (if any) */
Tcl_Obj
*
pRollbackHook
;
/* Rollback hook script (if any) */
Tcl_Obj
*
pWalHook
;
/* WAL hook script (if any) */
Tcl_Obj
*
pUnlockNotify
;
/* Unlock notify script (if any) */
SqlCollate
*
pCollate
;
/* List of SQL collation functions */
int
rc
;
/* Return code of most recent sqlite3_exec() */
Tcl_Obj
*
pCollateNeeded
;
/* Collation needed script */
SqlPreparedStmt
*
stmtList
;
/* List of prepared statements*/
SqlPreparedStmt
*
stmtLast
;
/* Last statement in the list */
int
maxStmt
;
/* The next maximum number of stmtList */
int
nStmt
;
/* Number of statements in stmtList */
IncrblobChannel
*
pIncrblob
;
/* Linked list of open incrblob channels */
int
nStep
,
nSort
,
nIndex
;
/* Statistics for most recent operation */
int
nVMStep
;
/* Another statistic for most recent operation */
int
nTransaction
;
/* Number of nested [transaction] methods */
int
openFlags
;
/* Flags used to open. (SQLITE_OPEN_URI) */
#ifdef
SQLITE_TEST
int
bLegacyPrepare
;
/* True to use sqlite3_prepare() */
#endif
};
struct
IncrblobChannel
{
sqlite3_blob
*
pBlob
;
/* sqlite3 blob handle */
SqliteDb
*
pDb
;
/* Associated database connection */
int
iSeek
;
/* Current seek offset */
Tcl_Channel
channel
;
/* Channel identifier */
IncrblobChannel
*
pNext
;
/* Linked list of all open incrblob channels */
IncrblobChannel
*
pPrev
;
/* Linked list of all open incrblob channels */
};
/*
** Compute a string length that is limited to what can be stored in
** lower 30 bits of a 32-bit signed integer.
*/
static
int
strlen30
(
const
char
*
z
){
const
char
*
z2
=
z
;
while
(
*
z2
){
z2
++
; }
return
0x3fffffff
&
(
int
)(
z2
-
z
);
}
#ifndef
SQLITE_OMIT_INCRBLOB
/*
** Close all incrblob channels opened using database connection pDb.
** This is called when shutting down the database connection.
*/
static
void
closeIncrblobChannels
(
SqliteDb
*
pDb
){
IncrblobChannel
*
p
;
IncrblobChannel
*
pNext
;
for
(
p
=
pDb
->
pIncrblob
;
p
;
p
=
pNext
){
pNext
=
p
->
pNext
;
/* Note: Calling unregister here call Tcl_Close on the incrblob channel,
** which deletes the IncrblobChannel structure at *p. So do not
** call Tcl_Free() here.
*/
Tcl_UnregisterChannel
(
pDb
->
interp
,
p
->
channel
);
}
}
/*
** Close an incremental blob channel.
*/
static
int
SQLITE_TCLAPI
incrblobClose
(
ClientData
instanceData
,
Tcl_Interp
*
interp
){
IncrblobChannel
*
p
=
(
IncrblobChannel
*
)
instanceData
;
int
rc
=
sqlite3_blob_close
(
p
->
pBlob
);
sqlite3
*
db
=
p
->
pDb
->
db
;
/* Remove the channel from the SqliteDb.pIncrblob list. */
if
(
p
->
pNext
){
p
->
pNext
->
pPrev
=
p
->
pPrev
;
}
if
(
p
->
pPrev
){
p
->
pPrev
->
pNext
=
p
->
pNext
;
}
if
(
p
->
pDb
->
pIncrblob
==
p
){
p
->
pDb
->
pIncrblob
=
p
->
pNext
;
}
/* Free the IncrblobChannel structure */
Tcl_Free
((
char
*
)
p
);
if
(
rc
!=
SQLITE_OK
){
Tcl_SetResult
(
interp
, (
char
*
)
sqlite3_errmsg
(
db
),
TCL_VOLATILE
);
return
TCL_ERROR
;
}
return
TCL_OK
;
}
/*
** Read data from an incremental blob channel.
*/
static
int
SQLITE_TCLAPI
incrblobInput
(
ClientData
instanceData
,
char
*
buf
,
int
bufSize
,
int
*
errorCodePtr
){
IncrblobChannel
*
p
=
(
IncrblobChannel
*
)
instanceData
;
int
nRead
=
bufSize
;
/* Number of bytes to read */
int
nBlob
;
/* Total size of the blob */
int
rc
;
/* sqlite error code */
nBlob
=
sqlite3_blob_bytes
(
p
->
pBlob
);
if
( (
p
->
iSeek
+
nRead
)
>
nBlob
){
nRead
=
nBlob
-
p
->
iSeek
;
}
if
(
nRead
<=
0
){
return
0
;
}
rc
=
sqlite3_blob_read
(
p
->
pBlob
, (
void
*
)
buf
,
nRead
,
p
->
iSeek
);
if
(
rc
!=
SQLITE_OK
){
*
errorCodePtr
=
rc
;
return
-1
;
}
p
->
iSeek
+=
nRead
;
return
nRead
;
}
/*
** Write data to an incremental blob channel.
*/
static
int
SQLITE_TCLAPI
incrblobOutput
(
ClientData
instanceData
,
CONST
char
*
buf
,
int
toWrite
,
int
*
errorCodePtr
){
IncrblobChannel
*
p
=
(
IncrblobChannel
*
)
instanceData
;
int
nWrite
=
toWrite
;
/* Number of bytes to write */
int
nBlob
;
/* Total size of the blob */
int
rc
;
/* sqlite error code */
nBlob
=
sqlite3_blob_bytes
(
p
->
pBlob
);
if
( (
p
->
iSeek
+
nWrite
)
>
nBlob
){
*
errorCodePtr
=
EINVAL
;
return
-1
;
}
if
(
nWrite
<=
0
){
return
0
;
}
rc
=
sqlite3_blob_write
(
p
->
pBlob
, (
void
*
)
buf
,
nWrite
,
p
->
iSeek
);
if
(
rc
!=
SQLITE_OK
){
*
errorCodePtr
=
EIO
;
return
-1
;
}
p
->
iSeek
+=
nWrite
;
return
nWrite
;
}
/*
** Seek an incremental blob channel.
*/
static
int
SQLITE_TCLAPI
incrblobSeek
(
ClientData
instanceData
,
long
offset
,
int
seekMode
,
int
*
errorCodePtr
){
IncrblobChannel
*
p
=
(
IncrblobChannel
*
)
instanceData
;
switch
(
seekMode
){
case
SEEK_SET
:
p
->
iSeek
=
offset
;
break
;
case
SEEK_CUR
:
p
->
iSeek
+=
offset
;
break
;
case
SEEK_END
:
p
->
iSeek
=
sqlite3_blob_bytes
(
p
->
pBlob
)
+
offset
;
break
;
default
:
assert
(!
"Bad seekMode"
);
}
return
p
->
iSeek
;
}
static
void
SQLITE_TCLAPI
incrblobWatch
(
ClientData
instanceData
,
int
mode
){
/* NO-OP */
}
static
int
SQLITE_TCLAPI
incrblobHandle
(
ClientData
instanceData
,
int
dir
,
ClientData
*
hPtr
){
return
TCL_ERROR
;
}
static
Tcl_ChannelType
IncrblobChannelType
=
{
"incrblob"
,
/* typeName */
TCL_CHANNEL_VERSION_2
,
/* version */
incrblobClose
,
/* closeProc */
incrblobInput
,
/* inputProc */
incrblobOutput
,
/* outputProc */
incrblobSeek
,
/* seekProc */
0
,
/* setOptionProc */
0
,
/* getOptionProc */
incrblobWatch
,
/* watchProc (this is a no-op) */
incrblobHandle
,
/* getHandleProc (always returns error) */
0
,
/* close2Proc */
0
,
/* blockModeProc */
0
,
/* flushProc */
0
,
/* handlerProc */
0
,
/* wideSeekProc */
};
/*
** Create a new incrblob channel.
*/
static
int
createIncrblobChannel
(
Tcl_Interp
*
interp
,
SqliteDb
*
pDb
,
const
char
*
zDb
,
const
char
*
zTable
,
const
char
*
zColumn
,
sqlite_int64
iRow
,
int
isReadonly
){
IncrblobChannel
*
p
;
sqlite3
*
db
=
pDb
->
db
;
sqlite3_blob
*
pBlob
;
int
rc
;
int
flags
=
TCL_READABLE
|(
isReadonly
?
0
:
TCL_WRITABLE
);
/* This variable is used to name the channels: "incrblob_[incr count]" */
static
int
count
=
0
;
char
zChannel
[
64
];
rc
=
sqlite3_blob_open
(
db
,
zDb
,
zTable
,
zColumn
,
iRow
, !
isReadonly
,
&
pBlob
);
if
(
rc
!=
SQLITE_OK
){
Tcl_SetResult
(
interp
, (
char
*
)
sqlite3_errmsg
(
pDb
->
db
),
TCL_VOLATILE
);
return
TCL_ERROR
;
}
p
=
(
IncrblobChannel
*
)
Tcl_Alloc
(
sizeof
(
IncrblobChannel
));
p
->
iSeek
=
0
;
p
->
pBlob
=
pBlob
;
sqlite3_snprintf
(
sizeof
(
zChannel
),
zChannel
,
"incrblob_%d"
,
++
count
);
p
->
channel
=
Tcl_CreateChannel
(
&
IncrblobChannelType
,
zChannel
,
p
,
flags
);
Tcl_RegisterChannel
(
interp
,
p
->
channel
);
/* Link the new channel into the SqliteDb.pIncrblob list. */
p
->
pNext
=
pDb
->
pIncrblob
;
p
->
pPrev
=
0
;
if
(
p
->
pNext
){
p
->
pNext
->
pPrev
=
p
;
}
pDb
->
pIncrblob
=
p
;
p
->
pDb
=
pDb
;
Tcl_SetResult
(
interp
, (
char
*
)
Tcl_GetChannelName
(
p
->
channel
),
TCL_VOLATILE
);
return
TCL_OK
;
}
#else
/* else clause for "#ifndef SQLITE_OMIT_INCRBLOB" */
#define
closeIncrblobChannels
(
pDb
)
#endif
/*
** Look at the script prefix in pCmd. We will be executing this script
** after first appending one or more arguments. This routine analyzes
** the script to see if it is safe to use Tcl_EvalObjv() on the script
** rather than the more general Tcl_EvalEx(). Tcl_EvalObjv() is much
** faster.
**
** Scripts that are safe to use with Tcl_EvalObjv() consists of a
** command name followed by zero or more arguments with no [...] or $
** or {...} or ; to be seen anywhere. Most callback scripts consist
** of just a single procedure name and they meet this requirement.
*/
static
int
safeToUseEvalObjv
(
Tcl_Interp
*
interp
,
Tcl_Obj
*
pCmd
){
/* We could try to do something with Tcl_Parse(). But we will instead
** just do a search for forbidden characters. If any of the forbidden
** characters appear in pCmd, we will report the string as unsafe.
*/
const
char
*
z
;
int
n
;
z
=
Tcl_GetStringFromObj
(
pCmd
,
&
n
);
while
(
n
--
>
0
){
int
c
=
*
(
z
++
);
if
(
c
==
'$'
||
c
==
'['
||
c
==
';'
)
return
0
;
}
return
1
;
}
/*
** Find an SqlFunc structure with the given name. Or create a new
** one if an existing one cannot be found. Return a pointer to the
** structure.
*/
static
SqlFunc
*
findSqlFunc
(
SqliteDb
*
pDb
,
const
char
*
zName
){
SqlFunc
*
p
,
*
pNew
;
int
nName
=
strlen30
(
zName
);
pNew
=
(
SqlFunc
*
)
Tcl_Alloc
(
sizeof
(
*
pNew
)
+
nName
+
1
);
pNew
->
zName
=
(
char
*
)
&
pNew
[
1
];
memcpy
(
pNew
->
zName
,
zName
,
nName
+
1
);
for
(
p
=
pDb
->
pFunc
;
p
;
p
=
p
->
pNext
){
if
(
sqlite3_stricmp
(
p
->
zName
,
pNew
->
zName
)
==
0
){
Tcl_Free
((
char
*
)
pNew
);
return
p
;
}
}
pNew
->
interp
=
pDb
->
interp
;
pNew
->
pDb
=
pDb
;
pNew
->
pScript
=
0
;
pNew
->
pNext
=
pDb
->
pFunc
;
pDb
->
pFunc
=
pNew
;
return
pNew
;
}
/*
** Free a single SqlPreparedStmt object.
*/
static
void
dbFreeStmt
(
SqlPreparedStmt
*
pStmt
){
#ifdef
SQLITE_TEST
if
(
sqlite3_sql
(
pStmt
->
pStmt
)
==
0
){
Tcl_Free
((
char
*
)
pStmt
->
zSql
);
}
#endif
sqlite3_finalize
(
pStmt
->
pStmt
);
Tcl_Free
((
char
*
)
pStmt
);
}
/*
** Finalize and free a list of prepared statements
*/
static
void
flushStmtCache
(
SqliteDb
*
pDb
){
SqlPreparedStmt
*
pPreStmt
;
SqlPreparedStmt
*
pNext
;
for
(
pPreStmt
=
pDb
->
stmtList
;
pPreStmt
;
pPreStmt
=
pNext
){
pNext
=
pPreStmt
->
pNext
;
dbFreeStmt
(
pPreStmt
);
}
pDb
->
nStmt
=
0
;
pDb
->
stmtLast
=
0
;
pDb
->
stmtList
=
0
;
}
/*
** TCL calls this procedure when an sqlite3 database command is
** deleted.
*/
static
void
SQLITE_TCLAPI
DbDeleteCmd
(
void
*
db
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
db
;
flushStmtCache
(
pDb
);
closeIncrblobChannels
(
pDb
);
sqlite3_close
(
pDb
->
db
);
while
(
pDb
->
pFunc
){
SqlFunc
*
pFunc
=
pDb
->
pFunc
;
pDb
->
pFunc
=
pFunc
->
pNext
;
assert
(
pFunc
->
pDb
==
pDb
);
Tcl_DecrRefCount
(
pFunc
->
pScript
);
Tcl_Free
((
char
*
)
pFunc
);
}
while
(
pDb
->
pCollate
){
SqlCollate
*
pCollate
=
pDb
->
pCollate
;
pDb
->
pCollate
=
pCollate
->
pNext
;
Tcl_Free
((
char
*
)
pCollate
);
}
if
(
pDb
->
zBusy
){
Tcl_Free
(
pDb
->
zBusy
);
}
if
(
pDb
->
zTrace
){
Tcl_Free
(
pDb
->
zTrace
);
}
if
(
pDb
->
zTraceV2
){
Tcl_Free
(
pDb
->
zTraceV2
);
}
if
(
pDb
->
zProfile
){
Tcl_Free
(
pDb
->
zProfile
);
}
if
(
pDb
->
zBindFallback
){
Tcl_Free
(
pDb
->
zBindFallback
);
}
if
(
pDb
->
zAuth
){
Tcl_Free
(
pDb
->
zAuth
);
}
if
(
pDb
->
zNull
){
Tcl_Free
(
pDb
->
zNull
);
}
if
(
pDb
->
pUpdateHook
){
Tcl_DecrRefCount
(
pDb
->
pUpdateHook
);
}
if
(
pDb
->
pPreUpdateHook
){
Tcl_DecrRefCount
(
pDb
->
pPreUpdateHook
);
}
if
(
pDb
->
pRollbackHook
){
Tcl_DecrRefCount
(
pDb
->
pRollbackHook
);
}
if
(
pDb
->
pWalHook
){
Tcl_DecrRefCount
(
pDb
->
pWalHook
);
}
if
(
pDb
->
pCollateNeeded
){
Tcl_DecrRefCount
(
pDb
->
pCollateNeeded
);
}
Tcl_Free
((
char
*
)
pDb
);
}
/*
** This routine is called when a database file is locked while trying
** to execute SQL.
*/
static
int
DbBusyHandler
(
void
*
cd
,
int
nTries
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
cd
;
int
rc
;
char
zVal
[
30
];
sqlite3_snprintf
(
sizeof
(
zVal
),
zVal
,
"%d"
,
nTries
);
rc
=
Tcl_VarEval
(
pDb
->
interp
,
pDb
->
zBusy
,
" "
,
zVal
, (
char
*
)
0
);
if
(
rc
!=
TCL_OK
||
atoi
(
Tcl_GetStringResult
(
pDb
->
interp
)) ){
return
0
;
}
return
1
;
}
#ifndef
SQLITE_OMIT_PROGRESS_CALLBACK
/*
** This routine is invoked as the 'progress callback' for the database.
*/
static
int
DbProgressHandler
(
void
*
cd
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
cd
;
int
rc
;
assert
(
pDb
->
zProgress
);
rc
=
Tcl_Eval
(
pDb
->
interp
,
pDb
->
zProgress
);
if
(
rc
!=
TCL_OK
||
atoi
(
Tcl_GetStringResult
(
pDb
->
interp
)) ){
return
1
;
}
return
0
;
}
#endif
#if
!defined(
SQLITE_OMIT_TRACE
)
&&
!defined(
SQLITE_OMIT_FLOATING_POINT
)
&&
\
!defined(
SQLITE_OMIT_DEPRECATED
)
/*
** This routine is called by the SQLite trace handler whenever a new
** block of SQL is executed. The TCL script in pDb->zTrace is executed.
*/
static
void
DbTraceHandler
(
void
*
cd
,
const
char
*
zSql
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
cd
;
Tcl_DString
str
;
Tcl_DStringInit
(
&
str
);
Tcl_DStringAppend
(
&
str
,
pDb
->
zTrace
,
-1
);
Tcl_DStringAppendElement
(
&
str
,
zSql
);
Tcl_Eval
(
pDb
->
interp
,
Tcl_DStringValue
(
&
str
));
Tcl_DStringFree
(
&
str
);
Tcl_ResetResult
(
pDb
->
interp
);
}
#endif
#ifndef
SQLITE_OMIT_TRACE
/*
** This routine is called by the SQLite trace_v2 handler whenever a new
** supported event is generated. Unsupported event types are ignored.
** The TCL script in pDb->zTraceV2 is executed, with the arguments for
** the event appended to it (as list elements).
*/
static
int
DbTraceV2Handler
(
unsigned
type
,
/* One of the SQLITE_TRACE_* event types. */
void
*
cd
,
/* The original context data pointer. */
void
*
pd
,
/* Primary event data, depends on event type. */
void
*
xd
/* Extra event data, depends on event type. */
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
cd
;
Tcl_Obj
*
pCmd
;
switch
(
type
){
case
SQLITE_TRACE_STMT
: {
sqlite3_stmt
*
pStmt
=
(
sqlite3_stmt
*
)
pd
;
char
*
zSql
=
(
char
*
)
xd
;
pCmd
=
Tcl_NewStringObj
(
pDb
->
zTraceV2
,
-1
);
Tcl_IncrRefCount
(
pCmd
);
Tcl_ListObjAppendElement
(
pDb
->
interp
,
pCmd
,
Tcl_NewWideIntObj
((
Tcl_WideInt
)
pStmt
));
Tcl_ListObjAppendElement
(
pDb
->
interp
,
pCmd
,
Tcl_NewStringObj
(
zSql
,
-1
));
Tcl_EvalObjEx
(
pDb
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
Tcl_ResetResult
(
pDb
->
interp
);
break
;
}
case
SQLITE_TRACE_PROFILE
: {
sqlite3_stmt
*
pStmt
=
(
sqlite3_stmt
*
)
pd
;
sqlite3_int64
ns
=
*
(
sqlite3_int64
*
)
xd
;
pCmd
=
Tcl_NewStringObj
(
pDb
->
zTraceV2
,
-1
);
Tcl_IncrRefCount
(
pCmd
);
Tcl_ListObjAppendElement
(
pDb
->
interp
,
pCmd
,
Tcl_NewWideIntObj
((
Tcl_WideInt
)
pStmt
));
Tcl_ListObjAppendElement
(
pDb
->
interp
,
pCmd
,
Tcl_NewWideIntObj
((
Tcl_WideInt
)
ns
));
Tcl_EvalObjEx
(
pDb
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
Tcl_ResetResult
(
pDb
->
interp
);
break
;
}
case
SQLITE_TRACE_ROW
: {
sqlite3_stmt
*
pStmt
=
(
sqlite3_stmt
*
)
pd
;
pCmd
=
Tcl_NewStringObj
(
pDb
->
zTraceV2
,
-1
);
Tcl_IncrRefCount
(
pCmd
);
Tcl_ListObjAppendElement
(
pDb
->
interp
,
pCmd
,
Tcl_NewWideIntObj
((
Tcl_WideInt
)
pStmt
));
Tcl_EvalObjEx
(
pDb
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
Tcl_ResetResult
(
pDb
->
interp
);
break
;
}
case
SQLITE_TRACE_CLOSE
: {
sqlite3
*
db
=
(
sqlite3
*
)
pd
;
pCmd
=
Tcl_NewStringObj
(
pDb
->
zTraceV2
,
-1
);
Tcl_IncrRefCount
(
pCmd
);
Tcl_ListObjAppendElement
(
pDb
->
interp
,
pCmd
,
Tcl_NewWideIntObj
((
Tcl_WideInt
)
db
));
Tcl_EvalObjEx
(
pDb
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
Tcl_ResetResult
(
pDb
->
interp
);
break
;
}
}
return
SQLITE_OK
;
}
#endif
#if
!defined(
SQLITE_OMIT_TRACE
)
&&
!defined(
SQLITE_OMIT_FLOATING_POINT
)
&&
\
!defined(
SQLITE_OMIT_DEPRECATED
)
/*
** This routine is called by the SQLite profile handler after a statement
** SQL has executed. The TCL script in pDb->zProfile is evaluated.
*/
static
void
DbProfileHandler
(
void
*
cd
,
const
char
*
zSql
,
sqlite_uint64
tm
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
cd
;
Tcl_DString
str
;
char
zTm
[
100
];
sqlite3_snprintf
(
sizeof
(
zTm
)
-
1
,
zTm
,
"%lld"
,
tm
);
Tcl_DStringInit
(
&
str
);
Tcl_DStringAppend
(
&
str
,
pDb
->
zProfile
,
-1
);
Tcl_DStringAppendElement
(
&
str
,
zSql
);
Tcl_DStringAppendElement
(
&
str
,
zTm
);
Tcl_Eval
(
pDb
->
interp
,
Tcl_DStringValue
(
&
str
));
Tcl_DStringFree
(
&
str
);
Tcl_ResetResult
(
pDb
->
interp
);
}
#endif
/*
** This routine is called when a transaction is committed. The
** TCL script in pDb->zCommit is executed. If it returns non-zero or
** if it throws an exception, the transaction is rolled back instead
** of being committed.
*/
static
int
DbCommitHandler
(
void
*
cd
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
cd
;
int
rc
;
rc
=
Tcl_Eval
(
pDb
->
interp
,
pDb
->
zCommit
);
if
(
rc
!=
TCL_OK
||
atoi
(
Tcl_GetStringResult
(
pDb
->
interp
)) ){
return
1
;
}
return
0
;
}
static
void
DbRollbackHandler
(
void
*
clientData
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
clientData
;
assert
(
pDb
->
pRollbackHook
);
if
(
TCL_OK
!=
Tcl_EvalObjEx
(
pDb
->
interp
,
pDb
->
pRollbackHook
,
0
) ){
Tcl_BackgroundError
(
pDb
->
interp
);
}
}
/*
** This procedure handles wal_hook callbacks.
*/
static
int
DbWalHandler
(
void
*
clientData
,
sqlite3
*
db
,
const
char
*
zDb
,
int
nEntry
){
int
ret
=
SQLITE_OK
;
Tcl_Obj
*
p
;
SqliteDb
*
pDb
=
(
SqliteDb
*
)
clientData
;
Tcl_Interp
*
interp
=
pDb
->
interp
;
assert
(
pDb
->
pWalHook
);
assert
(
db
==
pDb
->
db
);
p
=
Tcl_DuplicateObj
(
pDb
->
pWalHook
);
Tcl_IncrRefCount
(
p
);
Tcl_ListObjAppendElement
(
interp
,
p
,
Tcl_NewStringObj
(
zDb
,
-1
));
Tcl_ListObjAppendElement
(
interp
,
p
,
Tcl_NewIntObj
(
nEntry
));
if
(
TCL_OK
!=
Tcl_EvalObjEx
(
interp
,
p
,
0
)
||
TCL_OK
!=
Tcl_GetIntFromObj
(
interp
,
Tcl_GetObjResult
(
interp
),
&
ret
)
){
Tcl_BackgroundError
(
interp
);
}
Tcl_DecrRefCount
(
p
);
return
ret
;
}
#if
defined(
SQLITE_TEST
)
&&
defined(
SQLITE_ENABLE_UNLOCK_NOTIFY
)
static
void
setTestUnlockNotifyVars
(
Tcl_Interp
*
interp
,
int
iArg
,
int
nArg
){
char
zBuf
[
64
];
sqlite3_snprintf
(
sizeof
(
zBuf
),
zBuf
,
"%d"
,
iArg
);
Tcl_SetVar
(
interp
,
"sqlite_unlock_notify_arg"
,
zBuf
,
TCL_GLOBAL_ONLY
);
sqlite3_snprintf
(
sizeof
(
zBuf
),
zBuf
,
"%d"
,
nArg
);
Tcl_SetVar
(
interp
,
"sqlite_unlock_notify_argcount"
,
zBuf
,
TCL_GLOBAL_ONLY
);
}
#else
# define
setTestUnlockNotifyVars
(
x
,
y
,
z
)
#endif
#ifdef
SQLITE_ENABLE_UNLOCK_NOTIFY
static
void
DbUnlockNotify
(
void
*
*
apArg
,
int
nArg
){
int
i
;
for
(
i
=
0
;
i
<
nArg
;
i
++
){
const
int
flags
=
(
TCL_EVAL_GLOBAL
|
TCL_EVAL_DIRECT
);
SqliteDb
*
pDb
=
(
SqliteDb
*
)
apArg
[
i
];
setTestUnlockNotifyVars
(
pDb
->
interp
,
i
,
nArg
);
assert
(
pDb
->
pUnlockNotify
);
Tcl_EvalObjEx
(
pDb
->
interp
,
pDb
->
pUnlockNotify
,
flags
);
Tcl_DecrRefCount
(
pDb
->
pUnlockNotify
);
pDb
->
pUnlockNotify
=
0
;
}
}
#endif
#ifdef
SQLITE_ENABLE_PREUPDATE_HOOK
/*
** Pre-update hook callback.
*/
static
void
DbPreUpdateHandler
(
void
*
p
,
sqlite3
*
db
,
int
op
,
const
char
*
zDb
,
const
char
*
zTbl
,
sqlite_int64
iKey1
,
sqlite_int64
iKey2
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
p
;
Tcl_Obj
*
pCmd
;
static
const
char
*
azStr
[]
=
{
"DELETE"
,
"INSERT"
,
"UPDATE"
};
assert
( (
SQLITE_DELETE
-
1
)/
9
==
0
);
assert
( (
SQLITE_INSERT
-
1
)/
9
==
1
);
assert
( (
SQLITE_UPDATE
-
1
)/
9
==
2
);
assert
(
pDb
->
pPreUpdateHook
);
assert
(
db
==
pDb
->
db
);
assert
(
op
==
SQLITE_INSERT
||
op
==
SQLITE_UPDATE
||
op
==
SQLITE_DELETE
);
pCmd
=
Tcl_DuplicateObj
(
pDb
->
pPreUpdateHook
);
Tcl_IncrRefCount
(
pCmd
);
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewStringObj
(
azStr
[(
op
-
1
)/
9
],
-1
));
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewStringObj
(
zDb
,
-1
));
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewStringObj
(
zTbl
,
-1
));
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewWideIntObj
(
iKey1
));
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewWideIntObj
(
iKey2
));
Tcl_EvalObjEx
(
pDb
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
}
#endif
/* SQLITE_ENABLE_PREUPDATE_HOOK */
static
void
DbUpdateHandler
(
void
*
p
,
int
op
,
const
char
*
zDb
,
const
char
*
zTbl
,
sqlite_int64
rowid
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
p
;
Tcl_Obj
*
pCmd
;
static
const
char
*
azStr
[]
=
{
"DELETE"
,
"INSERT"
,
"UPDATE"
};
assert
( (
SQLITE_DELETE
-
1
)/
9
==
0
);
assert
( (
SQLITE_INSERT
-
1
)/
9
==
1
);
assert
( (
SQLITE_UPDATE
-
1
)/
9
==
2
);
assert
(
pDb
->
pUpdateHook
);
assert
(
op
==
SQLITE_INSERT
||
op
==
SQLITE_UPDATE
||
op
==
SQLITE_DELETE
);
pCmd
=
Tcl_DuplicateObj
(
pDb
->
pUpdateHook
);
Tcl_IncrRefCount
(
pCmd
);
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewStringObj
(
azStr
[(
op
-
1
)/
9
],
-1
));
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewStringObj
(
zDb
,
-1
));
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewStringObj
(
zTbl
,
-1
));
Tcl_ListObjAppendElement
(
0
,
pCmd
,
Tcl_NewWideIntObj
(
rowid
));
Tcl_EvalObjEx
(
pDb
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
}
static
void
tclCollateNeeded
(
void
*
pCtx
,
sqlite3
*
db
,
int
enc
,
const
char
*
zName
){
SqliteDb
*
pDb
=
(
SqliteDb
*
)
pCtx
;
Tcl_Obj
*
pScript
=
Tcl_DuplicateObj
(
pDb
->
pCollateNeeded
);
Tcl_IncrRefCount
(
pScript
);
Tcl_ListObjAppendElement
(
0
,
pScript
,
Tcl_NewStringObj
(
zName
,
-1
));
Tcl_EvalObjEx
(
pDb
->
interp
,
pScript
,
0
);
Tcl_DecrRefCount
(
pScript
);
}
/*
** This routine is called to evaluate an SQL collation function implemented
** using TCL script.
*/
static
int
tclSqlCollate
(
void
*
pCtx
,
int
nA
,
const
void
*
zA
,
int
nB
,
const
void
*
zB
){
SqlCollate
*
p
=
(
SqlCollate
*
)
pCtx
;
Tcl_Obj
*
pCmd
;
pCmd
=
Tcl_NewStringObj
(
p
->
zScript
,
-1
);
Tcl_IncrRefCount
(
pCmd
);
Tcl_ListObjAppendElement
(
p
->
interp
,
pCmd
,
Tcl_NewStringObj
(
zA
,
nA
));
Tcl_ListObjAppendElement
(
p
->
interp
,
pCmd
,
Tcl_NewStringObj
(
zB
,
nB
));
Tcl_EvalObjEx
(
p
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
return
(
atoi
(
Tcl_GetStringResult
(
p
->
interp
)));
}
/*
** This routine is called to evaluate an SQL function implemented
** using TCL script.
*/
static
void
tclSqlFunc
(
sqlite3_context
*
context
,
int
argc
,
sqlite3_value
*
*
argv
){
SqlFunc
*
p
=
sqlite3_user_data
(
context
);
Tcl_Obj
*
pCmd
;
int
i
;
int
rc
;
if
(
argc
==
0
){
/* If there are no arguments to the function, call Tcl_EvalObjEx on the
** script object directly. This allows the TCL compiler to generate
** bytecode for the command on the first invocation and thus make
** subsequent invocations much faster. */
pCmd
=
p
->
pScript
;
Tcl_IncrRefCount
(
pCmd
);
rc
=
Tcl_EvalObjEx
(
p
->
interp
,
pCmd
,
0
);
Tcl_DecrRefCount
(
pCmd
);
}
else
{
/* If there are arguments to the function, make a shallow copy of the
** script object, lappend the arguments, then evaluate the copy.
**
** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated.
** The new Tcl_Obj contains pointers to the original list elements.
** That way, when Tcl_EvalObjv() is run and shimmers the first element
** of the list to tclCmdNameType, that alternate representation will
** be preserved and reused on the next invocation.
*/
Tcl_Obj
*
*
aArg
;
int
nArg
;
if
(
Tcl_ListObjGetElements
(
p
->
interp
,
p
->
pScript
,
&
nArg
,
&
aArg
) ){
sqlite3_result_error
(
context
,
Tcl_GetStringResult
(
p
->
interp
),
-1
);
return
;
}
pCmd
=
Tcl_NewListObj
(
nArg
,
aArg
);
Tcl_IncrRefCount
(
pCmd
);
for
(
i
=
0
;
i
<
argc
;
i
++
){
sqlite3_value
*
pIn
=
argv
[
i
];
Tcl_Obj
*
pVal
;
/* Set pVal to contain the i'th column of this row. */
switch
(
sqlite3_value_type
(
pIn
) ){
case
SQLITE_BLOB
: {
int
bytes
=
sqlite3_value_bytes
(
pIn
);
pVal
=
Tcl_NewByteArrayObj
(
sqlite3_value_blob
(
pIn
),
bytes
);
break
;
}
case
SQLITE_INTEGER
: {
sqlite_int64
v
=
sqlite3_value_int64
(
pIn
);
if
(
v
>=
-2147483647
&&
v
<=
2147483647
){
pVal
=
Tcl_NewIntObj
((
int
)
v
);
}
else
{
pVal
=
Tcl_NewWideIntObj
(
v
);
}
break
;
}
case
SQLITE_FLOAT
: {
double
r
=
sqlite3_value_double
(
pIn
);
pVal
=
Tcl_NewDoubleObj
(
r
);
break
;
}
case
SQLITE_NULL
: {
pVal
=
Tcl_NewStringObj
(
p
->
pDb
->
zNull
,
-1
);
break
;
}
default
: {
int
bytes
=
sqlite3_value_bytes
(
pIn
);
pVal
=
Tcl_NewStringObj
((
char
*
)
sqlite3_value_text
(
pIn
),
bytes
);
break
;
}
}
rc
=
Tcl_ListObjAppendElement
(
p
->
interp
,
pCmd
,
pVal
);
if
(
rc
){
Tcl_DecrRefCount
(
pCmd
);
sqlite3_result_error
(
context
,
Tcl_GetStringResult
(
p
->
interp
),
-1
);
return
;
}
}
if
( !
p
->
useEvalObjv
){
/* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd
** is a list without a string representation. To prevent this from
** happening, make sure pCmd has a valid string representation */
Tcl_GetString
(
pCmd
);
}
rc
=
Tcl_EvalObjEx
(
p
->
interp
,
pCmd
,
TCL_EVAL_DIRECT
);
Tcl_DecrRefCount
(
pCmd
);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL