os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/select7.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
# The author disclaims copyright to this source code.  In place of
sl@0
     2
# a legal notice, here is a blessing:
sl@0
     3
#
sl@0
     4
#    May you do good and not evil.
sl@0
     5
#    May you find forgiveness for yourself and forgive others.
sl@0
     6
#    May you share freely, never taking more than you give.
sl@0
     7
#
sl@0
     8
#***********************************************************************
sl@0
     9
# This file implements regression tests for SQLite library.  The
sl@0
    10
# focus of this file is testing compute SELECT statements and nested
sl@0
    11
# views.
sl@0
    12
#
sl@0
    13
# $Id: select7.test,v 1.11 2007/09/12 17:01:45 danielk1977 Exp $
sl@0
    14
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
ifcapable compound {
sl@0
    20
sl@0
    21
# A 3-way INTERSECT.  Ticket #875
sl@0
    22
ifcapable tempdb {
sl@0
    23
  do_test select7-1.1 {
sl@0
    24
    execsql {
sl@0
    25
      create temp table t1(x);
sl@0
    26
      insert into t1 values('amx');
sl@0
    27
      insert into t1 values('anx');
sl@0
    28
      insert into t1 values('amy');
sl@0
    29
      insert into t1 values('bmy');
sl@0
    30
      select * from t1 where x like 'a__'
sl@0
    31
        intersect select * from t1 where x like '_m_'
sl@0
    32
        intersect select * from t1 where x like '__x';
sl@0
    33
    }
sl@0
    34
  } {amx}
sl@0
    35
}
sl@0
    36
sl@0
    37
sl@0
    38
# Nested views do not handle * properly.  Ticket #826.
sl@0
    39
#
sl@0
    40
ifcapable view {
sl@0
    41
do_test select7-2.1 {
sl@0
    42
  execsql {
sl@0
    43
    CREATE TABLE x(id integer primary key, a TEXT NULL);
sl@0
    44
    INSERT INTO x (a) VALUES ('first');
sl@0
    45
    CREATE TABLE tempx(id integer primary key, a TEXT NULL);
sl@0
    46
    INSERT INTO tempx (a) VALUES ('t-first');
sl@0
    47
    CREATE VIEW tv1 AS SELECT x.id, tx.id FROM x JOIN tempx tx ON tx.id=x.id;
sl@0
    48
    CREATE VIEW tv1b AS SELECT x.id, tx.id FROM x JOIN tempx tx on tx.id=x.id;
sl@0
    49
    CREATE VIEW tv2 AS SELECT * FROM tv1 UNION SELECT * FROM tv1b;
sl@0
    50
    SELECT * FROM tv2;
sl@0
    51
  }
sl@0
    52
} {1 1}
sl@0
    53
} ;# ifcapable view
sl@0
    54
sl@0
    55
} ;# ifcapable compound
sl@0
    56
sl@0
    57
# Do not allow GROUP BY without an aggregate. Ticket #1039.
sl@0
    58
#
sl@0
    59
# Change: force any query with a GROUP BY clause to be processed as
sl@0
    60
# an aggregate query, whether it contains aggregates or not.
sl@0
    61
#
sl@0
    62
ifcapable subquery {
sl@0
    63
  # do_test select7-3.1 {
sl@0
    64
  #   catchsql {
sl@0
    65
  #     SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
sl@0
    66
  #   }
sl@0
    67
  # } {1 {GROUP BY may only be used on aggregate queries}}
sl@0
    68
  do_test select7-3.1 {
sl@0
    69
    catchsql {
sl@0
    70
      SELECT * FROM (SELECT * FROM sqlite_master) GROUP BY name
sl@0
    71
    }
sl@0
    72
  } [list 0 [execsql {SELECT * FROM sqlite_master ORDER BY name}]]
sl@0
    73
}
sl@0
    74
sl@0
    75
# Ticket #2018 - Make sure names are resolved correctly on all
sl@0
    76
# SELECT statements of a compound subquery.
sl@0
    77
#
sl@0
    78
