# This test should work in embedded server after mysqltest is fixed
-- source include/not_embedded.inc

# This test uses chmod, can't be run with root permissions
-- source include/not_as_root.inc

# ============================================================================
#
# Test of mysqltest itself
#
# There are three rules that determines what belong to each command
# 1. A normal command is delimited by the <delimiter> which by default is
#    set to ';'
#
#   ex: | select *
#       |   from t1;
#       |
#   Command: "select * from t1"
#
# 2. Special case is a line that starts with "--", this is a comment
#    ended when the new line character is reached. But the first word
#    in the comment may contain a valid command, which then will be
#    executed. This can be useful when sending commands that
#    contains <delimiter>
#
# 3. Special case is also a line that starts with '#' which is treated
#     as a comment and will be ended by new line character
#
# ============================================================================

# ----------------------------------------------------------------------------
# $mysql_errno contains the return code of the last command
# sent to the server.
# ----------------------------------------------------------------------------
# get $mysql_errno before the first statement
#     $mysql_errno should be -1
eval select $mysql_errno as "before_use_test" ;


# ----------------------------------------------------------------------------
# Positive case(statement)
# ----------------------------------------------------------------------------

select otto from (select 1 as otto) as t1;
# expectation = response
--error 0
select otto from (select 1 as otto) as t1;

# expectation <> response
-- // --error 1054
-- // select otto from (select 1 as otto) as t1;


# ----------------------------------------------------------------------------
# Negative case(statement):
# The derived table t1 does not contain a column named 'friedrich' . 
# --> ERROR 42S22: Unknown column 'friedrich' in 'field list and
# --> 1054: Unknown column 'friedrich' in 'field list'
# ----------------------------------------------------------------------------

# expectation <> response
#--error 0
#select friedrich from (select 1 as otto) as t1
--error 1
--exec echo "select friedrich from (select 1 as otto) as t1;" | $MYSQL_TEST  2>&1

# expectation = response
--error 1054
select friedrich from (select 1 as otto) as t1;

# The following unmasked unsuccessful statement must give
# 1. mysqltest gives a 'failed'
# 2. does not produce a r/<test case>.reject file !!!
# PLEASE uncomment it and check its effect
#select friedrich from (select 1 as otto) as t1;


# ----------------------------------------------------------------------------
# Tests for the new feature - SQLSTATE error code matching
# Positive case(statement)
# ----------------------------------------------------------------------------

# This syntax not allowed anymore, use --error S00000, see below
# expectation = response
#!S00000 select otto from (select 1 as otto) as t1;

--error S00000
select otto from (select 1 as otto) as t1;

# expectation <> response
#!S42S22 select otto from (select 1 as otto) as t1;
#--error S42S22
#select otto from (select 1 as otto) as t1;
--error 1
--exec echo "error S42S22; select otto from (select 1 as otto) as t1;" | $MYSQL_TEST  2>&1



# ----------------------------------------------------------------------------
# Negative case(statement)
# ----------------------------------------------------------------------------

# This syntax not allowed anymore, use --error S42S22, see below
# expectation = response
#!S42S22 select friedrich from (select 1 as otto) as t1;
--error S42S22
select friedrich from (select 1 as otto) as t1;

# expectation !=response
#!S00000 select friedrich from (select 1 as otto) as t1;
#--error S00000
#select friedrich from (select 1 as otto) as t1;
--error 1
--exec echo "error S00000; select friedrich from (select 1 as otto) as t1;" | $MYSQL_TEST  2>&1

# ----------------------------------------------------------------------------
# test cases for $mysql_errno
#
# $mysql_errno is a builtin variable of mysqltest and contains the return code
# of the last command sent to the server.
#
#      The following test cases often initialize $mysql_errno to 1064 by 
#      a command with wrong syntax.
#      Example: --error 1064      To prevent the abort after the error.
#               garbage ;
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# check mysql_errno = 0 after successful statement
# ----------------------------------------------------------------------------
select otto from (select 1 as otto) as t1;
eval select $mysql_errno as "after_successful_stmt_errno" ;

#----------------------------------------------------------------------------
# check mysql_errno = 1064 after statement with wrong syntax
# ----------------------------------------------------------------------------
--error 1064
garbage ;
eval select $mysql_errno as "after_wrong_syntax_errno" ;

# ----------------------------------------------------------------------------
# check if let $my_var= 'abc' ; affects $mysql_errno
# ----------------------------------------------------------------------------
--error 1064
garbage ;
let $my_var= 'abc' ;
eval select $mysql_errno as "after_let_var_equal_value" ;

# ----------------------------------------------------------------------------
# check if set @my_var= 'abc' ; affects $mysql_errno
# ----------------------------------------------------------------------------
--error 1064
garbage ;
set @my_var= 'abc' ;
eval select $mysql_errno as "after_set_var_equal_value" ;

