Changes for beta10 (#5)

- Add In/InArray support
- Add ORDER BY support for `Find` functions
- Update dependencies
- Implement fixes identified via static analysis

Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
2024-09-27 02:15:00 +00:00
parent 9e0e663811
commit d067f8983f
66 changed files with 1728 additions and 664 deletions

View File

@@ -51,7 +51,7 @@ class Query
*/
public static function whereById(string $paramName = ':id', mixed $docId = null): string
{
return self::whereByFields([Field::EQ(Configuration::$idField, $docId ?? '', $paramName)]);
return self::whereByFields([Field::equal(Configuration::$idField, $docId ?? '', $paramName)]);
}
/**
@@ -142,4 +142,46 @@ class Query
{
return "UPDATE $tableName SET data = :data WHERE " . self::whereById();
}
/**
* Create an `ORDER BY` clause ('n:' treats field as number, 'i:' does case-insensitive ordering)
*
* @param Field[] $fields The fields, named for the field plus directions (ex. 'field DESC NULLS FIRST')
* @return string The ORDER BY clause with the given fields
* @throws Exception If the database mode has not been set
*/
public static function orderBy(array $fields): string
{
if (empty($fields)) return "";
$mode = Configuration::mode('render ORDER BY clause');
$sqlFields = array_map(function (Field $it) use ($mode) {
if (str_contains($it->fieldName, ' ')) {
$parts = explode(' ', $it->fieldName);
$it->fieldName = array_shift($parts);
$direction = ' ' . implode(' ', $parts);
} else {
$direction = '';
}
if (str_starts_with($it->fieldName, 'n:')) {
$it->fieldName = substr($it->fieldName, 2);
$value = match ($mode) {
Mode::PgSQL => '(' . $it->path() . ')::numeric',
Mode::SQLite => $it->path()
};
} elseif (str_starts_with($it->fieldName, 'i:')) {
$it->fieldName = substr($it->fieldName, 2);
$value = match ($mode) {
Mode::PgSQL => 'LOWER(' . $it->path() . ')',
Mode::SQLite => $it->path() . ' COLLATE NOCASE'
};
} else {
$value = $it->path();
}
return $value . $direction;
}, $fields);
return ' ORDER BY ' . implode(', ', $sqlFields);
}
}