• 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

82.76
/system/Database/OCI8/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\OCI8;
15

16
use BadMethodCallException;
17
use CodeIgniter\Database\BasePreparedQuery;
18
use CodeIgniter\Database\Exceptions\DatabaseException;
19
use OCILob;
20

21
/**
22
 * Prepared query for OCI8
23
 *
24
 * @extends BasePreparedQuery<resource, resource, resource>
25
 */
26
class PreparedQuery extends BasePreparedQuery
27
{
28
    /**
29
     * A reference to the db connection to use.
30
     *
31
     * @var Connection
32
     */
33
    protected $db;
34

35
    /**
36
     * Latest inserted table name.
37
     */
38
    private ?string $lastInsertTableName = null;
39

40
    /**
41
     * Prepares the query against the database, and saves the connection
42
     * info necessary to execute the query later.
43
     *
44
     * NOTE: This version is based on SQL code. Child classes should
45
     * override this method.
46
     *
47
     * @param array $options Passed to the connection's prepare statement.
48
     *                       Unused in the OCI8 driver.
49
     */
50
    public function _prepare(string $sql, array $options = []): PreparedQuery
51
    {
52
        if (! $this->statement = oci_parse($this->db->connID, $this->parameterize($sql))) {
13✔
53
            $error             = oci_error($this->db->connID);
×
54
            $this->errorCode   = $error['code'] ?? 0;
×
UNCOV
55
            $this->errorString = $error['message'] ?? '';
×
56

57
            if ($this->db->DBDebug) {
×
UNCOV
58
                throw new DatabaseException($this->errorString . ' code: ' . $this->errorCode);
×
59
            }
60
        }
61

62
        $this->lastInsertTableName = $this->db->parseInsertTableName($sql);
13✔
63

64
        return $this;
13✔
65
    }
66

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

77
        $binaryData = null;
9✔
78

79
        foreach (array_keys($data) as $key) {
9✔
80
            if (is_string($data[$key]) && $this->isBinary($data[$key])) {
9✔
81
                $binaryData = oci_new_descriptor($this->db->connID, OCI_D_LOB);
1✔
82
                $binaryData->writeTemporary($data[$key], OCI_TEMP_BLOB);
1✔
83
                oci_bind_by_name($this->statement, ':' . $key, $binaryData, -1, OCI_B_BLOB);
1✔
84
            } else {
85
                oci_bind_by_name($this->statement, ':' . $key, $data[$key]);
8✔
86
            }
87
        }
88

89
        $result = oci_execute($this->statement, $this->db->commitMode);
8✔
90

91
        if ($binaryData instanceof OCILob) {
8✔
92
            $binaryData->free();
1✔
93
        }
94

95
        if ($result && $this->lastInsertTableName !== '') {
8✔
96
            $this->db->lastInsertedTableName = $this->lastInsertTableName;
6✔
97
        }
98

99
        return $result;
8✔
100
    }
101

102
    /**
103
     * Returns the statement resource for the prepared query or false when preparing failed.
104
     *
105
     * @return resource|null
106
     */
107
    public function _getResult()
108
    {
109
        return $this->statement;
2✔
110
    }
111

112
    /**
113
     * Deallocate prepared statements.
114
     */
115
    protected function _close(): bool
116
    {
117
        return oci_free_statement($this->statement);
12✔
118
    }
119

120
    /**
121
     * Replaces the ? placeholders with :0, :1, etc parameters for use
122
     * within the prepared query.
123
     */
124
    public function parameterize(string $sql): string
125
    {
126
        // Track our current value
127
        $count = 0;
13✔
128

129
        return preg_replace_callback('/\?/', static function ($matches) use (&$count): string {
13✔
130
            return ':' . ($count++);
13✔
131
        }, $sql);
13✔
132
    }
133
}
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