summaryrefslogtreecommitdiffstats
path: root/test/shell1.test
blob: 5d4243f47a93eab48aa31d20feef0064e87727b3 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
# 2009 Nov 11
#
# 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.
#
#***********************************************************************
#
# The focus of this file is testing the CLI shell tool.
#
#

# Test plan:
#
#   shell1-1.*: Basic command line option handling.
#   shell1-2.*: Basic "dot" command token parsing.
#   shell1-3.*: Basic test that "dot" command can be called.
#   shell1-{4-8}.*: Test various "dot" commands's functionality.
#   shell1-9.*: Basic test that "dot" commands and SQL intermix ok.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set CLI [test_cli_invocation]
db close
forcedelete test.db test.db-journal test.db-wal
sqlite3 db test.db

#----------------------------------------------------------------------------
# Test cases shell1-1.*: Basic command line option handling.
#

# invalid option
do_test shell1-1.1.1 {
  set res [catchcmd "-bad test.db" ""]
  set rc [lindex $res 0]
  list $rc \
       [regexp {Error: unknown option: -bad} $res]
} {1 1}
do_test shell1-1.1.1b {
  set res [catchcmd "test.db -bad" ""]
  set rc [lindex $res 0]
  list $rc \
       [regexp {Error: unknown option: -bad} $res]
} {1 1}
# error on extra options
do_test shell1-1.1.2 {
  catchcmd "test.db \"select+3\" \"select+4\"" ""
} {0 {3
4}}
# error on extra options
do_test shell1-1.1.3 {
  catchcmd "test.db FOO test.db BAD" ".quit"
} {/1 .Error: in prepare, near "FOO": syntax error*/}

# -help
do_test shell1-1.2.1 {
  set res [catchcmd "-help test.db" ""]
  set rc [lindex $res 0]
  list $rc \
       [regexp {Usage} $res] \
       [regexp {\-init} $res] \
       [regexp {\-version} $res]
} {1 1 1 1}

# -init filename       read/process named file
forcedelete FOO
set out [open FOO w]
puts $out ""
close $out
do_test shell1-1.3.1 {
  catchcmd "-init FOO test.db" ""
} {0 {}}
do_test shell1-1.3.2 {
  catchcmd "-init FOO test.db .quit BAD" ""
} {0 {}}
do_test shell1-1.3.3 {
  catchcmd "-init FOO test.db BAD .quit" ""
} {/1 .Error: in prepare, near "BAD": syntax error*/}

# -echo                print commands before execution
do_test shell1-1.4.1 {
  catchcmd "-echo test.db" ""
} {0 {}}

# -[no]header          turn headers on or off
do_test shell1-1.5.1 {
  catchcmd "-header test.db" ""
} {0 {}}
do_test shell1-1.5.2 {
  catchcmd "-noheader test.db" ""
} {0 {}}

# -bail                stop after hitting an error
do_test shell1-1.6.1 {
  catchcmd "-bail test.db" ""
} {0 {}}

# -interactive         force interactive I/O
do_test shell1-1.7.1 {
  set res [catchcmd "-interactive test.db" ".quit"]
  set rc [lindex $res 0]
  list $rc \
       [regexp {SQLite version} $res] \
       [regexp {Enter ".help" for usage hints} $res]
} {0 1 1}

# -batch               force batch I/O
do_test shell1-1.8.1 {
  catchcmd "-batch test.db" ""
} {0 {}}

# -column              set output mode to 'column'
do_test shell1-1.9.1 {
  catchcmd "-column test.db" ""
} {0 {}}

# -csv                 set output mode to 'csv'
do_test shell1-1.10.1 {
  catchcmd "-csv test.db" ""
} {0 {}}

# -html                set output mode to HTML
do_test shell1-1.11.1 {
  catchcmd "-html test.db" ""
} {0 {}}

# -line                set output mode to 'line'
do_test shell1-1.12.1 {
  catchcmd "-line test.db" ""
} {0 {}}

# -list                set output mode to 'list'
do_test shell1-1.13.1 {
  catchcmd "-list test.db" ""
} {0 {}}

# -separator 'x'       set output field separator (|)
do_test shell1-1.14.1 {
  catchcmd "-separator 'x' test.db" ""
} {0 {}}
do_test shell1-1.14.2 {
  catchcmd "-separator x test.db" ""
} {0 {}}
do_test shell1-1.14.3 {
  set res [catchcmd "-separator" ""]
  set rc [lindex $res 0]
  list $rc \
       [regexp {Error: missing argument to -separator} $res]
} {1 1}

# -stats               print memory stats before each finalize
do_test shell1-1.14b.1 {
  catchcmd "-stats test.db" ""
} {0 {}}

# -nullvalue 'text'    set text string for NULL values
do_test shell1-1.15.1 {
  catchcmd "-nullvalue 'x' test.db" ""
} {0 {}}
do_test shell1-1.15.2 {
  catchcmd "-nullvalue x test.db" ""
} {0 {}}
do_test shell1-1.15.3 {
  set res [catchcmd "-nullvalue" ""]
  set rc [lindex $res 0]
  list $rc \
       [regexp {Error: missing argument to -nullvalue} $res]
} {1 1}

# -version             show SQLite version
do_test shell1-1.16.1 {
  set x [catchcmd "-version test.db" ""]
} {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}

