MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
write_result_to_file.inc
1 # ==== Purpose ====
2 #
3 # Execute a statement and write the result to a file. This is useful
4 # if the output needs more advanced parsing than can be done by
5 # mysqltest commands.
6 #
7 # The statement is sent to mysqld on current connection using
8 # the mysql command line client.
9 #
10 # ==== Usage ====
11 #
12 # --let $statement= <STATEMENT>
13 # --let $output_file= {<FILE>|GENERATE}
14 # [--let $dont_print_statement= 1]
15 # --source include/write_result_to_file.inc
16 #
17 # Parameters:
18 # $statement
19 # The statement to execute.
20 #
21 # $output_file
22 # Name of file to write. If omitted, generates a new filename and
23 # stores the name both in the mysqltest variable $output_file and
24 # in the environment variable $OUTPUT_FILE.
25 #
26 # $dont_print_statement
27 # By default, the statement is echoed to the result log. If the
28 # statement contains non-deterministic output, set this variable
29 # to suppress it.
30 
31 # Get the port and socket used by mysqld on current connection
32 --let _WRTF_SERVER_PORT= `SELECT @@PORT`
33 --let _WRTF_SERVER_SOCKET= `SELECT @@SOCKET`
34 
35 --let $_write_result_msg= [connection=$CURRENT_CONNECTION]
36 if (!$dont_print_statement)
37 {
38  --let $_write_result_msg= [connection=$CURRENT_CONNECTION statement=$statement]
39 }
40 
41 --let $include_filename= write_result_to_file.inc $_write_result_msg
42 --source include/begin_include_file.inc
43 
44 if ($statement == '')
45 {
46  --die !!!ERROR IN TEST: mysqltest variable 'statement' not set in write_result_to_file.inc
47 }
48 --let _WRTF_STATEMENT= $statement
49 
50 if (!$output_file)
51 {
52  --die !!!ERROR IN TEST: mysqltest variable 'output_file' not set in write_result_to_file.inc
53 }
54 if ($output_file == GENERATE)
55 {
56  --let $output_file= `SELECT UUID()`
57  --let $output_file= $MYSQLTEST_VARDIR/tmp/_stmt_file_$output_file
58 }
59 --let _WRTF_OUTPUT_FILE= $output_file
60 
61 perl;
62  use strict;
63  my $stmt= $ENV{'_WRTF_STATEMENT'};
64  # Connecting mysql to same mysqld as current connectiona
65  # by overriding port and socket
66  my $mysql = $ENV{'MYSQL'};
67  my $server_port= $ENV{'_WRTF_SERVER_PORT'};
68  my $server_socket= $ENV{'_WRTF_SERVER_SOCKET'};
69  $mysql .= " --port=$server_port --socket=$server_socket";
70  my $outfile = $ENV{'_WRTF_OUTPUT_FILE'};
71  open MYSQL, "| $mysql > $outfile" or die "Failed to open MYSQL pipe: '$mysql > $outfile'";
72  print MYSQL $stmt, ';' or die "Error printing statement to MYSQL pipe: $!";
73  close MYSQL or die "Error closing MYSQL pipe: $!";
74 EOF
75 
76 --let $include_filename= write_result_to_file.inc [$write_result_msg]
77 --source include/end_include_file.inc