os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/insert5.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2007 November 23
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
#
sl@0
    12
# The tests in this file ensure that a temporary table is used
sl@0
    13
# when required by an "INSERT INTO ... SELECT ..." statement.
sl@0
    14
#
sl@0
    15
# $Id: insert5.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
ifcapable !subquery {
sl@0
    21
  finish_test
sl@0
    22
  return
sl@0
    23
}
sl@0
    24
sl@0
    25
# Return true if the compilation of the sql passed as an argument 
sl@0
    26
# includes the opcode OpenEphemeral. An "INSERT INTO ... SELECT"
sl@0
    27
# statement includes such an opcode if a temp-table is used
sl@0
    28
# to store intermediate results.
sl@0
    29
# 
sl@0
    30
proc uses_temp_table {sql} {
sl@0
    31
  return [expr {[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]>=0}]
sl@0
    32
}
sl@0
    33
sl@0
    34
# Construct the sample database.
sl@0
    35
#
sl@0
    36
do_test insert5-1.0 {
sl@0
    37
  file delete -force test2.db test2.db-journal
sl@0
    38
  execsql {
sl@0
    39
    CREATE TABLE MAIN(Id INTEGER, Id1 INTEGER); 
sl@0
    40
    CREATE TABLE B(Id INTEGER, Id1 INTEGER); 
sl@0
    41
    CREATE VIEW v1 AS SELECT * FROM B;
sl@0
    42
    CREATE VIEW v2 AS SELECT * FROM MAIN;
sl@0
    43
    INSERT INTO MAIN(Id,Id1) VALUES(2,3); 
sl@0
    44
    INSERT INTO B(Id,Id1) VALUES(2,3); 
sl@0
    45
  }
sl@0
    46
} {}
sl@0
    47
sl@0
    48
# Run the query.
sl@0
    49
#
sl@0
    50
ifcapable compound {
sl@0
    51
  do_test insert5-1.1 {
sl@0
    52
    execsql {
sl@0
    53
      INSERT INTO B 
sl@0
    54
        SELECT * FROM B UNION ALL 
sl@0
    55
        SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
sl@0
    56
      SELECT * FROM B;
sl@0
    57
    }
sl@0
    58
  } {2 3 2 3 2 3}
sl@0
    59
} else {
sl@0
    60
  do_test insert5-1.1 {
sl@0
    61
    execsql {
sl@0
    62
      INSERT INTO B SELECT * FROM B;
sl@0
    63
      INSERT INTO B
sl@0
    64
        SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
sl@0
    65
      SELECT * FROM B;
sl@0
    66
    }
sl@0
    67
  } {2 3 2 3 2 3}
sl@0
    68
}
sl@0
    69
do_test insert5-2.1 {
sl@0
    70
  uses_temp_table { INSERT INTO b SELECT * FROM main }
sl@0
    71
} {0}
sl@0
    72
do_test insert5-2.2 {
sl@0
    73
  uses_temp_table { INSERT INTO b SELECT * FROM b }
sl@0
    74
} {1}
sl@0
    75
do_test insert5-2.3 {
sl@0
    76
  uses_temp_table { INSERT INTO b SELECT (SELECT id FROM b), id1 FROM main }
sl@0
    77
} {1}
sl@0
    78
do_test insert5-2.4 {
sl@0
    79
  uses_temp_table { INSERT INTO b SELECT id1, (SELECT id FROM b) FROM main }
sl@0
    80
} {1}
sl@0
    81
do_test insert5-2.5 {
sl@0
    82
  uses_temp_table { 
sl@0
    83
    INSERT INTO b 
sl@0
    84
      SELECT * FROM main WHERE id = (SELECT id1 FROM b WHERE main.id = b.id) }
sl@0
    85
} {1}
sl@0
    86
do_test insert5-2.6 {
sl@0
    87
  uses_temp_table { INSERT INTO b SELECT * FROM v1 }
sl@0
    88
} {1}
sl@0
    89
do_test insert5-2.7 {
sl@0
    90
  uses_temp_table { INSERT INTO b SELECT * FROM v2 }
sl@0
    91
} {0}
sl@0
    92
do_test insert5-2.8 {
sl@0
    93
  uses_temp_table { 
sl@0
    94
    INSERT INTO b 
sl@0
    95
    SELECT * FROM main WHERE id > 10 AND max(id1, (SELECT id FROM b)) > 10;
sl@0
    96
  }
sl@0
    97
} {1}
sl@0
    98
sl@0
    99
# UPDATE: Using a column from the outer query (main.id) in the GROUP BY
sl@0
   100
# or ORDER BY of a sub-query is no longer supported.
sl@0
   101
#
sl@0
   102
# do_test insert5-2.9 {
sl@0
   103
#   uses_temp_table { 
sl@0
   104
#     INSERT INTO b 
sl@0
   105
#     SELECT * FROM main 
sl@0
   106
#     WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
sl@0
   107
#   }
sl@0
   108
# } {}
sl@0
   109
do_test insert5-2.9 {
sl@0
   110
  catchsql { 
sl@0
   111
    INSERT INTO b 
sl@0
   112
    SELECT * FROM main 
sl@0
   113
    WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
sl@0
   114
  }
sl@0
   115
} {1 {no such column: main.id}}
sl@0
   116
sl@0
   117
finish_test