# Handle no-more-options option
forcedelete ./--db
do_test shell1-1.17.1 {
  catchcmd {-- --db "CREATE TABLE T(c1);"}
} {0 {}}
do_test shell1-1.17.2 {
  catchcmd {-- --db "SELECT name from sqlite_schema;"}
} {0 T}
forcedelete ./--db

#----------------------------------------------------------------------------
# Test cases shell1-2.*: Basic "dot" command token parsing.
#

# check first token handling
do_test shell1-2.1.1 {
  catchcmd "test.db" ".foo"
} {1 {Error: unknown command or invalid arguments:  "foo". Enter ".help" for help}}
do_test shell1-2.1.2 {
  catchcmd "test.db" ".\"foo OFF\""
} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
do_test shell1-2.1.3 {
  catchcmd "test.db" ".\'foo OFF\'"
} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}

# unbalanced quotes
do_test shell1-2.2.1 {
  catchcmd "test.db" ".\"foo OFF"
} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
do_test shell1-2.2.2 {
  catchcmd "test.db" ".\'foo OFF"
} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
do_test shell1-2.2.3 {
  catchcmd "test.db" ".explain \"OFF"
} {0 {}}
do_test shell1-2.2.4 {
  catchcmd "test.db" ".explain \'OFF"
} {0 {}}
do_test shell1-2.2.5 {
  catchcmd "test.db" ".mode \"insert FOO"
} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
do_test shell1-2.2.6 {
  catchcmd "test.db" ".mode \'insert FOO"
} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}

