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

aplus-framework / database / 15202200207

21 Apr 2025 08:34PM UTC coverage: 99.02%. Remained the same
15202200207

push

github

natanfelles
Merge branch 'development'

2426 of 2450 relevant lines covered (99.02%)

10.85 hits per line

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

90.48
/src/PreparedStatement.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Database Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Database;
11

12
use InvalidArgumentException;
13
use RuntimeException;
14

15
/**
16
 * Class PreparedStatement.
17
 *
18
 * @package database
19
 */
20
class PreparedStatement
21
{
22
    protected \mysqli_stmt $statement;
23
    protected bool $sendingBlob = false;
24

25
    public function __construct(\mysqli_stmt $statement)
26
    {
27
        $this->statement = $statement;
9✔
28
    }
29

30
    /**
31
     * Executes the prepared statement, returning a result set as a Result object.
32
     *
33
     * @param bool|float|int|string|null ...$params Parameters sent to the prepared statement
34
     *
35
     * @throws RuntimeException if it cannot obtain a result set from the prepared statement
36
     *
37
     * @return Result
38
     */
39
    public function query(bool | float | int | string | null ...$params) : Result
40
    {
41
        $this->bindParams($params);
4✔
42
        $this->statement->execute();
4✔
43
        $result = $this->statement->get_result();
4✔
44
        if ($result === false) {
4✔
45
            throw new RuntimeException('Failed while trying to obtain a result set from the prepared statement');
×
46
        }
47
        return new Result($result, true);
4✔
48
    }
49

50
    /**
51
     * Executes the prepared statement and return the number of affected rows.
52
     *
53
     * @param bool|float|int|string|null ...$params Parameters sent to the prepared statement
54
     *
55
     * @return int|string
56
     */
57
    public function exec(bool | float | int | string | null ...$params) : int | string
58
    {
59
        $this->bindParams($params);
4✔
60
        $this->statement->execute();
4✔
61
        if ($this->statement->field_count) {
4✔
62
            $this->statement->free_result();
1✔
63
        }
64
        return $this->statement->affected_rows;
4✔
65
    }
66

67
    /**
68
     * @param array|mixed[] $params Values types: bool, float, int, string or null
69
     */
70
    protected function bindParams(array $params) : void
71
    {
72
        $this->sendingBlob = false;
8✔
73
        if (empty($params)) {
8✔
74
            return;
5✔
75
        }
76
        $types = '';
4✔
77
        foreach ($params as &$param) {
4✔
78
            $type = \gettype($param);
4✔
79
            switch ($type) {
80
                case 'boolean':
4✔
81
                    $types .= 'i';
2✔
82
                    $param = (int) $param;
2✔
83
                    break;
2✔
84
                case 'double':
4✔
85
                    $types .= 'd';
1✔
86
                    break;
1✔
87
                case 'integer':
4✔
88
                    $types .= 'i';
4✔
89
                    break;
4✔
90
                case 'NULL':
3✔
91
                case 'string':
3✔
92
                    $types .= 's';
3✔
93
                    break;
3✔
94
                default:
95
                    throw new InvalidArgumentException(
×
96
                        "Invalid param data type: {$type}"
×
97
                    );
×
98
            }
99
        }
100
        unset($param);
4✔
101
        $this->statement->bind_param($types, ...$params);
4✔
102
    }
103

104
    public function sendBlob(string $chunk) : bool
105
    {
106
        if (!$this->sendingBlob) {
1✔
107
            $this->sendingBlob = true;
1✔
108
            $null = null;
1✔
109
            $this->statement->bind_param('b', $null);
1✔
110
        }
111
        return $this->statement->send_long_data(0, $chunk);
1✔
112
    }
113
}
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

© 2026 Coveralls, Inc