# ----------------------------------------------------------------------------
#  check if the setting of --disable-warnings itself affects $mysql_errno
#  (May be --<whatever> modifies $mysql_errno.)
# ----------------------------------------------------------------------------
--error 1064
garbage ;
--disable_warnings
eval select $mysql_errno as "after_disable_warnings_command" ;

# ----------------------------------------------------------------------------
# check if --disable-warnings + command with warning affects the errno
# stored within $mysql_errno
# (May be disabled warnings affect $mysql_errno.)
# ----------------------------------------------------------------------------
drop table if exists t1 ;
--error 1064
garbage ;
drop table if exists t1 ;
eval select $mysql_errno as "after_disable_warnings" ;
--enable_warnings

# ----------------------------------------------------------------------------
# check if masked errors affect $mysql_errno
# ----------------------------------------------------------------------------
--error 1064
garbage ;
--error 1146
select 3 from t1 ;
eval select $mysql_errno as "after_minus_masked" ;
--error 1064
garbage ;
--error 1146
select 3 from t1 ;
eval select $mysql_errno as "after_!_masked" ;

# ----------------------------------------------------------------------------
# Will manipulations of $mysql_errno be possible and visible ?
# ----------------------------------------------------------------------------
--error 1064
garbage ;
let $mysql_errno= -1;
eval select $mysql_errno as "after_let_errno_equal_value" ;

# ----------------------------------------------------------------------------
# How affect actions on prepared statements $mysql_errno ?
# ----------------------------------------------------------------------------
# failing prepare
--error 1064
garbage ;
--error 1146
prepare stmt from "select 3 from t1" ;
eval select $mysql_errno as "after_failing_prepare" ;
create table t1 ( f1 char(10));

# successful prepare
--error 1064
garbage ;
prepare stmt from "select 3 from t1" ;
eval select $mysql_errno as "after_successful_prepare" ;

# successful execute
--error 1064
garbage ;
execute stmt;
eval select $mysql_errno as "after_successful_execute" ;

# failing execute (table has been dropped)
drop table t1;
--error 1064
garbage ;
--error 1146
execute stmt;
eval select $mysql_errno as "after_failing_execute" ;

# failing execute (unknown statement)
--error 1064
garbage ;
--error 1243
execute __stmt_;
eval select $mysql_errno as "after_failing_execute" ;

# successful deallocate
--error 1064
garbage ;
deallocate prepare stmt;
eval select $mysql_errno as "after_successful_deallocate" ;

# failing deallocate ( statement handle does not exist )
--error 1064
garbage ;
--error 1243
deallocate prepare __stmt_;
eval select $mysql_errno as "after_failing_deallocate" ;


# ----------------------------------------------------------------------------
# test cases for "--disable_abort_on_error"
#
# "--disable_abort_on_error" switches off the abort of mysqltest
# after "unmasked" failing statements.
#
# The default is "--enable_abort_on_error".
#
# "Maskings" are
#   --error <error number>  and  --error <error number>
# in the line before the failing statement.
#
# There are some additional test cases for $mysql_errno
# because "--disable_abort_on_error" enables a new situation.
# Example: "unmasked" statement fails + analysis of $mysql_errno
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# Switch off the abort on error and check the effect on $mysql_errno
# ----------------------------------------------------------------------------
--error 1064
garbage ;
--disable_abort_on_error
eval select $mysql_errno as "after_--disable_abort_on_error" ;

# ----------------------------------------------------------------------------
# "unmasked" failing statement should not cause an abort
# ----------------------------------------------------------------------------
select 3 from t1 ;

# ----------------------------------------------------------------------------
# masked failing statements
# ----------------------------------------------------------------------------
# expected error = response
--error 1146
select 3 from t1 ;
--error 1146
select 3 from t1 ;
eval select $mysql_errno as "after_!errno_masked_error" ;
# expected error <> response
# --error 1000
# select 3 from t1 ;
# --error 1000
# select 3 from t1 ;
--error 1
--exec echo "disable_abort_on_error; error 1000; select 3 from t1; error 1000; select 3 from t1;" | $MYSQL_TEST  2>&1

# ----------------------------------------------------------------------------
# Switch the abort on error on and check the effect on $mysql_errno
# ----------------------------------------------------------------------------
--error 1064
garbage ;
--enable_abort_on_error
eval select $mysql_errno as "after_--enable_abort_on_error" ;

# ----------------------------------------------------------------------------
# masked failing statements
# ----------------------------------------------------------------------------
# expected error = response
--error 1146
select 3 from t1 ;

# ----------------------------------------------------------------------------
# check that the old default behaviour is not changed
# Please remove the '#' to get the abort on error
# ----------------------------------------------------------------------------
#--error 1064
#select 3 from t1 ;
#
#select 3 from t1 ;

--error 1
--exec echo "disable_abort_on_error; enable_abort_on_error; error 1064; select 3 from t1; select 3 from t1;" | $MYSQL_TEST  2>&1


# ----------------------------------------------------------------------------
# Test comments
# ----------------------------------------------------------------------------

# This is a comment
# This is a ;  comment
# This is a -- comment
-- This is also a comment
-- # This is also a comment
-- This is also a ; comment

# ----------------------------------------------------------------------------
# Test comments with embedded command
# ----------------------------------------------------------------------------

--echo hello
--     echo hello
--    echo ;;;;;;;;

--echo # MySQL: -- The

# ----------------------------------------------------------------------------
# Test detect end of line "junk"
# Most likely caused by a missing delimiter
# ----------------------------------------------------------------------------

# Too many parameters to function
--error 1
--exec echo "sleep 5 6;" | $MYSQL_TEST 2>&1

# Too many parameters to function
--error 1
--exec echo "--sleep 5 6" | $MYSQL_TEST 2>&1

#
# Missing delimiter
# The comment will be "sucked into" the sleep command since
# delimiter is missing until after "show status"
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest.sql
sleep 4
# A comment
show status;
EOF
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

#
# Missing delimiter until eof
# The comment will be "sucked into" the sleep command since
# delimiter is missing
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest.sql
sleep 7
# Another comment
EOF
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

#
# Missing delimiter until "disable_query_log"
#
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest.sql
disconnect default

#
# comment
# comment 3
disable_query_log;
EOF
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

#
# Missing delimiter until "disable_query_log"
#
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest.sql
disconnect default

#
# comment

# comment 3
disable_query_log;
EOF
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

#
# Missing delimiter until eof
#
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest.sql
disconnect default

#
# comment
# comment2

# comment 3
--disable_query_log
EOF
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

#
# Missing delimiter until eof
#
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest.sql
disconnect default # comment
# comment part2

# comment 3
--disable_query_log
EOF
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

#
# Extra delimiter
#
--error 1
--exec echo "--sleep 4;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--disable_query_log;" | $MYSQL_TEST 2>&1


# Allow trailing # comment
--sleep 1 # Wait for insert delayed to be executed.
--sleep 1 	 # Wait for insert delayed to be executed.

# ----------------------------------------------------------------------------
# Test error
# ----------------------------------------------------------------------------

# Missing argument
--error 1
--exec echo "error;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--error" | $MYSQL_TEST 2>&1

# First char must be uppercase 'S' or 'E' or [0-9]
--error 1
--exec echo "--error s99999" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--error e99999" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--error 9eeeee" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--error 1sssss" | $MYSQL_TEST 2>&1

# First char 'S' but too long
--error 1
--exec echo "--error S999999" | $MYSQL_TEST 2>&1

# First char 'S' but lowercase char found
--error 1
--exec echo "--error S99a99" | $MYSQL_TEST 2>&1

# First char 'S' but too short
--error 1
--exec echo "--error S9999" | $MYSQL_TEST 2>&1

# First char 'E' but not found in error array
--error 1
--exec echo "--error E9999" | $MYSQL_TEST 2>&1

# First char [0-9] but contains chars
--error 1
--exec echo "--error 999e9" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--error 9b" | $MYSQL_TEST 2>&1

# Multiple errorcodes separated by ','
--error 1,1,1,1
#--error 9,ER_PARSE_ERROR
#--error ER_PARSE_ERROR
#--error 9,ER_PARSE_ERROR,9,ER_PARSE_ERROR
#--error 9, ER_PARSE_ERROR,  9,   ER_PARSE_ERROR
#--error 9,S00000,9,ER_PARSE_ERROR
#--error 9,S00000,9,ER_PARSE_ERROR,ER_PARSE_ERROR,ER_PARSE_ERROR,9,10,11,12
--error 9,S00000,9
--error 9,S00000,9,9,10,11,12
--error 9 ,10
--error 9 , 10
--error    9   ,   10
--error    9   ,   10

# Too many errorcodes specified
--error 1
--exec echo "--error 1,2,3,4,5,6,7,8,9,10,11" | $MYSQL_TEST 2>&1


# ----------------------------------------------------------------------------
# Test echo command
# ----------------------------------------------------------------------------

echo MySQL;
echo "MySQL";
echo MySQL: The world''s most popular open source database;
echo "MySQL: The world's most popular open source database";

echo MySQL: The world''s
     most popular open
     source database;

echo # MySQL: The world''s
# most popular open
# source database;

echo - MySQL: The world''s
- most popular open
- source database;

echo - MySQL: The world''s
-- most popular
-- open source database;

echo # MySQL: The
--world''s
# most popular
-- open
- source database;

echo "MySQL: The world's most popular; open source database";
echo "MySQL: The world's most popular ; open source database";
echo "MySQL: The world's most popular ;open source database";
echo echo message echo message;


echo ;

# Illegal use of echo

--error 1
--exec echo "echo \$;" | $MYSQL_TEST 2>&1


# ----------------------------------------------------------------------------
# Test exec command
# ----------------------------------------------------------------------------

# Illegal use of exec
--error 1
--exec echo "--exec false" | $MYSQL_TEST 2>&1

--error 1
--exec echo "--exec " | $MYSQL_TEST 2>&1

# ----------------------------------------------------------------------------
# Test let command
# ----------------------------------------------------------------------------

let $message=MySQL;
echo $message;

let $message="MySQL";
echo $message;

let $message= MySQL: The
 world''s most
 popular open
 source database;
echo $message;

let $message= # MySQL: The
# world''s most
# popular open
# source database;
echo $message;

let $message=  -- MySQL: The
-- world''s most
-- popular
-- open source database;
echo $message;

let $message=  # MySQL: The
- world''s most
-- popular open
# source database;
echo $message;

echo '$message';
echo "$message";

let $1=hej;
echo $1;

let   $1   =hej ;
echo $1;

let $1 = hej;
echo $1;

let $1=1;
let $2=$1;
echo $2;
let $5=$6;
echo $5;
echo $6;

let $where=a long variable content;
echo $where;

let $where2= $where;
echo $where2;

let $where3=a long $where variable content;
echo $where3;

let $where3=a long \\\$where variable content;
echo $where3;

let $novar1= $novar2;
echo $novar1;

let $cat=na;
let $cat=ba$cat$cat;
echo banana = $cat;

# ba\$cat\$cat should have been sufficient.
# ba\\\$cat\\\$cat -> ba\$cat\$cat -> ba$cat$cat -> banana
# Magnus' upcoming patch will fix the missing second interpretation.
let $cat=ba\\\$cat\\\$cat;
echo Not a banana: $cat;


# Test illegal uses of let

--error 1
--exec echo "let ;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "let \$=hi;" | $MYSQL_TEST  2>&1

--error 1
--exec echo "let \$1 hi;" | $MYSQL_TEST  2>&1

--error 1
--exec echo "let \$m hi;" | $MYSQL_TEST  2>&1

--error 1
--exec echo "let \$hi;" | $MYSQL_TEST  2>&1

--error 1
--exec echo "let \$ hi;" | $MYSQL_TEST  2>&1

--error 1
--exec echo "let =hi;" | $MYSQL_TEST  2>&1

--error 1
--exec echo "let hi;" | $MYSQL_TEST  2>&1

# More advanced test for bug#17280
let $success= 1;
--echo # Execute: --echo # <whatever> success: \$success
--echo # <whatever> success: $success
--echo # Execute: echo # <whatever> success: \$success ;
echo # <whatever> success: $success ;

--echo # The next two variants work fine and expand the content of \$success
--echo # Execute: --echo \$success
--echo $success
--echo # Execute: echo \$success ;
echo $success ;

# ----------------------------------------------------------------------------
# Test to assign let from query
# let $<var_name>=`<query>`;
# ----------------------------------------------------------------------------
--disable_parsing
echo var1;
let $var1= `select "hi" as "Col", 1 as "Column1", "hi there" as Col3`;
echo $var1;
echo $var1_Col;
echo $var1_Column1;
echo $var1_Col3;

echo var2;
let $var2= `select 2 as "Column num 2"`;
echo $var2;
echo $var2_Column num 2;
echo $var2_Column;

echo var2 again;
let $var2= `select 2 as "Column num 2"`;
echo $var2;
echo $var2_Column num 2;
echo $var2_Column_num_2;
echo $var2_Column;

echo var3 two columns with same name;
let $var3= `select 1 as "Col", 2 as "Col", 3 as "var3"`;
echo $var3;
echo $var3_Col;
echo $var3_Col;
echo $var3_var3;

#echo failing query in let;
#--error 1
#--exec echo "let $var2= `failing query;`" | $MYSQL_TEST 2>&1
--enable_parsing
# ----------------------------------------------------------------------------
# Test source command
# ----------------------------------------------------------------------------

# Test illegal uses of source

--error 1
--exec echo "source ;" | $MYSQL_TEST 2>&1

# Fix win paths
--replace_result \\ /
# Source a nonexisting file
--error 1
--exec echo "source non_existingFile;" | $MYSQL_TEST 2>&1

# Too many source
--exec echo "source $MYSQLTEST_VARDIR/tmp/recursive.sql;" > $MYSQLTEST_VARDIR/tmp/recursive.sql
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--error 1
--exec echo "source $MYSQLTEST_VARDIR/tmp/recursive.sql;" | $MYSQL_TEST 2>&1

# Source a file with error
--exec echo "garbage ;" > $MYSQLTEST_VARDIR/tmp/error.sql
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--error 1
--exec echo "source $MYSQLTEST_VARDIR/tmp/error.sql;" | $MYSQL_TEST 2>&1


# Test execution of source in a while loop
--write_file $MYSQLTEST_VARDIR/tmp/sourced.inc
echo here is the sourced script;
EOF
--disable_query_log
let $outer= 2; # Number of outer loops
while ($outer)
{
  eval SELECT '$outer = outer loop variable after while' AS "";

  --source $MYSQLTEST_VARDIR/tmp/sourced.inc

  eval SELECT '$outer = outer loop variable before dec' AS "";
  dec $outer;
  eval SELECT '$outer = outer loop variable after dec' AS "";
}