# check multiple tokens, and quoted tokens
do_test shell1-2.3.1 {
  catchcmd "test.db" ".explain 1"
} {0 {}}
do_test shell1-2.3.2 {
  catchcmd "test.db" ".explain on"
} {0 {}}
do_test shell1-2.3.3 {
  catchcmd "test.db" ".explain \"1 2 3\""
} {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
do_test shell1-2.3.4 {
  catchcmd "test.db" ".explain \"OFF\""
} {0 {}}
do_test shell1-2.3.5 {
  catchcmd "test.db" ".\'explain\' \'OFF\'"
} {0 {}}
do_test shell1-2.3.6 {
  catchcmd "test.db" ".explain \'OFF\'"
} {0 {}}
do_test shell1-2.3.7 {
  catchcmd "test.db" ".\'explain\' \'OFF\'"
} {0 {}}

# check quoted args are unquoted
do_test shell1-2.4.1 {
  catchcmd "test.db" ".mode FOO"
} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
do_test shell1-2.4.2 {
  catchcmd "test.db" ".mode csv"
} {0 {}}
do_test shell1-2.4.2 {
  catchcmd "test.db" ".mode \"csv\""
} {0 {}}

# check that certain quoted arg escapes work
do_test shell1-2.5.1 {
  catchcmd ":memory:" ".print \"\\060\\077 \\x3f\\x30 \\a\\t\""
} [list 0 "0? ?0 \a\t"]


#----------------------------------------------------------------------------
# Test cases shell1-3.*: Basic test that "dot" command can be called.
#

# .backup ?DB? FILE      Backup DB (default "main") to FILE
do_test shell1-3.1.1 {
  catchcmd "test.db" ".backup"
} {1 {missing FILENAME argument on .backup}}
forcedelete FOO
do_test shell1-3.1.2 {
  catchcmd "test.db" ".backup FOO"
} {0 {}}
do_test shell1-3.1.3 {
  catchcmd "test.db" ".backup FOO BAR"
} {1 {Error: unknown database FOO}}
do_test shell1-3.1.4 {
  # too many arguments
  catchcmd "test.db" ".backup FOO BAR BAD"
} {1 {Usage: .backup ?DB? ?OPTIONS? FILENAME}}

# .bail ON|OFF           Stop after hitting an error.  Default OFF
do_test shell1-3.2.1 {
  catchcmd "test.db" ".bail"
} {1 {Usage: .bail on|off}}
do_test shell1-3.2.2 {
  catchcmd "test.db" ".bail ON"
} {0 {}}
do_test shell1-3.2.3 {
  catchcmd "test.db" ".bail OFF"
} {0 {}}
do_test shell1-3.2.4 {
  # too many arguments
  catchcmd "test.db" ".bail OFF BAD"
} {1 {Usage: .bail on|off}}

ifcapable vtab {
# .databases             List names and files of attached databases
do_test shell1-3.3.1 {
  catchcmd "-csv test.db" ".databases"
} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
do_test shell1-3.3.2 {
  # extra arguments ignored
  catchcmd "test.db" ".databases BAD"
} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
}

# .dump ?TABLE? ...      Dump the database in an SQL text format
#                          If TABLE specified, only dump tables matching
#                          LIKE pattern TABLE.
do_test shell1-3.4.1 {
  set res [catchcmd "test.db" ".dump"]
  list [regexp {BEGIN TRANSACTION;} $res] \
       [regexp {COMMIT;} $res]
} {1 1}
do_test shell1-3.4.2 {
  set res [catchcmd "test.db" ".dump FOO"]
  list [regexp {BEGIN TRANSACTION;} $res] \
       [regexp {COMMIT;} $res]
} {1 1}
# The .dump command now accepts multiple arguments
#do_test shell1-3.4.3 {
#  # too many arguments
#  catchcmd "test.db" ".dump FOO BAD"
#} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}

# .echo ON|OFF           Turn command echo on or off
do_test shell1-3.5.1 {
  catchcmd "test.db" ".echo"
} {1 {Usage: .echo on|off}}
do_test shell1-3.5.2 {
  catchcmd "test.db" ".echo ON"
} {0 {}}
do_test shell1-3.5.3 {
  catchcmd "test.db" ".echo OFF"
} {0 {}}
do_test shell1-3.5.4 {
  # too many arguments
  catchcmd "test.db" ".echo OFF BAD"
} {1 {Usage: .echo on|off}}

# .exit                  Exit this program
do_test shell1-3.6.1 {
  catchcmd "test.db" ".exit"
} {0 {}}

# .explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.
do_test shell1-3.7.1 {
  catchcmd "test.db" ".explain"
  # explain is the exception to the booleans.  without an option, it turns it on.
} {0 {}}
do_test shell1-3.7.2 {
  catchcmd "test.db" ".explain ON"
} {0 {}}
do_test shell1-3.7.3 {
  catchcmd "test.db" ".explain OFF"
} {0 {}}
do_test shell1-3.7.4 {
  # extra arguments ignored
  catchcmd "test.db" ".explain OFF BAD"
} {0 {}}


# .header(s) ON|OFF      Turn display of headers on or off
do_test shell1-3.9.1 {
  catchcmd "test.db" ".header"
} {1 {Usage: .headers on|off}}
do_test shell1-3.9.2 {
  catchcmd "test.db" ".header ON"
} {0 {}}
do_test shell1-3.9.3 {
  catchcmd "test.db" ".header OFF"
} {0 {}}
do_test shell1-3.9.4 {
  # too many arguments
  catchcmd "test.db" ".header OFF BAD"
} {1 {Usage: .headers on|off}}

do_test shell1-3.9.5 {
  catchcmd "test.db" ".headers"
} {1 {Usage: .headers on|off}}
do_test shell1-3.9.6 {
  catchcmd "test.db" ".headers ON"
} {0 {}}
do_test shell1-3.9.7 {
  catchcmd "test.db" ".headers OFF"
} {0 {}}
do_test shell1-3.9.8 {
  # too many arguments
  catchcmd "test.db" ".headers OFF BAD"
} {1 {Usage: .headers on|off}}

# .help                  Show this message
do_test shell1-3.10.1 {
  set res [catchcmd "test.db" ".help"]
  # look for a few of the possible help commands
  list [regexp {.help} $res] \
       [regexp {.quit} $res] \
       [regexp {.show} $res]
} {1 1 1}
do_test shell1-3.10.2 {
  # we allow .help to take extra args (it is help after all)
  set res [catchcmd "test.db" ".help *"]
  # look for a few of the possible help commands
  list [regexp {.help} $res] \
       [regexp {.quit} $res] \
       [regexp {.show} $res]
} {1 1 1}

# .import FILE TABLE     Import data from FILE into TABLE
do_test shell1-3.11.1 {
  catchcmd "test.db" ".import"
} {/1 .ERROR: missing FILE argument.*/}
do_test shell1-3.11.2 {
  catchcmd "test.db" ".import FOO"
} {/1 .ERROR: missing TABLE argument.*/}
do_test shell1-3.11.3 {
  # too many arguments
  catchcmd "test.db" ".import FOO BAR BAD"
} {/1 .ERROR: extra argument: "BAD".*./}

# .indexes ?TABLE?       Show names of all indexes
#                          If TABLE specified, only show indexes for tables
#                          matching LIKE pattern TABLE.
do_test shell1-3.12.1 {
  catchcmd "test.db" ".indexes"
} {0 {}}
do_test shell1-3.12.2 {
  catchcmd "test.db" ".indexes FOO"
} {0 {}}
do_test shell1-3.12.2-legacy {
  catchcmd "test.db" ".indices FOO"
} {0 {}}
do_test shell1-3.12.3 {
  # too many arguments
  catchcmd "test.db" ".indexes FOO BAD"
} {1 {Usage: .indexes ?LIKE-PATTERN?}}

# .mode MODE ?TABLE?     Set output mode where MODE is one of:
#                          ascii    Columns/rows delimited by 0x1F and 0x1E
#                          csv      Comma-separated values
#                          column   Left-aligned columns.  (See .width)
#                          html     HTML <table> code
#                          insert   SQL insert statements for TABLE
#                          line     One value per line
#                          list     Values delimited by .separator strings
#                          tabs     Tab-separated values
#                          tcl      TCL list elements
do_test shell1-3.13.1 {
  catchcmd "test.db" ".mode"
} {0 {current output mode: list}}
do_test shell1-3.13.2 {
  catchcmd "test.db" ".mode FOO"
} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
do_test shell1-3.13.3 {
  catchcmd "test.db" ".mode csv"
} {0 {}}
do_test shell1-3.13.4 {
  catchcmd "test.db" ".mode column"
} {0 {}}
do_test shell1-3.13.5 {
  catchcmd "test.db" ".mode html"
} {0 {}}
do_test shell1-3.13.6 {
  catchcmd "test.db" ".mode insert"
} {0 {}}
do_test shell1-3.13.7 {
  catchcmd "test.db" ".mode line"
} {0 {}}
do_test shell1-3.13.8 {
  catchcmd "test.db" ".mode list"
} {0 {}}
do_test shell1-3.13.9 {
  catchcmd "test.db" ".mode tabs"
} {0 {}}
do_test shell1-3.13.10 {
  catchcmd "test.db" ".mode tcl"
} {0 {}}
do_test shell1-3.13.11 {
  # extra arguments ignored
  catchcmd "test.db" ".mode tcl BAD"
} {0 {}}

# .nullvalue STRING      Print STRING in place of NULL values
do_test shell1-3.14.1 {
  catchcmd "test.db" ".nullvalue"
} {1 {Usage: .nullvalue STRING}}
do_test shell1-3.14.2 {
  catchcmd "test.db" ".nullvalue FOO"
} {0 {}}
do_test shell1-3.14.3 {
  # too many arguments
  catchcmd "test.db" ".nullvalue FOO BAD"
} {1 {Usage: .nullvalue STRING}}

# .output FILENAME       Send output to FILENAME
do_test shell1-3.15.1 {
  catchcmd "test.db" ".output
.print x"
} {0 x}
do_test shell1-3.15.2 {
  catchcmd "test.db" ".output FOO
.print x
.output
SELECT readfile('FOO');"
} {0 {x
}}
do_test shell1-3.15.3 {
  # too many arguments
  catchcmd "test.db" ".output FOO BAD"
} {1 {ERROR: extra parameter: "BAD".  Usage:
.output ?FILE?           Send output to FILE or stdout if FILE is omitted
   If FILE begins with '|' then open it as a pipe.
   Options:
     --bom                 Prefix output with a UTF8 byte-order mark
     -e                    Send output to the system text editor
     -x                    Send output as CSV to a spreadsheet
child process exited abnormally}}

# .output stdout         Send output to the screen
do_test shell1-3.16.1 {
  catchcmd "test.db" ".output stdout"
} {0 {}}
do_test shell1-3.16.2 {
  # too many arguments
  catchcmd "test.db" ".output stdout BAD"
} {1 {ERROR: extra parameter: "BAD".  Usage:
.output ?FILE?           Send output to FILE or stdout if FILE is omitted
   If FILE begins with '|' then open it as a pipe.
   Options:
     --bom                 Prefix output with a UTF8 byte-order mark
     -e                    Send output to the system text editor
     -x                    Send output as CSV to a spreadsheet
child process exited abnormally}}

# .prompt MAIN CONTINUE  Replace the standard prompts
do_test shell1-3.17.1 {
  catchcmd "test.db" ".prompt"
} {0 {}}
do_test shell1-3.17.2 {
  catchcmd "test.db" ".prompt FOO"
} {0 {}}
do_test shell1-3.17.3 {
  catchcmd "test.db" ".prompt FOO BAR"
} {0 {}}
do_test shell1-3.17.4 {
  # too many arguments
  catchcmd "test.db" ".prompt FOO BAR BAD"
} {0 {}}

# .quit                  Exit this program
do_test shell1-3.18.1 {
  catchcmd "test.db" ".quit"
} {0 {}}
do_test shell1-3.18.2 {
  # too many arguments
  catchcmd "test.db" ".quit BAD"
} {0 {}}

# .read FILENAME         Execute SQL in FILENAME
do_test shell1-3.19.1 {
  catchcmd "test.db" ".read"
} {1 {Usage: .read FILE}}
do_test shell1-3.19.2 {
  forcedelete FOO
  catchcmd "test.db" ".read FOO"
} {1 {Error: cannot open "FOO"}}
do_test shell1-3.19.3 {
  # too many arguments
  catchcmd "test.db" ".read FOO BAD"
} {1 {Usage: .read FILE}}

# .restore ?DB? FILE     Restore content of DB (default "main") from FILE
do_test shell1-3.20.1 {
  catchcmd "test.db" ".restore"
} {1 {Usage: .restore ?DB? FILE}}
do_test shell1-3.20.2 {
  catchcmd "test.db" ".restore FOO"
} {0 {}}
do_test shell1-3.20.3 {
  catchcmd "test.db" ".restore FOO BAR"
} {1 {Error: unknown database FOO}}
do_test shell1-3.20.4 {
  # too many arguments
  catchcmd "test.db" ".restore FOO BAR BAD"
} {1 {Usage: .restore ?DB? FILE}}

ifcapable vtab {
# .schema ?TABLE?        Show the CREATE statements
#                          If TABLE specified, only show tables matching
#                          LIKE pattern TABLE.
do_test shell1-3.21.1 {
  catchcmd "test.db" ".schema"
} {0 {}}
do_test shell1-3.21.2 {
  catchcmd "test.db" ".schema FOO"
} {0 {}}
do_test shell1-3.21.3 {
  # too many arguments
  catchcmd "test.db" ".schema FOO BAD"
} {1 {Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?}}

do_test shell1-3.21.4 {
  catchcmd "test.db" {
     CREATE TABLE t1(x);
     CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
     CREATE VIEW v1 AS SELECT y+1 FROM v2;
  }
  catchcmd "test.db" ".schema"
} {0 {CREATE TABLE t1(x);
CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
/* v2(y) */;
CREATE VIEW v1 AS SELECT y+1 FROM v2
/* v1("y+1") */;}}

  catch {db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}}
}

