summaryrefslogtreecommitdiffstats
path: root/mysql-test/include/write_result_to_file.inc
blob: 3e2ddf489570d0fe93aa419bba25ee1113c2b2e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ==== Purpose ====
#
# Execute a statement and write the result to a file.  This is useful
# if the output needs more advanced parsing than can be done by
# mysqltest commands.
#
# ==== Usage ====
#
# --let $statement= <STATEMENT>
# --let $output_file= {<FILE>|GENERATE}
# [--let $server_number= <NUMBER>]
# [--let $dont_print_statement= 1]
# --source include/write_result_to_file.inc
#
# Parameters:
#   $statement
#     The statement to execute.
#
#   $server_number
#     1 for the master, 2 for the slave, 3 for next server, etc.
#     If omitted, uses current connection.
#
#   $output_file
#     Name of file to write. If omitted, generates a new filename and
#     stores the name both in the mysqltest variable $output_file and
#     in the environment variable $OUTPUT_FILE.
#
#   $dont_print_statement
#     By default, the statement is echoed to the result log.  If the
#     statement contains non-deterministic output, set this variable
#     to suppress it.

--let _WRTF_SERVER_NUMBER= $server_number
if (!$server_number)
{
  --let _WRTF_SERVER_NUMBER= `SELECT 1 + @@PORT - $MASTER_MYPORT`
}

--let $_write_result_msg= [server=$_WRTF_SERVER_NUMBER]
if (!$dont_print_statement)
{
  --let $_write_result_msg= [server=$_WRTF_SERVER_NUMBER statement=$statement]
}

--let $include_filename= write_result_to_file.inc $_write_result_msg
--source include/begin_include_file.inc

if ($statement == '')
{
  --die !!!ERROR IN TEST: mysqltest variable 'statement' not set in write_result_to_file.inc
}
--let _WRTF_STATEMENT= $statement

if (!$output_file)
{
  --die !!!ERROR IN TEST: mysqltest variable 'output_file' not set in write_result_to_file.inc
}
if ($output_file == GENERATE)
{
  --let $output_file= `SELECT UUID()`
  --let $output_file= $MYSQLTEST_VARDIR/tmp/_stmt_file_$output_file
}
--let _WRTF_OUTPUT_FILE= $output_file

perl;
  my $stmt= $ENV{'_WRTF_STATEMENT'};
  my $server_number= $ENV{'_WRTF_SERVER_NUMBER'};
  my $mysql = $ENV{'MYSQL'};
  $mysql =~ s/( --|$)/ --defaults-group-suffix=.$server_number$1/;
  my $outfile = $ENV{'_WRTF_OUTPUT_FILE'};
  open MYSQL, "| $mysql > $outfile" or die "Failed to open pipe to client on server '$server_number': $!";
  print MYSQL $stmt, ';' or die "Error printing statement to MYSQL pipe: $!";
  close MYSQL or die "Error closing MYSQL pipe: $!";
EOF

--let $include_filename= write_result_to_file.inc [$write_result_msg]
--source include/end_include_file.inc