1# Copyright (c) 1999, 2000 D. Richard Hipp 2# 3# This program is free software; you can redistribute it and/or 4# modify it under the terms of the GNU General Public 5# License as published by the Free Software Foundation; either 6# version 2 of the License, or (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11# General Public License for more details. 12# 13# You should have received a copy of the GNU General Public 14# License along with this library; if not, write to the 15# Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16# Boston, MA 02111-1307, USA. 17# 18# Author contact information: 19# [email protected] 20# http://www.hwaci.com/drh/ 21# 22#*********************************************************************** 23# This file implements regression tests for SQLite library. The 24# focus of this file is testing the INSERT statement that takes is 25# result from a SELECT. 26# 27# $Id: insert2.test,v 1.2 2000/06/07 15:11:27 drh Exp $ 28 29set testdir [file dirname $argv0] 30source $testdir/tester.tcl 31 32# Create some tables with data that we can select against 33# 34do_test insert2-1.0 { 35 execsql {CREATE TABLE d1(n int, log int);} 36 for {set i 1} {$i<=20} {incr i} { 37 for {set j 0} {pow(2,$j)<$i} {incr j} {} 38 execsql "INSERT INTO d1 VALUES($i,$j)" 39 } 40 execsql {SELECT * FROM d1 ORDER BY n} 41} {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5} 42 43# Insert into a new table from the old one. 44# 45do_test insert2-1.1 { 46 execsql { 47 CREATE TABLE t1(log int, cnt int); 48 INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log; 49 } 50 execsql {SELECT * FROM t1 ORDER BY log} 51} {0 1 1 1 2 2 3 4 4 8 5 4} 52do_test insert2-1.2 { 53 catch {execsql {DROP TABLE t1}} 54 execsql { 55 CREATE TABLE t1(log int, cnt int); 56 INSERT INTO t1 57 SELECT log, count(*) FROM d1 GROUP BY log 58 EXCEPT SELECT n-1,log FROM d1; 59 SELECT * FROM t1 ORDER BY log; 60 } 61} {0 1 3 4 4 8 5 4} 62do_test insert2-1.3 { 63 catch {execsql {DROP TABLE t1}} 64 execsql { 65 CREATE TABLE t1(log int, cnt int); 66 INSERT INTO t1 67 SELECT log, count(*) FROM d1 GROUP BY log 68 INTERSECT SELECT n-1,log FROM d1; 69 SELECT * FROM t1 ORDER BY log; 70 } 71} {1 1 2 2} 72do_test insert2-1.4 { 73 catch {execsql {DROP TABLE t1}} 74 set r [execsql { 75 CREATE TABLE t1(log int, cnt int); 76 CREATE INDEX i1 ON t1(log); 77 CREATE INDEX i2 ON t1(cnt); 78 INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log; 79 SELECT * FROM t1 ORDER BY log; 80 }] 81 lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}] 82 lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}] 83} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}} 84 85finish_test 86