# .separator STRING  Change column separator used by output and .import
do_test shell1-3.22.1 {
  catchcmd "test.db" ".separator"
} {1 {Usage: .separator COL ?ROW?}}
do_test shell1-3.22.2 {
  catchcmd "test.db" ".separator FOO"
} {0 {}}
do_test shell1-3.22.3 {
  catchcmd "test.db" ".separator ABC XYZ"
} {0 {}}
do_test shell1-3.22.4 {
  # too many arguments
  catchcmd "test.db" ".separator FOO BAD BAD2"
} {1 {Usage: .separator COL ?ROW?}}

# .show                  Show the current values for various settings
do_test shell1-3.23.1 {
  set res [catchcmd "test.db" ".show"]
  list [regexp {echo:} $res] \
       [regexp {explain:} $res] \
       [regexp {headers:} $res] \
       [regexp {mode:} $res] \
       [regexp {nullvalue:} $res] \
       [regexp {output:} $res] \
       [regexp {colseparator:} $res] \
       [regexp {rowseparator:} $res] \
       [regexp {stats:} $res] \
       [regexp {width:} $res]
} {1 1 1 1 1 1 1 1 1 1}
do_test shell1-3.23.2 {
  # too many arguments
  catchcmd "test.db" ".show BAD"
} {1 {Usage: .show}}

