• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12518821104

27 Dec 2024 05:21PM UTC coverage: 84.426% (+0.02%) from 84.404%
12518821104

Pull #9339

github

web-flow
Merge 5caee6ae0 into 6cbbf601b
Pull Request #9339: refactor: enable instanceof and strictBooleans rector set

55 of 60 new or added lines in 34 files covered. (91.67%)

19 existing lines in 3 files now uncovered.

20437 of 24207 relevant lines covered (84.43%)

189.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.67
/system/Database/MySQLi/PreparedQuery.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Database\MySQLi;
15

16
use BadMethodCallException;
17
use CodeIgniter\Database\BasePreparedQuery;
18
use CodeIgniter\Database\Exceptions\DatabaseException;
19
use mysqli;
20
use mysqli_result;
21
use mysqli_sql_exception;
22
use mysqli_stmt;
23

24
/**
25
 * Prepared query for MySQLi
26
 *
27
 * @extends BasePreparedQuery<mysqli, mysqli_stmt, mysqli_result>
28
 */
29
class PreparedQuery extends BasePreparedQuery
30
{
31
    /**
32
     * Prepares the query against the database, and saves the connection
33
     * info necessary to execute the query later.
34
     *
35
     * NOTE: This version is based on SQL code. Child classes should
36
     * override this method.
37
     *
38
     * @param array $options Passed to the connection's prepare statement.
39
     *                       Unused in the MySQLi driver.
40
     */
41
    public function _prepare(string $sql, array $options = []): PreparedQuery
42
    {
43
        // Mysqli driver doesn't like statements
44
        // with terminating semicolons.
45
        $sql = rtrim($sql, ';');
12✔
46

47
        if (! $this->statement = $this->db->mysqli->prepare($sql)) {
12✔
48
            $this->errorCode   = $this->db->mysqli->errno;
×
49
            $this->errorString = $this->db->mysqli->error;
×
50

51
            if ($this->db->DBDebug) {
×
52
                throw new DatabaseException($this->errorString . ' code: ' . $this->errorCode);
×
53
            }
54
        }
55

56
        return $this;
12✔
57
    }
58

59
    /**
60
     * Takes a new set of data and runs it against the currently
61
     * prepared query. Upon success, will return a Results object.
62
     */
63
    public function _execute(array $data): bool
64
    {
65
        if (! isset($this->statement)) {
9✔
66
            throw new BadMethodCallException('You must call prepare before trying to execute a prepared statement.');
1✔
67
        }
68

69
        // First off - bind the parameters
70
        $bindTypes  = '';
8✔
71
        $binaryData = [];
8✔
72

73
        // Determine the type string
74
        foreach ($data as $key => $item) {
8✔
75
            if (is_int($item)) {
8✔
UNCOV
76
                $bindTypes .= 'i';
×
77
            } elseif (is_numeric($item)) {
8✔
UNCOV
78
                $bindTypes .= 'd';
×
79
            } elseif (is_string($item) && $this->isBinary($item)) {
8✔
80
                $bindTypes .= 'b';
1✔
81
                $binaryData[$key] = $item;
1✔
82
            } else {
83
                $bindTypes .= 's';
7✔
84
            }
85
        }
86

87
        // Bind it
88
        $this->statement->bind_param($bindTypes, ...$data);
8✔
89

90
        // Stream binary data
91
        foreach ($binaryData as $key => $value) {
7✔
92
            $this->statement->send_long_data($key, $value);
1✔
93
        }
94

95
        try {
96
            return $this->statement->execute();
7✔
97
        } catch (mysqli_sql_exception $e) {
2✔
98
            if ($this->db->DBDebug) {
2✔
UNCOV
99
                throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
×
100
            }
101

102
            return false;
2✔
103
        }
104
    }
105

106
    /**
107
     * Returns the result object for the prepared query or false on failure.
108
     *
109
     * @return false|mysqli_result
110
     */
111
    public function _getResult()
112
    {
113
        return $this->statement->get_result();
2✔
114
    }
115

116
    /**
117
     * Deallocate prepared statements.
118
     */
119
    protected function _close(): bool
120
    {
121
        return $this->statement->close();
12✔
122
    }
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc