1
0

wherelimit.test 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # 2008 October 6
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the LIMIT ... OFFSET ... clause
  13. # of UPDATE and DELETE statements.
  14. #
  15. # $Id: wherelimit.test,v 1.2 2008/10/10 18:25:46 shane Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. proc create_test_data {size} {
  19. # Build some test data
  20. #
  21. execsql {
  22. DROP TABLE IF EXISTS t1;
  23. CREATE TABLE t1(x int, y int);
  24. BEGIN;
  25. }
  26. for {set i 1} {$i<=$size} {incr i} {
  27. for {set j 1} {$j<=$size} {incr j} {
  28. execsql "INSERT INTO t1 VALUES([expr {$i}],[expr {$j}])"
  29. }
  30. }
  31. execsql {
  32. COMMIT;
  33. }
  34. return {}
  35. }
  36. ifcapable {update_delete_limit} {
  37. # check syntax error support
  38. do_test wherelimit-0.1 {
  39. catchsql {DELETE FROM t1 ORDER BY x}
  40. } {1 {ORDER BY without LIMIT on DELETE}}
  41. do_test wherelimit-0.2 {
  42. catchsql {DELETE FROM t1 WHERE x=1 ORDER BY x}
  43. } {1 {ORDER BY without LIMIT on DELETE}}
  44. do_test wherelimit-0.3 {
  45. catchsql {UPDATE t1 SET y=1 WHERE x=1 ORDER BY x}
  46. } {1 {ORDER BY without LIMIT on UPDATE}}
  47. # no AS on table sources
  48. do_test wherelimit-0.4 {
  49. catchsql {DELETE FROM t1 AS a WHERE x=1}
  50. } {1 {near "AS": syntax error}}
  51. do_test wherelimit-0.5 {
  52. catchsql {UPDATE t1 AS a SET y=1 WHERE x=1}
  53. } {1 {near "AS": syntax error}}
  54. # OFFSET w/o LIMIT
  55. do_test wherelimit-0.6 {
  56. catchsql {DELETE FROM t1 WHERE x=1 OFFSET 2}
  57. } {1 {near "OFFSET": syntax error}}
  58. do_test wherelimit-0.7 {
  59. catchsql {UPDATE t1 SET y=1 WHERE x=1 OFFSET 2}
  60. } {1 {near "OFFSET": syntax error}}
  61. # check deletes w/o where clauses but with limit/offsets
  62. create_test_data 5
  63. do_test wherelimit-1.0 {
  64. execsql {SELECT count(*) FROM t1}
  65. } {25}
  66. do_test wherelimit-1.1 {
  67. execsql {DELETE FROM t1}
  68. execsql {SELECT count(*) FROM t1}
  69. } {0}
  70. create_test_data 5
  71. do_test wherelimit-1.2 {
  72. execsql {DELETE FROM t1 LIMIT 5}
  73. execsql {SELECT count(*) FROM t1}
  74. } {20}
  75. do_test wherelimit-1.3 {
  76. # limit 5
  77. execsql {DELETE FROM t1 ORDER BY x LIMIT 5}
  78. execsql {SELECT count(*) FROM t1}
  79. } {15}
  80. do_test wherelimit-1.4 {
  81. # limit 5, offset 2
  82. execsql {DELETE FROM t1 ORDER BY x LIMIT 5 OFFSET 2}
  83. execsql {SELECT count(*) FROM t1}
  84. } {10}
  85. do_test wherelimit-1.5 {
  86. # limit 5, offset -2
  87. execsql {DELETE FROM t1 ORDER BY x LIMIT 5 OFFSET -2}
  88. execsql {SELECT count(*) FROM t1}
  89. } {5}
  90. do_test wherelimit-1.6 {
  91. # limit -5 (no limit), offset 2
  92. execsql {DELETE FROM t1 ORDER BY x LIMIT 2, -5}
  93. execsql {SELECT count(*) FROM t1}
  94. } {2}
  95. do_test wherelimit-1.7 {
  96. # limit 5, offset -2 (no offset)
  97. execsql {DELETE FROM t1 ORDER BY x LIMIT -2, 5}
  98. execsql {SELECT count(*) FROM t1}
  99. } {0}
  100. create_test_data 5
  101. do_test wherelimit-1.8 {
  102. # limit -5 (no limit), offset -2 (no offset)
  103. execsql {DELETE FROM t1 ORDER BY x LIMIT -2, -5}
  104. execsql {SELECT count(*) FROM t1}
  105. } {0}
  106. create_test_data 3
  107. do_test wherelimit-1.9 {
  108. # limit 5, offset 2
  109. execsql {DELETE FROM t1 ORDER BY x LIMIT 2, 5}
  110. execsql {SELECT count(*) FROM t1}
  111. } {4}
  112. do_test wherelimit-1.10 {
  113. # limit 5, offset 5
  114. execsql {DELETE FROM t1 ORDER BY x LIMIT 5 OFFSET 5}
  115. execsql {SELECT count(*) FROM t1}
  116. } {4}
  117. do_test wherelimit-1.11 {
  118. # limit 50, offset 30
  119. execsql {DELETE FROM t1 ORDER BY x LIMIT 50 OFFSET 30}
  120. execsql {SELECT count(*) FROM t1}
  121. } {4}
  122. do_test wherelimit-1.12 {
  123. # limit 50, offset 30
  124. execsql {DELETE FROM t1 ORDER BY x LIMIT 30, 50}
  125. execsql {SELECT count(*) FROM t1}
  126. } {4}
  127. do_test wherelimit-1.13 {
  128. execsql {DELETE FROM t1 ORDER BY x LIMIT 50 OFFSET 50}
  129. execsql {SELECT count(*) FROM t1}
  130. } {4}
  131. create_test_data 6
  132. do_test wherelimit-2.0 {
  133. execsql {SELECT count(*) FROM t1}
  134. } {36}
  135. do_test wherelimit-2.1 {
  136. execsql {DELETE FROM t1 WHERE x=1}
  137. execsql {SELECT count(*) FROM t1}
  138. } {30}
  139. create_test_data 6
  140. do_test wherelimit-2.2 {
  141. execsql {DELETE FROM t1 WHERE x=1 LIMIT 5}
  142. execsql {SELECT count(*) FROM t1}
  143. } {31}
  144. do_test wherelimit-2.3 {
  145. # limit 5
  146. execsql {DELETE FROM t1 WHERE x=1 ORDER BY x LIMIT 5}
  147. execsql {SELECT count(*) FROM t1}
  148. } {30}
  149. do_test wherelimit-2.4 {
  150. # limit 5, offset 2
  151. execsql {DELETE FROM t1 WHERE x=2 ORDER BY x LIMIT 5 OFFSET 2}
  152. execsql {SELECT count(*) FROM t1}
  153. } {26}
  154. do_test wherelimit-2.5 {
  155. # limit 5, offset -2
  156. execsql {DELETE FROM t1 WHERE x=2 ORDER BY x LIMIT 5 OFFSET -2}
  157. execsql {SELECT count(*) FROM t1}
  158. } {24}
  159. do_test wherelimit-2.6 {
  160. # limit -5 (no limit), offset 2
  161. execsql {DELETE FROM t1 WHERE x=3 ORDER BY x LIMIT 2, -5}
  162. execsql {SELECT count(*) FROM t1}
  163. } {20}
  164. do_test wherelimit-2.7 {
  165. # limit 5, offset -2 (no offset)
  166. execsql {DELETE FROM t1 WHERE x=3 ORDER BY x LIMIT -2, 5}
  167. execsql {SELECT count(*) FROM t1}
  168. } {18}
  169. do_test wherelimit-2.8 {
  170. # limit -5 (no limit), offset -2 (no offset)
  171. execsql {DELETE FROM t1 WHERE x=4 ORDER BY x LIMIT -2, -5}
  172. execsql {SELECT count(*) FROM t1}
  173. } {12}
  174. create_test_data 6
  175. do_test wherelimit-2.9 {
  176. # limit 5, offset 2
  177. execsql {DELETE FROM t1 WHERE x=5 ORDER BY x LIMIT 2, 5}
  178. execsql {SELECT count(*) FROM t1}
  179. } {32}
  180. do_test wherelimit-2.10 {
  181. # limit 5, offset 5
  182. execsql {DELETE FROM t1 WHERE x=6 ORDER BY x LIMIT 5 OFFSET 5}
  183. execsql {SELECT count(*) FROM t1}
  184. } {31}
  185. do_test wherelimit-2.11 {
  186. # limit 50, offset 30
  187. execsql {DELETE FROM t1 WHERE x=1 ORDER BY x LIMIT 50 OFFSET 30}
  188. execsql {SELECT count(*) FROM t1}
  189. } {31}
  190. do_test wherelimit-2.12 {
  191. # limit 50, offset 30
  192. execsql {DELETE FROM t1 WHERE x=2 ORDER BY x LIMIT 30, 50}
  193. execsql {SELECT count(*) FROM t1}
  194. } {31}
  195. do_test wherelimit-2.13 {
  196. execsql {DELETE FROM t1 WHERE x=3 ORDER BY x LIMIT 50 OFFSET 50}
  197. execsql {SELECT count(*) FROM t1}
  198. } {31}
  199. create_test_data 6
  200. do_test wherelimit-3.0 {
  201. execsql {SELECT count(*) FROM t1}
  202. } {36}
  203. do_test wherelimit-3.1 {
  204. execsql {UPDATE t1 SET y=1 WHERE x=1}
  205. execsql {SELECT count(*) FROM t1 WHERE y=1}
  206. } {11}
  207. create_test_data 6
  208. do_test wherelimit-3.2 {
  209. execsql {UPDATE t1 SET y=1 WHERE x=1 LIMIT 5}
  210. execsql {SELECT count(*) FROM t1 WHERE y=1}
  211. } {10}
  212. do_test wherelimit-3.3 {
  213. # limit 5
  214. execsql {UPDATE t1 SET y=2 WHERE x=2 ORDER BY x LIMIT 5}
  215. execsql {SELECT count(*) FROM t1 WHERE y=2}
  216. } {9}
  217. create_test_data 6
  218. do_test wherelimit-3.4 {
  219. # limit 5, offset 2
  220. execsql {UPDATE t1 SET y=2 WHERE x=2 ORDER BY x LIMIT 5 OFFSET 2}
  221. execsql {SELECT count(*) FROM t1 WHERE y=1}
  222. } {6}
  223. do_test wherelimit-3.5 {
  224. # limit 5, offset -2
  225. execsql {UPDATE t1 SET y=2 WHERE x=2 ORDER BY x LIMIT 5 OFFSET -2}
  226. execsql {SELECT count(*) FROM t1 WHERE y=1}
  227. } {5}
  228. do_test wherelimit-3.6 {
  229. # limit -5 (no limit), offset 2
  230. execsql {UPDATE t1 SET y=3 WHERE x=3 ORDER BY x LIMIT 2, -5}
  231. execsql {SELECT count(*) FROM t1 WHERE y=3}
  232. } {8}
  233. do_test wherelimit-3.7 {
  234. # limit 5, offset -2 (no offset)
  235. execsql {UPDATE t1 SET y=3 WHERE x=3 ORDER BY x LIMIT -2, 5}
  236. execsql {SELECT count(*) FROM t1 WHERE y=3}
  237. } {10}
  238. do_test wherelimit-3.8 {
  239. # limit -5 (no limit), offset -2 (no offset)
  240. execsql {UPDATE t1 SET y=4 WHERE x=4 ORDER BY x LIMIT -2, -5}
  241. execsql {SELECT count(*) FROM t1 WHERE y=4}
  242. } {9}
  243. create_test_data 6
  244. do_test wherelimit-3.9 {
  245. # limit 5, offset 2
  246. execsql {UPDATE t1 SET y=4 WHERE x=5 ORDER BY x LIMIT 2, 5}
  247. execsql {SELECT count(*) FROM t1 WHERE y=4}
  248. } {9}
  249. do_test wherelimit-3.10 {
  250. # limit 5, offset 5
  251. execsql {UPDATE t1 SET y=4 WHERE x=6 ORDER BY x LIMIT 5 OFFSET 5}
  252. execsql {SELECT count(*) FROM t1 WHERE y=1}
  253. } {6}
  254. do_test wherelimit-3.11 {
  255. # limit 50, offset 30
  256. execsql {UPDATE t1 SET y=1 WHERE x=1 ORDER BY x LIMIT 50 OFFSET 30}
  257. execsql {SELECT count(*) FROM t1 WHERE y=1}
  258. } {6}
  259. do_test wherelimit-3.12 {
  260. # limit 50, offset 30
  261. execsql {UPDATE t1 SET y=1 WHERE x=2 ORDER BY x LIMIT 30, 50}
  262. execsql {SELECT count(*) FROM t1 WHERE y=1}
  263. } {6}
  264. do_test wherelimit-3.13 {
  265. execsql {UPDATE t1 SET y=1 WHERE x=3 ORDER BY x LIMIT 50 OFFSET 50}
  266. execsql {SELECT count(*) FROM t1 WHERE y=1}
  267. } {6}
  268. }
  269. finish_test