/ Hex Artifact Content
Login

Artifact 519f360d15b8009845e7957789801807346286ff:


0000: 23 20 32 30 30 36 20 4a 75 6e 65 20 31 30 0a 23  # 2006 June 10.#
0010: 0a 23 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  .# The author di
0020: 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
0030: 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
0040: 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
0050: 20 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20 6e 6f   of.# a legal no
0060: 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
0070: 62 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20 20 20  blessing:.#.#   
0080: 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
0090: 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 23   and not evil..#
00a0: 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
00b0: 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
00c0: 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
00d0: 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 23 20  rgive others..# 
00e0: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
00f0: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
0100: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
0110: 79 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a 2a 2a  you give..#.#***
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 2a 0a 23 20 54 68 69 73 20 66 69 6c 65  ****.# This file
0170: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 72 65 67 72   implements regr
0180: 65 73 73 69 6f 6e 20 74 65 73 74 73 20 66 6f 72  ession tests for
0190: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
01a0: 20 20 54 68 65 0a 23 20 66 6f 63 75 73 20 6f 66    The.# focus of
01b0: 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 63 72   this file is cr
01c0: 65 61 74 69 6e 67 20 61 6e 64 20 64 72 6f 70 70  eating and dropp
01d0: 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ing virtual tabl
01e0: 65 73 2e 0a 23 0a 23 20 24 49 64 3a 20 76 74 61  es..#.# $Id: vta
01f0: 62 31 2e 74 65 73 74 2c 76 20 31 2e 31 37 20 32  b1.test,v 1.17 2
0200: 30 30 36 2f 30 36 2f 31 35 20 31 30 3a 34 31 3a  006/06/15 10:41:
0210: 31 36 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  16 danielk1977 E
0220: 78 70 20 24 0a 0a 73 65 74 20 74 65 73 74 64 69  xp $..set testdi
0230: 72 20 5b 66 69 6c 65 20 64 69 72 6e 61 6d 65 20  r [file dirname 
0240: 24 61 72 67 76 30 5d 0a 73 6f 75 72 63 65 20 24  $argv0].source $
0250: 74 65 73 74 64 69 72 2f 74 65 73 74 65 72 2e 74  testdir/tester.t
0260: 63 6c 0a 0a 69 66 63 61 70 61 62 6c 65 20 21 76  cl..ifcapable !v
0270: 74 61 62 20 7b 0a 20 20 66 69 6e 69 73 68 5f 74  tab {.  finish_t
0280: 65 73 74 0a 20 20 72 65 74 75 72 6e 0a 7d 0a 0a  est.  return.}..
0290: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
02a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02d0: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 4f 72 67 61 6e 69  -------.# Organi
02e0: 7a 61 74 69 6f 6e 20 6f 66 20 74 65 73 74 73 20  zation of tests 
02f0: 69 6e 20 74 68 69 73 20 66 69 6c 65 3a 0a 23 0a  in this file:.#.
0300: 23 20 76 74 61 62 31 2d 31 2e 2a 3a 20 45 72 72  # vtab1-1.*: Err
0310: 6f 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e  or conditions an
0320: 64 20 6f 74 68 65 72 20 69 73 73 75 65 73 20 73  d other issues s
0330: 75 72 72 6f 75 6e 64 69 6e 67 20 63 72 65 61 74  urrounding creat
0340: 69 6f 6e 2f 63 6f 6e 6e 65 63 74 69 6f 6e 0a 23  ion/connection.#
0350: 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 61              of a
0360: 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 2e   virtual module.
0370: 0a 23 20 76 74 61 62 31 2d 32 2e 2a 3a 20 54 65  .# vtab1-2.*: Te
0380: 73 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  st sqlite3_decla
0390: 72 65 5f 76 74 61 62 28 29 20 61 6e 64 20 74 68  re_vtab() and th
03a0: 65 20 78 43 6f 6e 6e 65 63 74 2f 78 44 69 73 63  e xConnect/xDisc
03b0: 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 73 2e 0a  onnect methods..
03c0: 23 20 76 74 61 62 31 2d 33 2e 2a 3a 20 54 61 62  # vtab1-3.*: Tab
03d0: 6c 65 20 73 63 61 6e 73 20 61 6e 64 20 57 48 45  le scans and WHE
03e0: 52 45 20 63 6c 61 75 73 65 73 2e 0a 23 20 76 74  RE clauses..# vt
03f0: 61 62 31 2d 34 2e 2a 3a 20 54 61 62 6c 65 20 73  ab1-4.*: Table s
0400: 63 61 6e 73 20 61 6e 64 20 4f 52 44 45 52 20 42  cans and ORDER B
0410: 59 20 63 6c 61 75 73 65 73 2e 0a 23 20 76 74 61  Y clauses..# vta
0420: 62 31 2d 35 2e 2a 3a 20 54 65 73 74 20 71 75 65  b1-5.*: Test que
0430: 72 69 65 73 20 74 68 61 74 20 69 6e 63 6c 75 64  ries that includ
0440: 65 20 6a 6f 69 6e 73 2e 20 54 68 69 73 20 62 72  e joins. This br
0450: 69 6e 67 73 20 74 68 65 0a 23 20 20 20 20 20 20  ings the.#      
0460: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
0470: 64 65 78 5f 69 6e 66 6f 2e 65 73 74 69 6d 61 74  dex_info.estimat
0480: 65 64 43 6f 73 74 20 76 61 72 69 61 62 6c 65 20  edCost variable 
0490: 69 6e 74 6f 20 70 6c 61 79 2e 0a 23 0a 23 20 54  into play..#.# T
04a0: 68 69 73 20 66 69 6c 65 20 75 73 65 73 20 74 68  his file uses th
04b0: 65 20 22 65 63 68 6f 22 20 6d 6f 64 75 6c 65 20  e "echo" module 
04c0: 28 73 65 65 20 73 72 63 2f 74 65 73 74 38 2e 63  (see src/test8.c
04d0: 29 2e 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d  ). Refer to comm
04e0: 65 6e 74 73 0a 23 20 69 6e 20 74 68 61 74 20 66  ents.# in that f
04f0: 69 6c 65 20 66 6f 72 20 74 68 65 20 73 70 65 63  ile for the spec
0500: 69 61 6c 20 62 65 68 61 76 69 6f 75 72 20 6f 66  ial behaviour of
0510: 20 74 68 65 20 54 63 6c 20 24 65 63 68 6f 5f 6d   the Tcl $echo_m
0520: 6f 64 75 6c 65 20 76 61 72 69 61 62 6c 65 2e 0a  odule variable..
0530: 23 0a 23 20 54 4f 44 4f 3a 20 0a 23 20 20 20 2a  #.# TODO: .#   *
0540: 20 48 6f 77 20 74 6f 20 74 65 73 74 20 74 68 65   How to test the
0550: 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
0560: 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 2e  onstraint_usage.
0570: 6f 6d 69 74 20 66 69 65 6c 64 3f 0a 23 20 20 20  omit field?.#   
0580: 2a 20 76 74 61 62 31 2d 35 2e 2a 0a 23 20 20 20  * vtab1-5.*.#   
0590: 2a 20 0a 23 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  * .#...#--------
05a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
05b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
05c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
05d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
05e0: 20 54 65 73 74 20 63 61 73 65 73 20 76 74 61 62   Test cases vtab
05f0: 31 2e 31 2e 2a 0a 23 0a 0a 23 20 57 65 20 63 61  1.1.*.#..# We ca
0600: 6e 6e 6f 74 20 63 72 65 61 74 65 20 61 20 76 69  nnot create a vi
0610: 72 74 75 61 6c 20 74 61 62 6c 65 20 69 66 20 74  rtual table if t
0620: 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e 6f  he module has no
0630: 74 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  t been registere
0640: 64 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 76 74 61  d..#.do_test vta
0650: 62 31 2d 31 2e 31 20 7b 0a 20 20 63 61 74 63 68  b1-1.1 {.  catch
0660: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
0670: 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74   VIRTUAL TABLE t
0680: 31 20 55 53 49 4e 47 20 65 63 68 6f 3b 0a 20 20  1 USING echo;.  
0690: 7d 0a 7d 20 7b 31 20 7b 6e 6f 20 73 75 63 68 20  }.} {1 {no such 
06a0: 6d 6f 64 75 6c 65 3a 20 65 63 68 6f 7d 7d 0a 64  module: echo}}.d
06b0: 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 31 2e 32  o_test vtab1-1.2
06c0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
06d0: 20 20 20 53 45 4c 45 43 54 20 6e 61 6d 65 20 46     SELECT name F
06e0: 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  ROM sqlite_maste
06f0: 72 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20 7d  r ORDER BY 1.  }
0700: 0a 7d 20 7b 7d 0a 0a 23 20 52 65 67 69 73 74 65  .} {}..# Registe
0710: 72 20 74 68 65 20 6d 6f 64 75 6c 65 0a 72 65 67  r the module.reg
0720: 69 73 74 65 72 5f 65 63 68 6f 5f 6d 6f 64 75 6c  ister_echo_modul
0730: 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 6e 65  e [sqlite3_conne
0740: 63 74 69 6f 6e 5f 70 6f 69 6e 74 65 72 20 64 62  ction_pointer db
0750: 5d 0a 0a 23 20 4f 6e 63 65 20 61 20 6d 6f 64 75  ]..# Once a modu
0760: 6c 65 20 68 61 73 20 62 65 65 6e 20 72 65 67 69  le has been regi
0770: 73 74 65 72 65 64 2c 20 76 69 72 74 75 61 6c 20  stered, virtual 
0780: 74 61 62 6c 65 73 20 75 73 69 6e 67 20 74 68 61  tables using tha
0790: 74 20 6d 6f 64 75 6c 65 0a 23 20 6d 61 79 20 62  t module.# may b
07a0: 65 20 63 72 65 61 74 65 64 2e 20 48 6f 77 65 76  e created. Howev
07b0: 65 72 20 69 66 20 61 20 6d 6f 64 75 6c 65 20 78  er if a module x
07c0: 43 72 65 61 74 65 28 29 20 66 61 69 6c 73 20 74  Create() fails t
07d0: 6f 20 63 61 6c 6c 0a 23 20 73 71 6c 69 74 65 33  o call.# sqlite3
07e0: 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20  _declare_vtab() 
07f0: 61 6e 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65  an error will be
0800: 20 72 61 69 73 65 64 20 61 6e 64 20 74 68 65 20   raised and the 
0810: 74 61 62 6c 65 20 6e 6f 74 20 63 72 65 61 74 65  table not create
0820: 64 2e 0a 23 0a 23 20 54 68 65 20 22 65 63 68 6f  d..#.# The "echo
0830: 22 20 6d 6f 64 75 6c 65 20 64 6f 65 73 20 6e 6f  " module does no
0840: 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  t invoke sqlite3
0850: 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20  _declare_vtab() 
0860: 69 66 20 69 74 20 69 73 0a 23 20 70 61 73 73 65  if it is.# passe
0870: 64 20 7a 65 72 6f 20 61 72 67 75 6d 65 6e 74 73  d zero arguments
0880: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 76 74 61 62  ..#.do_test vtab
0890: 31 2d 31 2e 33 20 7b 0a 20 20 63 61 74 63 68 73  1-1.3 {.  catchs
08a0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
08b0: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31  VIRTUAL TABLE t1
08c0: 20 55 53 49 4e 47 20 65 63 68 6f 3b 0a 20 20 7d   USING echo;.  }
08d0: 0a 7d 20 7b 31 20 7b 76 74 61 62 6c 65 20 63 6f  .} {1 {vtable co
08e0: 6e 73 74 72 75 63 74 6f 72 20 64 69 64 20 6e 6f  nstructor did no
08f0: 74 20 64 65 63 6c 61 72 65 20 73 63 68 65 6d 61  t declare schema
0900: 3a 20 74 31 7d 7d 0a 64 6f 5f 74 65 73 74 20 76  : t1}}.do_test v
0910: 74 61 62 31 2d 31 2e 34 20 7b 0a 20 20 65 78 65  tab1-1.4 {.  exe
0920: 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43  csql {.    SELEC
0930: 54 20 6e 61 6d 65 20 46 52 4f 4d 20 73 71 6c 69  T name FROM sqli
0940: 74 65 5f 6d 61 73 74 65 72 20 4f 52 44 45 52 20  te_master ORDER 
0950: 42 59 20 31 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23  BY 1.  }.} {}..#
0960: 20 54 68 65 20 22 65 63 68 6f 22 20 6d 6f 64 75   The "echo" modu
0970: 6c 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f  le xCreate metho
0980: 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  d returns an err
0990: 6f 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  or and does not 
09a0: 63 72 65 61 74 65 0a 23 20 74 68 65 20 76 69 72  create.# the vir
09b0: 74 75 61 6c 20 74 61 62 6c 65 20 69 66 20 69 74  tual table if it
09c0: 20 69 73 20 70 61 73 73 65 64 20 61 6e 20 61 72   is passed an ar
09d0: 67 75 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73  gument that does
09e0: 20 6e 6f 74 20 63 6f 72 72 65 73 70 6f 6e 64 0a   not correspond.
09f0: 23 20 74 6f 20 61 6e 20 65 78 69 73 74 69 6e 67  # to an existing
0a00: 20 72 65 61 6c 20 74 61 62 6c 65 20 69 6e 20 74   real table in t
0a10: 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
0a20: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 76 74 61 62  ..#.do_test vtab
0a30: 31 2d 31 2e 35 20 7b 0a 20 20 63 61 74 63 68 73  1-1.5 {.  catchs
0a40: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
0a50: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31  VIRTUAL TABLE t1
0a60: 20 55 53 49 4e 47 20 65 63 68 6f 28 6e 6f 5f 73   USING echo(no_s
0a70: 75 63 68 5f 74 61 62 6c 65 29 3b 0a 20 20 7d 0a  uch_table);.  }.
0a80: 7d 20 7b 31 20 7b 76 74 61 62 6c 65 20 63 6f 6e  } {1 {vtable con
0a90: 73 74 72 75 63 74 6f 72 20 66 61 69 6c 65 64 3a  structor failed:
0aa0: 20 74 31 7d 7d 0a 64 6f 5f 74 65 73 74 20 76 74   t1}}.do_test vt
0ab0: 61 62 31 2d 31 2e 36 20 7b 0a 20 20 65 78 65 63  ab1-1.6 {.  exec
0ac0: 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54  sql {.    SELECT
0ad0: 20 6e 61 6d 65 20 46 52 4f 4d 20 73 71 6c 69 74   name FROM sqlit
0ae0: 65 5f 6d 61 73 74 65 72 20 4f 52 44 45 52 20 42  e_master ORDER B
0af0: 59 20 31 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 20  Y 1.  }.} {}..# 
0b00: 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72  Test to make sur
0b10: 65 20 6e 6f 74 68 69 6e 67 20 67 6f 65 73 20 77  e nothing goes w
0b20: 72 6f 6e 67 20 61 6e 64 20 6e 6f 20 6d 65 6d 6f  rong and no memo
0b30: 72 79 20 69 73 20 6c 65 61 6b 65 64 20 69 66 20  ry is leaked if 
0b40: 77 65 20 0a 23 20 73 65 6c 65 63 74 20 61 6e 20  we .# select an 
0b50: 69 6c 6c 65 67 61 6c 20 74 61 62 6c 65 2d 6e 61  illegal table-na
0b60: 6d 65 20 28 69 2e 65 20 61 20 72 65 73 65 72 76  me (i.e a reserv
0b70: 65 64 20 6e 61 6d 65 20 6f 72 20 74 68 65 20 6e  ed name or the n
0b80: 61 6d 65 20 6f 66 20 61 0a 23 20 74 61 62 6c 65  ame of a.# table
0b90: 20 74 68 61 74 20 61 6c 72 65 61 64 79 20 65 78   that already ex
0ba0: 69 73 74 73 29 2e 0a 23 0a 64 6f 5f 74 65 73 74  ists)..#.do_test
0bb0: 20 76 74 61 62 31 2d 31 2e 37 20 7b 0a 20 20 63   vtab1-1.7 {.  c
0bc0: 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 43 52  atchsql {.    CR
0bd0: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
0be0: 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  LE sqlite_master
0bf0: 20 55 53 49 4e 47 20 65 63 68 6f 3b 0a 20 20 7d   USING echo;.  }
0c00: 0a 7d 20 7b 31 20 7b 6f 62 6a 65 63 74 20 6e 61  .} {1 {object na
0c10: 6d 65 20 72 65 73 65 72 76 65 64 20 66 6f 72 20  me reserved for 
0c20: 69 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20 73 71  internal use: sq
0c30: 6c 69 74 65 5f 6d 61 73 74 65 72 7d 7d 0a 64 6f  lite_master}}.do
0c40: 5f 74 65 73 74 20 76 74 61 62 31 2d 31 2e 38 20  _test vtab1-1.8 
0c50: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a 20  {.  catchsql {. 
0c60: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
0c70: 74 72 65 61 6c 28 61 2c 20 62 2c 20 63 29 3b 0a  treal(a, b, c);.
0c80: 20 20 20 20 43 52 45 41 54 45 20 56 49 52 54 55      CREATE VIRTU
0c90: 41 4c 20 54 41 42 4c 45 20 74 72 65 61 6c 20 55  AL TABLE treal U
0ca0: 53 49 4e 47 20 65 63 68 6f 28 74 72 65 61 6c 29  SING echo(treal)
0cb0: 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 74 61 62 6c  ;.  }.} {1 {tabl
0cc0: 65 20 74 72 65 61 6c 20 61 6c 72 65 61 64 79 20  e treal already 
0cd0: 65 78 69 73 74 73 7d 7d 0a 64 6f 5f 74 65 73 74  exists}}.do_test
0ce0: 20 76 74 61 62 31 2d 31 2e 39 20 7b 0a 20 20 65   vtab1-1.9 {.  e
0cf0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 52 4f  xecsql {.    DRO
0d00: 50 20 54 41 42 4c 45 20 74 72 65 61 6c 3b 0a 20  P TABLE treal;. 
0d10: 20 20 20 53 45 4c 45 43 54 20 6e 61 6d 65 20 46     SELECT name F
0d20: 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  ROM sqlite_maste
0d30: 72 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20 7d  r ORDER BY 1.  }
0d40: 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  .} {}..#--------
0d50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
0d90: 20 54 65 73 74 20 63 61 73 65 73 20 76 74 61 62   Test cases vtab
0da0: 31 2e 32 2e 2a 0a 23 0a 23 20 41 74 20 74 68 69  1.2.*.#.# At thi
0db0: 73 20 70 6f 69 6e 74 2c 20 74 68 65 20 64 61 74  s point, the dat
0dc0: 61 62 61 73 65 20 69 73 20 63 6f 6d 70 6c 65 74  abase is complet
0dd0: 65 6c 79 20 65 6d 70 74 79 2e 20 54 68 65 20 65  ely empty. The e
0de0: 63 68 6f 20 6d 6f 64 75 6c 65 0a 23 20 68 61 73  cho module.# has
0df0: 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65   already been re
0e00: 67 69 73 74 65 72 65 64 2e 0a 0a 23 20 49 66 20  gistered...# If 
0e10: 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  a single argumen
0e20: 74 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74  t is passed to t
0e30: 68 65 20 65 63 68 6f 20 6d 6f 64 75 6c 65 20 64  he echo module d
0e40: 75 72 69 6e 67 20 74 61 62 6c 65 0a 23 20 63 72  uring table.# cr
0e50: 65 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 61 73  eation, it is as
0e60: 73 75 6d 65 64 20 74 6f 20 62 65 20 74 68 65 20  sumed to be the 
0e70: 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20  name of a table 
0e80: 69 6e 20 74 68 65 20 73 61 6d 65 0a 23 20 64 61  in the same.# da
0e90: 74 61 62 61 73 65 2e 20 54 68 65 20 65 63 68 6f  tabase. The echo
0ea0: 20 6d 6f 64 75 6c 65 20 61 74 74 65 6d 70 74 73   module attempts
0eb0: 20 74 6f 20 73 65 74 20 74 68 65 20 73 63 68 65   to set the sche
0ec0: 6d 61 20 6f 66 20 74 68 65 0a 23 20 6e 65 77 20  ma of the.# new 
0ed0: 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f  virtual table to
0ee0: 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
0ef0: 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61 74  the existing dat
0f00: 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 23 0a 64  abase table..#.d
0f10: 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 32 2e 31  o_test vtab1-2.1
0f20: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
0f30: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
0f40: 74 65 6d 70 6c 61 74 65 28 61 2c 20 62 2c 20 63  template(a, b, c
0f50: 29 3b 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c  );.  }.  execsql
0f60: 20 7b 20 50 52 41 47 4d 41 20 74 61 62 6c 65 5f   { PRAGMA table_
0f70: 69 6e 66 6f 28 74 65 6d 70 6c 61 74 65 29 3b 20  info(template); 
0f80: 7d 0a 7d 20 5b 6c 69 73 74 20 20 20 20 20 20 20  }.} [list       
0f90: 20 20 5c 0a 20 20 30 20 61 20 7b 7d 20 30 20 7b    \.  0 a {} 0 {
0fa0: 7d 20 30 20 5c 0a 20 20 31 20 62 20 7b 7d 20 30  } 0 \.  1 b {} 0
0fb0: 20 7b 7d 20 30 20 5c 0a 20 20 32 20 63 20 7b 7d   {} 0 \.  2 c {}
0fc0: 20 30 20 7b 7d 20 30 20 5c 0a 5d 0a 64 6f 5f 74   0 {} 0 \.].do_t
0fd0: 65 73 74 20 76 74 61 62 31 2d 32 2e 32 20 7b 0a  est vtab1-2.2 {.
0fe0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
0ff0: 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
1000: 41 42 4c 45 20 74 31 20 55 53 49 4e 47 20 65 63  ABLE t1 USING ec
1010: 68 6f 28 74 65 6d 70 6c 61 74 65 29 3b 0a 20 20  ho(template);.  
1020: 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52  }.  execsql { PR
1030: 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e 66 6f 28  AGMA table_info(
1040: 74 31 29 3b 20 7d 0a 7d 20 5b 6c 69 73 74 20 20  t1); }.} [list  
1050: 20 20 20 20 20 20 20 5c 0a 20 20 30 20 61 20 7b         \.  0 a {
1060: 7d 20 30 20 7b 7d 20 30 20 5c 0a 20 20 31 20 62  } 0 {} 0 \.  1 b
1070: 20 7b 7d 20 30 20 7b 7d 20 30 20 5c 0a 20 20 32   {} 0 {} 0 \.  2
1080: 20 63 20 7b 7d 20 30 20 7b 7d 20 30 20 5c 0a 5d   c {} 0 {} 0 \.]
1090: 0a 0a 23 20 54 65 73 74 20 74 68 61 74 20 74 68  ..# Test that th
10a0: 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62  e database can b
10b0: 65 20 75 6e 6c 6f 61 64 65 64 2e 20 54 68 69 73  e unloaded. This
10c0: 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 74   should invoke t
10d0: 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29  he xDisconnect()
10e0: 0a 23 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  .# callback for 
10f0: 74 68 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79  the successfully
1100: 20 63 72 65 61 74 65 20 76 69 72 74 75 61 6c 20   create virtual 
1110: 74 61 62 6c 65 20 28 74 31 29 2e 0a 23 0a 64 6f  table (t1)..#.do
1120: 5f 74 65 73 74 20 76 74 61 62 31 2d 32 2e 33 20  _test vtab1-2.3 
1130: 7b 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f 64  {.  set echo_mod
1140: 75 6c 65 20 5b 6c 69 73 74 5d 0a 20 20 64 62 20  ule [list].  db 
1150: 63 6c 6f 73 65 0a 20 20 73 65 74 20 65 63 68 6f  close.  set echo
1160: 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c 69 73 74 20  _module.} [list 
1170: 78 44 69 73 63 6f 6e 6e 65 63 74 5d 0a 0a 23 20  xDisconnect]..# 
1180: 52 65 2d 6f 70 65 6e 20 74 68 65 20 64 61 74 61  Re-open the data
1190: 62 61 73 65 2e 20 54 68 69 73 20 73 68 6f 75 6c  base. This shoul
11a0: 64 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20  d not cause any 
11b0: 76 69 72 74 75 61 6c 20 6d 65 74 68 6f 64 73 20  virtual methods 
11c0: 74 6f 20 0a 23 20 62 65 20 63 61 6c 6c 65 64 2e  to .# be called.
11d0: 20 54 68 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20   The invocation 
11e0: 6f 66 20 78 43 6f 6e 6e 65 63 74 28 29 20 69 73  of xConnect() is
11f0: 20 64 65 6c 61 79 65 64 20 75 6e 74 69 6c 20 74   delayed until t
1200: 68 65 20 76 69 72 74 75 61 6c 0a 23 20 74 61 62  he virtual.# tab
1210: 6c 65 20 73 63 68 65 6d 61 20 69 73 20 66 69 72  le schema is fir
1220: 73 74 20 72 65 71 75 69 72 65 64 20 62 79 20 74  st required by t
1230: 68 65 20 63 6f 6d 70 69 6c 65 72 2e 0a 23 0a 64  he compiler..#.d
1240: 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 32 2e 34  o_test vtab1-2.4
1250: 20 7b 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f   {.  set echo_mo
1260: 64 75 6c 65 20 5b 6c 69 73 74 5d 0a 20 20 73 71  dule [list].  sq
1270: 6c 69 74 65 33 20 64 62 20 74 65 73 74 2e 64 62  lite3 db test.db
1280: 0a 20 20 64 62 20 63 61 63 68 65 20 73 69 7a 65  .  db cache size
1290: 20 30 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f   0.  set echo_mo
12a0: 64 75 6c 65 0a 7d 20 7b 7d 0a 0a 23 20 54 72 79  dule.} {}..# Try
12b0: 20 74 6f 20 71 75 65 72 79 20 74 68 65 20 76 69   to query the vi
12c0: 72 74 75 61 6c 20 74 61 62 6c 65 20 73 63 68 65  rtual table sche
12d0: 6d 61 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20  ma. This should 
12e0: 66 61 69 6c 2c 20 61 73 20 74 68 65 0a 23 20 65  fail, as the.# e
12f0: 63 68 6f 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e  cho module has n
1300: 6f 74 20 62 65 65 6e 20 72 65 67 69 73 74 65 72  ot been register
1310: 65 64 20 77 69 74 68 20 74 68 69 73 20 64 61 74  ed with this dat
1320: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1330: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 76 74 61 62  ..#.do_test vtab
1340: 31 2e 32 2e 36 20 7b 0a 62 72 65 61 6b 70 6f 69  1.2.6 {.breakpoi
1350: 6e 74 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20  nt.  catchsql { 
1360: 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e 66  PRAGMA table_inf
1370: 6f 28 74 31 29 3b 20 7d 0a 7d 20 7b 31 20 7b 6e  o(t1); }.} {1 {n
1380: 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 65  o such module: e
1390: 63 68 6f 7d 7d 0a 0a 23 20 52 65 67 69 73 74 65  cho}}..# Registe
13a0: 72 20 74 68 65 20 6d 6f 64 75 6c 65 0a 72 65 67  r the module.reg
13b0: 69 73 74 65 72 5f 65 63 68 6f 5f 6d 6f 64 75 6c  ister_echo_modul
13c0: 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 6e 65  e [sqlite3_conne
13d0: 63 74 69 6f 6e 5f 70 6f 69 6e 74 65 72 20 64 62  ction_pointer db
13e0: 5d 0a 0a 23 20 54 72 79 20 74 6f 20 71 75 65 72  ]..# Try to quer
13f0: 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  y the virtual ta
1400: 62 6c 65 20 73 63 68 65 6d 61 20 61 67 61 69 6e  ble schema again
1410: 2e 20 54 68 69 73 20 74 69 6d 65 20 69 74 20 73  . This time it s
1420: 68 6f 75 6c 64 0a 23 20 69 6e 76 6f 6b 65 20 74  hould.# invoke t
1430: 68 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68  he xConnect meth
1440: 6f 64 20 61 6e 64 20 73 75 63 63 65 65 64 2e 0a  od and succeed..
1450: 23 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2e  #.do_test vtab1.
1460: 32 2e 37 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  2.7 {.  execsql 
1470: 7b 20 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69  { PRAGMA table_i
1480: 6e 66 6f 28 74 31 29 3b 20 7d 0a 7d 20 5b 6c 69  nfo(t1); }.} [li
1490: 73 74 20 20 20 20 20 20 20 20 20 5c 0a 20 20 30  st         \.  0
14a0: 20 61 20 7b 7d 20 30 20 7b 7d 20 30 20 5c 0a 20   a {} 0 {} 0 \. 
14b0: 20 31 20 62 20 7b 7d 20 30 20 7b 7d 20 30 20 5c   1 b {} 0 {} 0 \
14c0: 0a 20 20 32 20 63 20 7b 7d 20 30 20 7b 7d 20 30  .  2 c {} 0 {} 0
14d0: 20 5c 0a 5d 0a 64 6f 5f 74 65 73 74 20 76 74 61   \.].do_test vta
14e0: 62 31 2e 32 2e 38 20 7b 0a 20 20 73 65 74 20 65  b1.2.8 {.  set e
14f0: 63 68 6f 5f 6d 6f 64 75 6c 65 0a 7d 20 7b 78 43  cho_module.} {xC
1500: 6f 6e 6e 65 63 74 20 65 63 68 6f 20 74 65 6d 70  onnect echo temp
1510: 6c 61 74 65 7d 0a 0a 23 20 44 72 6f 70 20 74 61  late}..# Drop ta
1520: 62 6c 65 20 74 31 2e 20 54 68 69 73 20 73 68 6f  ble t1. This sho
1530: 75 6c 64 20 63 61 75 73 65 20 74 68 65 20 78 44  uld cause the xD
1540: 65 73 74 72 6f 79 20 28 62 75 74 20 6e 6f 74 20  estroy (but not 
1550: 78 44 69 73 63 6f 6e 6e 65 63 74 29 20 6d 65 74  xDisconnect) met
1560: 68 6f 64 20 0a 23 20 74 6f 20 62 65 20 69 6e 76  hod .# to be inv
1570: 6f 6b 65 64 2e 0a 64 6f 5f 74 65 73 74 20 76 74  oked..do_test vt
1580: 61 62 31 2d 32 2e 35 20 7b 0a 20 20 73 65 74 20  ab1-2.5 {.  set 
1590: 65 63 68 6f 5f 6d 6f 64 75 6c 65 20 22 22 0a 20  echo_module "". 
15a0: 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20   execsql { .    
15b0: 44 52 4f 50 20 54 41 42 4c 45 20 74 31 3b 0a 20  DROP TABLE t1;. 
15c0: 20 7d 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f   }.  set echo_mo
15d0: 64 75 6c 65 0a 7d 20 7b 78 44 65 73 74 72 6f 79  dule.} {xDestroy
15e0: 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d  }.do_test vtab1-
15f0: 32 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  2.6 {.  execsql 
1600: 7b 20 0a 20 20 20 20 50 52 41 47 4d 41 20 74 61  { .    PRAGMA ta
1610: 62 6c 65 5f 69 6e 66 6f 28 74 31 29 3b 20 0a 20  ble_info(t1); . 
1620: 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20   }.} {}.do_test 
1630: 76 74 61 62 31 2d 32 2e 37 20 7b 0a 20 20 65 78  vtab1-2.7 {.  ex
1640: 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45  ecsql {.    SELE
1650: 43 54 20 73 71 6c 20 46 52 4f 4d 20 73 71 6c 69  CT sql FROM sqli
1660: 74 65 5f 6d 61 73 74 65 72 3b 0a 20 20 7d 0a 7d  te_master;.  }.}
1670: 20 5b 6c 69 73 74 20 7b 43 52 45 41 54 45 20 54   [list {CREATE T
1680: 41 42 4c 45 20 74 65 6d 70 6c 61 74 65 28 61 2c  ABLE template(a,
1690: 20 62 2c 20 63 29 7d 5d 0a 0a 23 20 43 6c 65 61   b, c)}]..# Clea
16a0: 6e 20 75 70 20 6f 74 68 65 72 20 74 65 73 74 20  n up other test 
16b0: 61 72 74 69 66 61 63 74 73 3a 0a 64 6f 5f 74 65  artifacts:.do_te
16c0: 73 74 20 76 74 61 62 31 2d 32 2e 38 20 7b 0a 20  st vtab1-2.8 {. 
16d0: 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20   execsql { .    
16e0: 44 52 4f 50 20 54 41 42 4c 45 20 74 65 6d 70 6c  DROP TABLE templ
16f0: 61 74 65 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  ate;.    SELECT 
1700: 73 71 6c 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f  sql FROM sqlite_
1710: 6d 61 73 74 65 72 3b 0a 20 20 7d 0a 7d 20 5b 6c  master;.  }.} [l
1720: 69 73 74 5d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  ist]..#---------
1730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1740: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1750: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1760: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
1770: 54 65 73 74 20 63 61 73 65 20 76 74 61 62 31 2d  Test case vtab1-
1780: 33 20 74 65 73 74 20 74 61 62 6c 65 20 73 63 61  3 test table sca
1790: 6e 73 20 61 6e 64 20 74 68 65 20 65 63 68 6f 20  ns and the echo 
17a0: 6d 6f 64 75 6c 65 27 73 20 0a 23 20 78 42 65 73  module's .# xBes
17b0: 74 49 6e 64 65 78 2f 78 46 69 6c 74 65 72 20 68  tIndex/xFilter h
17c0: 61 6e 64 6c 69 6e 67 20 6f 66 20 57 48 45 52 45  andling of WHERE
17d0: 20 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 0a 64 6f   conditions...do
17e0: 5f 74 65 73 74 20 76 74 61 62 31 2d 33 2e 31 20  _test vtab1-3.1 
17f0: 7b 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f 64  {.  set echo_mod
1800: 75 6c 65 20 22 22 0a 20 20 65 78 65 63 73 71 6c  ule "".  execsql
1810: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
1820: 42 4c 45 20 74 72 65 61 6c 28 61 20 49 4e 54 45  BLE treal(a INTE
1830: 47 45 52 2c 20 62 20 49 4e 54 45 47 45 52 2c 20  GER, b INTEGER, 
1840: 63 29 3b 20 0a 20 20 20 20 43 52 45 41 54 45 20  c); .    CREATE 
1850: 49 4e 44 45 58 20 74 72 65 61 6c 5f 69 64 78 20  INDEX treal_idx 
1860: 4f 4e 20 74 72 65 61 6c 28 62 29 3b 0a 20 20 20  ON treal(b);.   
1870: 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
1880: 54 41 42 4c 45 20 74 31 20 55 53 49 4e 47 20 65  TABLE t1 USING e
1890: 63 68 6f 28 74 72 65 61 6c 29 3b 0a 20 20 7d 0a  cho(treal);.  }.
18a0: 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f 64 75 6c    set echo_modul
18b0: 65 0a 7d 20 5b 6c 69 73 74 20 78 43 72 65 61 74  e.} [list xCreat
18c0: 65 20 65 63 68 6f 20 74 72 65 61 6c 5d 0a 0a 23  e echo treal]..#
18d0: 20 54 65 73 74 20 74 68 61 74 20 61 20 53 45 4c   Test that a SEL
18e0: 45 43 54 20 6f 6e 20 74 31 20 64 6f 65 73 6e 27  ECT on t1 doesn'
18f0: 74 20 63 72 61 73 68 2e 20 4e 6f 20 72 6f 77 73  t crash. No rows
1900: 20 61 72 65 20 72 65 74 75 72 6e 65 64 0a 23 20   are returned.# 
1910: 62 65 63 61 75 73 65 20 74 68 65 20 75 6e 64 65  because the unde
1920: 72 6c 79 69 6e 67 20 72 65 61 6c 20 74 61 62 6c  rlying real tabl
1930: 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 65  e is currently e
1940: 6d 70 74 79 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  mpty..#.do_test 
1950: 76 74 61 62 31 2d 33 2e 32 20 7b 0a 20 20 65 78  vtab1-3.2 {.  ex
1960: 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45  ecsql {.    SELE
1970: 43 54 20 61 2c 20 62 2c 20 63 20 46 52 4f 4d 20  CT a, b, c FROM 
1980: 74 31 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 20  t1;.  }.} {}..# 
1990: 50 75 74 20 73 6f 6d 65 20 64 61 74 61 20 69 6e  Put some data in
19a0: 74 6f 20 74 68 65 20 74 61 62 6c 65 20 74 72 65  to the table tre
19b0: 61 6c 2e 20 54 68 65 6e 20 74 72 79 20 61 20 66  al. Then try a f
19c0: 65 77 20 73 69 6d 70 6c 65 20 53 45 4c 45 43 54  ew simple SELECT
19d0: 20 0a 23 20 73 74 61 74 65 6d 65 6e 74 73 20 6f   .# statements o
19e0: 6e 20 74 31 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  n t1..#.do_test 
19f0: 76 74 61 62 31 2d 33 2e 33 20 7b 0a 20 20 65 78  vtab1-3.3 {.  ex
1a00: 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45  ecsql {.    INSE
1a10: 52 54 20 49 4e 54 4f 20 74 72 65 61 6c 20 56 41  RT INTO treal VA
1a20: 4c 55 45 53 28 31 2c 20 32 2c 20 33 29 3b 0a 20  LUES(1, 2, 3);. 
1a30: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1a40: 72 65 61 6c 20 56 41 4c 55 45 53 28 34 2c 20 35  real VALUES(4, 5
1a50: 2c 20 36 29 3b 0a 20 20 20 20 53 45 4c 45 43 54  , 6);.    SELECT
1a60: 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d 0a   * FROM t1;.  }.
1a70: 7d 20 7b 31 20 32 20 33 20 34 20 35 20 36 7d 0a  } {1 2 3 4 5 6}.
1a80: 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 33 2e  do_test vtab1-3.
1a90: 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  4 {.  execsql {.
1aa0: 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f      SELECT a FRO
1ab0: 4d 20 74 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 34  M t1;.  }.} {1 4
1ac0: 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d  }.do_test vtab1-
1ad0: 33 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  3.5 {.  execsql 
1ae0: 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 72 6f 77  {.    SELECT row
1af0: 69 64 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d 0a  id FROM t1;.  }.
1b00: 7d 20 7b 31 20 32 7d 0a 64 6f 5f 74 65 73 74 20  } {1 2}.do_test 
1b10: 76 74 61 62 31 2d 33 2e 36 20 7b 0a 20 20 73 65  vtab1-3.6 {.  se
1b20: 74 20 65 63 68 6f 5f 6d 6f 64 75 6c 65 20 22 22  t echo_module ""
1b30: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
1b40: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
1b50: 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 32 20 33 20  1;.  }.} {1 2 3 
1b60: 34 20 35 20 36 7d 0a 64 6f 5f 74 65 73 74 20 76  4 5 6}.do_test v
1b70: 74 61 62 31 2d 33 2e 37 20 7b 0a 20 20 65 78 65  tab1-3.7 {.  exe
1b80: 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43  csql {.    SELEC
1b90: 54 20 72 6f 77 69 64 2c 20 2a 20 46 52 4f 4d 20  T rowid, * FROM 
1ba0: 74 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31 20 32  t1;.  }.} {1 1 2
1bb0: 20 33 20 32 20 34 20 35 20 36 7d 0a 0a 23 20 45   3 2 4 5 6}..# E
1bc0: 78 65 63 75 74 65 20 73 6f 6d 65 20 53 45 4c 45  xecute some SELE
1bd0: 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69  CT statements wi
1be0: 74 68 20 57 48 45 52 45 20 63 6c 61 75 73 65 73  th WHERE clauses
1bf0: 20 6f 6e 20 74 68 65 20 74 31 20 74 61 62 6c 65   on the t1 table
1c00: 2e 0a 23 20 54 68 65 6e 20 63 68 65 63 6b 20 74  ..# Then check t
1c10: 68 65 20 65 63 68 6f 5f 6d 6f 64 75 6c 65 20 76  he echo_module v
1c20: 61 72 69 61 62 6c 65 20 28 77 72 69 74 74 65 6e  ariable (written
1c30: 20 74 6f 20 62 79 20 74 68 65 20 6d 6f 64 75 6c   to by the modul
1c40: 65 20 6d 65 74 68 6f 64 73 0a 23 20 69 6e 20 74  e methods.# in t
1c50: 65 73 74 38 2e 63 29 20 74 6f 20 6d 61 6b 65 20  est8.c) to make 
1c60: 73 75 72 65 20 74 68 65 20 78 42 65 73 74 49 6e  sure the xBestIn
1c70: 64 65 78 28 29 20 61 6e 64 20 78 46 69 6c 74 65  dex() and xFilte
1c80: 72 28 29 20 6d 65 74 68 6f 64 73 20 77 65 72 65  r() methods were
1c90: 0a 23 20 63 61 6c 6c 65 64 20 63 6f 72 72 65 63  .# called correc
1ca0: 74 6c 79 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 76  tly..#.do_test v
1cb0: 74 61 62 31 2d 33 2e 38 20 7b 0a 20 20 73 65 74  tab1-3.8 {.  set
1cc0: 20 65 63 68 6f 5f 6d 6f 64 75 6c 65 20 22 22 0a   echo_module "".
1cd0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
1ce0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
1cf0: 3b 0a 20 20 7d 0a 20 20 73 65 74 20 65 63 68 6f  ;.  }.  set echo
1d00: 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c 69 73 74 20  _module.} [list 
1d10: 78 42 65 73 74 49 6e 64 65 78 20 7b 53 45 4c 45  xBestIndex {SELE
1d20: 43 54 20 72 6f 77 69 64 2c 20 2a 20 46 52 4f 4d  CT rowid, * FROM
1d30: 20 27 74 72 65 61 6c 27 7d 20 5c 0a 20 20 20 20   'treal'} \.    
1d40: 20 20 20 20 78 46 69 6c 74 65 72 20 20 20 20 7b      xFilter    {
1d50: 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20  SELECT rowid, * 
1d60: 46 52 4f 4d 20 27 74 72 65 61 6c 27 7d 20 5d 0a  FROM 'treal'} ].
1d70: 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 33 2e  do_test vtab1-3.
1d80: 39 20 7b 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d  9 {.  set echo_m
1d90: 6f 64 75 6c 65 20 22 22 0a 20 20 65 78 65 63 73  odule "".  execs
1da0: 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20  ql {.    SELECT 
1db0: 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  * FROM t1 WHERE 
1dc0: 62 20 3d 20 35 3b 0a 20 20 7d 0a 7d 20 7b 34 20  b = 5;.  }.} {4 
1dd0: 35 20 36 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61  5 6}.do_test vta
1de0: 62 31 2d 33 2e 31 30 20 7b 0a 20 20 73 65 74 20  b1-3.10 {.  set 
1df0: 65 63 68 6f 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c  echo_module.} [l
1e00: 69 73 74 20 78 42 65 73 74 49 6e 64 65 78 20 7b  ist xBestIndex {
1e10: 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20  SELECT rowid, * 
1e20: 46 52 4f 4d 20 27 74 72 65 61 6c 27 20 57 48 45  FROM 'treal' WHE
1e30: 52 45 20 62 20 3d 20 3f 7d 20 20 20 5c 0a 20 20  RE b = ?}   \.  
1e40: 20 20 20 20 20 20 78 46 69 6c 74 65 72 20 20 20        xFilter   
1e50: 20 7b 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20   {SELECT rowid, 
1e60: 2a 20 46 52 4f 4d 20 27 74 72 65 61 6c 27 20 57  * FROM 'treal' W
1e70: 48 45 52 45 20 62 20 3d 20 3f 7d 20 35 20 5d 0a  HERE b = ?} 5 ].
1e80: 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 33 2e  do_test vtab1-3.
1e90: 31 30 20 7b 0a 20 20 73 65 74 20 65 63 68 6f 5f  10 {.  set echo_
1ea0: 6d 6f 64 75 6c 65 20 22 22 0a 20 20 65 78 65 63  module "".  exec
1eb0: 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54  sql {.    SELECT
1ec0: 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   * FROM t1 WHERE
1ed0: 20 62 20 3e 3d 20 35 20 41 4e 44 20 62 20 3c 3d   b >= 5 AND b <=
1ee0: 20 31 30 3b 0a 20 20 7d 0a 7d 20 7b 34 20 35 20   10;.  }.} {4 5 
1ef0: 36 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31  6}.do_test vtab1
1f00: 2d 33 2e 31 31 20 7b 0a 20 20 73 65 74 20 65 63  -3.11 {.  set ec
1f10: 68 6f 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c 69 73  ho_module.} [lis
1f20: 74 20 78 42 65 73 74 49 6e 64 65 78 20 7b 53 45  t xBestIndex {SE
1f30: 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20 46 52  LECT rowid, * FR
1f40: 4f 4d 20 27 74 72 65 61 6c 27 20 57 48 45 52 45  OM 'treal' WHERE
1f50: 20 62 20 3e 3d 20 3f 20 41 4e 44 20 62 20 3c 3d   b >= ? AND b <=
1f60: 20 3f 7d 20 20 20 20 20 20 5c 0a 20 20 20 20 20   ?}      \.     
1f70: 20 20 20 78 46 69 6c 74 65 72 20 20 20 20 7b 53     xFilter    {S
1f80: 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20 46  ELECT rowid, * F
1f90: 52 4f 4d 20 27 74 72 65 61 6c 27 20 57 48 45 52  ROM 'treal' WHER
1fa0: 45 20 62 20 3e 3d 20 3f 20 41 4e 44 20 62 20 3c  E b >= ? AND b <
1fb0: 3d 20 3f 7d 20 35 20 31 30 20 5d 0a 64 6f 5f 74  = ?} 5 10 ].do_t
1fc0: 65 73 74 20 76 74 61 62 31 2d 33 2e 31 32 20 7b  est vtab1-3.12 {
1fd0: 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f 64 75  .  set echo_modu
1fe0: 6c 65 20 22 22 0a 20 20 65 78 65 63 73 71 6c 20  le "".  execsql 
1ff0: 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  {.    SELECT * F
2000: 52 4f 4d 20 74 31 20 57 48 45 52 45 20 62 20 42  ROM t1 WHERE b B
2010: 45 54 57 45 45 4e 20 32 20 41 4e 44 20 31 30 3b  ETWEEN 2 AND 10;
2020: 0a 20 20 7d 0a 7d 20 7b 31 20 32 20 33 20 34 20  .  }.} {1 2 3 4 
2030: 35 20 36 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61  5 6}.do_test vta
2040: 62 31 2d 33 2e 31 33 20 7b 0a 20 20 73 65 74 20  b1-3.13 {.  set 
2050: 65 63 68 6f 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c  echo_module.} [l
2060: 69 73 74 20 78 42 65 73 74 49 6e 64 65 78 20 7b  ist xBestIndex {
2070: 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20  SELECT rowid, * 
2080: 46 52 4f 4d 20 27 74 72 65 61 6c 27 20 57 48 45  FROM 'treal' WHE
2090: 52 45 20 62 20 3e 3d 20 3f 20 41 4e 44 20 62 20  RE b >= ? AND b 
20a0: 3c 3d 20 3f 7d 20 20 20 20 20 20 5c 0a 20 20 20  <= ?}      \.   
20b0: 20 20 20 20 20 78 46 69 6c 74 65 72 20 20 20 20       xFilter    
20c0: 7b 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a  {SELECT rowid, *
20d0: 20 46 52 4f 4d 20 27 74 72 65 61 6c 27 20 57 48   FROM 'treal' WH
20e0: 45 52 45 20 62 20 3e 3d 20 3f 20 41 4e 44 20 62  ERE b >= ? AND b
20f0: 20 3c 3d 20 3f 7d 20 32 20 31 30 20 5d 0a 0a 23   <= ?} 2 10 ]..#
2100: 20 41 64 64 20 61 20 66 75 6e 63 74 69 6f 6e 20   Add a function 
2110: 66 6f 72 20 74 68 65 20 4d 41 54 43 48 20 6f 70  for the MATCH op
2120: 65 72 61 74 6f 72 2e 20 45 76 65 72 79 74 68 69  erator. Everythi
2130: 6e 67 20 61 6c 77 61 79 73 20 6d 61 74 63 68 65  ng always matche
2140: 73 21 0a 23 70 72 6f 63 20 74 65 73 74 5f 6d 61  s!.#proc test_ma
2150: 74 63 68 20 7b 6c 68 73 20 72 68 73 7d 20 7b 0a  tch {lhs rhs} {.
2160: 23 20 20 6c 61 70 70 65 6e 64 20 3a 3a 65 63 68  #  lappend ::ech
2170: 6f 5f 6d 6f 64 75 6c 65 20 4d 41 54 43 48 20 24  o_module MATCH $
2180: 6c 68 73 20 24 72 68 73 0a 23 20 20 72 65 74 75  lhs $rhs.#  retu
2190: 72 6e 20 31 0a 23 7d 0a 23 64 62 20 66 75 6e 63  rn 1.#}.#db func
21a0: 74 69 6f 6e 20 6d 61 74 63 68 20 74 65 73 74 5f  tion match test_
21b0: 6d 61 74 63 68 0a 0a 73 65 74 20 65 63 68 6f 5f  match..set echo_
21c0: 6d 6f 64 75 6c 65 20 22 22 0a 64 6f 5f 74 65 73  module "".do_tes
21d0: 74 20 76 74 61 62 31 2d 33 2e 31 32 20 7b 0a 20  t vtab1-3.12 {. 
21e0: 20 73 65 74 20 65 63 68 6f 5f 6d 6f 64 75 6c 65   set echo_module
21f0: 20 22 22 0a 20 20 63 61 74 63 68 73 71 6c 20 7b   "".  catchsql {
2200: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
2210: 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20 4d 41  OM t1 WHERE a MA
2220: 54 43 48 20 27 73 74 72 69 6e 67 27 3b 0a 20 20  TCH 'string';.  
2230: 7d 0a 7d 20 7b 31 20 7b 4d 41 54 43 48 20 69 73  }.} {1 {MATCH is
2240: 20 6e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64   not implemented
2250: 7d 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31  }}.do_test vtab1
2260: 2d 33 2e 31 33 20 7b 0a 20 20 73 65 74 20 65 63  -3.13 {.  set ec
2270: 68 6f 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c 69 73  ho_module.} [lis
2280: 74 20 78 42 65 73 74 49 6e 64 65 78 20 7b 53 45  t xBestIndex {SE
2290: 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20 46 52  LECT rowid, * FR
22a0: 4f 4d 20 27 74 72 65 61 6c 27 7d 20 5c 0a 20 20  OM 'treal'} \.  
22b0: 20 20 20 20 20 20 78 46 69 6c 74 65 72 20 20 20        xFilter   
22c0: 20 7b 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20   {SELECT rowid, 
22d0: 2a 20 46 52 4f 4d 20 27 74 72 65 61 6c 27 7d 5d  * FROM 'treal'}]
22e0: 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 33  .do_test vtab1-3
22f0: 2e 31 34 20 7b 0a 20 20 73 65 74 20 65 63 68 6f  .14 {.  set echo
2300: 5f 6d 6f 64 75 6c 65 20 22 22 0a 62 74 72 65 65  _module "".btree
2310: 5f 62 72 65 61 6b 70 6f 69 6e 74 0a 20 20 65 78  _breakpoint.  ex
2320: 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45  ecsql {.    SELE
2330: 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
2340: 52 45 20 62 20 4d 41 54 43 48 20 27 73 74 72 69  RE b MATCH 'stri
2350: 6e 67 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  ng';.  }.} {}.do
2360: 5f 74 65 73 74 20 76 74 61 62 31 2d 33 2e 31 35  _test vtab1-3.15
2370: 20 7b 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f   {.  set echo_mo
2380: 64 75 6c 65 0a 7d 20 5b 6c 69 73 74 20 78 42 65  dule.} [list xBe
2390: 73 74 49 6e 64 65 78 20 5c 0a 20 20 20 20 20 20  stIndex \.      
23a0: 20 20 7b 53 45 4c 45 43 54 20 72 6f 77 69 64 2c    {SELECT rowid,
23b0: 20 2a 20 46 52 4f 4d 20 27 74 72 65 61 6c 27 20   * FROM 'treal' 
23c0: 57 48 45 52 45 20 62 20 4c 49 4b 45 20 28 53 45  WHERE b LIKE (SE
23d0: 4c 45 43 54 20 27 25 27 7c 7c 3f 7c 7c 27 25 27  LECT '%'||?||'%'
23e0: 29 7d 20 20 5c 0a 20 20 20 20 20 20 20 20 78 46  )}  \.        xF
23f0: 69 6c 74 65 72 20 5c 0a 20 20 20 20 20 20 20 20  ilter \.        
2400: 7b 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a  {SELECT rowid, *
2410: 20 46 52 4f 4d 20 27 74 72 65 61 6c 27 20 57 48   FROM 'treal' WH
2420: 45 52 45 20 62 20 4c 49 4b 45 20 28 53 45 4c 45  ERE b LIKE (SELE
2430: 43 54 20 27 25 27 7c 7c 3f 7c 7c 27 25 27 29 7d  CT '%'||?||'%')}
2440: 20 5c 0a 20 20 20 20 20 20 20 20 73 74 72 69 6e   \.        strin
2450: 67 20 5d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  g ]..#----------
2460: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2470: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2480: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2490: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54  ------------.# T
24a0: 65 73 74 20 63 61 73 65 20 76 74 61 62 31 2d 33  est case vtab1-3
24b0: 20 74 65 73 74 20 74 61 62 6c 65 20 73 63 61 6e   test table scan
24c0: 73 20 61 6e 64 20 74 68 65 20 65 63 68 6f 20 6d  s and the echo m
24d0: 6f 64 75 6c 65 27 73 20 0a 23 20 78 42 65 73 74  odule's .# xBest
24e0: 49 6e 64 65 78 2f 78 46 69 6c 74 65 72 20 68 61  Index/xFilter ha
24f0: 6e 64 6c 69 6e 67 20 6f 66 20 4f 52 44 45 52 20  ndling of ORDER 
2500: 42 59 20 63 6c 61 75 73 65 73 2e 0a 0a 23 20 54  BY clauses...# T
2510: 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 65 78  his procedure ex
2520: 65 63 75 74 65 73 20 74 68 65 20 53 51 4c 2e 20  ecutes the SQL. 
2530: 20 54 68 65 6e 20 69 74 20 63 68 65 63 6b 73 20   Then it checks 
2540: 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 4f 50  to see if the OP
2550: 5f 53 6f 72 74 0a 23 20 6f 70 63 6f 64 65 20 77  _Sort.# opcode w
2560: 61 73 20 65 78 65 63 75 74 65 64 2e 20 20 49 66  as executed.  If
2570: 20 61 6e 20 4f 50 5f 53 6f 72 74 20 64 69 64 20   an OP_Sort did 
2580: 6f 63 63 75 72 2c 20 74 68 65 6e 20 22 73 6f 72  occur, then "sor
2590: 74 22 20 69 73 20 61 70 70 65 6e 64 65 64 0a 23  t" is appended.#
25a0: 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 2e 20   to the result. 
25b0: 20 49 66 20 6e 6f 20 4f 50 5f 53 6f 72 74 20 68   If no OP_Sort h
25c0: 61 70 70 65 6e 65 64 2c 20 74 68 65 6e 20 22 6e  appened, then "n
25d0: 6f 73 6f 72 74 22 20 69 73 20 61 70 70 65 6e 64  osort" is append
25e0: 65 64 2e 0a 23 0a 23 20 54 68 69 73 20 70 72 6f  ed..#.# This pro
25f0: 63 65 64 75 72 65 20 69 73 20 75 73 65 64 20 74  cedure is used t
2600: 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  o check to make 
2610: 73 75 72 65 20 73 6f 72 74 69 6e 67 20 69 73 20  sure sorting is 
2620: 6f 72 20 69 73 20 6e 6f 74 0a 23 20 6f 63 63 75  or is not.# occu
2630: 72 72 69 6e 67 20 61 73 20 65 78 70 65 63 74 65  rring as expecte
2640: 64 2e 0a 23 0a 70 72 6f 63 20 63 6b 73 6f 72 74  d..#.proc cksort
2650: 20 7b 73 71 6c 7d 20 7b 0a 20 20 73 65 74 20 3a   {sql} {.  set :
2660: 3a 73 71 6c 69 74 65 5f 73 6f 72 74 5f 63 6f 75  :sqlite_sort_cou
2670: 6e 74 20 30 0a 20 20 73 65 74 20 64 61 74 61 20  nt 0.  set data 
2680: 5b 65 78 65 63 73 71 6c 20 24 73 71 6c 5d 0a 20  [execsql $sql]. 
2690: 20 69 66 20 7b 24 3a 3a 73 71 6c 69 74 65 5f 73   if {$::sqlite_s
26a0: 6f 72 74 5f 63 6f 75 6e 74 7d 20 7b 73 65 74 20  ort_count} {set 
26b0: 78 20 73 6f 72 74 7d 20 7b 73 65 74 20 78 20 6e  x sort} {set x n
26c0: 6f 73 6f 72 74 7d 0a 20 20 6c 61 70 70 65 6e 64  osort}.  lappend
26d0: 20 64 61 74 61 20 24 78 0a 20 20 72 65 74 75 72   data $x.  retur
26e0: 6e 20 24 64 61 74 61 0a 7d 0a 0a 64 6f 5f 74 65  n $data.}..do_te
26f0: 73 74 20 76 74 61 62 31 2d 34 2e 31 20 7b 0a 20  st vtab1-4.1 {. 
2700: 20 73 65 74 20 65 63 68 6f 5f 6d 6f 64 75 6c 65   set echo_module
2710: 20 22 22 0a 20 20 63 6b 73 6f 72 74 20 7b 0a 20   "".  cksort {. 
2720: 20 20 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d     SELECT b FROM
2730: 20 74 31 20 4f 52 44 45 52 20 42 59 20 62 3b 0a   t1 ORDER BY b;.
2740: 20 20 7d 0a 7d 20 7b 32 20 35 20 6e 6f 73 6f 72    }.} {2 5 nosor
2750: 74 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31  t}.do_test vtab1
2760: 2d 34 2e 32 20 7b 0a 20 20 73 65 74 20 65 63 68  -4.2 {.  set ech
2770: 6f 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c 69 73 74  o_module.} [list
2780: 20 78 42 65 73 74 49 6e 64 65 78 20 7b 53 45 4c   xBestIndex {SEL
2790: 45 43 54 20 72 6f 77 69 64 2c 20 2a 20 46 52 4f  ECT rowid, * FRO
27a0: 4d 20 27 74 72 65 61 6c 27 20 4f 52 44 45 52 20  M 'treal' ORDER 
27b0: 42 59 20 62 20 41 53 43 7d 20 5c 0a 20 20 20 20  BY b ASC} \.    
27c0: 20 20 20 20 78 46 69 6c 74 65 72 20 20 20 20 7b      xFilter    {
27d0: 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20  SELECT rowid, * 
27e0: 46 52 4f 4d 20 27 74 72 65 61 6c 27 20 4f 52 44  FROM 'treal' ORD
27f0: 45 52 20 42 59 20 62 20 41 53 43 7d 20 5d 0a 64  ER BY b ASC} ].d
2800: 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 34 2e 33  o_test vtab1-4.3
2810: 20 7b 0a 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f   {.  set echo_mo
2820: 64 75 6c 65 20 22 22 0a 20 20 63 6b 73 6f 72 74  dule "".  cksort
2830: 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 62 20   {.    SELECT b 
2840: 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59  FROM t1 ORDER BY
2850: 20 62 20 44 45 53 43 3b 0a 20 20 7d 0a 7d 20 7b   b DESC;.  }.} {
2860: 35 20 32 20 6e 6f 73 6f 72 74 7d 0a 64 6f 5f 74  5 2 nosort}.do_t
2870: 65 73 74 20 76 74 61 62 31 2d 34 2e 34 20 7b 0a  est vtab1-4.4 {.
2880: 20 20 73 65 74 20 65 63 68 6f 5f 6d 6f 64 75 6c    set echo_modul
2890: 65 0a 7d 20 5b 6c 69 73 74 20 78 42 65 73 74 49  e.} [list xBestI
28a0: 6e 64 65 78 20 7b 53 45 4c 45 43 54 20 72 6f 77  ndex {SELECT row
28b0: 69 64 2c 20 2a 20 46 52 4f 4d 20 27 74 72 65 61  id, * FROM 'trea
28c0: 6c 27 20 4f 52 44 45 52 20 42 59 20 62 20 44 45  l' ORDER BY b DE
28d0: 53 43 7d 20 5c 0a 20 20 20 20 20 20 20 20 78 46  SC} \.        xF
28e0: 69 6c 74 65 72 20 20 20 20 7b 53 45 4c 45 43 54  ilter    {SELECT
28f0: 20 72 6f 77 69 64 2c 20 2a 20 46 52 4f 4d 20 27   rowid, * FROM '
2900: 74 72 65 61 6c 27 20 4f 52 44 45 52 20 42 59 20  treal' ORDER BY 
2910: 62 20 44 45 53 43 7d 20 5d 0a 64 6f 5f 74 65 73  b DESC} ].do_tes
2920: 74 20 76 74 61 62 31 2d 34 2e 33 20 7b 0a 20 20  t vtab1-4.3 {.  
2930: 73 65 74 20 65 63 68 6f 5f 6d 6f 64 75 6c 65 20  set echo_module 
2940: 22 22 0a 20 20 63 6b 73 6f 72 74 20 7b 0a 20 20  "".  cksort {.  
2950: 20 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d 20    SELECT b FROM 
2960: 74 31 20 4f 52 44 45 52 20 42 59 20 62 7c 7c 27  t1 ORDER BY b||'
2970: 27 3b 0a 20 20 7d 0a 7d 20 7b 32 20 35 20 73 6f  ';.  }.} {2 5 so
2980: 72 74 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61 62  rt}.do_test vtab
2990: 31 2d 34 2e 34 20 7b 0a 20 20 73 65 74 20 65 63  1-4.4 {.  set ec
29a0: 68 6f 5f 6d 6f 64 75 6c 65 0a 7d 20 5b 6c 69 73  ho_module.} [lis
29b0: 74 20 78 42 65 73 74 49 6e 64 65 78 20 7b 53 45  t xBestIndex {SE
29c0: 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20 46 52  LECT rowid, * FR
29d0: 4f 4d 20 27 74 72 65 61 6c 27 7d 20 5c 0a 20 20  OM 'treal'} \.  
29e0: 20 20 20 20 20 20 78 46 69 6c 74 65 72 20 20 20        xFilter   
29f0: 20 7b 53 45 4c 45 43 54 20 72 6f 77 69 64 2c 20   {SELECT rowid, 
2a00: 2a 20 46 52 4f 4d 20 27 74 72 65 61 6c 27 7d 20  * FROM 'treal'} 
2a10: 5d 0a 0a 65 78 65 63 73 71 6c 20 7b 0a 20 20 44  ]..execsql {.  D
2a20: 52 4f 50 20 54 41 42 4c 45 20 74 31 3b 0a 20 20  ROP TABLE t1;.  
2a30: 44 52 4f 50 20 54 41 42 4c 45 20 74 72 65 61 6c  DROP TABLE treal
2a40: 3b 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ;.}..#----------
2a50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2a60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2a70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2a80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54  ------------.# T
2a90: 65 73 74 20 63 61 73 65 73 20 76 74 61 62 31 2d  est cases vtab1-
2aa0: 36 20 74 65 73 74 20 49 4e 53 45 52 54 2c 20 55  6 test INSERT, U
2ab0: 50 44 41 54 45 20 61 6e 64 20 44 45 4c 45 54 45  PDATE and DELETE
2ac0: 20 6f 70 65 72 61 74 69 6f 6e 73 20 0a 23 20 6f   operations .# o
2ad0: 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  n virtual tables
2ae0: 2e 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d  ..do_test vtab1-
2af0: 36 2d 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  6-1 {.  execsql 
2b00: 7b 20 53 45 4c 45 43 54 20 73 71 6c 20 46 52 4f  { SELECT sql FRO
2b10: 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  M sqlite_master 
2b20: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 76  }.} {}.do_test v
2b30: 74 61 62 31 2d 36 2d 32 20 7b 0a 20 20 65 78 65  tab1-6-2 {.  exe
2b40: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
2b50: 45 20 54 41 42 4c 45 20 74 72 65 61 6c 28 61 20  E TABLE treal(a 
2b60: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 2c 20  PRIMARY KEY, b, 
2b70: 63 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 56  c);.    CREATE V
2b80: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 65 63  IRTUAL TABLE tec
2b90: 68 6f 20 55 53 49 4e 47 20 65 63 68 6f 28 74 72  ho USING echo(tr
2ba0: 65 61 6c 29 3b 0a 20 20 20 20 53 45 4c 45 43 54  eal);.    SELECT
2bb0: 20 6e 61 6d 65 20 46 52 4f 4d 20 73 71 6c 69 74   name FROM sqlit
2bc0: 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 74  e_master WHERE t
2bd0: 79 70 65 20 3d 20 27 74 61 62 6c 65 27 3b 0a 20  ype = 'table';. 
2be0: 20 7d 0a 7d 20 7b 74 72 65 61 6c 20 74 65 63 68   }.} {treal tech
2bf0: 6f 7d 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31  o}.do_test vtab1
2c00: 2d 36 2d 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  -6-3 {.  execsql
2c10: 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e   {.    INSERT IN
2c20: 54 4f 20 74 65 63 68 6f 20 56 41 4c 55 45 53 28  TO techo VALUES(
2c30: 31 2c 20 32 2c 20 33 29 3b 0a 20 20 20 20 53 45  1, 2, 3);.    SE
2c40: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 65 63 68  LECT * FROM tech
2c50: 6f 3b 0a 20 20 7d 0a 7d 20 7b 31 20 32 20 33 7d  o;.  }.} {1 2 3}
2c60: 0a 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 36  .do_test vtab1-6
2c70: 2d 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  -4 {.  execsql {
2c80: 0a 20 20 20 20 55 50 44 41 54 45 20 74 65 63 68  .    UPDATE tech
2c90: 6f 20 53 45 54 20 61 20 3d 20 35 3b 0a 20 20 20  o SET a = 5;.   
2ca0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
2cb0: 65 63 68 6f 3b 0a 20 20 7d 0a 7d 20 7b 35 20 32  echo;.  }.} {5 2
2cc0: 20 33 7d 0a 0a 23 20 54 4f 44 4f 3a 20 54 68 69   3}..# TODO: Thi
2cd0: 73 20 63 61 75 73 65 73 20 61 20 63 72 61 73 68  s causes a crash
2ce0: 20 61 74 20 70 72 65 73 65 6e 74 2e 0a 23 0a 23   at present..#.#
2cf0: 20 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 36   do_test vtab1-6
2d00: 2d 35 20 7b 0a 23 20 65 78 70 6c 61 69 6e 20 7b  -5 {.# explain {
2d10: 0a 23 20 20 20 20 55 50 44 41 54 45 20 74 65 63  .#    UPDATE tec
2d20: 68 6f 20 73 65 74 20 61 20 3d 20 61 7c 7c 62 7c  ho set a = a||b|
2d30: 7c 63 3b 0a 23 20 20 7d 0a 23 20 20 65 78 65 63  |c;.#  }.#  exec
2d40: 73 71 6c 20 7b 0a 23 20 20 20 20 55 50 44 41 54  sql {.#    UPDAT
2d50: 45 20 74 65 63 68 6f 20 73 65 74 20 61 20 3d 20  E techo set a = 
2d60: 61 7c 7c 62 7c 7c 63 3b 0a 23 20 20 20 20 53 45  a||b||c;.#    SE
2d70: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 65 63 68  LECT * FROM tech
2d80: 6f 3b 0a 23 20 20 7d 0a 23 7d 20 7b 35 32 33 20  o;.#  }.#} {523 
2d90: 32 20 33 7d 0a 0a 64 6f 5f 74 65 73 74 20 76 74  2 3}..do_test vt
2da0: 61 62 31 2d 36 2d 36 20 7b 0a 20 20 65 78 65 63  ab1-6-6 {.  exec
2db0: 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45  sql {.    UPDATE
2dc0: 20 74 65 63 68 6f 20 73 65 74 20 72 6f 77 69 64   techo set rowid
2dd0: 20 3d 20 31 30 3b 0a 20 20 20 20 53 45 4c 45 43   = 10;.    SELEC
2de0: 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 65 63  T rowid FROM tec
2df0: 68 6f 3b 0a 20 20 7d 0a 7d 20 7b 31 30 7d 0a 0a  ho;.  }.} {10}..
2e00: 64 6f 5f 74 65 73 74 20 76 74 61 62 31 2d 36 2d  do_test vtab1-6-
2e10: 37 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  7 {.  execsql {.
2e20: 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20      DELETE FROM 
2e30: 74 65 63 68 6f 3b 0a 20 20 20 20 53 45 4c 45 43  techo;.    SELEC
2e40: 54 20 2a 20 46 52 4f 4d 20 74 65 63 68 6f 3b 0a  T * FROM techo;.
2e50: 20 20 7d 0a 7d 20 7b 7d 0a 0a 66 69 6c 65 20 64    }.} {}..file d
2e60: 65 6c 65 74 65 20 2d 66 6f 72 63 65 20 74 65 73  elete -force tes
2e70: 74 32 2e 64 62 0a 66 69 6c 65 20 64 65 6c 65 74  t2.db.file delet
2e80: 65 20 2d 66 6f 72 63 65 20 74 65 73 74 32 2e 64  e -force test2.d
2e90: 62 2d 6a 6f 75 72 6e 61 6c 0a 73 71 6c 69 74 65  b-journal.sqlite
2ea0: 33 20 64 62 32 20 74 65 73 74 32 2e 64 62 0a 65  3 db2 test2.db.e
2eb0: 78 65 63 73 71 6c 20 7b 0a 20 20 43 52 45 41 54  xecsql {.  CREAT
2ec0: 45 20 54 41 42 4c 45 20 74 65 63 68 6f 28 61 20  E TABLE techo(a 
2ed0: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 2c 20  PRIMARY KEY, b, 
2ee0: 63 29 3b 0a 7d 20 64 62 32 0a 70 72 6f 63 20 63  c);.} db2.proc c
2ef0: 68 65 63 6b 5f 65 63 68 6f 5f 74 61 62 6c 65 20  heck_echo_table 
2f00: 7b 74 6e 7d 20 7b 0a 20 20 73 65 74 20 3a 3a 64  {tn} {.  set ::d
2f10: 61 74 61 31 20 5b 65 78 65 63 73 71 6c 20 7b 53  ata1 [execsql {S
2f20: 45 4c 45 43 54 20 72 6f 77 69 64 2c 20 2a 20 46  ELECT rowid, * F
2f30: 52 4f 4d 20 74 65 63 68 6f 7d 5d 0a 20 20 73 65  ROM techo}].  se
2f40: 74 20 3a 3a 64 61 74 61 32 20 5b 65 78 65 63 73  t ::data2 [execs
2f50: 71 6c 20 7b 53 45 4c 45 43 54 20 72 6f 77 69 64  ql {SELECT rowid
2f60: 2c 20 2a 20 46 52 4f 4d 20 74 65 63 68 6f 7d 20  , * FROM techo} 
2f70: 64 62 32 5d 0a 20 20 64 6f 5f 74 65 73 74 20 24  db2].  do_test $
2f80: 74 6e 20 7b 0a 20 20 20 20 73 74 72 69 6e 67 20  tn {.    string 
2f90: 65 71 75 61 6c 20 24 3a 3a 64 61 74 61 31 20 24  equal $::data1 $
2fa0: 3a 3a 64 61 74 61 32 0a 20 20 7d 20 31 0a 7d 0a  ::data2.  } 1.}.
2fb0: 73 65 74 20 74 6e 20 30 0a 66 6f 72 65 61 63 68  set tn 0.foreach
2fc0: 20 73 74 6d 74 20 5b 6c 69 73 74 20 5c 0a 20 20   stmt [list \.  
2fd0: 7b 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 65 63  {INSERT INTO tec
2fe0: 68 6f 20 56 41 4c 55 45 53 28 27 61 62 63 27 2c  ho VALUES('abc',
2ff0: 20 27 64 65 66 27 2c 20 27 67 68 69 27 29 7d 20   'def', 'ghi')} 
3000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3010: 20 20 20 20 20 20 20 5c 0a 20 20 7b 49 4e 53 45         \.  {INSE
3020: 52 54 20 49 4e 54 4f 20 74 65 63 68 6f 20 53 45  RT INTO techo SE
3030: 4c 45 43 54 20 61 7c 7c 27 2e 27 7c 7c 72 6f 77  LECT a||'.'||row
3040: 69 64 2c 20 62 2c 20 63 20 46 52 4f 4d 20 74 65  id, b, c FROM te
3050: 63 68 6f 7d 20 20 20 20 20 20 20 20 20 20 20 20  cho}            
3060: 20 20 5c 0a 20 20 7b 49 4e 53 45 52 54 20 49 4e    \.  {INSERT IN
3070: 54 4f 20 74 65 63 68 6f 20 53 45 4c 45 43 54 20  TO techo SELECT 
3080: 61 7c 7c 27 78 27 7c 7c 72 6f 77 69 64 2c 20 62  a||'x'||rowid, b
3090: 2c 20 63 20 46 52 4f 4d 20 74 65 63 68 6f 7d 20  , c FROM techo} 
30a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
30b0: 20 7b 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 65   {INSERT INTO te
30c0: 63 68 6f 20 53 45 4c 45 43 54 20 61 7c 7c 27 79  cho SELECT a||'y
30d0: 27 7c 7c 72 6f 77 69 64 2c 20 62 2c 20 63 20 46  '||rowid, b, c F
30e0: 52 4f 4d 20 74 65 63 68 6f 7d 20 20 20 20 20 20  ROM techo}      
30f0: 20 20 20 20 20 20 20 20 5c 0a 20 20 7b 44 45 4c          \.  {DEL
3100: 45 54 45 20 46 52 4f 4d 20 74 65 63 68 6f 20 57  ETE FROM techo W
3110: 48 45 52 45 20 28 6f 69 64 20 25 20 33 29 20 3d  HERE (oid % 3) =
3120: 20 30 7d 20 20 20 20 20 20 20 20 20 20 20 20 20   0}             
3130: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3140: 20 20 20 5c 0a 20 20 7b 55 50 44 41 54 45 20 74     \.  {UPDATE t
3150: 65 63 68 6f 20 73 65 74 20 72 6f 77 69 64 20 3d  echo set rowid =
3160: 20 31 30 30 20 57 48 45 52 45 20 72 6f 77 69 64   100 WHERE rowid
3170: 20 3d 20 31 7d 20 20 20 20 20 20 20 20 20 20 20   = 1}           
3180: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
3190: 20 20 7b 49 4e 53 45 52 54 20 49 4e 54 4f 20 74    {INSERT INTO t
31a0: 65 63 68 6f 28 61 2c 20 62 29 20 56 41 4c 55 45  echo(a, b) VALUE
31b0: 53 28 27 68 65 6c 6c 6f 27 2c 20 27 77 6f 72 6c  S('hello', 'worl
31c0: 64 27 29 7d 20 20 20 20 20 20 20 20 20 20 20 20  d')}            
31d0: 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7b 44 45           \.  {DE
31e0: 4c 45 54 45 20 46 52 4f 4d 20 74 65 63 68 6f 7d  LETE FROM techo}
31f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3200: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3210: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3220: 20 20 20 20 5c 0a 5d 20 7b 0a 20 20 65 78 65 63      \.] {.  exec
3230: 73 71 6c 20 24 73 74 6d 74 0a 20 20 65 78 65 63  sql $stmt.  exec
3240: 73 71 6c 20 24 73 74 6d 74 20 64 62 32 0a 20 20  sql $stmt db2.  
3250: 63 68 65 63 6b 5f 65 63 68 6f 5f 74 61 62 6c 65  check_echo_table
3260: 20 76 74 61 62 31 2d 36 2e 36 2e 5b 69 6e 63 72   vtab1-6.6.[incr
3270: 20 74 6e 5d 0a 7d 0a 0a 0a 64 62 32 20 63 6c 6f   tn].}...db2 clo
3280: 73 65 0a 66 69 6e 69 73 68 5f 74 65 73 74 0a 0a  se.finish_test..