feat: support BackedEnum values in database binds#10256
Conversation
- Allow database escaping to use BackedEnum backing values - Support BackedEnum values in query bindings and Query Builder binds - Add focused tests and user guide examples Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
|
|
||
| $expectedSQL = 'SELECT * FROM "jobs" WHERE "status" IN (\'active\',\'inactive\')'; | ||
|
|
||
| $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
There was a problem hiding this comment.
While reviewing this (specifically the use of str_replace for query formatting), a broader architectural thought came to my mind: Why don't we have a dedicated assertSqlEquals method?
Currently, across many test files, we are writing boilerplate code like str_replace("\n", ' ', ...) to normalize SQL strings before comparing them. We could easily add an assertSqlEquals() method directly into use CodeIgniter\Test\DatabaseTestTrait;.
Something like this:
public function assertSqlEquals(string $expected, string $actual, string $message = ''): void
{
$normalize = fn(string $sql) => trim(strtolower(preg_replace('/\s+/', ' ', $sql)));
$this->assertSame($normalize($expected), $normalize($actual), $message);
}Let's see what the team thinks about this, though I believe it should be followed up separately in its own PR.
There was a problem hiding this comment.
Thanks for the review!
Yeah, I like that idea as a follow-up. I'd keep this PR focused, but a small test helper for this repeated SQL formatting pattern would definitely be worth exploring separately.
Only thing I'd be careful with is making the normalization too clever, so it doesn't accidentally hide meaningful SQL differences.
There was a problem hiding this comment.
I'm not sure about this normalization:
trim(strtolower(preg_replace('/\s+/', ' ', $sql)))That would transform our SQL way too much silently.
Personally, I would stick to what we use currently, so:
public function assertSqlEquals(string $expected, string $actual, string $message = ''): void
{
$this->assertSame(
$expected,
str_replace("\n", ' ', $actual),
$message,
);
}would make sense to me. But I would place it in the CIUnitTestCase because many builder unit tests don't use/need DatabaseTestTrait.
michalsn
left a comment
There was a problem hiding this comment.
Seems like native prepared statements are not supported.
$query = $db->prepare(...);
$query->execute(StatusEnum::ACTIVE);| $expectedSQL = 'INSERT INTO "jobs" ("id", "status") VALUES (1, \'active\')'; | ||
|
|
||
| $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert())); | ||
| } |
There was a problem hiding this comment.
Please add a test to make sure objects are also supported.
|
|
||
| $expectedSQL = 'SELECT * FROM "jobs" WHERE "status" IN (\'active\',\'inactive\')'; | ||
|
|
||
| $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
There was a problem hiding this comment.
I'm not sure about this normalization:
trim(strtolower(preg_replace('/\s+/', ' ', $sql)))That would transform our SQL way too much silently.
Personally, I would stick to what we use currently, so:
public function assertSqlEquals(string $expected, string $actual, string $message = ''): void
{
$this->assertSame(
$expected,
str_replace("\n", ' ', $actual),
$message,
);
}would make sense to me. But I would place it in the CIUnitTestCase because many builder unit tests don't use/need DatabaseTestTrait.
Description
This PR adds support for PHP
BackedEnumvalues in database escaping, query bindings, and escaped Query Builder values.It was inspired by @maniaba's forum post and #10223.
For apps that use native PHP enums for domain values, this makes database code a bit nicer to write:
CodeIgniter will use the enum's backing value when escaping it, so string-backed enums are treated like strings and int-backed enums are treated like integers.
This avoids repeating
->valueat every query call site, while keeping raw SQL and escape-disabled values in the caller's control.The implementation is intentionally small: enum cases are unwrapped during database escaping, so regular query bindings and escaped Query Builder values use the same existing path.
Checklist: