summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_poll_mixing_insert_select.phpt
blob: 44397ca37c19024dd25c1974d7253350b7d55234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
--TEST--
mysqli_poll() & INSERT SELECT
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('connect.inc');
require_once('skipifconnectfailure.inc');

if (!$IS_MYSQLND)
    die("skip mysqlnd only feature, compile PHP using --with-mysqli=mysqlnd");
?>
--FILE--
<?php
    require_once('table.inc');

    function get_connection() {
        global $host, $user, $passwd, $db, $port, $socket;

        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
            printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
        return $link;
    }


    // Note: some queries will fail! They are supposed to fail.
    $queries = array(
            'CREATE TABLE IF NOT EXISTS bogus(id INT)',
            'SET @a = 1',
            'SELECT * FROM test ORDER BY id ASC LIMIT 2',
            "INSERT INTO test(id, label) VALUES (100, 'z')",
            'SELECT * FROM test ORDER BY id ASC LIMIT 2',
            'SELECT',
            'UPDATE test SET id = 101 WHERE id > 3',
            'UPDATE_FIX test SET id = 101 WHERE id > 3',
            'DROP TABLE IF EXISTS bogus',
            'DELETE FROM test WHERE id = @a',
            'DELETE FROM test WHERE id = 1',
    );

    $link = get_connection();
    $have_proc = false;
    mysqli_real_query($link, "DROP PROCEDURE IF EXISTS p");
    if (mysqli_real_query($link, 'CREATE PROCEDURE p(IN ver_in VARCHAR(25), OUT ver_out VARCHAR(25)) BEGIN SELECT ver_in INTO ver_out; END;')) {
            $have_proc = true;
            $queries[] = "CALL p('myversion', @version)";
    }
    mysqli_close($link);

    $links = array();
    for ($i = 0; $i < count($queries); $i++) {

        $link = get_connection();

        if (true !== ($tmp = mysqli_query($link, $queries[$i], MYSQLI_ASYNC |  MYSQLI_USE_RESULT)))
            printf("[002] Expecting true got %s/%s\n", gettype($tmp), var_export($tmp, true));

        // WARNING KLUDGE NOTE
        // Add a tiny delay to ensure that queries get executed in a certain order
        // If your MySQL server is very slow the test may randomly fail!
        usleep(20000);

        $links[mysqli_thread_id($link)] = array(
            'query' => $queries[$i],
            'link' => $link,
            'processed' => false,
        );
    }

    $saved_errors = array();
    do {
        $poll_links = $poll_errors = $poll_reject = array();
        foreach ($links as $thread_id => $link) {
            if (!$link['processed']) {
                $poll_links[] = $link['link'];
                $poll_errors[] = $link['link'];
                $poll_reject[] = $link['link'];
            }
        }
        if (0 == count($poll_links))
            break;

        if (0 === ($num_ready = mysqli_poll($poll_links, $poll_errors, $poll_reject, 0, 200000)))
            continue;

        if (!empty($poll_errors)) {
            die(var_dump($poll_errors));
        }

        if (FALSE === $num_ready) {
            die("Some mysqli indicated error");
        }

        foreach ($poll_links as $link) {
            $thread_id = mysqli_thread_id($link);
            $links[$thread_id]['processed'] = true;

            if (is_object($res = mysqli_reap_async_query($link))) {
                // result set object
                while ($row = mysqli_fetch_assoc($res)) {
                    // eat up all results
                    ;
                }
                mysqli_free_result($res);
            } else {
                // either there is no result (no SELECT) or there is an error
                if (mysqli_errno($link) > 0) {
                    $saved_errors[$thread_id] = mysqli_errno($link);
                }
            }
        }

    } while (true);

    // Checking if all lines are still usable
    foreach ($links as $thread_id => $link) {
        if (isset($saved_errors[$thread_id])) {
            printf("[003] '%s' caused %d\n",
                $links[$thread_id]['query'], $saved_errors[$thread_id]);
            if ($saved_errors[$thread_id] != mysqli_errno($link['link'])) {
                printf("[004] Error state not saved for query '%s', %d != %d\n", $link['query'],
                    $saved_errors[$thread_id], mysqli_errno($link['link']));
            }
        }

        if (!$res = mysqli_query($link['link'], 'SELECT * FROM test WHERE id = 100'))
            printf("[005] Expecting true got %s/%s\n", gettype($tmp), var_export($tmp, true));
        if (!$row = mysqli_fetch_row($res))
            printf("[006] Expecting true got %s/%s\n", gettype($tmp), var_export($tmp, true));

        mysqli_free_result($res);
    }

    if ($res = mysqli_query($link['link'], "SELECT * FROM test WHERE id = 100")) {
        $row = mysqli_fetch_assoc($res);
        var_dump($row);
        mysqli_free_result($res);
    }

    if ($have_proc && ($res = mysqli_query($link['link'], "SELECT @version as _version"))) {
        $row = mysqli_fetch_assoc($res);
        if ($row['_version'] != 'myversion') {
            printf("[007] Check procedures\n");
        }
        mysqli_free_result($res);
    }

    foreach ($links as $link)
        mysqli_close($link['link']);

    $link = get_connection();
    if (!mysqli_query($link, 'SELECT 1', MYSQLI_ASYNC))
        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

    if (!mysqli_query($link, 'SELECT 1', MYSQLI_ASYNC))
        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));

    mysqli_close($link);

    print "done!";
?>
--CLEAN--
<?php
require_once("connect.inc");
if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
   printf("[c001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());

if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
    printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));

if (!mysqli_query($link, "DROP TABLE IF EXISTS bogus"))
    printf("[c002] Cannot drop table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));

mysqli_query($link, "DROP PROCEDURE IF EXISTS p");

mysqli_close($link);
?>
--EXPECTF--
[003] 'SELECT' caused 1064
[003] 'UPDATE test SET id = 101 WHERE id > 3' caused 1062
[003] 'UPDATE_FIX test SET id = 101 WHERE id > 3' caused 1064
array(2) {
  ["id"]=>
  string(3) "100"
  ["label"]=>
  string(1) "z"
}
[009] [2014] %s
done!