1--TEST--
2Fetching BIT column values using the PS API
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'connect.inc';
8if (!$link = @my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
9    die(sprintf("skip Can't connect to MySQL Server - [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
10}
11
12if (mysqli_get_server_version($link) < 50003) {
13    // b'001' syntax not supported before 5.0.3
14    die("skip Syntax used for test not supported with MySQL Server before 5.0.3");
15}
16?>
17--FILE--
18<?php
19    require_once 'connect.inc';
20
21    function dec32bin($dec, $bits) {
22
23        $maxval = pow(2, $bits);
24        $bin = '';
25        for ($bitval = $maxval; $bitval >= 1; $bitval = $bitval / 2) {
26            if (($dec / $bitval) >= 1) {
27                $bin .= '1';
28                $dec -= $bitval;
29            } else {
30                $bin .= '0';
31            }
32        }
33        return $bin;
34    }
35
36    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
37        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
38            $host, $user, $db, $port, $socket);
39
40    for ($bits = 1; $bits <= 46; $bits++) {
41        if (1 == $bits)
42            $max_value = 1;
43        else
44            $max_value = pow(2, $bits) - 1;
45
46        if (!mysqli_query($link, "DROP TABLE IF EXISTS test") ||
47            !mysqli_query($link, $sql = sprintf('CREATE TABLE test(id BIGINT UNSIGNED, bit_value BIT(%d) NOT NULL, bit_null BIT(%d) DEFAULT NULL) ENGINE="%s"', $bits, $bits, $engine)))
48            printf("[002 - %d] [%d] %s\n",$bits, mysqli_errno($link), mysqli_error($link));
49
50        if (!$stmt = mysqli_stmt_init($link))
51            printf("[003 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));
52
53        $tests = 0;
54        $rand_max = mt_getrandmax();
55        while ($tests < 10) {
56
57            $tests++;
58            if (1 == $tests)
59                $value = 0;
60            else if (2 == $tests)
61                $value = $max_value;
62            else {
63                if ($max_value > $rand_max) {
64                    $max_loops = floor($max_value/$rand_max);
65                    $num_loops = mt_rand(1, $max_loops);
66                    $value = 0;
67                    for ($i = 0; $i < $num_loops; $i++)
68                        $value += mt_rand(0, $rand_max);
69                } else {
70                    $value = mt_rand(0, $max_value);
71                }
72            }
73
74            $bin = ($bits < 32) ? decbin($value) : dec32bin($value, $bits);
75            $sql = sprintf("INSERT INTO test(id, bit_value) VALUES (%s, b'%s')", $value, $bin);
76            for ($i = 0; ($i < strlen($bin)) && ($bin[$i] == '0'); $i++)
77                ;
78            $bin2 = substr($bin, $i, strlen($bin));
79
80            if (!mysqli_stmt_prepare($stmt, $sql) ||
81                    !mysqli_stmt_execute($stmt))
82                printf("[004 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
83
84            $sql = sprintf("SELECT bin(bit_value) AS _bin, id, bit_value, bit_null FROM test WHERE id = %s", $value);
85            if (!mysqli_stmt_prepare($stmt, $sql) ||
86                    !mysqli_stmt_execute($stmt))
87                printf("[005 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
88
89            if (!$res = mysqli_stmt_get_result($stmt))
90                printf("[006 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
91
92            if (!$row = mysqli_fetch_assoc($res))
93                printf("[007 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));
94            mysqli_free_result($res);
95
96            if (($value != $row['id']) || (($bin != $row['_bin']) && ($bin2 != $row['_bin']))) {
97                debug_zval_dump($row);
98                printf("[008 - %d] Insert of %s in BIT(%d) column might have failed. id = %s, bin = %s (%s/%s)\n",
99                    $bits, $value, $bits, $row['id'], $row['_bin'], $bin, $bin2);
100                break;
101            }
102            if ($value != $row['bit_value']) {
103                debug_zval_dump($row);
104                printf("%10s %64s\n%10s %64s\n", '_bin', $row['_bin'], 'insert', $bin);
105                printf("[009 - %d] Expecting %s got %s\n", $bits, $value, $row['bit_value']);
106                break;
107            }
108
109            if (null !== $row['bit_null']) {
110                debug_zval_dump($row);
111                printf("[010 - %d] Expecting null got %s/%s\n", $bits, gettype($row['bit_value']), $row['bit_value']);
112                break;
113            }
114
115        }
116
117        mysqli_stmt_close($stmt);
118
119    }
120
121    mysqli_close($link);
122    print "done!";
123?>
124--CLEAN--
125<?php
126require_once 'clean_table.inc';
127?>
128--EXPECT--
129done!
130