1--TEST--
2mysqli_change_user() - table locks, GET_LOCK(), temporary tables
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7die("skip - is the server still buggy?");
8?>
9--FILE--
10<?php
11    require_once('connect.inc');
12    require_once('table.inc');
13
14    if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
15        printf("[001] Cannot create second connection handle, [%d] %s\n",
16            mysqli_connect_errno(), mysqli_connect_error());
17
18    if (!mysqli_query($link, 'LOCK TABLE test WRITE'))
19        printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
20
21    /*
22    if ($res = mysqli_query($link2, 'SELECT COUNT(*) AS _num FROM test')) {
23        printf("[003] Reading from test should not be possible due to a lock, [%d] %s\n",
24            mysqli_errno($link2), mysqli_error($link2));
25        mysqli_free_result($res);
26    }
27    */
28
29    // LOCKS should be removed
30    mysqli_change_user($link, $user, $passwd, $db);
31
32    if (!$res = mysqli_query($link2, 'SELECT COUNT(*) AS _num FROM test'))
33        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
34
35    if (!$row = mysqli_fetch_assoc($res))
36        printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
37
38    if ($row['_num'] < 1)
39        printf("[005] There should be some rows in the table test\n");
40
41    mysqli_free_result($res);
42    mysqli_close($link2);
43
44    if (!mysqli_query($link, 'DROP TABLE test'))
45        printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
46
47    if (!mysqli_query($link, 'CREATE TEMPORARY TABLE test(id INT)'))
48        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
49
50    if (!mysqli_query($link, 'INSERT INTO test(id) VALUES (1), (2), (3)'))
51        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
52
53    if (!$res = mysqli_query($link, 'SELECT COUNT(*) AS _num FROM test'))
54        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
55
56    if (!$row = mysqli_fetch_assoc($res))
57        printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58
59    if ($row['_num'] != 3)
60        printf("[011] There should be three rows in the table test\n");
61
62    mysqli_free_result($res);
63
64    // Temporary tables should be dropped
65    mysqli_change_user($link, $user, $passwd, $db);
66
67    if ($res = mysqli_query($link, 'SELECT COUNT(*) AS _num FROM test')) {
68        printf("[012] There should be no table test any more, [%d] %s\n",
69            mysqli_errno($link), mysqli_error($link));
70        mysqli_free_result($res);
71    }
72
73    if (!$res = mysqli_query($link, 'SELECT GET_LOCK("phptest", 2) AS _ok'))
74        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
75
76    if (!$row = mysqli_fetch_assoc($res))
77        printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
78
79    if ($row['_ok'] != 1)
80        printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
81
82    mysqli_free_result($res);
83
84    // GET_LOCK("phptest") should be released
85    mysqli_change_user($link, $user, $passwd, $db);
86
87    if (!$res = mysqli_query($link, 'SELECT IS_FREE_LOCK("phptest") AS _ok'))
88        printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
89
90    if (!$row = mysqli_fetch_assoc($res))
91        printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
92
93    if ($row['_ok'] != 1)
94        printf("[018] Lock 'phptest' should have been released, [%d] %s\n",
95            mysqli_errno($link), mysqli_error($link));
96
97    mysqli_free_result($res);
98    mysqli_close($link);
99    print "done!";
100?>
101--CLEAN--
102<?php
103    require_once("clean_table.inc");
104?>
105--EXPECT--
106done!
107