# .stats ON|OFF          Turn stats on or off
#do_test shell1-3.23b.1 {
#  catchcmd "test.db" ".stats"
#} {1 {Usage: .stats on|off|stmt|vmstep}}
do_test shell1-3.23b.2 {
  catchcmd "test.db" ".stats ON"
} {0 {}}
do_test shell1-3.23b.3 {
  catchcmd "test.db" ".stats OFF"
} {0 {}}
do_test shell1-3.23b.4 {
  # too many arguments
  catchcmd "test.db" ".stats OFF BAD"
} {1 {Usage: .stats ?on|off|stmt|vmstep?}}

# Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
# Adverse interaction between .stats and .eqp
#
do_test shell1-3.23b.5 {
  catchcmd "test.db" [string map {"\n    " "\n"} {
    CREATE TEMP TABLE t1(x);
    INSERT INTO t1 VALUES(1),(2);
    .stats on
    .eqp full
    SELECT * FROM t1;
  }]
} {/1\n2\n/}

# .tables ?TABLE?        List names of tables
#                          If TABLE specified, only list tables matching
#                          LIKE pattern TABLE.
do_test shell1-3.24.1 {
  catchcmd "test.db" ".tables"
} {0 {}}
do_test shell1-3.24.2 {
  catchcmd "test.db" ".tables FOO"
} {0 {}}
do_test shell1-3.24.3 {
  # too many arguments
  catchcmd "test.db" ".tables FOO BAD"
} {0 {}}

# .timeout MS            Try opening locked tables for MS milliseconds
do_test shell1-3.25.1 {
  catchcmd "test.db" ".timeout"
} {0 {}}
do_test shell1-3.25.2 {
  catchcmd "test.db" ".timeout zzz"
  # this should be treated the same as a '0' timeout
} {0 {}}
do_test shell1-3.25.3 {
  catchcmd "test.db" ".timeout 1"
} {0 {}}
do_test shell1-3.25.4 {
  # too many arguments
  catchcmd "test.db" ".timeout 1 BAD"
} {0 {}}

# .width NUM NUM ...     Set column widths for "column" mode
do_test shell1-3.26.1 {
  catchcmd "test.db" ".width"
} {0 {}}
do_test shell1-3.26.2 {
  catchcmd "test.db" ".width xxx"
  # this should be treated the same as a '0' width for col 1
} {0 {}}
do_test shell1-3.26.3 {
  catchcmd "test.db" ".width xxx yyy"
  # this should be treated the same as a '0' width for col 1 and 2
} {0 {}}
do_test shell1-3.26.4 {
  catchcmd "test.db" ".width 1 1"
  # this should be treated the same as a '1' width for col 1 and 2
} {0 {}}
do_test shell1-3.26.5 {
  catchcmd "test.db" ".mode column\n.header off\n.width 10 -10\nSELECT 'abcdefg', 123456;"
  # this should be treated the same as a '1' width for col 1 and 2
} {0 {abcdefg         123456}}
do_test shell1-3.26.6 {
  catchcmd "test.db" ".mode column\n.header off\n.width -10 10\nSELECT 'abcdefg', 123456;"
  # this should be treated the same as a '1' width for col 1 and 2
} {0 {   abcdefg  123456    }}


# .timer ON|OFF          Turn the CPU timer measurement on or off
do_test shell1-3.27.1 {
  catchcmd "test.db" ".timer"
} {1 {Usage: .timer on|off}}
do_test shell1-3.27.2 {
  catchcmd "test.db" ".timer ON"
} {0 {}}
do_test shell1-3.27.3 {
  catchcmd "test.db" ".timer OFF"
} {0 {}}
do_test shell1-3.27.4 {
  # too many arguments
  catchcmd "test.db" ".timer OFF BAD"
} {1 {Usage: .timer on|off}}

do_test shell1-3-28.1 {
  catchcmd test.db \
     ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
} "0 {(123) hello\n456}"

