1# include/wait_show_condition.inc
2#
3# SUMMARY
4#
5#    Waits until the show statement ($show_statement) has one or all of the
6#    rows of the result set for the field ($field) a value which fulfils
7#    a condition ($condition), or the operation times out.
8#
9#
10# USAGE
11#
12#    All rows of the result must fulfil the condition if $all_rows_fulfil is 1
13#    else at least one of the result must fulfil the condition.
14#    let $wait_for_all= 1;
15#    let $show_statement= SHOW PROCESSLIST;
16#    let $field= State;
17#    let $condition= = 'Updating';
18#    --source include/wait_show_condition.inc
19#
20#   OR
21#
22#    let $wait_timeout= 60; # Override default of 30 seconds with 60.
23#    let $show_statement= SHOW PROCESSLIST;
24#    let $field= State;
25#    let $condition= = 'Updating';
26#    --source include/wait_show_condition.inc
27#
28# Please do not use this use routine if you can replace the SHOW statement
29# with a select. In such a case include/wait_condition.inc is recommended.
30#
31# Created: 2009-02-18 mleich
32#
33
34if (!$condition)
35{
36  --die ERROR IN TEST: the "condition" variable must be set
37}
38
39if (!$field)
40{
41  --die ERROR IN TEST: the "field" variable must be set
42}
43
44if (!$show_statement)
45{
46  --die ERROR IN TEST: the "show_statement" variable must be set
47}
48
49let $max_run_time= 30;
50if ($wait_timeout)
51{
52  let $max_run_time= $wait_timeout;
53}
54# Reset $wait_timeout so that its value won't be used on subsequent
55# calls, and default will be used instead.
56let $wait_timeout= 0;
57
58# The smallest timespan till UNIX_TIMESTAMP() gets incremented is ~0 seconds.
59# We add one second to avoid the case that somebody measures timespans on a
60# real clock with fractions of seconds, detects that n seconds are sufficient,
61# assigns n to this routine and suffers because he sometimes gets n - 1
62# seconds in reality.
63inc $max_run_time;
64
65let $found= 0;
66let $max_end_time= `SELECT UNIX_TIMESTAMP() + $max_run_time`;
67
68if ($wait_for_all != 1)
69{
70  while (`SELECT UNIX_TIMESTAMP() <= $max_end_time AND $found = 0`)
71  {
72     # Sleep a bit to avoid too heavy load.
73     real_sleep 0.2;
74     let $rowno= 1;
75     let $process_result= 1;
76     let $do_loop= 1;
77     while ($do_loop)
78     {
79        let $field_value= query_get_value($show_statement, $field, $rowno);
80        if (`SELECT '$field_value' $condition`)
81        {
82           let $found= 1;
83	   let $do_loop= 0;
84        }
85        if ($field_value == No such row)
86        {
87           # We are behind the last row of the result set.
88           let $process_result= 0;
89	   let $do_loop= 0;
90        }
91        inc $rowno;
92     }
93  }
94}
95
96if ($wait_for_all == 1)
97{
98  while (`SELECT UNIX_TIMESTAMP() <= $max_end_time AND $found = 0`)
99  {
100     # Sleep a bit to avoid too heavy load.
101     real_sleep 0.2;
102     let $rowno= 1;
103     let $process_result= 1;
104     let $do_loop= 1;
105     while ($do_loop)
106     {
107        let $field_value= query_get_value($show_statement, $field, $rowno);
108        if ($field_value == No such row)
109        {
110           let $found= 1;
111	   let $do_loop= 0;
112        }
113        if (`SELECT $found = 0 AND NOT '$field_value' $condition`)
114        {
115           let process_result= 0;
116	   let $do_loop= 0;
117        }
118        inc $rowno;
119     }
120  }
121}
122
123if (!$found)
124{
125  echo # Timeout in include/wait_show_condition.inc for $condition;
126  echo #         show_statement : $show_statement;
127  echo #         field          : $field;
128  echo #         condition      : $condition;
129  echo #         max_run_time   : $max_run_time;
130  eval $show_statement;
131}
132
133