os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/select2.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
# 2001 September 15
sl@0
     2
#
sl@0
     3
# Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.
sl@0
     4
#
sl@0
     5
# The author disclaims copyright to this source code.  In place of
sl@0
     6
# a legal notice, here is a blessing:
sl@0
     7
#
sl@0
     8
#    May you do good and not evil.
sl@0
     9
#    May you find forgiveness for yourself and forgive others.
sl@0
    10
#    May you share freely, never taking more than you give.
sl@0
    11
#
sl@0
    12
#***********************************************************************
sl@0
    13
# This file implements regression tests for SQLite library.  The
sl@0
    14
# focus of this file is testing the SELECT statement.
sl@0
    15
#
sl@0
    16
# $Id: select2.test,v 1.27 2008/07/12 14:52:20 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
# Create a table with some data
sl@0
    22
#
sl@0
    23
execsql {CREATE TABLE tbl1(f1 int, f2 int)}
sl@0
    24
execsql {BEGIN}
sl@0
    25
for {set i 0} {$i<=30} {incr i} {
sl@0
    26
  execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])"
sl@0
    27
}
sl@0
    28
execsql {COMMIT}
sl@0
    29
sl@0
    30
# Do a second query inside a first.
sl@0
    31
#
sl@0
    32
do_test select2-1.1 {
sl@0
    33
  set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
sl@0
    34
  set r {}
sl@0
    35
  catch {unset data}
sl@0
    36
  db eval $sql data {
sl@0
    37
    set f1 $data(f1)
sl@0
    38
    lappend r $f1:
sl@0
    39
    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
sl@0
    40
    db eval $sql2 d2 {
sl@0
    41
      lappend r $d2(f2)
sl@0
    42
    }
sl@0
    43
  }
sl@0
    44
  set r
sl@0
    45
} {0: 0 7 8 9 1: 0 1 8 9 2: 0 1 2 9 3: 0 1 2 3 4: 2 3 4 5: 3 4 5 6: 4 5 6 7: 5 6 7 8: 6 7 8}
sl@0
    46
sl@0
    47
do_test select2-1.2 {
sl@0
    48
  set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
sl@0
    49
  set r {}
sl@0
    50
  db eval $sql data {
sl@0
    51
    set f1 $data(f1)
sl@0
    52
    lappend r $f1:
sl@0
    53
    set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
sl@0
    54
    db eval $sql2 d2 {
sl@0
    55
      lappend r $d2(f2)
sl@0
    56
    }
sl@0
    57
  }
sl@0
    58
  set r
sl@0
    59
} {4: 2 3 4}
sl@0
    60
unset data
sl@0
    61
sl@0
    62
# Create a largish table. Do this twice, once using the TCL cache and once
sl@0
    63
# without.  Compare the performance to make sure things go faster with the
sl@0
    64
# cache turned on.
sl@0
    65
#
sl@0
    66
ifcapable tclvar {
sl@0
    67
  do_test select2-2.0.1 {
sl@0
    68
    set t1 [time {
sl@0
    69
      execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
sl@0
    70
      for {set i 1} {$i<=30000} {incr i} {
sl@0
    71
        set i2 [expr {$i*2}]
sl@0
    72
        set i3 [expr {$i*3}]
sl@0
    73
        db eval {INSERT INTO tbl2 VALUES($i,$i2,$i3)}
sl@0
    74
      }
sl@0
    75
      execsql {COMMIT}
sl@0
    76
    }]
sl@0
    77
    list
sl@0
    78
  } {}
sl@0
    79
  puts "time with cache: $::t1"
sl@0
    80
}
sl@0
    81
catch {execsql {DROP TABLE tbl2}}
sl@0
    82
do_test select2-2.0.2 {
sl@0
    83
  set t2 [time {
sl@0
    84
    execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
sl@0
    85
    for {set i 1} {$i<=30000} {incr i} {
sl@0
    86
      set i2 [expr {$i*2}]
sl@0
    87
      set i3 [expr {$i*3}]
sl@0
    88
      execsql "INSERT INTO tbl2 VALUES($i,$i2,$i3)"
sl@0
    89
    }
sl@0
    90
    execsql {COMMIT}
sl@0
    91
  }]
sl@0
    92
  list
sl@0
    93
} {}
sl@0
    94
puts "time without cache: $t2"
sl@0
    95
#Symbian OS: This is a tclsqlite3.exe test. We do not use the TCL cache.
sl@0
    96