let $outer= 2; # Number of outer loops
while ($outer)
{
  eval SELECT '$outer = outer loop variable after while' AS "";

  echo here is the sourced script;

  eval SELECT '$outer = outer loop variable before dec' AS "";
  dec $outer;
  eval SELECT '$outer = outer loop variable after dec' AS "";
}


# Test execution of source in a while loop
--disable_abort_on_error
# Sourcing of a file within while loop, sourced file will
# source other file
let $num= 9;
while ($num)
{
   SELECT 'In loop' AS "";
   --source $MYSQLTEST_VARDIR/tmp/sourced.inc
   dec $num;
}
--enable_abort_on_error
--enable_query_log
--remove_file $MYSQLTEST_VARDIR/tmp/sourced.inc

# ----------------------------------------------------------------------------
# Test sleep command
# ----------------------------------------------------------------------------

sleep 0.5;
sleep 1;
real_sleep 1;

# Missing parameter
--error 1
--exec echo "sleep ;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "real_sleep ;" | $MYSQL_TEST 2>&1

# Illegal parameter
--error 1
--exec echo "sleep abc;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "real_sleep abc;" | $MYSQL_TEST 2>&1

# ----------------------------------------------------------------------------
# Test inc
# ----------------------------------------------------------------------------
inc $i;
echo $i;
inc $i;
echo $i;
let $i=100;
inc $i;
echo $i;

let $i=hej;
echo $i;
inc $i;
echo $i;

--error 1
--exec echo "inc;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "inc i;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "let \$i=100; inc \$i 1000; echo \$i;" | $MYSQL_TEST 2>&1

inc $i; inc $i; inc $i; --echo $i
echo $i;


# ----------------------------------------------------------------------------
# Test dec
# ----------------------------------------------------------------------------

dec $d;
echo $d;
dec $d;
echo $d;
let $d=100;
dec $d;
echo $d;

let $d=hej;
echo $d;
dec $d;
echo $d;

--error 1
--exec echo "dec;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "dec i;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "let \$i=100; dec \$i 1000; echo \$i;" | $MYSQL_TEST 2>&1


# ----------------------------------------------------------------------------
# Test system
# ----------------------------------------------------------------------------
system ls > /dev/null;
system echo "hej" > /dev/null;
--system ls > /dev/null
--system echo "hej" > /dev/null;

--error 1
--exec echo "system;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "system $NONEXISTSINFVAREABLI;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "system false;" | $MYSQL_TEST 2>&1

--disable_abort_on_error
system NonExistsinfComamdn 2> /dev/null;
--enable_abort_on_error


# ----------------------------------------------------------------------------
# Test delimiter
# ----------------------------------------------------------------------------

delimiter stop;
echo teststop
delimiter ;stop
echo test2;
--delimiter stop
echo test3stop
--delimiter ;
echo test4;


# ----------------------------------------------------------------------------
# Test if
# ----------------------------------------------------------------------------

let $counter=10;
if ($counter)
{
  echo Counter is greater than 0, (counter=10);
}
if (!$counter)
{
  echo Counter is not 0, (counter=10);
}
let $counter=0;
if ($counter)
{
  echo Counter is greater than 0, (counter=0);
}
if (!$counter)
{
  echo Counter is not 0, (counter=0);
}

# ----------------------------------------------------------------------------
# Test while, { and }
# ----------------------------------------------------------------------------

let $i=1;
while ($i)
{
  echo $i;
  dec $i;
}
# One liner
#let $i=1;while ($i){echo $i;dec $i;}

let $i=0;
while (!$i)
{
  echo Testing while with not;
  inc $i;
}

