1*1da57d55SToomas Soome#
27c478bd9Sstevel@tonic-gate# 2001 September 15
37c478bd9Sstevel@tonic-gate#
47c478bd9Sstevel@tonic-gate# The author disclaims copyright to this source code.  In place of
57c478bd9Sstevel@tonic-gate# a legal notice, here is a blessing:
67c478bd9Sstevel@tonic-gate#
77c478bd9Sstevel@tonic-gate#    May you do good and not evil.
87c478bd9Sstevel@tonic-gate#    May you find forgiveness for yourself and forgive others.
97c478bd9Sstevel@tonic-gate#    May you share freely, never taking more than you give.
107c478bd9Sstevel@tonic-gate#
117c478bd9Sstevel@tonic-gate#***********************************************************************
127c478bd9Sstevel@tonic-gate# This file implements regression tests for SQLite library.  The
137c478bd9Sstevel@tonic-gate# focus of this file is testing the sqlite_exec_printf() and
147c478bd9Sstevel@tonic-gate# sqlite_get_table_printf() APIs.
157c478bd9Sstevel@tonic-gate#
167c478bd9Sstevel@tonic-gate# $Id: tableapi.test,v 1.7 2004/02/02 12:29:25 drh Exp $
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gateset testdir [file dirname $argv0]
197c478bd9Sstevel@tonic-gatesource $testdir/tester.tcl
207c478bd9Sstevel@tonic-gate
217c478bd9Sstevel@tonic-gatedo_test tableapi-1.0 {
227c478bd9Sstevel@tonic-gate  set ::dbx [sqlite_open test.db]
237c478bd9Sstevel@tonic-gate  catch {sqlite_exec_printf $::dbx {DROP TABLE xyz} {}}
247c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::dbx {CREATE TABLE %s(a int, b text)} xyz
257c478bd9Sstevel@tonic-gate} {0 {}}
267c478bd9Sstevel@tonic-gatedo_test tableapi-1.1 {
277c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::dbx {
287c478bd9Sstevel@tonic-gate    INSERT INTO xyz VALUES(1,'%q')
297c478bd9Sstevel@tonic-gate  } {Hi Y'all}
307c478bd9Sstevel@tonic-gate} {0 {}}
317c478bd9Sstevel@tonic-gatedo_test tableapi-1.2 {
327c478bd9Sstevel@tonic-gate  sqlite_exec_printf $::dbx {SELECT * FROM xyz} {}
337c478bd9Sstevel@tonic-gate} {0 {a b 1 {Hi Y'all}}}
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gatedo_test tableapi-2.1 {
367c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
377c478bd9Sstevel@tonic-gate    BEGIN TRANSACTION;
387c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE b='%q'
397c478bd9Sstevel@tonic-gate  } {Hi Y'all}
407c478bd9Sstevel@tonic-gate} {0 1 2 a b 1 {Hi Y'all}}
417c478bd9Sstevel@tonic-gatedo_test tableapi-2.2 {
427c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
437c478bd9Sstevel@tonic-gate    SELECT * FROM xyz
447c478bd9Sstevel@tonic-gate  } {}
457c478bd9Sstevel@tonic-gate} {0 1 2 a b 1 {Hi Y'all}}
467c478bd9Sstevel@tonic-gatedo_test tableapi-2.3 {
477c478bd9Sstevel@tonic-gate  for {set i 2} {$i<=50} {incr i} {
487c478bd9Sstevel@tonic-gate    sqlite_get_table_printf $::dbx \
497c478bd9Sstevel@tonic-gate       "INSERT INTO xyz VALUES($i,'(%s)')" $i
507c478bd9Sstevel@tonic-gate  }
517c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
527c478bd9Sstevel@tonic-gate    SELECT * FROM xyz ORDER BY a
537c478bd9Sstevel@tonic-gate  } {}
547c478bd9Sstevel@tonic-gate} {0 50 2 a b 1 {Hi Y'all} 2 (2) 3 (3) 4 (4) 5 (5) 6 (6) 7 (7) 8 (8) 9 (9) 10 (10) 11 (11) 12 (12) 13 (13) 14 (14) 15 (15) 16 (16) 17 (17) 18 (18) 19 (19) 20 (20) 21 (21) 22 (22) 23 (23) 24 (24) 25 (25) 26 (26) 27 (27) 28 (28) 29 (29) 30 (30) 31 (31) 32 (32) 33 (33) 34 (34) 35 (35) 36 (36) 37 (37) 38 (38) 39 (39) 40 (40) 41 (41) 42 (42) 43 (43) 44 (44) 45 (45) 46 (46) 47 (47) 48 (48) 49 (49) 50 (50)}
557c478bd9Sstevel@tonic-gatedo_test tableapi-2.3.1 {
567c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
577c478bd9Sstevel@tonic-gate    SELECT * FROM xyz  WHERE a>49 ORDER BY a
587c478bd9Sstevel@tonic-gate  } {}
597c478bd9Sstevel@tonic-gate} {0 1 2 a b 50 (50)}
607c478bd9Sstevel@tonic-gatedo_test tableapi-2.3.2 {
617c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
627c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a>47 ORDER BY a
637c478bd9Sstevel@tonic-gate  } {}
647c478bd9Sstevel@tonic-gate} {0 3 2 a b 48 (48) 49 (49) 50 (50)}
657c478bd9Sstevel@tonic-gatedo_test tableapi-2.4 {
667c478bd9Sstevel@tonic-gate  set manyquote ''''''''
677c478bd9Sstevel@tonic-gate  append manyquote $manyquote
687c478bd9Sstevel@tonic-gate  append manyquote $manyquote
697c478bd9Sstevel@tonic-gate  append manyquote $manyquote
707c478bd9Sstevel@tonic-gate  append manyquote $manyquote
717c478bd9Sstevel@tonic-gate  append manyquote $manyquote
727c478bd9Sstevel@tonic-gate  append manyquote $manyquote
737c478bd9Sstevel@tonic-gate  set ::big_str "$manyquote Hello $manyquote"
747c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
757c478bd9Sstevel@tonic-gate    INSERT INTO xyz VALUES(51,'%q')
767c478bd9Sstevel@tonic-gate  } $::big_str
777c478bd9Sstevel@tonic-gate} {0 0 0}
787c478bd9Sstevel@tonic-gatedo_test tableapi-2.5 {
797c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
807c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a>49 ORDER BY a;
817c478bd9Sstevel@tonic-gate  } {}
827c478bd9Sstevel@tonic-gate} "0 2 2 a b 50 (50) 51 \173$::big_str\175"
837c478bd9Sstevel@tonic-gatedo_test tableapi-2.6 {
847c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
857c478bd9Sstevel@tonic-gate    INSERT INTO xyz VALUES(52,NULL)
867c478bd9Sstevel@tonic-gate  } {}
877c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
887c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC
897c478bd9Sstevel@tonic-gate  } {}
907c478bd9Sstevel@tonic-gate} {0 3 2 a b 52 NULL 50 (50) 42 (42)}
917c478bd9Sstevel@tonic-gatedo_test tableapi-2.7 {
927c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
937c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a>1000
947c478bd9Sstevel@tonic-gate  } {}
957c478bd9Sstevel@tonic-gate} {0 0 0}
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate# Repeat all tests with the empty_result_callbacks pragma turned on
987c478bd9Sstevel@tonic-gate#
997c478bd9Sstevel@tonic-gatedo_test tableapi-3.1 {
1007c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1017c478bd9Sstevel@tonic-gate    ROLLBACK;
1027c478bd9Sstevel@tonic-gate    PRAGMA empty_result_callbacks = ON;
1037c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE b='%q'
1047c478bd9Sstevel@tonic-gate  } {Hi Y'all}
1057c478bd9Sstevel@tonic-gate} {0 1 2 a b 1 {Hi Y'all}}
1067c478bd9Sstevel@tonic-gatedo_test tableapi-3.2 {
1077c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1087c478bd9Sstevel@tonic-gate    SELECT * FROM xyz
1097c478bd9Sstevel@tonic-gate  } {}
1107c478bd9Sstevel@tonic-gate} {0 1 2 a b 1 {Hi Y'all}}
1117c478bd9Sstevel@tonic-gatedo_test tableapi-3.3 {
1127c478bd9Sstevel@tonic-gate  for {set i 2} {$i<=50} {incr i} {
1137c478bd9Sstevel@tonic-gate    sqlite_get_table_printf $::dbx \
1147c478bd9Sstevel@tonic-gate       "INSERT INTO xyz VALUES($i,'(%s)')" $i
1157c478bd9Sstevel@tonic-gate  }
1167c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1177c478bd9Sstevel@tonic-gate    SELECT * FROM xyz ORDER BY a
1187c478bd9Sstevel@tonic-gate  } {}
1197c478bd9Sstevel@tonic-gate} {0 50 2 a b 1 {Hi Y'all} 2 (2) 3 (3) 4 (4) 5 (5) 6 (6) 7 (7) 8 (8) 9 (9) 10 (10) 11 (11) 12 (12) 13 (13) 14 (14) 15 (15) 16 (16) 17 (17) 18 (18) 19 (19) 20 (20) 21 (21) 22 (22) 23 (23) 24 (24) 25 (25) 26 (26) 27 (27) 28 (28) 29 (29) 30 (30) 31 (31) 32 (32) 33 (33) 34 (34) 35 (35) 36 (36) 37 (37) 38 (38) 39 (39) 40 (40) 41 (41) 42 (42) 43 (43) 44 (44) 45 (45) 46 (46) 47 (47) 48 (48) 49 (49) 50 (50)}
1207c478bd9Sstevel@tonic-gatedo_test tableapi-3.3.1 {
1217c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1227c478bd9Sstevel@tonic-gate    SELECT * FROM xyz  WHERE a>49 ORDER BY a
1237c478bd9Sstevel@tonic-gate  } {}
1247c478bd9Sstevel@tonic-gate} {0 1 2 a b 50 (50)}
1257c478bd9Sstevel@tonic-gatedo_test tableapi-3.3.2 {
1267c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1277c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a>47 ORDER BY a
1287c478bd9Sstevel@tonic-gate  } {}
1297c478bd9Sstevel@tonic-gate} {0 3 2 a b 48 (48) 49 (49) 50 (50)}
1307c478bd9Sstevel@tonic-gatedo_test tableapi-3.4 {
1317c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1327c478bd9Sstevel@tonic-gate    INSERT INTO xyz VALUES(51,'%q')
1337c478bd9Sstevel@tonic-gate  } $::big_str
1347c478bd9Sstevel@tonic-gate} {0 0 0}
1357c478bd9Sstevel@tonic-gatedo_test tableapi-3.5 {
1367c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1377c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a>49 ORDER BY a;
1387c478bd9Sstevel@tonic-gate  } {}
1397c478bd9Sstevel@tonic-gate} "0 2 2 a b 50 (50) 51 \173$::big_str\175"
1407c478bd9Sstevel@tonic-gatedo_test tableapi-3.6 {
1417c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1427c478bd9Sstevel@tonic-gate    INSERT INTO xyz VALUES(52,NULL)
1437c478bd9Sstevel@tonic-gate  } {}
1447c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1457c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC
1467c478bd9Sstevel@tonic-gate  } {}
1477c478bd9Sstevel@tonic-gate} {0 3 2 a b 52 NULL 50 (50) 42 (42)}
1487c478bd9Sstevel@tonic-gatedo_test tableapi-3.7 {
1497c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {
1507c478bd9Sstevel@tonic-gate    SELECT * FROM xyz WHERE a>1000
1517c478bd9Sstevel@tonic-gate  } {}
1527c478bd9Sstevel@tonic-gate} {0 0 2 a b}
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gatedo_test tableapi-4.1 {
1557c478bd9Sstevel@tonic-gate  set rc [catch {
1567c478bd9Sstevel@tonic-gate    sqlite_get_table_printf $::dbx {
1577c478bd9Sstevel@tonic-gate      SELECT * FROM xyz;  SELECT * FROM sqlite_master
1587c478bd9Sstevel@tonic-gate    } {}
1597c478bd9Sstevel@tonic-gate  } msg]
1607c478bd9Sstevel@tonic-gate  concat $rc $msg
1617c478bd9Sstevel@tonic-gate} {0 1 {sqlite_get_table() called with two or more incompatible queries}}
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate# A report on the mailing list says that the sqlite_get_table() api fails
1647c478bd9Sstevel@tonic-gate# on queries involving more than 40 columns.  The following code attempts
1657c478bd9Sstevel@tonic-gate# to test that complaint
1667c478bd9Sstevel@tonic-gate#
1677c478bd9Sstevel@tonic-gatedo_test tableapi-5.1 {
1687c478bd9Sstevel@tonic-gate  set sql "CREATE TABLE t2("
1697c478bd9Sstevel@tonic-gate  set sep ""
1707c478bd9Sstevel@tonic-gate  for {set i 1} {$i<=100} {incr i} {
1717c478bd9Sstevel@tonic-gate    append sql ${sep}x$i
1727c478bd9Sstevel@tonic-gate    set sep ,
1737c478bd9Sstevel@tonic-gate  }
1747c478bd9Sstevel@tonic-gate  append sql )
1757c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx $sql {}
1767c478bd9Sstevel@tonic-gate  set sql "INSERT INTO t2 VALUES("
1777c478bd9Sstevel@tonic-gate  set sep ""
1787c478bd9Sstevel@tonic-gate  for {set i 1} {$i<=100} {incr i} {
1797c478bd9Sstevel@tonic-gate    append sql ${sep}$i
1807c478bd9Sstevel@tonic-gate    set sep ,
1817c478bd9Sstevel@tonic-gate  }
1827c478bd9Sstevel@tonic-gate  append sql )
1837c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx $sql {}
1847c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {SELECT * FROM t2} {}
1857c478bd9Sstevel@tonic-gate} {0 1 100 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90 x91 x92 x93 x94 x95 x96 x97 x98 x99 x100 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}
1867c478bd9Sstevel@tonic-gatedo_test tableapi-5.2 {
1877c478bd9Sstevel@tonic-gate  set sql "INSERT INTO t2 VALUES("
1887c478bd9Sstevel@tonic-gate  set sep ""
1897c478bd9Sstevel@tonic-gate  for {set i 1} {$i<=100} {incr i} {
1907c478bd9Sstevel@tonic-gate    append sql ${sep}[expr {$i+1000}]
1917c478bd9Sstevel@tonic-gate    set sep ,
1927c478bd9Sstevel@tonic-gate  }
1937c478bd9Sstevel@tonic-gate  append sql )
1947c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx $sql {}
1957c478bd9Sstevel@tonic-gate  sqlite_get_table_printf $::dbx {SELECT * FROM t2} {}
1967c478bd9Sstevel@tonic-gate} {0 2 100 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90 x91 x92 x93 x94 x95 x96 x97 x98 x99 x100 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 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}
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gatedo_test tableapi-99.0 {
1997c478bd9Sstevel@tonic-gate  sqlite_close $::dbx
2007c478bd9Sstevel@tonic-gate} {}
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gatefinish_test
203