# 2004 November 22
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#*************************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is DECLARE...CURSOR functionality
#
# $Id: cursor.test,v 1.1 2004/11/22 19:12:21 drh Exp $
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# If SQLITE_OMIT_CURSOR is defined, omit this file.
ifcapable {!cursor} {
  finish_test
  return
}

########
# Test the logic that creates and destroys cursors
########
do_test cursor-1.1 {
  execsql {
    CREATE TABLE t1(a,b,c);
    CREATE INDEX t1i1 ON t1(a);
    CREATE INDEX t1i2 ON t1(b,c);
  }
  execsql {
    DECLARE c1 CURSOR FOR SELECT c FROM t1 ORDER BY a;
  }
} {}
ifcapable schema_pragmas {
  do_test cursor-1.2 {
    execsql {PRAGMA cursor_list}
  } {0 c1}
}
do_test cursor-1.3 {
  execsql {
    DECLARE c2 CURSOR FOR SELECT a FROM t1 ORDER BY b, c;
  }
} {}
ifcapable schema_pragmas {
  do_test cursor-1.4 {
    execsql {PRAGMA cursor_list}
  } {0 c1 1 c2}
}
do_test cursor-1.5 {
  catchsql {
    CLOSE c3;
  }
} {1 {no such cursor: c3}}
ifcapable schema_pragmas {
  do_test cursor-1.6 {
    execsql {PRAGMA cursor_list}
  } {0 c1 1 c2}
}
do_test cursor-1.7 {
  catchsql {
    CLOSE c1;
  }
} {0 {}}
ifcapable schema_pragmas {
  do_test cursor-1.8 {
    execsql {PRAGMA cursor_list}
  } {1 c2}
}
do_test cursor-1.9 {
  catchsql {
    CLOSE c1;
  }
} {1 {no such cursor: c1}}
ifcapable schema_pragmas {
  do_test cursor-1.10 {
    execsql {PRAGMA cursor_list}
  } {1 c2}
}
do_test cursor-1.11 {
  catchsql {
    DECLARE c2 CURSOR FOR SELECT * FROM t1;
  }
} {1 {another cursor named c2 already exists}}
do_test cursor-1.12 {
  catchsql {
    DECLARE c3 CURSOR FOR SELECT * FROM t1;
  }
} {0 {}}
ifcapable schema_pragmas {
  do_test cursor-1.13 {
    execsql {PRAGMA cursor_list}
  } {0 c3 1 c2}
}
do_test cursor-1.14 {
  execsql {
    CLOSE c2;
    CLOSE c3;
  }
} {}
ifcapable schema_pragmas {
  do_test cursor-1.15 {
    execsql {PRAGMA cursor_list}
  } {}
}

set all {}
for {set i 1} {$i<=50} {incr i} {
  lappend all [expr {$i-1}] x$i
  do_test cursor-2.1.$i.1 {
    execsql "DECLARE x$i CURSOR FOR SELECT * FROM t1"
  } {}
  ifcapable schema_pragmas {
    do_test cursor-2.1.$i.2 {
      execsql {PRAGMA cursor_list}
    } $all
  }
}
for {set i 1} {$i<=50} {incr i} {
  set all [lrange $all 2 end]
  do_test cursor-2.2.$i.1 {
    execsql "CLOSE x$i"
  } {}
  ifcapable schema_pragmas {
    do_test cursor-2.2.$i.2 {
      execsql {PRAGMA cursor_list}
    } $all
  }
}  


finish_test