# Exceed max nesting level
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest_while.inc
let $1 = 10;
while ($1)
{
while ($1)
{
while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
 while ($1)
{
  echo $1;
  dec $1;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
EOF
# Fix win path
--replace_result \\ / $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--error 1
--exec echo "source $MYSQLTEST_VARDIR/tmp/mysqltest_while.inc;" | $MYSQL_TEST 2>&1
--remove_file $MYSQLTEST_VARDIR/tmp/mysqltest_while.inc
--error 1
--exec echo "while \$i;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "while (\$i;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "let \$i=1; while (\$i) dec \$i;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "};" | $MYSQL_TEST 2>&1
--error 1
--exec echo "end;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "{;" | $MYSQL_TEST 2>&1

--system echo "while (0)" > $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "echo hej;" >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

--system echo "while (0)" > $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "{echo hej;" >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

--system echo "while (0){" > $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "echo hej;" >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--error 1
--exec $MYSQL_TEST < $MYSQLTEST_VARDIR/tmp/mysqltest.sql 2>&1

# ----------------------------------------------------------------------------
# Test error messages returned from comments starting with a command
# ----------------------------------------------------------------------------
--error 1
--exec echo "--if the other server is down" | $MYSQL_TEST 2>&1

--error 1
--exec echo "-- end when ..." | $MYSQL_TEST 2>&1

# ----------------------------------------------------------------------------
# Test replace
# ----------------------------------------------------------------------------
--replace_result a b
select "a" as col1, "c" as col2;

--replace_result a b c d
select "a" as col1, "c" as col2;

--error 1
--exec echo "--replace_result a" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--replace_result a;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "replace_result a;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "replace_result a ;" | $MYSQL_TEST 2>&1
--exec echo "replace_result a b; echo OK;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--replace_result a b c" | $MYSQL_TEST 2>&1
--error 1
--exec echo "replace_result a b c ;" | $MYSQL_TEST 2>&1


--replace_column 1 b
select "a" as col1, "c" as col2;

--replace_column 1 b 2 d
select "a" as col1, "c" as col2;

--error 1
--exec echo "--replace_column a" | $MYSQL_TEST 2>&1

--error 1
--exec echo "--replace_column 1" | $MYSQL_TEST 2>&1

--error 1
--exec echo "--replace_column a b" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--replace_column a 1" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--replace_column 1 b c " | $MYSQL_TEST 2>&1


# ----------------------------------------------------------------------------
# Test sync_with_master
# ----------------------------------------------------------------------------
--error 1
--exec echo "save_master_pos; sync_with_master 10!;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "save_master_pos; sync_with_master 10 !;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "save_master_pos; sync_with_master a;" | $MYSQL_TEST 2>&1

# ----------------------------------------------------------------------------
# Test connect
# ----------------------------------------------------------------------------

--error 1
--exec echo "connect;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "connect ();" | $MYSQL_TEST 2>&1
--error 1
--exec echo "connect (con2);" | $MYSQL_TEST 2>&1
--error 1
--exec echo "connect (con2,);" | $MYSQL_TEST 2>&1
--error 1
--exec echo "connect (con2,localhost,root,,illegal_db);" | $MYSQL_TEST 2>&1
--error 1
--exec echo "connect (con1,localhost,root,,,illegal_port,);" | $MYSQL_TEST 2>&1
--error 1
--exec echo "connect (con1,localhost,root,,,,,SMTP POP);" | $MYSQL_TEST 2>&1

# Repeat connect/disconnect
--write_file $MYSQLTEST_VARDIR/tmp/mysqltest.sql
let $i=100;
while ($i)
{
  connect (test_con1,localhost,root,,);
  disconnect test_con1;
  dec $i;
}
EOF
--exec echo "source $MYSQLTEST_VARDIR/tmp/mysqltest.sql; echo OK;" | $MYSQL_TEST 2>&1

# Repeat connect/disconnect, exceed max number of connections
--system echo "let \$i=200;"                              > $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "while (\$i)"                              >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "{"                                        >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo " connect (test_con1,localhost,root,,); "  >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo " disconnect test_con1; "                  >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo " dec \$i;                               " >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "}"                                        >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--error 1
--exec echo "source $MYSQLTEST_VARDIR/tmp/mysqltest.sql;" | $MYSQL_TEST 2>&1

# Select disconnected connection
--system echo "connect (test_con1,localhost,root,,);"    > $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "disconnect test_con1; "                  >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "connection test_con1;"                   >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--error 1
--exec echo "source $MYSQLTEST_VARDIR/tmp/mysqltest.sql;" | $MYSQL_TEST 2>&1

# Connection name already used
--system echo "connect (test_con1,localhost,root,,);"    > $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--system echo "connect (test_con1,localhost,root,,);"   >> $MYSQLTEST_VARDIR/tmp/mysqltest.sql
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
--error 1
--exec echo "source $MYSQLTEST_VARDIR/tmp/mysqltest.sql;" | $MYSQL_TEST 2>&1

# connect when "disable_abort_on_error" caused "connection not found"
--replace_result $MASTER_MYSOCK MASTER_SOCKET $MASTER_MYPORT MASTER_PORT
--disable_abort_on_error
connect (con1,localhost,root,,);
connection default;
connection con1;
--enable_abort_on_error

# ----------------------------------------------------------------------------
# Test mysqltest arguments
# ----------------------------------------------------------------------------

# -x <file_name>, use the file specified after -x as the test file
--exec $MYSQL_TEST < $MYSQL_TEST_DIR/include/mysqltest-x.inc
--exec $MYSQL_TEST -x $MYSQL_TEST_DIR/include/mysqltest-x.inc
--exec $MYSQL_TEST --test_file=$MYSQL_TEST_DIR/include/mysqltest-x.inc
# Fix Win paths
--replace_result \\ /
--error 1
--exec $MYSQL_TEST -x non_existing_file.inc 2>&1


# ----------------------------------------------------------------------------
# TODO Test queries, especially their errormessages... so it's easy to debug 
# new scripts and diagnose errors
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# Test bug#12386
# ----------------------------------------------------------------------------
let $num= 2;
while ($num)
{
   --error 1064
   failing_statement;

   dec $num;
}

SELECT 1 as a;


#
# Bug #10251: Identifiers containing quotes not handled correctly
#
select 1 as `a'b`, 2 as `a"b`;

# Test escaping of quotes
select 'aaa\\','aa''a',"aa""a";

#
# Check of include/show_msg.inc and include/show_msg80.inc
#

# The message contains in most cases a string with the default character set
let $message= Here comes a message;
--source include/show_msg.inc

# The message could also contain a string with character set utf8
let $message= `SELECT USER()`;
--source include/show_msg.inc

# The message contains more then 80 characters on multiple lines
# and is kept between double quotes.
let $message= 
"Here comes a very very long message that
    - is longer then 80 characters    and
    - consists of several lines";
--source include/show_msg80.inc

# The message contains more then 80 characters on multiple lines
# and uses the auxiliary character "." at the beginning of the message lines.
let $message= . Here comes a very very long message that
              .      - is longer then 80 characters    and
              .      - consists of several lines;
--source include/show_msg80.inc

#
# Test --enable_parsing / disable_parsing
#
--disable_query_log
--disable_parsing
# The following will not enable query logging
--enable_query_log
select "this will not be executed";
--enable_parsing
select "this will be executed";
--enable_query_log

#
# Test zero length result file. Should not pass
#
--exec touch $MYSQLTEST_VARDIR/tmp/zero_length_file.result
--exec echo "echo ok;" > $MYSQLTEST_VARDIR/tmp/query.sql
--error 1
--exec $MYSQL_TEST -x $MYSQLTEST_VARDIR/tmp/query.sql -R $MYSQLTEST_VARDIR/tmp/zero_length_file.result  2>&1
#
# Test that a test file that does not generate any output fails.
#
--exec echo "let \$i= 1;" > $MYSQLTEST_VARDIR/tmp/query.sql
--error 1
--exec $MYSQL_TEST -x $MYSQLTEST_VARDIR/tmp/query.sql  2>&1

#
# Test that mysqltest fails when there are no queries executed
# but a result file exists
# NOTE! This will never happen as long as it's not allowed to have
# test files that produce no output
#--exec echo "something" > $MYSQLTEST_VARDIR/tmp/result_file.result
#--exec echo "let \$i= 1;" > $MYSQLTEST_VARDIR/tmp/query.sql
#--error 1
#--exec $MYSQL_TEST -x $MYSQLTEST_VARDIR/tmp/query.sql -R $MYSQLTEST_VARDIR/tmp/result_file.result 2>&1

#
# Bug #11731 mysqltest in multi-statement queries ignores errors in
#            non-1st queries
#

echo Failing multi statement query;
# PS does not support multi statement
--exec echo "--disable_ps_protocol"                    > $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "delimiter ||||;"                         >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "create table t1 (a int primary key);"    >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "insert into t1 values (1);"              >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "select 'select-me';"                     >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "insertz 'error query'||||"               >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "delimiter ;||||"                         >> $MYSQLTEST_VARDIR/tmp/bug11731.sql

--error 1
--exec $MYSQL_TEST -x $MYSQLTEST_VARDIR/tmp/bug11731.sql 2>&1
drop table t1;

--error 1
--exec $MYSQL_TEST --record -x $MYSQLTEST_VARDIR/tmp/bug11731.sql -R $MYSQLTEST_VARDIR/tmp/bug11731.out 2>&1
# The .out file should be non existent
--exec test ! -s $MYSQLTEST_VARDIR/tmp/bug11731.out
drop table t1;


echo Multi statement using expected error;
# PS does not support multi statement
--exec echo "--disable_ps_protocol"                    > $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "delimiter ||||;"                         >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "--error 1064"                            >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "create table t1 (a int primary key);"    >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "insert into t1 values (1);"              >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "select 'select-me';"                     >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "insertz "error query"||||"               >> $MYSQLTEST_VARDIR/tmp/bug11731.sql
--exec echo "delimiter ;||||"                         >> $MYSQLTEST_VARDIR/tmp/bug11731.sql

# These two should work since the error is expected
--exec $MYSQL_TEST -x $MYSQLTEST_VARDIR/tmp/bug11731.sql  2>&1
drop table t1;

--exec $MYSQL_TEST --record -x $MYSQLTEST_VARDIR/tmp/bug11731.sql -R $MYSQLTEST_VARDIR/tmp/bug11731.out 2>&1
# The .out file should exist
--exec test -s $MYSQLTEST_VARDIR/tmp/bug11731.out
drop table t1;

#
# Bug#19890  	mysqltest: "query" command is broken
#

# It should be possible to use the command "query" to force mysqltest to
# send the command to the server although it's a builtin mysqltest command.
--error 1064
query sleep;

--error 1064
--query sleep

# Just an empty query command
--error 1065
query ;

# test for replace_regex
--replace_regex /at/b/
select "at" as col1, "c" as col2;

--replace_regex /at/b/i
select "at" as col1, "AT" as col2, "c" as col3;

--replace_regex /a/b/ /ct/d/
select "a" as col1, "ct" as col2; 

--replace_regex /(strawberry)/raspberry and \1/ /blueberry/blackberry/ /potato/tomato/;
select "strawberry","blueberry","potato";

--error 1
--exec echo "--replace_regex a" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--replace_regex a;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "replace_regex a;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "replace_regex a ;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "replace_regex a b; echo OK;" | $MYSQL_TEST 2>&1
--error 1
--exec echo "--replace_regex /a b c" | $MYSQL_TEST 2>&1
--error 1
--exec echo "replace_regex /a /b c ;" | $MYSQL_TEST 2>&1 

# REQUIREMENT
#   replace_regex should replace substitutions from left to right in output

create table t1 (a int, b int);
insert into t1 values (1,3);
insert into t1 values (2,4);
--replace_regex /A/C/ /B/D/i /3/2/ /2/1/
select * from t1;
drop table t1;

# ----------------------------------------------------------------------------
# test for remove_file
# ----------------------------------------------------------------------------

--error 1
--exec echo "remove_file ;" | $MYSQL_TEST 2>&1

--error 1
remove_file non_existing_file;

# ----------------------------------------------------------------------------
# test for write_file
# ----------------------------------------------------------------------------
--error 1
--exec echo "write_file ;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "write_file filename ;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "write_file filename \";" | $MYSQL_TEST 2>&1

write_file $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
Content for test_file1
EOF
file_exists $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
remove_file $MYSQLTEST_VARDIR/tmp/test_file1.tmp;

write_file $MYSQLTEST_VARDIR/tmp/test_file1.tmp END_DELIMITER;
Content for test_file1 contains EOF
END_DELIMITER
file_exists $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
remove_file $MYSQLTEST_VARDIR/tmp/test_file1.tmp;

# ----------------------------------------------------------------------------
# test for file_exist
# ----------------------------------------------------------------------------
--error 1
--exec echo "file_exists ;" | $MYSQL_TEST 2>&1

--error 0,1
remove_file $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
--error 1
file_exists $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
write_file $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
Content for test_file1
EOF
file_exists $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
remove_file $MYSQLTEST_VARDIR/tmp/test_file1.tmp;
--error 1
file_exists $MYSQLTEST_VARDIR/tmp/test_file1.tmp;


# ----------------------------------------------------------------------------
# test for copy_file
# ----------------------------------------------------------------------------
--write_file $MYSQLTEST_VARDIR/tmp/file1.tmp
file1
EOF

copy_file $MYSQLTEST_VARDIR/tmp/file1.tmp $MYSQLTEST_VARDIR/tmp/file2.tmp;
file_exists $MYSQLTEST_VARDIR/tmp/file2.tmp;
remove_file $MYSQLTEST_VARDIR/tmp/file1.tmp;
remove_file $MYSQLTEST_VARDIR/tmp/file2.tmp;

--error 1
--exec echo "copy_file ;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "copy_file from_file;" | $MYSQL_TEST 2>&1

# ----------------------------------------------------------------------------
# test for chmod
# ----------------------------------------------------------------------------
--write_file $MYSQLTEST_VARDIR/tmp/file1.tmp
file1
EOF

chmod 0000 $MYSQLTEST_VARDIR/tmp/file1.tmp;
# The below write fails, but --error is not implemented
# for write_file
#--write_file $MYSQLTEST_VARDIR/tmp/file1.tmp
#test should fail
#EOF

chmod 0777 $MYSQLTEST_VARDIR/tmp/file1.tmp;
--write_file $MYSQLTEST_VARDIR/tmp/file1.tmp
test2
EOF

remove_file $MYSQLTEST_VARDIR/tmp/file1.tmp;

--error 1
--exec echo "chmod ;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "chmod 0 from_file;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "chmod 08 from_file;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "chmod from_file;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "chmod ABZD from_file;" | $MYSQL_TEST 2>&1

--error 1
--exec echo "chmod 06789 from_file;" | $MYSQL_TEST 2>&1


# ----------------------------------------------------------------------------
# test for perl
# ----------------------------------------------------------------------------
--perl
print "hello\n";
EOF

--perl EOF
print "hello\n";
EOF

--perl DELIMITER
print "hello\n";
DELIMITER

--error 1
--exec echo "perl TOO_LONG_DELIMITER ;" | $MYSQL_TEST 2>&1

perl;
print "hello\n";
EOF

perl;
  # Print "hello"
  print "hello\n";
EOF

# ----------------------------------------------------------------------------
# test for die
# ----------------------------------------------------------------------------

--error 1
--exec echo "die test of die;" | $MYSQL_TEST 2>&1


# ----------------------------------------------------------------------------
# test for exit
# ----------------------------------------------------------------------------

--exec echo "echo Some output; exit; echo Not this;" | $MYSQL_TEST 2>&1


--echo End of tests
