sessionstat1.test 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # 2018 January 12
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. #
  12. if {![info exists testdir]} {
  13. set testdir [file join [file dirname [info script]] .. .. test]
  14. }
  15. source [file join [file dirname [info script]] session_common.tcl]
  16. source $testdir/tester.tcl
  17. ifcapable !session {finish_test; return}
  18. set testprefix sessionstat1
  19. do_execsql_test 1.0 {
  20. CREATE TABLE t1(a PRIMARY KEY, b, c);
  21. CREATE INDEX t1b ON t1(b);
  22. CREATE INDEX t1c ON t1(c);
  23. WITH s(i) AS (
  24. SELECT 0 UNION ALL SELECT i+1 FROM s WHERE (i+1)<32
  25. )
  26. INSERT INTO t1 SELECT i, i%8, i%2 FROM s;
  27. }
  28. do_iterator_test 1.1 {} {
  29. ANALYZE
  30. } {
  31. {INSERT sqlite_stat1 0 XX. {} {t t1 t sqlite_autoindex_t1_1 t {32 1}}}
  32. {INSERT sqlite_stat1 0 XX. {} {t t1 t t1b t {32 4}}}
  33. {INSERT sqlite_stat1 0 XX. {} {t t1 t t1c t {32 16}}}
  34. }
  35. do_execsql_test 1.2 {
  36. WITH s(i) AS (
  37. SELECT 32 UNION ALL SELECT i+1 FROM s WHERE (i+1)<64
  38. )
  39. INSERT INTO t1 SELECT i, i%8, i%2 FROM s;
  40. }
  41. do_iterator_test 1.3 {} {
  42. ANALYZE
  43. } {
  44. {UPDATE sqlite_stat1 0 XX. {t t1 t sqlite_autoindex_t1_1 t {32 1}} {{} {} {} {} t {64 1}}}
  45. {UPDATE sqlite_stat1 0 XX. {t t1 t t1b t {32 4}} {{} {} {} {} t {64 8}}}
  46. {UPDATE sqlite_stat1 0 XX. {t t1 t t1c t {32 16}} {{} {} {} {} t {64 32}}}
  47. }
  48. do_iterator_test 1.5 {} {
  49. DROP INDEX t1b;
  50. } {
  51. {DELETE sqlite_stat1 0 XX. {t t1 t t1b t {64 8}} {}}
  52. }
  53. do_iterator_test 1.6 {} {
  54. DROP TABLE t1;
  55. } {
  56. {DELETE sqlite_stat1 0 XX. {t t1 t sqlite_autoindex_t1_1 t {64 1}} {}}
  57. {DELETE sqlite_stat1 0 XX. {t t1 t t1c t {64 32}} {}}
  58. }
  59. #-------------------------------------------------------------------------
  60. #
  61. catch { db2 close }
  62. forcedelete test.db2
  63. sqlite3 db2 test.db2
  64. do_test 2.0 {
  65. do_common_sql {
  66. CREATE TABLE t1(a PRIMARY KEY, b, c);
  67. CREATE INDEX t1b ON t1(b);
  68. CREATE INDEX t1c ON t1(c);
  69. ANALYZE;
  70. }
  71. } {}
  72. do_test 2.1 {
  73. do_then_apply_sql -ignorenoop {
  74. WITH s(i) AS (
  75. SELECT 0 UNION ALL SELECT i+1 FROM s WHERE (i+1)<32
  76. )
  77. INSERT INTO t1 SELECT i, i%8, i%2 FROM s;
  78. ANALYZE;
  79. }
  80. } {}
  81. do_execsql_test -db db2 2.2 {
  82. SELECT * FROM sqlite_stat1 ORDER BY tbl, idx
  83. } {
  84. t1 sqlite_autoindex_t1_1 {32 1}
  85. t1 t1b {32 4}
  86. t1 t1c {32 16}
  87. }
  88. do_test 2.3 {
  89. do_then_apply_sql -ignorenoop { DROP INDEX t1c }
  90. } {}
  91. do_execsql_test -db db2 2.4 {
  92. SELECT * FROM sqlite_stat1 ORDER BY tbl, idx;
  93. } {
  94. t1 sqlite_autoindex_t1_1 {32 1}
  95. t1 t1b {32 4}
  96. }
  97. do_test 2.3 {
  98. do_then_apply_sql -ignorenoop { DROP TABLE t1 }
  99. } {}
  100. do_execsql_test -db db2 2.4 {
  101. SELECT * FROM sqlite_stat1
  102. } {
  103. }
  104. do_execsql_test -db db2 2.5 { SELECT count(*) FROM t1 } 32
  105. #-------------------------------------------------------------------------
  106. db2 close
  107. forcedelete test.db2
  108. reset_db
  109. sqlite3 db2 test.db2
  110. do_test 3.0 {
  111. do_common_sql {
  112. CREATE TABLE t1(a, b, c);
  113. ANALYZE;
  114. DELETE FROM sqlite_stat1;
  115. }
  116. execsql {
  117. INSERT INTO t1 VALUES(1, 1, 1);
  118. INSERT INTO t1 VALUES(2, 2, 2);
  119. INSERT INTO t1 VALUES(3, 3, 3);
  120. INSERT INTO t1 VALUES(4, 4, 4);
  121. }
  122. } {}
  123. do_iterator_test 3.1 {} {
  124. ANALYZE
  125. } {
  126. {INSERT sqlite_stat1 0 XX. {} {t t1 b {} t 4}}
  127. }
  128. db null null
  129. db2 null null
  130. do_execsql_test 3.2 {
  131. SELECT * FROM sqlite_stat1;
  132. } {t1 null 4}
  133. do_test 3.3 {
  134. execsql { DELETE FROM sqlite_stat1 }
  135. do_then_apply_sql -ignorenoop { ANALYZE }
  136. execsql { SELECT * FROM sqlite_stat1 } db2
  137. } {t1 null 4}
  138. do_test 3.4 {
  139. execsql { INSERT INTO t1 VALUES(5,5,5) }
  140. do_then_apply_sql -ignorenoop { ANALYZE }
  141. execsql { SELECT * FROM sqlite_stat1 } db2
  142. } {t1 null 5}
  143. do_test 3.5 {
  144. do_then_apply_sql -ignorenoop { DROP TABLE t1 }
  145. execsql { SELECT * FROM sqlite_stat1 } db2
  146. } {}
  147. do_test 3.6.1 {
  148. execsql {
  149. CREATE TABLE t1(a, b, c);
  150. CREATE TABLE t2(x, y, z);
  151. INSERT INTO t1 VALUES(1,1,1), (2,2,2), (3,3,3), (4,4,4), (5,5,5);
  152. INSERT INTO t2 SELECT * FROM t1;
  153. DELETE FROM sqlite_stat1;
  154. }
  155. sqlite3session S db main
  156. S attach sqlite_stat1
  157. execsql { ANALYZE }
  158. } {}
  159. do_changeset_test 3.6.2 S {
  160. {INSERT sqlite_stat1 0 XX. {} {t t2 b {} t 5}}
  161. {INSERT sqlite_stat1 0 XX. {} {t t1 b {} t 5}}
  162. }
  163. do_changeset_invert_test 3.6.3 S {
  164. {DELETE sqlite_stat1 0 XX. {t t2 b {} t 5} {}}
  165. {DELETE sqlite_stat1 0 XX. {t t1 b {} t 5} {}}
  166. }
  167. do_test 3.6.4 { S delete } {}
  168. proc sql_changeset_concat {args} {
  169. foreach sql $args {
  170. sqlite3session S db main
  171. S attach sqlite_stat1
  172. execsql $sql
  173. set change [S changeset]
  174. S delete
  175. if {[info vars ret]!=""} {
  176. set ret [sqlite3changeset_concat $ret $change]
  177. } else {
  178. set ret $change
  179. }
  180. }
  181. changeset_to_list $ret
  182. }
  183. proc do_scc_test {tn args} {
  184. uplevel [list \
  185. do_test $tn [concat sql_changeset_concat [lrange $args 0 end-1]] \
  186. [list {*}[ lindex $args end ]]
  187. ]
  188. }
  189. do_execsql_test 3.7.0 {
  190. DELETE FROM sqlite_stat1;
  191. }
  192. do_scc_test 3.7.1 {
  193. ANALYZE;
  194. } {
  195. INSERT INTO t2 VALUES(6,6,6);
  196. ANALYZE;
  197. } {
  198. {INSERT sqlite_stat1 0 XX. {} {t t1 b {} t 5}}
  199. {INSERT sqlite_stat1 0 XX. {} {t t2 b {} t 6}}
  200. }
  201. #-------------------------------------------------------------------------
  202. catch { db2 close }
  203. reset_db
  204. forcedelete test.db2
  205. sqlite3 db2 test.db2
  206. do_test 4.1.0 {
  207. do_common_sql {
  208. CREATE TABLE t1(a, b);
  209. CREATE INDEX i1 ON t1(a);
  210. CREATE INDEX i2 ON t1(b);
  211. INSERT INTO t1 VALUES(1,1), (2,2);
  212. ANALYZE;
  213. }
  214. execsql { DELETE FROM sqlite_stat1 }
  215. } {}
  216. do_test 4.1.1 {
  217. execsql { INSERT INTO t1 VALUES(3,3); }
  218. set C [changeset_from_sql {ANALYZE}]
  219. set ::c [list]
  220. proc xConflict {args} {
  221. lappend ::c $args
  222. return "OMIT"
  223. }
  224. sqlite3changeset_apply db2 $C xConflict
  225. set ::c
  226. } [list {*}{
  227. {INSERT sqlite_stat1 CONFLICT {t t1 t i1 t {3 1}} {t t1 t i1 t {2 1}}}
  228. {INSERT sqlite_stat1 CONFLICT {t t1 t i2 t {3 1}} {t t1 t i2 t {2 1}}}
  229. }]
  230. do_execsql_test -db db2 4.1.2 {
  231. SELECT * FROM sqlite_stat1 ORDER BY 1,2;
  232. } {t1 i1 {2 1} t1 i2 {2 1}}
  233. do_test 4.1.3 {
  234. proc xConflict {args} {
  235. return "REPLACE"
  236. }
  237. sqlite3changeset_apply db2 $C xConflict
  238. execsql { SELECT * FROM sqlite_stat1 ORDER BY 1,2 } db2
  239. } {t1 i1 {3 1} t1 i2 {3 1}}
  240. do_test 4.2.0 {
  241. do_common_sql {
  242. DROP TABLE t1;
  243. CREATE TABLE t3(x,y);
  244. INSERT INTO t3 VALUES('a','a');
  245. INSERT INTO t3 VALUES('b','b');
  246. ANALYZE;
  247. }
  248. execsql { DELETE FROM sqlite_stat1 }
  249. } {}
  250. do_test 4.2.1 {
  251. execsql { INSERT INTO t3 VALUES('c','c'); }
  252. set C [changeset_from_sql {ANALYZE}]
  253. set ::c [list]
  254. proc xConflict {args} {
  255. lappend ::c $args
  256. return "OMIT"
  257. }
  258. sqlite3changeset_apply db2 $C xConflict
  259. set ::c
  260. } [list {*}{
  261. {INSERT sqlite_stat1 CONFLICT {t t3 b {} t 3} {t t3 b {} t 2}}
  262. }]
  263. db2 null null
  264. do_execsql_test -db db2 4.2.2 {
  265. SELECT * FROM sqlite_stat1 ORDER BY 1,2;
  266. } {t3 null 2}
  267. do_test 4.2.3 {
  268. proc xConflict {args} {
  269. return "REPLACE"
  270. }
  271. sqlite3changeset_apply db2 $C xConflict
  272. execsql { SELECT * FROM sqlite_stat1 ORDER BY 1,2 } db2
  273. } {t3 null 3}
  274. finish_test