os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/alias.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
# 2008 August 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
#
sl@0
    12
# This file implements regression tests for SQLite library.  The
sl@0
    13
# focus of this script is correct code generation of aliased result-set
sl@0
    14
# values.  See ticket #3343.
sl@0
    15
#
sl@0
    16
# $Id: alias.test,v 1.1 2008/08/29 02:14:03 drh Exp $
sl@0
    17
#
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
# A procedure to return a sequence of increasing integers.
sl@0
    22
#
sl@0
    23
namespace eval ::seq {
sl@0
    24
  variable counter 0
sl@0
    25
  proc value {args} {
sl@0
    26
    variable counter
sl@0
    27
    incr counter
sl@0
    28
    return $counter
sl@0
    29
  }
sl@0
    30
  proc reset {} {
sl@0
    31
    variable counter
sl@0
    32
    set counter 0
sl@0
    33
  }
sl@0
    34
}
sl@0
    35
sl@0
    36
sl@0
    37
do_test alias-1.1 {
sl@0
    38
  db function sequence ::seq::value
sl@0
    39
  db eval {
sl@0
    40
    CREATE TABLE t1(x);
sl@0
    41
    INSERT INTO t1 VALUES(9);
sl@0
    42
    INSERT INTO t1 VALUES(8);
sl@0
    43
    INSERT INTO t1 VALUES(7);
sl@0
    44
    SELECT x, sequence() FROM t1;
sl@0
    45
  }
sl@0
    46
} {9 1 8 2 7 3}
sl@0
    47
do_test alias-1.2 {
sl@0
    48
  ::seq::reset
sl@0
    49
  db eval {
sl@0
    50
--pragma vdbe_listing=on; pragma vdbe_trace=on;
sl@0
    51
    SELECT x, sequence() AS y FROM t1 WHERE y>0
sl@0
    52
  }
sl@0
    53
} {9 1 8 2 7 3}
sl@0
    54
do_test alias-1.3 {
sl@0
    55
  ::seq::reset
sl@0
    56
  db eval {
sl@0
    57
    SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99
sl@0
    58
  }
sl@0
    59
} {9 1 8 2 7 3}
sl@0
    60
do_test alias-1.4 {
sl@0
    61
  ::seq::reset
sl@0
    62
  db eval {
sl@0
    63
    SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99 AND y!=55
sl@0
    64
  }
sl@0
    65
} {9 1 8 2 7 3}
sl@0
    66
do_test alias-1.5 {
sl@0
    67
  ::seq::reset
sl@0
    68
  db eval {
sl@0
    69
    SELECT x, sequence() AS y FROM t1
sl@0
    70
     WHERE y>0 AND y<99 AND y!=55 AND y NOT IN (56,57,58)
sl@0
    71
       AND y NOT LIKE 'abc%' AND y%10==2
sl@0
    72
  }
sl@0
    73
} {8 2}
sl@0
    74
do_test alias-1.6 {
sl@0
    75
  ::seq::reset
sl@0
    76
  db eval {
sl@0
    77
    SELECT x, sequence() AS y FROM t1 WHERE y BETWEEN 0 AND 99
sl@0
    78
  }
sl@0
    79
} {9 1 8 2 7 3}
sl@0
    80
do_test alias-1.7 {
sl@0
    81
  ::seq::reset
sl@0
    82
  db eval {
sl@0
    83
    SELECT x, sequence() AS y FROM t1 WHERE y IN (55,66,3)
sl@0
    84
  }
sl@0
    85
} {7 3}
sl@0
    86
do_test alias-1.8 {
sl@0
    87
  ::seq::reset
sl@0
    88
  db eval {
sl@0
    89
    SELECT x, 1-sequence() AS y FROM t1 ORDER BY y
sl@0
    90
  }
sl@0
    91
} {7 -2 8 -1 9 0}
sl@0
    92
do_test alias-1.9 {
sl@0
    93
  ::seq::reset
sl@0
    94
  db eval {
sl@0
    95
    SELECT x, sequence() AS y FROM t1 ORDER BY -y
sl@0
    96
  }
sl@0
    97
} {7 3 8 2 9 1}
sl@0
    98
do_test alias-1.10 {
sl@0
    99
  ::seq::reset
sl@0
   100
  db eval {
sl@0
   101
    SELECT x, sequence() AS y FROM t1 ORDER BY x%2, y
sl@0
   102
  }
sl@0
   103
} {8 2 9 1 7 3}
sl@0
   104
sl@0
   105
unset -nocomplain random_int_list
sl@0
   106
set random_int_list [db eval {
sl@0
   107
   SELECT random()&2147483647 AS r FROM t1, t1, t1, t1 ORDER BY r
sl@0
   108
}]
sl@0
   109
do_test alias-1.11 {
sl@0
   110
  lsort -integer $::random_int_list
sl@0
   111
} $random_int_list
sl@0
   112
sl@0
   113
sl@0
   114
do_test alias-2.1 {
sl@0
   115
  db eval {
sl@0
   116
    SELECT 4 UNION SELECT 1 ORDER BY 1
sl@0
   117
  }
sl@0
   118
} {1 4}
sl@0
   119
do_test alias-2.2 {
sl@0
   120
  db eval {
sl@0
   121
    SELECT 4 UNION SELECT 1 UNION SELECT 9 ORDER BY 1
sl@0
   122
  }
sl@0
   123
} {1 4 9}
sl@0
   124
sl@0
   125
if 0 {
sl@0
   126
  # Aliases in the GROUP BY clause cause the expression to be evaluated
sl@0
   127
  # twice in the current implementation.  This might change in the future.
sl@0
   128
  #
sl@0
   129
  do_test alias-3.1 {
sl@0
   130
    ::seq::reset
sl@0
   131
    db eval {
sl@0
   132
      SELECT sequence(*) AS y, count(*) AS z FROM t1 GROUP BY y ORDER BY z, y
sl@0
   133
    }
sl@0
   134
  } {1 1 2 1 3 1}
sl@0
   135
}
sl@0
   136
sl@0
   137
finish_test