Skip to content

RawSql not working in UpdateBatch #6554

Closed
@pixobit

Description

@pixobit

PHP Version

8.0

CodeIgniter4 Version

4.2.6

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MySQL 5.6

What happened?

I'm trying to implement a decrement with UpdateBatch, and previously used the escape, but was recommended to use the RawSql class instead. However, when I checked the query, it seems to be escaped, and when I digged deeper, I didn't see RawSql being implemented in the UpdateBatch method.

Steps to Reproduce

$this->table('table')->updateBatch([
    'id' => 1,
    'int_column' => new RawSql('(CASE WHEN int_column > 1 THEN int_column - 1 ELSE 0  END)')
], 'id')

Expected Output

UPDATE `table` SET `int_column` = CASE WHEN `id` = '1' THEN (CASE WHEN int_column > 1 THEN int_column - 1 ELSE 0 END) ELSE `int_column` END WHERE `id` IN('1')

Anything else?

One way to solve this, is to update this line (in Database/BaseBuilder.php)
$clean[$this->db->protectIdentifiers($k2, false)] = $escape ? $this->db->escape($v2) : $v2;
to
$clean[$this->db->protectIdentifiers($k2, false)] = $escape && !($v2 instanceof RawSql) ? $this->db->escape($v2) : (string) $v2;

Metadata

Metadata

Assignees

No one assigned

    Labels

    databaseIssues or pull requests that affect the database layerenhancementPRs that improve existing functionalities

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions