os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/insert5.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/insert5.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,117 @@
     1.4 +# 2007 November 23
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +#
    1.15 +# The tests in this file ensure that a temporary table is used
    1.16 +# when required by an "INSERT INTO ... SELECT ..." statement.
    1.17 +#
    1.18 +# $Id: insert5.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $
    1.19 +
    1.20 +set testdir [file dirname $argv0]
    1.21 +source $testdir/tester.tcl
    1.22 +
    1.23 +ifcapable !subquery {
    1.24 +  finish_test
    1.25 +  return
    1.26 +}
    1.27 +
    1.28 +# Return true if the compilation of the sql passed as an argument 
    1.29 +# includes the opcode OpenEphemeral. An "INSERT INTO ... SELECT"
    1.30 +# statement includes such an opcode if a temp-table is used
    1.31 +# to store intermediate results.
    1.32 +# 
    1.33 +proc uses_temp_table {sql} {
    1.34 +  return [expr {[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]>=0}]
    1.35 +}
    1.36 +
    1.37 +# Construct the sample database.
    1.38 +#
    1.39 +do_test insert5-1.0 {
    1.40 +  file delete -force test2.db test2.db-journal
    1.41 +  execsql {
    1.42 +    CREATE TABLE MAIN(Id INTEGER, Id1 INTEGER); 
    1.43 +    CREATE TABLE B(Id INTEGER, Id1 INTEGER); 
    1.44 +    CREATE VIEW v1 AS SELECT * FROM B;
    1.45 +    CREATE VIEW v2 AS SELECT * FROM MAIN;
    1.46 +    INSERT INTO MAIN(Id,Id1) VALUES(2,3); 
    1.47 +    INSERT INTO B(Id,Id1) VALUES(2,3); 
    1.48 +  }
    1.49 +} {}
    1.50 +
    1.51 +# Run the query.
    1.52 +#
    1.53 +ifcapable compound {
    1.54 +  do_test insert5-1.1 {
    1.55 +    execsql {
    1.56 +      INSERT INTO B 
    1.57 +        SELECT * FROM B UNION ALL 
    1.58 +        SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
    1.59 +      SELECT * FROM B;
    1.60 +    }
    1.61 +  } {2 3 2 3 2 3}
    1.62 +} else {
    1.63 +  do_test insert5-1.1 {
    1.64 +    execsql {
    1.65 +      INSERT INTO B SELECT * FROM B;
    1.66 +      INSERT INTO B
    1.67 +        SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
    1.68 +      SELECT * FROM B;
    1.69 +    }
    1.70 +  } {2 3 2 3 2 3}
    1.71 +}
    1.72 +do_test insert5-2.1 {
    1.73 +  uses_temp_table { INSERT INTO b SELECT * FROM main }
    1.74 +} {0}
    1.75 +do_test insert5-2.2 {
    1.76 +  uses_temp_table { INSERT INTO b SELECT * FROM b }
    1.77 +} {1}
    1.78 +do_test insert5-2.3 {
    1.79 +  uses_temp_table { INSERT INTO b SELECT (SELECT id FROM b), id1 FROM main }
    1.80 +} {1}
    1.81 +do_test insert5-2.4 {
    1.82 +  uses_temp_table { INSERT INTO b SELECT id1, (SELECT id FROM b) FROM main }
    1.83 +} {1}
    1.84 +do_test insert5-2.5 {
    1.85 +  uses_temp_table { 
    1.86 +    INSERT INTO b 
    1.87 +      SELECT * FROM main WHERE id = (SELECT id1 FROM b WHERE main.id = b.id) }
    1.88 +} {1}
    1.89 +do_test insert5-2.6 {
    1.90 +  uses_temp_table { INSERT INTO b SELECT * FROM v1 }
    1.91 +} {1}
    1.92 +do_test insert5-2.7 {
    1.93 +  uses_temp_table { INSERT INTO b SELECT * FROM v2 }
    1.94 +} {0}
    1.95 +do_test insert5-2.8 {
    1.96 +  uses_temp_table { 
    1.97 +    INSERT INTO b 
    1.98 +    SELECT * FROM main WHERE id > 10 AND max(id1, (SELECT id FROM b)) > 10;
    1.99 +  }
   1.100 +} {1}
   1.101 +
   1.102 +# UPDATE: Using a column from the outer query (main.id) in the GROUP BY
   1.103 +# or ORDER BY of a sub-query is no longer supported.
   1.104 +#
   1.105 +# do_test insert5-2.9 {
   1.106 +#   uses_temp_table { 
   1.107 +#     INSERT INTO b 
   1.108 +#     SELECT * FROM main 
   1.109 +#     WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
   1.110 +#   }
   1.111 +# } {}
   1.112 +do_test insert5-2.9 {
   1.113 +  catchsql { 
   1.114 +    INSERT INTO b 
   1.115 +    SELECT * FROM main 
   1.116 +    WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
   1.117 +  }
   1.118 +} {1 {no such column: main.id}}
   1.119 +
   1.120 +finish_test