#!/usr/bin/env tclsh

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

do_execsql_test select-const-1 {
  SELECT 1
} {1}

do_execsql_test select-const-2 {
  SELECT 2
} {2}

do_execsql_test select-true {
  SELECT true
} {1}

do_execsql_test select-false {
  SELECT false
} {0}

do_execsql_test select-text-escape-1 {
  SELECT '''a'
} {'a}

do_execsql_test select-blob-empty {
  SELECT x'';
} {}

do_execsql_test select-blob-ascii {
  SELECT x'6C696D626f';
} {limbo}

do_execsql_test select-blob-emoji {
  SELECT x'F09FA680';
} {🦀}

do_execsql_test select-limit-0 {
  SELECT id FROM users LIMIT 0;
} {}

# ORDER BY id here because sqlite uses age_idx here and we (yet) don't so force it to evaluate in ID order
do_execsql_test select-limit-true {
  SELECT id FROM users ORDER BY id LIMIT true;
} {1}

do_execsql_test select-limit-false {
  SELECT id FROM users ORDER BY id LIMIT false;
} {}

do_execsql_test realify {
    select price from products limit 1;
} {79.0}

do_execsql_test select-add {
    select u.age + 1 from users u where u.age = 91 limit 1;
} {92}

do_execsql_test select-subtract {
    select u.age - 1 from users u where u.age = 91 limit 1;
} {90}

do_execsql_test case-insensitive-columns {
    select u.aGe + 1 from USERS u where U.AGe = 91 limit 1;
} {92}

do_execsql_test table-star {
    select p.*, p.name from products p limit 1;
} {1|hat|79.0|hat}

do_execsql_test table-star-2 {
    select p.*, u.first_name from users u join products p on u.id = p.id limit 1;
} {1|hat|79.0|Jamie}

do_execsql_test select_with_quoting {
  select `users`.id from [users] where users.[id] = 5;
} {5}

do_execsql_test select_with_quoting_2 {
  select "users".`id` from users where `users`.[id] = 5;
} {5}

do_execsql_test seekrowid {
    select * from users u where u.id = 5;
} {"5|Edward|Miller|christiankramer@example.com|725-281-1033|08522 English Plain|Lake Keith|ID|23283|15"}

do_execsql_test select_parenthesized {
  select (price + 100) from products limit 1;
} {179.0}

do_execsql_test select_case_base_else {
  select case when 0 then 'false' when 1 then 'true' else 'null' end;
} {true}

do_execsql_test select_case_noelse_null {
  select case when 0 then 0 end;
} {}

do_execsql_test select_base_case_else {
  select case 1 when 0 then 'zero' when 1 then 'one' else 'two' end;
} {one}

do_execsql_test select_base_case_noelse_null {
  select case 'null else' when 0 then 0 when 1 then 1 end;
} {}
