File: connect.inc

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (107 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download
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
<?php
    /*
    Default values are "localhost", "root",
    database "test" and empty password.
    Change the MYSQL_TEST environment values
    if you want to use another configuration
    */

    $driver    = new mysqli_driver;
    $driver->report_mode = MYSQLI_REPORT_OFF;

    $host      = getenv("MYSQL_TEST_HOST")     ?: "127.0.0.1";
    $port      = getenv("MYSQL_TEST_PORT")     ?: 3306;
    $user      = getenv("MYSQL_TEST_USER")     ?: "root";
    $passwd    = getenv("MYSQL_TEST_PASSWD")   ?: "";
    $db        = getenv("MYSQL_TEST_DB")       ?: "test";
    $engine    = getenv("MYSQL_TEST_ENGINE")   ?: "InnoDB";
    $socket    = getenv("MYSQL_TEST_SOCKET")   ?: null;
    if ($socket) {
        ini_set('mysqli.default_socket', $socket);
    }

    function get_environment_connection_flags(): int {
        static $connect_flags = null;
        if ($connect_flags === null) {
            $connect_flags = (int)getenv("MYSQL_TEST_CONNECT_FLAGS") ?: 0;
        }
        return $connect_flags;
    }

    /**
    * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
    *
    * @param bool $enable_env_flags Enable setting of connection flags through 	env(MYSQL_TEST_CONNECT_FLAGS)?
    */
    function my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
        $flags = $enable_env_flags? get_environment_connection_flags():0;
        if ($flags !== 0) {
            $link = mysqli_init();
            if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
                $link = false;
        } else {
            $link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
        }

        return $link;
    }

    /**
    * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
    *
    * @param bool $enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
    */
    function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
        if ($enable_env_flags) {
            $flags = $flags | get_environment_connection_flags();
        }

        return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
    }

    class my_mysqli extends mysqli {
        public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
            $flags = ($enable_env_flags) ? get_environment_connection_flags() : 0;

            // Because the tests are meant to test both error modes, they can set the report_mode to a different value,
            // which we do not want to override. However, we want to make sure that if a connection cannot be made,
            // the constuctor will throw an exception. We store current report_mode in variable and restore it later.
            $driver = new mysqli_driver;
            $report_mode = $driver->report_mode;
            $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;

            if ($flags !== 0) {
                parent::__construct();
                $this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
            } else {
                parent::__construct($host, $user, $passwd, $db, $port, $socket);
            }

            // Restore error mode
            $driver->report_mode = $report_mode;
        }
    }

    function have_innodb($link) {
        if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'"))
            && ($row = $res->fetch_row())
            && !empty($row)
        ) {
            return !($row[1] == 'DISABLED' || $row[1] == 'NO');
        }
        // MySQL 5.6.1+
        if ($res = $link->query('SHOW ENGINES')) {
            while ($row = $res->fetch_assoc()) {
                if (!isset($row['Engine']) || !isset($row['Support'])) {
                    return false;
                }

                if (($row['Engine'] == 'InnoDB')
                    && (($row['Support'] == 'YES') || ($row['Support'] == 'DEFAULT'))
                ) {
                    return true;
                }
            }
        }
        return false;
    }