1# ==== Purpose ====
2#
3# Write the contents of $write_var to file $write_to_file.
4#
5#
6# ==== Usage ====
7#
8# --let $write_var = <value>
9# --let $write_to_file = [<file>|GENERATE]
10# --source include/write_var_to_file.inc
11#
12# $write_var is evaluated in sql 'string' context, so escapes like \n
13# are interpolated.
14#
15# $write_to_file can be either a filename, or the special string
16# GENERATE.  If it is GENERATE, a unique filename is generated (based
17# on UUID()). The filename is saved in $write_to_file so that it can
18# be retrieved later.
19#
20#
21# ==== Implementation ====
22#
23# We can't use mysqltest's write_file because it does not evaluate
24# variables. We can't use '--exec echo $write_var > $write_file'
25# because it will use \n\r line terminator under windows. So the only
26# working way is mysql's SELECT INTO DUMPFILE, which is subject to
27# @@secure_file_priv. That makes this more complex than you might
28# expect.
29
30if (!$write_to_file)
31{
32  --die You must set the mysqltest variable \$write_to_file before you source include/write_var_to_file.inc
33}
34
35if ($write_to_file == 'GENERATE')
36{
37  --let $_wvtf_suffix= `SELECT UUID()`
38  --let $write_to_file= $MYSQLTEST_VARDIR/tmp/_var_file_$_wvtf_suffix.inc
39}
40
41--error 0,1
42--remove_file $write_to_file
43
44if (`SELECT LENGTH(@@secure_file_priv) > 0`)
45{
46  --let $_wvtf_suffix= `SELECT UUID()`
47  --let $_wvtf_tmp_file= $MYSQLTEST_VARDIR/_wvtf_$_wvtf_suffix
48
49  --eval SELECT '$write_var' INTO DUMPFILE '$_wvtf_tmp_file'
50  --copy_file $_wvtf_tmp_file $write_to_file
51  --remove_file $_wvtf_tmp_file
52}
53if (`SELECT LENGTH(@@secure_file_priv) = 0`)
54{
55  --eval SELECT '$write_var' INTO DUMPFILE '$write_to_file'
56}
57