ifcapable {subquery && compound} {
sl@0
    79
  do_test select7-4.1 {
sl@0
    80
    execsql {
sl@0
    81
      CREATE TABLE IF NOT EXISTS photo(pk integer primary key, x);
sl@0
    82
      CREATE TABLE IF NOT EXISTS tag(pk integer primary key, fk int, name);
sl@0
    83
    
sl@0
    84
      SELECT P.pk from PHOTO P WHERE NOT EXISTS ( 
sl@0
    85
           SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk 
sl@0
    86
           EXCEPT 
sl@0
    87
           SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
sl@0
    88
      );
sl@0
    89
    }
sl@0
    90
  } {}
sl@0
    91
  do_test select7-4.2 {
sl@0
    92
    execsql {
sl@0
    93
      INSERT INTO photo VALUES(1,1);
sl@0
    94
      INSERT INTO photo VALUES(2,2);
sl@0
    95
      INSERT INTO photo VALUES(3,3);
sl@0
    96
      INSERT INTO tag VALUES(11,1,'one');
sl@0
    97
      INSERT INTO tag VALUES(12,1,'two');
sl@0
    98
      INSERT INTO tag VALUES(21,1,'one-b');
sl@0
    99
      SELECT P.pk from PHOTO P WHERE NOT EXISTS ( 
sl@0
   100
           SELECT T2.pk from TAG T2 WHERE T2.fk = P.pk 
sl@0
   101
           EXCEPT 
sl@0
   102
           SELECT T3.pk from TAG T3 WHERE T3.fk = P.pk AND T3.name LIKE '%foo%'
sl@0
   103
      );
sl@0
   104
    }
sl@0
   105
  } {2 3}
sl@0
   106
}
sl@0
   107
sl@0
   108
# ticket #2347
sl@0
   109
#
sl@0
   110
ifcapable {subquery && compound} {
sl@0
   111
  do_test select7-5.1 {
sl@0
   112
    catchsql {
sl@0
   113
      CREATE TABLE t2(a,b);
sl@0
   114
      SELECT 5 IN (SELECT a,b FROM t2);
sl@0
   115
    }
sl@0
   116
  } [list 1 \
sl@0
   117
     {only a single result allowed for a SELECT that is part of an expression}]
sl@0
   118
  do_test select7-5.2 {
sl@0
   119
    catchsql {
sl@0
   120
      SELECT 5 IN (SELECT * FROM t2);
sl@0
   121
    }
sl@0
   122
  } [list 1 \
sl@0
   123
     {only a single result allowed for a SELECT that is part of an expression}]
sl@0
   124
  do_test select7-5.3 {
sl@0
   125
    catchsql {
sl@0
   126
      SELECT 5 IN (SELECT a,b FROM t2 UNION SELECT b,a FROM t2);
sl@0
   127
    }
sl@0
   128
  } [list 1 \
sl@0
   129
     {only a single result allowed for a SELECT that is part of an expression}]
sl@0
   130
  do_test select7-5.4 {
sl@0
   131
    catchsql {
sl@0
   132
      SELECT 5 IN (SELECT * FROM t2 UNION SELECT * FROM t2);
sl@0
   133
    }
sl@0
   134
  } [list 1 \
sl@0
   135
     {only a single result allowed for a SELECT that is part of an expression}]
sl@0
   136
}
sl@0
   137
sl@0
   138
# Verify that an error occurs if you have too many terms on a
sl@0
   139
# compound select statement.
sl@0
   140
#
sl@0
   141
ifcapable compound {
sl@0
   142
  if {$SQLITE_MAX_COMPOUND_SELECT>0} {
sl@0
   143
    set sql {SELECT 0}
sl@0
   144
    set result 0
sl@0
   145
    for {set i 1} {$i<$SQLITE_MAX_COMPOUND_SELECT} {incr i} {
sl@0
   146
      append sql " UNION ALL SELECT $i"
sl@0
   147
      lappend result $i
sl@0
   148
    }
sl@0
   149
    do_test select7-6.1 {
sl@0
   150
      catchsql $sql
sl@0
   151
    } [list 0 $result]
sl@0
   152
    append sql { UNION ALL SELECT 99999999}
sl@0
   153
    do_test select7-6.2 {
sl@0
   154
      catchsql $sql
sl@0
   155
    } {1 {too many terms in compound SELECT}}
sl@0
   156
  }
sl@0
   157
}
sl@0
   158
sl@0
   159
finish_test