do_test shell1-3-29.1 {
  catchcmd "test.db" ".print this is a test"
} {0 {this is a test}}

# dot-command argument quoting
do_test shell1-3-30.1 {
  catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
} {0 {this"is'a-test this\"is\\a\055test}}
do_test shell1-3-31.1 {
  catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
} [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]


# Test the output of the ".dump" command
#
do_test shell1-4.1 {
  db close
  forcedelete test.db
  sqlite3 db test.db
  db eval {
    PRAGMA encoding=UTF16;
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
    CREATE TABLE t3(x,y);
    INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
                         (4,2.25), (5,'hello'), (6,x'807f');
  }
  catchcmd test.db {.dump}
} {0 {PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1(x);
INSERT INTO t1 VALUES(NULL);
INSERT INTO t1 VALUES('');
INSERT INTO t1 VALUES(1);
INSERT INTO t1 VALUES(2.25);
INSERT INTO t1 VALUES('hello');
INSERT INTO t1 VALUES(X'807f');
CREATE TABLE t3(x,y);
INSERT INTO t3 VALUES(1,NULL);
INSERT INTO t3 VALUES(2,'');
INSERT INTO t3 VALUES(3,1);
INSERT INTO t3 VALUES(4,2.25);
INSERT INTO t3 VALUES(5,'hello');
INSERT INTO t3 VALUES(6,X'807f');
COMMIT;}}


ifcapable vtab {

# The --preserve-rowids option to .dump
#
do_test shell1-4.1.1 {
  catchcmd test.db {.dump --preserve-rowids}
} {0 {PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1(x);
INSERT INTO t1(rowid,x) VALUES(1,NULL);
INSERT INTO t1(rowid,x) VALUES(2,'');
INSERT INTO t1(rowid,x) VALUES(3,1);
INSERT INTO t1(rowid,x) VALUES(4,2.25);
INSERT INTO t1(rowid,x) VALUES(5,'hello');
INSERT INTO t1(rowid,x) VALUES(6,X'807f');
CREATE TABLE t3(x,y);
INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f');
COMMIT;}}

# If the table contains an INTEGER PRIMARY KEY, do not record a separate
# rowid column in the output.
#
do_test shell1-4.1.2 {
  db close
  forcedelete test2.db
  sqlite3 db test2.db
  db eval {
    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
    INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
                         (4,2.25), (5,'hello'), (6,x'807f');
  }
  catchcmd test2.db {.dump --preserve-rowids}
} {0 {PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
INSERT INTO t1 VALUES(1,NULL);
INSERT INTO t1 VALUES(2,'');
INSERT INTO t1 VALUES(3,1);
INSERT INTO t1 VALUES(4,2.25);
INSERT INTO t1 VALUES(5,'hello');
INSERT INTO t1 VALUES(6,X'807f');
COMMIT;}}

# Verify that the table named [table] is correctly quoted and that
# an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
#
do_test shell1-4.1.3 {
  db close
  forcedelete test2.db
  sqlite3 db test2.db
  db eval {
    CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
    INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
                         (34,2.25), (45,'hello'), (56,x'807f');
  }
  catchcmd test2.db {.dump --preserve-rowids}
} {0 {PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f');
COMMIT;}}

# Do not record rowids for a WITHOUT ROWID table.  Also check correct quoting
# of table names that contain odd characters.
#
do_test shell1-4.1.4 {
  db close
  forcedelete test2.db
  sqlite3 db test2.db
  db eval {
    CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
    INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
                         (34,2.25), (45,'hello'), (56,x'807f');
  }
  catchcmd test2.db {.dump --preserve-rowids}
} {0 {PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
INSERT INTO "ta<>ble" VALUES(1,NULL);
INSERT INTO "ta<>ble" VALUES(12,'');
INSERT INTO "ta<>ble" VALUES(23,1);
INSERT INTO "ta<>ble" VALUES(34,2.25);
INSERT INTO "ta<>ble" VALUES(45,'hello');
INSERT INTO "ta<>ble" VALUES(56,X'807f');
COMMIT;}}

# Do not record rowids if the rowid is inaccessible
#
do_test shell1-4.1.5 {
  db close
  forcedelete test2.db
  sqlite3 db test2.db
  db eval {
    CREATE TABLE t1(_ROWID_,rowid,oid);
    INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
  }
  catchcmd test2.db {.dump --preserve-rowids}
} {0 {PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1(_ROWID_,rowid,oid);
INSERT INTO t1 VALUES(1,NULL,'alpha');
INSERT INTO t1 VALUES(12,'',99);
INSERT INTO t1 VALUES(23,1,X'b0b1b2');
COMMIT;}}

} else {

do_test shell1-4.1.6 {
  db close
  forcedelete test2.db
  sqlite3 db test2.db
  db eval {
    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
    INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
                         (4,2.25), (5,'hello'), (6,x'807f');
  }
  catchcmd test2.db {.dump --preserve-rowids}
} {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}

}


# Test the output of ".mode insert"
#
do_test shell1-4.2.1 {
  catchcmd test.db ".mode insert t1\nselect * from t1;"
} {0 {INSERT INTO t1 VALUES(NULL);
INSERT INTO t1 VALUES('');
INSERT INTO t1 VALUES(1);
INSERT INTO t1 VALUES(2.25);
INSERT INTO t1 VALUES('hello');
INSERT INTO t1 VALUES(X'807f');}}

# Test the output of ".mode insert" with headers
#
do_test shell1-4.2.2 {
  catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
} {0 {INSERT INTO t1(x) VALUES(NULL);
INSERT INTO t1(x) VALUES('');
INSERT INTO t1(x) VALUES(1);
INSERT INTO t1(x) VALUES(2.25);
INSERT INTO t1(x) VALUES('hello');
INSERT INTO t1(x) VALUES(X'807f');}}

# Test the output of ".mode insert"
#
do_test shell1-4.2.3 {
  catchcmd test.db ".mode insert t3\nselect * from t3;"
} {0 {INSERT INTO t3 VALUES(1,NULL);
INSERT INTO t3 VALUES(2,'');
INSERT INTO t3 VALUES(3,1);
INSERT INTO t3 VALUES(4,2.25);
INSERT INTO t3 VALUES(5,'hello');
INSERT INTO t3 VALUES(6,X'807f');}}

# Test the output of ".mode insert" with headers
#
do_test shell1-4.2.4 {
  catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
} {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
INSERT INTO t3(x,y) VALUES(2,'');
INSERT INTO t3(x,y) VALUES(3,1);
INSERT INTO t3(x,y) VALUES(4,2.25);
INSERT INTO t3(x,y) VALUES(5,'hello');
INSERT INTO t3(x,y) VALUES(6,X'807f');}}

# Test the output of ".mode tcl"
#
do_test shell1-4.3 {
  db close
  forcedelete test.db
  sqlite3 db test.db
  db eval {
    PRAGMA encoding=UTF8;
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
  }
  catchcmd test.db ".mode tcl\nselect * from t1;"
} {0 {""
""
"1"
"2.25"
"hello"
"\200\177"}}

# Test the output of ".mode tcl" with multiple columns
#
do_test shell1-4.4 {
  db eval {
    CREATE TABLE t2(x,y);
    INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
  }
  catchcmd test.db ".mode tcl\nselect * from t2;"
} {0 {"" ""
"1" "2.25"
"hello" "\200\177"}}

# Test the output of ".mode tcl" with ".nullvalue"
#
do_test shell1-4.5 {
  catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
} {0 {"NULL" ""
"1" "2.25"
"hello" "\200\177"}}

# Test the output of ".mode tcl" with Tcl reserved characters
#
do_test shell1-4.6 {
  db eval {
    CREATE TABLE tcl1(x);
    INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
  }
  foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
  list $x $y [llength $y]
} {0 {"\""
"["
"]"
"\\{"
"\\}"
";"
"$"} 7}

# Test the output of ".mode quote"
#
do_test shell1-4.7 {
  catchcmd test.db ".mode quote\nselect x'0123456789ABCDEF';"
} {0 X'0123456789abcdef'}

# Test using arbitrary byte data with the shell via standard input/output.
#
do_test shell1-5.0 {
  #
  # NOTE: Skip NUL byte because it appears to be incompatible with command
  #       shell argument parsing.
  #
  for {set i 1} {$i < 256} {incr i} {
    #
    # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
    #       command channels opened for it as textual ones), the carriage
    #       return character (and on Windows, the end-of-file character)
    #       cannot be used here.
    #
    if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
      continue
    }
    # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8.  So skip those tests.
    if {$i>=0x80 && $i<=0x9f} continue
    if {$i>=0xE0 && $tcl_platform(os)=="OpenBSD"}  continue
    if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os)=="Linux"}  continue
    set hex [format %02X $i]
    set char [subst \\x$hex]; set oldChar $char
    set escapes [list]
    if {$tcl_platform(platform)=="windows"} {
      #
      # NOTE: On Windows, we need to escape all the whitespace characters,
      #       the alarm (\a) character, and those with special meaning to
      #       the SQLite shell itself.
      #
      set escapes [list \
          \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
          " " "\" \"" \" \\\" \\ \\\\]
    } else {
      #
      # NOTE: On Unix, we need to escape most of the whitespace characters
      #       and those with special meaning to the SQLite shell itself.
      #       The alarm (\a), backspace (\b), and carriage-return (\r)
      #       characters do not appear to require escaping on Unix.  For
      #       the alarm and backspace characters, this is probably due to
      #       differences in the command shell.  For the carriage-return,
      #       it is probably due to differences in how Tcl handles command
      #       channel end-of-line translations.
      #
      set escapes [list \
          \t \\t \n \\n \v \\v \f \\f \
          " " "\" \"" \" \\\" \\ \\\\]
    }
    set char [string map $escapes $char]
    set x [catchcmdex test.db ".print \"$char\"\n"]
    set code [lindex $x 0]
    set res [lindex $x 1]
    if {$code ne "0"} {
      error "failed with error: $res"
    }
    if {$res ne "$oldChar\n"} {
      if {[llength $res] > 0} {
        set got [format %02X [scan $res %c]]
      } else {
        set got <empty>
      }
      error "failed with byte $hex mismatch, got $got"
    }
  }
} {}

