insert5.test 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # 2007 November 23
  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. # The tests in this file ensure that a temporary table is used
  13. # when required by an "INSERT INTO ... SELECT ..." statement.
  14. #
  15. # $Id: insert5.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. ifcapable !subquery {
  19. finish_test
  20. return
  21. }
  22. # Return true if the compilation of the sql passed as an argument
  23. # includes the opcode OpenEphemeral. An "INSERT INTO ... SELECT"
  24. # statement includes such an opcode if a temp-table is used
  25. # to store intermediate results.
  26. #
  27. proc uses_temp_table {sql} {
  28. return [expr {[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]>=0}]
  29. }
  30. # Construct the sample database.
  31. #
  32. do_test insert5-1.0 {
  33. forcedelete test2.db test2.db-journal
  34. execsql {
  35. CREATE TABLE MAIN(Id INTEGER, Id1 INTEGER);
  36. CREATE TABLE B(Id INTEGER, Id1 INTEGER);
  37. CREATE VIEW v1 AS SELECT * FROM B;
  38. CREATE VIEW v2 AS SELECT * FROM MAIN;
  39. INSERT INTO MAIN(Id,Id1) VALUES(2,3);
  40. INSERT INTO B(Id,Id1) VALUES(2,3);
  41. }
  42. } {}
  43. # Run the query.
  44. #
  45. ifcapable compound {
  46. do_test insert5-1.1 {
  47. execsql {
  48. INSERT INTO B
  49. SELECT * FROM B UNION ALL
  50. SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
  51. SELECT * FROM B;
  52. }
  53. } {2 3 2 3 2 3}
  54. } else {
  55. do_test insert5-1.1 {
  56. execsql {
  57. INSERT INTO B SELECT * FROM B;
  58. INSERT INTO B
  59. SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
  60. SELECT * FROM B;
  61. }
  62. } {2 3 2 3 2 3}
  63. }
  64. do_test insert5-2.1 {
  65. uses_temp_table { INSERT INTO b SELECT * FROM main }
  66. } {0}
  67. do_test insert5-2.2 {
  68. uses_temp_table { INSERT INTO b SELECT * FROM b }
  69. } {1}
  70. do_test insert5-2.3 {
  71. uses_temp_table { INSERT INTO b SELECT (SELECT id FROM b), id1 FROM main }
  72. } {1}
  73. do_test insert5-2.4 {
  74. uses_temp_table { INSERT INTO b SELECT id1, (SELECT id FROM b) FROM main }
  75. } {1}
  76. do_test insert5-2.5 {
  77. uses_temp_table {
  78. INSERT INTO b
  79. SELECT * FROM main WHERE id = (SELECT id1 FROM b WHERE main.id = b.id) }
  80. } {1}
  81. do_test insert5-2.6 {
  82. uses_temp_table { INSERT INTO b SELECT * FROM v1 }
  83. } {1}
  84. do_test insert5-2.7 {
  85. uses_temp_table { INSERT INTO b SELECT * FROM v2 }
  86. } {0}
  87. do_test insert5-2.8 {
  88. uses_temp_table {
  89. INSERT INTO b
  90. SELECT * FROM main WHERE id > 10 AND max(id1, (SELECT id FROM b)) > 10;
  91. }
  92. } {1}
  93. # UPDATE: Using a column from the outer query (main.id) in the GROUP BY
  94. # or ORDER BY of a sub-query is no longer supported.
  95. #
  96. # do_test insert5-2.9 {
  97. # uses_temp_table {
  98. # INSERT INTO b
  99. # SELECT * FROM main
  100. # WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
  101. # }
  102. # } {}
  103. do_test insert5-2.9 {
  104. catchsql {
  105. INSERT INTO b
  106. SELECT * FROM main
  107. WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
  108. }
  109. } {1 {no such column: main.id}}
  110. finish_test