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

codeigniter4 / CodeIgniter4 / 25637286072

10 May 2026 07:09PM UTC coverage: 88.366% (-0.05%) from 88.418%
25637286072

Pull #10182

github

web-flow
Merge 4a0b49c91 into df9f13771
Pull Request #10182: fix(database): classify prepared query exceptions

77 of 108 new or added lines in 9 files covered. (71.3%)

1 existing line in 1 file now uncovered.

23911 of 27059 relevant lines covered (88.37%)

218.09 hits per line

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

70.59
/system/Database/SQLite3/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\SQLite3;
15

16
use CodeIgniter\Database\BasePreparedQuery;
17
use CodeIgniter\Exceptions\BadMethodCallException;
18
use Exception;
19
use SQLite3;
20
use SQLite3Result;
21
use SQLite3Stmt;
22

23
/**
24
 * Prepared query for SQLite3
25
 *
26
 * @extends BasePreparedQuery<SQLite3, SQLite3Stmt, SQLite3Result>
27
 */
28
class PreparedQuery extends BasePreparedQuery
29
{
30
    /**
31
     * The SQLite3Result resource, or false.
32
     *
33
     * @var false|SQLite3Result
34
     */
35
    protected $result;
36

37
    /**
38
     * Prepares the query against the database, and saves the connection
39
     * info necessary to execute the query later.
40
     *
41
     * NOTE: This version is based on SQL code. Child classes should
42
     * override this method.
43
     *
44
     * @param array $options Passed to the connection's prepare statement.
45
     *                       Unused in the MySQLi driver.
46
     */
47
    public function _prepare(string $sql, array $options = []): PreparedQuery
48
    {
49
        if (! ($this->statement = $this->db->connID->prepare($sql))) {
15✔
50
            $this->errorCode   = $this->db->connID->lastErrorCode();
×
51
            $this->errorString = $this->db->connID->lastErrorMsg();
×
52

53
            if ($this->db->DBDebug) {
×
NEW
54
                throw $this->db->createDatabaseException($this->errorString, $this->errorCode);
×
55
            }
56
        }
57

58
        return $this;
15✔
59
    }
60

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

71
        foreach ($data as $key => $item) {
11✔
72
            // Determine the type string
73
            if (is_int($item)) {
11✔
74
                $bindType = SQLITE3_INTEGER;
×
75
            } elseif (is_float($item)) {
11✔
76
                $bindType = SQLITE3_FLOAT;
×
77
            } elseif (is_string($item) && $this->isBinary($item)) {
11✔
78
                $bindType = SQLITE3_BLOB;
1✔
79
            } else {
80
                $bindType = SQLITE3_TEXT;
10✔
81
            }
82

83
            // Bind it
84
            $this->statement->bindValue($key + 1, $item, $bindType);
11✔
85
        }
86

87
        try {
88
            $this->result = $this->statement->execute();
11✔
89
        } catch (Exception $e) {
5✔
90
            $error                   = $this->db->error();
5✔
91
            $this->errorCode         = $error['code'];
5✔
92
            $this->errorString       = $e->getMessage();
5✔
93
            $this->databaseException = $this->db->createDatabaseException($this->errorString, $this->errorCode, $e);
5✔
94

95
            if ($this->db->DBDebug) {
5✔
96
                throw $this->databaseException;
2✔
97
            }
98

99
            return false;
3✔
100
        }
101

102
        if ($this->result === false) {
10✔
NEW
103
            $this->errorCode   = $this->db->connID->lastErrorCode();
×
NEW
104
            $this->errorString = $this->db->connID->lastErrorMsg();
×
105

NEW
106
            if ($this->db->DBDebug) {
×
NEW
107
                throw $this->db->createDatabaseException($this->errorString, $this->errorCode);
×
108
            }
109
        }
110

111
        return $this->result !== false;
10✔
112
    }
113

114
    /**
115
     * Returns the result object for the prepared query or false on failure.
116
     *
117
     * @return false|SQLite3Result
118
     */
119
    public function _getResult()
120
    {
121
        return $this->result;
3✔
122
    }
123

124
    /**
125
     * Deallocate prepared statements.
126
     */
127
    protected function _close(): bool
128
    {
129
        return $this->statement->close();
15✔
130
    }
131
}
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