Skip to content

Commit 5a48910

Browse files
committed
fix FIELD()
1 parent dbd3252 commit 5a48910

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

code/SQLite3Database.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,28 @@ public function clearTable($table)
612612
$this->query("DELETE FROM \"$table\"");
613613
}
614614

615+
/**
616+
* Generate SQL for sorting by a specific field using SQLite-compatible CASE logic.
617+
*
618+
* Older framework versions may default to MySQL's FIELD() syntax, which SQLite
619+
* can't execute. Keeping this override in the adapter ensures custom relation
620+
* ordering stays portable across supported framework releases.
621+
*
622+
* @param string $field
623+
* @param array $values
624+
* @return string
625+
*/
626+
public function sortByField(string $field, array $values): string
627+
{
628+
$caseStatements = [];
629+
foreach ($values as $index => $value) {
630+
$escaped = is_int($value) ? $value : $this->quoteString($value);
631+
$caseStatements[] = sprintf('WHEN "%s" = %s THEN %d', $field, $escaped, $index);
632+
}
633+
634+
return sprintf('CASE %s ELSE %d END', implode(' ', $caseStatements), count($caseStatements));
635+
}
636+
615637
public function comparisonClause(
616638
$field,
617639
$value,

0 commit comments

Comments
 (0)