# These test cases do not work on MinGW
if 0 {

# The string used here is the word "test" in Chinese.
# In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95
set test \u6D4B\u8BD5

do_test shell1-6.0 {
  set fileName $test; append fileName .db
  catch {forcedelete $fileName}
  set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"]
  set code [lindex $x 0]
  set res [string trim [lindex $x 1]]
  if {$code ne "0"} {
    error "failed with error: $res"
  }
  if {$res ne "CREATE TABLE t1(x);"} {
    error "failed with mismatch: $res"
  }
  if {![file exists $fileName]} {
    error "file \"$fileName\" (Unicode) does not exist"
  }
  forcedelete $fileName
} {}

do_test shell1-6.1 {
  catch {forcedelete test3.db}
  set x [catchcmdex test3.db \
      "CREATE TABLE [encoding convertto utf-8 $test](x);\n.schema\n"]
  set code [lindex $x 0]
  set res [string trim [lindex $x 1]]
  if {$code ne "0"} {
    error "failed with error: $res"
  }
  if {$res ne "CREATE TABLE ${test}(x);"} {
    error "failed with mismatch: $res"
  }
  forcedelete test3.db
} {}
}

db close
forcedelete test.db test.db-journal test.db-wal
sqlite3 db test.db

# The shell tool ".schema" command uses virtual table "pragma_database_list"
#
ifcapable vtab {

do_test shell1-7.1.1 {
  db eval {
    CREATE TABLE Z (x TEXT PRIMARY KEY);
    CREATE TABLE _ (x TEXT PRIMARY KEY);
    CREATE TABLE YY (x TEXT PRIMARY KEY);
    CREATE TABLE __ (x TEXT PRIMARY KEY);
    CREATE TABLE WWW (x TEXT PRIMARY KEY);
    CREATE TABLE ___ (x TEXT PRIMARY KEY);
  }
} {}
do_test shell1-7.1.2 {
  catchcmd "test.db" ".schema _"
} {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
CREATE TABLE _ (x TEXT PRIMARY KEY);}}
do_test shell1-7.1.3 {
  catchcmd "test.db" ".schema \"\\\\_\""
} {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
do_test shell1-7.1.4 {
  catchcmd "test.db" ".schema __"
} {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
CREATE TABLE __ (x TEXT PRIMARY KEY);}}
do_test shell1-7.1.5 {
  catchcmd "test.db" ".schema \"\\\\_\\\\_\""
} {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
do_test shell1-7.1.6 {
  catchcmd "test.db" ".schema ___"
} {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
do_test shell1-7.1.7 {
  catchcmd "test.db" ".schema \"\\\\_\\\\_\\\\_\""
} {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}

}

# Test case for the ieee754 and decimal extensions in the shell.
# See the "floatingpoint.html" file in the documentation for more
# information.
#
do_test shell1-8.1 {
  catchcmd ":memory:" {
    -- The pow2 table will hold all the necessary powers of two.
    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
    WITH RECURSIVE c(x,v) AS (
      VALUES(0,'1')
      UNION ALL
      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
    WITH RECURSIVE c(x,v) AS (
      VALUES(-1,'0.5')
      UNION ALL
      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
    
    -- This query finds the decimal representation of each value in the "c" table.
    WITH c(n) AS (VALUES(47.49))
                     ----XXXXX----------- Replace with whatever you want
    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
  }
} {0 47.49000000000000198951966012828052043914794921875}
do_test shell1-8.2 {
  catchcmd :memory: {
.mode box
SELECT ieee754(47.49) AS x;
  }
} {0 {┌───────────────────────────────┐
│               x               │
├───────────────────────────────┤
│ ieee754(6683623321994527,-47) │
└───────────────────────────────┘}}
do_test shell1-8.3 {
  catchcmd ":memory: --box" {
    select ieee754(6683623321994527,-47) as x;
  }
} {0 {┌───────┐
│   x   │
├───────┤
│ 47.49 │
└───────┘}}
do_test shell1-8.4 {
  catchcmd ":memory: --table" {SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;}
} {0 {+------------------+-----+
|        M         |  E  |
+------------------+-----+
| 6683623321994527 | -47 |
+------------------+-----+}}
do_test shell1-8.5 {
  catchcmd ":memory: --box" {
create table t(a text, b int);
insert into t values ('too long for one line', 1), ('shorter', NULL);
.header on
.width 10 10
.nullvalue NADA
select * from t;}
} {0 {┌────────────┬────────────┐
│     a      │     b      │
├────────────┼────────────┤
│ too long f │ 1          │
│ or one lin │            │
│ e          │            │
├────────────┼────────────┤
│ shorter    │ NADA       │
└────────────┴────────────┘}}

#----------------------------------------------------------------------------
# Test cases shell1-9.*: Basic test that "dot" commands and SQL intermix ok.
#
do_test shell1-9.1 {
  catchcmd :memory: {
.mode csv
/*
x */ select 1,2; --x
 -- .nada
;
.mode csv
--x
select 2,1; select 3,4;
}
} {0 {1,2
2,1
3,4}}

#----------------------------------------------------------------------------
# Test cases shell1-10.*: Test that certain static extensions are there.
#
do_test shell1-10.1 {
  catchcmd :memory: {
.mode list
.header off
select base64(base64(cast('digity-doo' as blob))),
 base85(base85(cast('digity-doo' as blob)));
}
} {0 digity-doo|digity-doo}

finish_test