Skip to content

Commit

Permalink
fix: handling binary data for prepare statement
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Dec 20, 2024
1 parent cc1b8f2 commit 4d9ba48
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions system/Database/MySQLi/PreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function _execute(array $data): bool
$bindTypes .= 'i';
} elseif (is_numeric($item)) {
$bindTypes .= 'd';
} elseif (is_string($item) && mb_detect_encoding($item, 'UTF-8', true) === false) {
$bindTypes .= 'b';
} else {
$bindTypes .= 's';
}
Expand Down
2 changes: 2 additions & 0 deletions system/Database/SQLite3/PreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function _execute(array $data): bool
$bindType = SQLITE3_INTEGER;
} elseif (is_float($item)) {
$bindType = SQLITE3_FLOAT;
} elseif (is_string($item) && mb_detect_encoding($item, 'UTF-8', true) === false) {
$bindType = SQLITE3_BLOB;
} else {
$bindType = SQLITE3_TEXT;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/system/Database/Live/PreparedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,23 @@ public function testDeallocatePreparedQueryThenTryToClose(): void

$this->query->close();
}

public function testInsertBinaryData()
{
if ($this->db->DBDriver === 'Postgre' || $this->db->DBDriver === 'SQLSRV') {
$this->markTestSkipped('Blob not supported for Postgre and SQLSRV.');
}

$this->query = $this->db->prepare(static fn ($db) => $db->table('type_test')->insert([
'type_blob' => 'blob',
]));

$fileContent = file_get_contents(TESTPATH . '_support/Images/Steveston_dusk.JPG');
$this->assertTrue($this->query->execute($fileContent));

$id = $this->db->insertId();
$file = $this->db->table('type_test')->where('id', $id)->get()->getRow();

$this->assertSame(strlen($fileContent), strlen($file->type_blob));
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.5.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Bugs Fixed
- **CURLRequest:** Added support for handling proxy responses using HTTP versions other than 1.1.
- **Database:** Fixed a bug that caused ``Postgre\Connection::reconnect()`` method to throw an error when the connection had not yet been established.
- **Model** Fixed a bug that caused the ``Model::getIdValue()`` method to not correctly recognize the primary key in the ``Entity`` object if a data mapping for the primary key was used.
- **Database:** Fixed a bug with handling binary data with prepared statements in SQLite3 and MySQLi drivers.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down

0 comments on commit 4d9ba48

Please sign in to comment.