if {$::tcl_platform(platform)!="symbian"} {
sl@0
    97
  ifcapable tclvar {
sl@0
    98
    do_test select2-2.0.3 {
sl@0
    99
      expr {[lindex $t1 0]<[lindex $t2 0]}
sl@0
   100
    } 1
sl@0
   101
  }
sl@0
   102
}
sl@0
   103
sl@0
   104
do_test select2-2.1 {
sl@0
   105
  execsql {SELECT count(*) FROM tbl2}
sl@0
   106
} {30000}
sl@0
   107
do_test select2-2.2 {
sl@0
   108
  execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
sl@0
   109
} {29500}
sl@0
   110
sl@0
   111
do_test select2-3.1 {
sl@0
   112
  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
sl@0
   113
} {500}
sl@0
   114
sl@0
   115
do_test select2-3.2a {
sl@0
   116
  execsql {CREATE INDEX idx1 ON tbl2(f2)}
sl@0
   117
} {}
sl@0
   118
do_test select2-3.2b {
sl@0
   119
  execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
sl@0
   120
} {500}
sl@0
   121
do_test select2-3.2c {
sl@0
   122
  execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
sl@0
   123
} {500}
sl@0
   124
do_test select2-3.2d {
sl@0
   125
  set sqlite_search_count 0
sl@0
   126
  execsql {SELECT * FROM tbl2 WHERE 1000=f2}
sl@0
   127
  set sqlite_search_count
sl@0
   128
} {3}
sl@0
   129
do_test select2-3.2e {
sl@0
   130
  set sqlite_search_count 0
sl@0
   131
  execsql {SELECT * FROM tbl2 WHERE f2=1000}
sl@0
   132
  set sqlite_search_count
sl@0
   133
} {3}
sl@0
   134
sl@0
   135
# Make sure queries run faster with an index than without
sl@0
   136
#
sl@0
   137
do_test select2-3.3 {
sl@0
   138
  execsql {DROP INDEX idx1}
sl@0
   139
  set sqlite_search_count 0
sl@0
   140
  execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
sl@0
   141
  set sqlite_search_count
sl@0
   142
} {29999}
sl@0
   143
sl@0
   144
# Make sure we can optimize functions in the WHERE clause that
sl@0
   145
# use fields from two or more different table.  (Bug #6)
sl@0
   146
#
sl@0
   147
do_test select2-4.1 {
sl@0
   148
  execsql {
sl@0
   149
    CREATE TABLE aa(a);
sl@0
   150
    CREATE TABLE bb(b);
sl@0
   151
    INSERT INTO aa VALUES(1);
sl@0
   152
    INSERT INTO aa VALUES(3);
sl@0
   153
    INSERT INTO bb VALUES(2);
sl@0
   154
    INSERT INTO bb VALUES(4);
sl@0
   155
    SELECT * FROM aa, bb WHERE max(a,b)>2;
sl@0
   156
  }
sl@0
   157
} {1 4 3 2 3 4}
sl@0
   158
do_test select2-4.2 {
sl@0
   159
  execsql {
sl@0
   160
    INSERT INTO bb VALUES(0);
sl@0
   161
    SELECT * FROM aa, bb WHERE b;
sl@0
   162
  }
sl@0
   163
} {1 2 1 4 3 2 3 4}
sl@0
   164
do_test select2-4.3 {
sl@0
   165
  execsql {
sl@0
   166
    SELECT * FROM aa, bb WHERE NOT b;
sl@0
   167
  }
sl@0
   168
} {1 0 3 0}
sl@0
   169
do_test select2-4.4 {
sl@0
   170
  execsql {
sl@0
   171
    SELECT * FROM aa, bb WHERE min(a,b);
sl@0
   172
  }
sl@0
   173
} {1 2 1 4 3 2 3 4}
sl@0
   174
do_test select2-4.5 {
sl@0
   175
  execsql {
sl@0
   176
    SELECT * FROM aa, bb WHERE NOT min(a,b);
sl@0
   177
  }
sl@0
   178
} {1 0 3 0}
sl@0
   179
do_test select2-4.6 {
sl@0
   180
  execsql {
sl@0
   181
    SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
sl@0
   182
  }
sl@0
   183
} {1 2 3 4}
sl@0
   184
do_test select2-4.7 {
sl@0
   185
  execsql {
sl@0
   186
    SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;
sl@0
   187
  }
sl@0
   188
} {1 4 1 0 3 2 3 0}
sl@0
   189
sl@0
   190
finish_test