1# ==== Purpose ====
2#
3# Execute a statement and compute the number of rows of the result set.
4#
5# This is intended to be used for queries like SHOW BINLOG EVENTS
6# where you cannot use COUNT(*). It would be nicer if mysqltest had
7# this feature. Now we have to execute the statement several times and
8# see if 'query_get_value(*, row) returns 'No such row'. Beware to not
9# use this for queries that have side effects.
10#
11# ==== Usage ====
12#
13# --let $statement= STATEMENT
14# --let $column= COLUMN_NAME
15# --source include/get_row_count.inc
16# --echo $statement has $row_count rows
17#
18# Parameters:
19#   $statement
20#     The statement to execute. Beware that this will be executed
21#     multiple times.
22#
23#   $column
24#     The name of one column of the result. This may seem redundant
25#     but unfortunately mysqltest forces you to specify a column name.
26#
27# Return value:
28#   $row_count
29#     The number of rows is stored in this variable.
30
31# Find upper bound on number of rows: check row numbers 1, 2, 4, 8, etc
32--let $_grc_max_row= 0
33--let $_grc_min_row= 0
34--let $_grc_row= 1
35while ($_grc_max_row == 0)
36{
37  if ($rpl_debug)
38  {
39    --echo row=$_grc_row min_row=$_grc_min_row max_row=$_grc_max_row
40  }
41  --let $_grc_result= query_get_value($statement, $column, $_grc_row)
42  if ($_grc_result == 'No such row')
43  {
44    --let $_grc_max_row= $_grc_row
45  }
46  if ($_grc_result != 'No such row')
47  {
48    --let $_grc_min_row= $_grc_row
49    --let $_grc_row= `SELECT $_grc_row * 2`
50  }
51}
52
53# Binary search to find exact count.
54while (`SELECT $_grc_min_row + 1 < $_grc_max_row`)
55{
56  if ($rpl_debug)
57  {
58    --echo row=$_grc_row min_row=$_grc_min_row max_row=$_grc_max_row
59  }
60  --let $_grc_row= `SELECT ($_grc_min_row + $_grc_max_row) DIV 2`
61  --let $_grc_result= query_get_value($statement, $column, $_grc_row)
62  if ($_grc_result == 'No such row')
63  {
64    --let $_grc_max_row= $_grc_row
65  }
66  if ($_grc_result != 'No such row')
67  {
68    --let $_grc_min_row= $_grc_row
69  }
70}
71
72# Store result
73--let $row_count= $_grc_min_row
74