mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 09:35:49 +02:00
Added a field extension for SQLite and let the database sort the elements by the given ID order directly
This commit is contained in:
parent
1ec4266f96
commit
a4d411656b
4 changed files with 46 additions and 11 deletions
|
@ -38,6 +38,7 @@ doctrine:
|
||||||
string_functions:
|
string_functions:
|
||||||
regexp: DoctrineExtensions\Query\Mysql\Regexp
|
regexp: DoctrineExtensions\Query\Mysql\Regexp
|
||||||
ifnull: DoctrineExtensions\Query\Mysql\IfNull
|
ifnull: DoctrineExtensions\Query\Mysql\IfNull
|
||||||
|
field: DoctrineExtensions\Query\Mysql\Field
|
||||||
|
|
||||||
when@test:
|
when@test:
|
||||||
doctrine:
|
doctrine:
|
||||||
|
|
|
@ -47,14 +47,48 @@ class SQLiteRegexExtension
|
||||||
|
|
||||||
//Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation
|
//Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation
|
||||||
if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) {
|
if($native_connection instanceof \PDO && method_exists($native_connection, 'sqliteCreateFunction' )) {
|
||||||
$native_connection->sqliteCreateFunction('REGEXP', function ($pattern, $value) {
|
$native_connection->sqliteCreateFunction('REGEXP', $this->regexp(...), 2);
|
||||||
|
$native_connection->sqliteCreateFunction('FIELD', $this->field(...));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function emulates the MySQL regexp function for SQLite
|
||||||
|
* @param string $pattern
|
||||||
|
* @param string $value
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private function regexp(string $pattern, string $value): int
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
return (mb_ereg($pattern, $value)) ? 1 : 0;
|
return (mb_ereg($pattern, $value)) ? 1 : 0;
|
||||||
} catch (\ErrorException $e) {
|
} catch (\ErrorException $e) {
|
||||||
throw InvalidRegexException::fromMBRegexError($e);
|
throw InvalidRegexException::fromMBRegexError($e);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* This function emulates the MySQL field function for SQLite
|
||||||
|
* This function returns the index (position) of the first argument in the subsequent arguments.#
|
||||||
|
* If the first argument is not found or is NULL, 0 is returned.
|
||||||
|
* @param string|int|null $value
|
||||||
|
* @param mixed ...$array
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private function field(string|int|null $value, ...$array): int
|
||||||
|
{
|
||||||
|
if ($value === null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//We are loose with the types here
|
||||||
|
$index = array_search($value, $array, false);
|
||||||
|
|
||||||
|
if ($index === false) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $index + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,13 +56,14 @@ class AttachmentContainingDBElementRepository extends NamedDBElementRepository
|
||||||
$qb = $this->createQueryBuilder('element');
|
$qb = $this->createQueryBuilder('element');
|
||||||
$q = $qb->select('element')
|
$q = $qb->select('element')
|
||||||
->where('element.id IN (?1)')
|
->where('element.id IN (?1)')
|
||||||
|
//Order the results in the same order as the IDs in the input array (mysql supports this native, for SQLite we emulate it)
|
||||||
|
->orderBy('FIELD(element.id, ?1)')
|
||||||
->setParameter(1, $ids)
|
->setParameter(1, $ids)
|
||||||
->getQuery();
|
->getQuery();
|
||||||
|
|
||||||
$q->setFetchMode($this->getEntityName(), 'master_picture_attachment', ClassMetadataInfo::FETCH_EAGER);
|
$q->setFetchMode($this->getEntityName(), 'master_picture_attachment', ClassMetadataInfo::FETCH_EAGER);
|
||||||
|
|
||||||
$result = $q->getResult();
|
$result = $q->getResult();
|
||||||
$this->sortResultArrayByIDArray($result, $ids);
|
|
||||||
|
|
||||||
//Cache the result
|
//Cache the result
|
||||||
$this->elementsAndPreviewAttachmentCache[$cache_key] = $result;
|
$this->elementsAndPreviewAttachmentCache[$cache_key] = $result;
|
||||||
|
|
|
@ -113,13 +113,12 @@ class DBElementRepository extends EntityRepository
|
||||||
$q = $qb->select('element')
|
$q = $qb->select('element')
|
||||||
->where('element.id IN (?1)')
|
->where('element.id IN (?1)')
|
||||||
->setParameter(1, $ids)
|
->setParameter(1, $ids)
|
||||||
|
//Order the results in the same order as the IDs in the input array (mysql supports this native, for SQLite we emulate it)
|
||||||
|
->orderBy('FIELD(element.id, ?1)')
|
||||||
->getQuery();
|
->getQuery();
|
||||||
|
|
||||||
$result = $q->getResult();
|
$result = $q->getResult();
|
||||||
|
|
||||||
//Sort the result so that the elements are in the same order as the input array
|
|
||||||
$this->sortResultArrayByIDArray($result, $ids);
|
|
||||||
|
|
||||||
//Cache the result
|
//Cache the result
|
||||||
$this->find_elements_by_id_cache[$cache_key] = $result;
|
$this->find_elements_by_id_cache[$cache_key] = $result;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue