os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/between.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
# 2005 July 28
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
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing the use of indices in WHERE clauses
sl@0
    13
# when the WHERE clause contains the BETWEEN operator.
sl@0
    14
#
sl@0
    15
# $Id: between.test,v 1.2 2006/01/17 09:35:02 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
# Build some test data
sl@0
    21
#
sl@0
    22
do_test between-1.0 {
sl@0
    23
  execsql {
sl@0
    24
    BEGIN;
sl@0
    25
    CREATE TABLE t1(w int, x int, y int, z int);
sl@0
    26
  }
sl@0
    27
  for {set i 1} {$i<=100} {incr i} {
sl@0
    28
    set w $i
sl@0
    29
    set x [expr {int(log($i)/log(2))}]
sl@0
    30
    set y [expr {$i*$i + 2*$i + 1}]
sl@0
    31
    set z [expr {$x+$y}]
sl@0
    32
    ifcapable tclvar {
sl@0
    33
      # Random unplanned test of the $varname variable syntax.
sl@0
    34
      execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)}
sl@0
    35
    } else {
sl@0
    36
      # If the $varname syntax is not available, use the regular variable
sl@0
    37
      # declaration syntax.
sl@0
    38
      execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)}
sl@0
    39
    }
sl@0
    40
  }
sl@0
    41
  execsql {
sl@0
    42
    CREATE UNIQUE INDEX i1w ON t1(w);
sl@0
    43
    CREATE INDEX i1xy ON t1(x,y);
sl@0
    44
    CREATE INDEX i1zyx ON t1(z,y,x);
sl@0
    45
    COMMIT;
sl@0
    46
  }
sl@0
    47
} {}
sl@0
    48
sl@0
    49
# This procedure executes the SQL.  Then it appends to the result the
sl@0
    50
# "sort" or "nosort" keyword depending on whether or not any sorting
sl@0
    51
# is done.  Then it appends the ::sqlite_query_plan variable.
sl@0
    52
#
sl@0
    53
proc queryplan {sql} {
sl@0
    54
  set ::sqlite_sort_count 0
sl@0
    55
  set data [execsql $sql]
sl@0
    56
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
sl@0
    57
  lappend data $x
sl@0
    58
  return [concat $data $::sqlite_query_plan]
sl@0
    59
}
sl@0
    60
sl@0
    61
do_test between-1.1.1 {
sl@0
    62
  queryplan {
sl@0
    63
    SELECT * FROM t1 WHERE w BETWEEN 5 AND 6 ORDER BY +w
sl@0
    64
  }
sl@0
    65
} {5 2 36 38 6 2 49 51 sort t1 i1w}
sl@0
    66
do_test between-1.1.2 {
sl@0
    67
  queryplan {
sl@0
    68
    SELECT * FROM t1 WHERE +w BETWEEN 5 AND 6 ORDER BY +w
sl@0
    69
  }
sl@0
    70
} {5 2 36 38 6 2 49 51 sort t1 {}}
sl@0
    71
do_test between-1.2.1 {
sl@0
    72
  queryplan {
sl@0
    73
    SELECT * FROM t1 WHERE w BETWEEN 5 AND 65-y ORDER BY +w
sl@0
    74
  }
sl@0
    75
} {5 2 36 38 6 2 49 51 sort t1 i1w}
sl@0
    76
do_test between-1.2.2 {
sl@0
    77
  queryplan {
sl@0
    78
    SELECT * FROM t1 WHERE +w BETWEEN 5 AND 65-y ORDER BY +w
sl@0
    79
  }
sl@0
    80
} {5 2 36 38 6 2 49 51 sort t1 {}}
sl@0
    81
do_test between-1.3.1 {
sl@0
    82
  queryplan {
sl@0
    83
    SELECT * FROM t1 WHERE w BETWEEN 41-y AND 6 ORDER BY +w
sl@0
    84
  }
sl@0
    85
} {5 2 36 38 6 2 49 51 sort t1 i1w}
sl@0
    86
do_test between-1.3.2 {
sl@0
    87
  queryplan {
sl@0
    88
    SELECT * FROM t1 WHERE +w BETWEEN 41-y AND 6 ORDER BY +w
sl@0
    89
  }
sl@0
    90
} {5 2 36 38 6 2 49 51 sort t1 {}}
sl@0
    91
do_test between-1.4 {
sl@0
    92
  queryplan {
sl@0
    93
    SELECT * FROM t1 WHERE w BETWEEN 41-y AND 65-y ORDER BY +w
sl@0
    94
  }
sl@0
    95
} {5 2 36 38 6 2 49 51 sort t1 {}}
sl@0
    96
do_test between-1.5.1 {
sl@0
    97
  queryplan {
sl@0
    98
    SELECT * FROM t1 WHERE 26 BETWEEN y AND z ORDER BY +w
sl@0
    99
  }
sl@0
   100
} {4 2 25 27 sort t1 i1zyx}
sl@0
   101
do_test between-1.5.2 {
sl@0
   102
  queryplan {
sl@0
   103
    SELECT * FROM t1 WHERE 26 BETWEEN +y AND z ORDER BY +w
sl@0
   104
  }
sl@0
   105
} {4 2 25 27 sort t1 i1zyx}
sl@0
   106
do_test between-1.5.3 {
sl@0
   107
  queryplan {
sl@0
   108
    SELECT * FROM t1 WHERE 26 BETWEEN y AND +z ORDER BY +w
sl@0
   109
  }
sl@0
   110
} {4 2 25 27 sort t1 {}}
sl@0
   111
sl@0
   112
sl@0
   113
finish_test