Commit 86a50988 authored by catch's avatar catch
Browse files

Issue #3230801 by alexpott, longwave, daffie, mondrake: Postgres does...

Issue #3230801 by alexpott, longwave, daffie, mondrake: Postgres does unnecessary work when writing a NULL to a blob field that triggers deprecations in PHP 8.1
parent c1ceedb7
......@@ -33,7 +33,7 @@ public function execute() {
$blob_count = 0;
foreach ($this->insertValues as $insert_values) {
foreach ($this->insertFields as $idx => $field) {
if (isset($table_information->blob_fields[$field])) {
if (isset($table_information->blob_fields[$field]) && $insert_values[$idx] !== NULL) {
$blobs[$blob_count] = fopen('php://memory', 'a');
fwrite($blobs[$blob_count], $insert_values[$idx]);
rewind($blobs[$blob_count]);
......
......@@ -47,7 +47,7 @@ public function execute() {
foreach ($fields as $field => $value) {
$placeholder = ':db_update_placeholder_' . ($max_placeholder++);
if (isset($table_information->blob_fields[$field])) {
if (isset($table_information->blob_fields[$field]) && $value !== NULL) {
$blobs[$blob_count] = fopen('php://memory', 'a');
fwrite($blobs[$blob_count], $value);
rewind($blobs[$blob_count]);
......
......@@ -29,7 +29,7 @@ public function execute() {
$blob_count = 0;
foreach ($this->insertValues as $insert_values) {
foreach ($this->insertFields as $idx => $field) {
if (isset($table_information->blob_fields[$field])) {
if (isset($table_information->blob_fields[$field]) && $insert_values[$idx] !== NULL) {
$blobs[$blob_count] = fopen('php://memory', 'a');
fwrite($blobs[$blob_count], $insert_values[$idx]);
rewind($blobs[$blob_count]);
......
......@@ -24,6 +24,17 @@ public function testInsertOneBlob() {
$this->assertSame($data, $r['blob1'], new FormattableMarkup('Can insert a blob: id @id, @data.', ['@id' => $id, '@data' => serialize($r)]));
}
/**
* Tests that we can insert a null into blob field.
*/
public function testInsertNullBlob() {
$id = $this->connection->insert('test_one_blob')
->fields(['blob1' => NULL])
->execute();
$r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id])->fetchAssoc();
$this->assertNull($r['blob1']);
}
/**
* Tests that we can insert multiple blob fields in the same query.
*/
......
......@@ -31,6 +31,24 @@ public function testUpdateOneBlob() {
$this->assertSame($data, $r['blob1'], new FormattableMarkup('Can update a blob: id @id, @data.', ['@id' => $id, '@data' => serialize($r)]));
}
/**
* Tests that we can update a blob column to null.
*/
public function testUpdateNullBlob() {
$id = $this->connection->insert('test_one_blob')
->fields(['blob1' => 'test'])
->execute();
$r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id])->fetchAssoc();
$this->assertSame('test', $r['blob1']);
$this->connection->update('test_one_blob')
->fields(['blob1' => NULL])
->condition('id', $id)
->execute();
$r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id])->fetchAssoc();
$this->assertNull($r['blob1']);
}
/**
* Confirms that we can update two blob columns in the same table.
*/
......
......@@ -122,4 +122,26 @@ public function testUpsertNonExistingTable(): void {
$upsert->execute();
}
/**
* Tests that we can upsert a null into blob field.
*/
public function testUpsertNullBlob() {
$id = $this->connection->insert('test_one_blob')
->fields(['blob1' => 'test'])
->execute();
$r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id])->fetchAssoc();
$this->assertSame('test', $r['blob1']);
$this->connection->upsert('test_one_blob')
->key('id')
->fields(['id', 'blob1'])
->values(['id' => $id, 'blob1' => NULL])
->values(['id' => $id + 1, 'blob1' => NULL])
->execute();
$r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id])->fetchAssoc();
$this->assertNull($r['blob1']);
$r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id + 1])->fetchAssoc();
$this->assertNull($r['blob1']);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment