2020-05-13 13:10:24 +01:00
|
|
|
<?php
|
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// {{{ License
|
2020-09-10 23:25:47 +01:00
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2020-09-10 23:25:47 +01:00
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// }}}
|
|
|
|
|
2020-05-13 13:10:24 +01:00
|
|
|
/**
|
|
|
|
* Doctrine entity manager static wrapper
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category DB
|
|
|
|
*
|
2021-02-19 23:29:43 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-05-13 13:10:24 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2020-06-04 22:00:05 +01:00
|
|
|
namespace App\Core\DB;
|
2020-05-13 13:10:24 +01:00
|
|
|
|
2021-04-25 22:15:24 +01:00
|
|
|
use App\Util\Exception\DuplicateFoundException;
|
2020-09-10 23:25:47 +01:00
|
|
|
use App\Util\Exception\NotFoundException;
|
2020-07-21 22:32:21 +01:00
|
|
|
use Doctrine\Common\Collections\Criteria;
|
2021-05-11 22:04:15 +01:00
|
|
|
use Doctrine\Common\Collections\Expr\Expression;
|
2020-07-21 22:32:21 +01:00
|
|
|
use Doctrine\Common\Collections\ExpressionBuilder;
|
2020-05-14 22:55:04 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-08-14 23:36:08 +01:00
|
|
|
use Doctrine\ORM\Query;
|
2020-09-04 19:45:28 +01:00
|
|
|
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
2021-04-15 23:34:55 +01:00
|
|
|
use Functional as F;
|
2020-05-13 13:10:24 +01:00
|
|
|
|
|
|
|
abstract class DB
|
|
|
|
{
|
2020-05-14 22:55:04 +01:00
|
|
|
private static ?EntityManagerInterface $em;
|
|
|
|
public static function setManager($m): void
|
2020-05-13 13:10:24 +01:00
|
|
|
{
|
|
|
|
self::$em = $m;
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:01:52 +01:00
|
|
|
/**
|
|
|
|
* Table name to class map, used to allow specifying table names instead of classes in doctrine calls
|
|
|
|
*/
|
|
|
|
private static array $table_map = [];
|
2021-07-21 17:40:48 +01:00
|
|
|
private static array $class_pk = [];
|
2021-04-15 18:01:52 +01:00
|
|
|
public static function initTableMap()
|
|
|
|
{
|
2021-05-05 17:03:03 +01:00
|
|
|
$all = self::$em->getMetadataFactory()->getAllMetadata();
|
|
|
|
foreach ($all as $meta) {
|
2021-07-21 17:40:48 +01:00
|
|
|
self::$table_map[$meta->getTableName()] = $meta->getMetadataValue('name');
|
|
|
|
self::$class_pk[$meta->getMetadataValue('name')] = $meta->getIdentifier();
|
2021-04-15 18:01:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:40:48 +01:00
|
|
|
public static function getTableForClass(string $class)
|
|
|
|
{
|
|
|
|
return array_search($class, self::$table_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getPKForClass(string $class)
|
|
|
|
{
|
|
|
|
return self::$class_pk[$class];
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Perform a Doctrine Query Language query
|
|
|
|
*/
|
2020-08-28 21:16:26 +01:00
|
|
|
public static function dql(string $query, array $params = [])
|
2020-08-14 23:36:08 +01:00
|
|
|
{
|
2021-04-15 23:34:55 +01:00
|
|
|
$query = preg_replace(F\map(self::$table_map, function ($_, $s) { return "/\\b{$s}\\b/"; }), self::$table_map, $query);
|
|
|
|
$q = new Query(self::$em);
|
2020-08-14 23:36:08 +01:00
|
|
|
$q->setDQL($query);
|
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
$q->setParameter($k, $v);
|
|
|
|
}
|
|
|
|
return $q->getResult();
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Perform a native, parameterized, SQL query. $entities is a map
|
|
|
|
* from table aliases to class names. Replaces '{select}' in
|
|
|
|
* $query with the appropriate select list
|
|
|
|
*/
|
2020-09-04 19:45:28 +01:00
|
|
|
public static function sql(string $query, array $entities, array $params = [])
|
|
|
|
{
|
|
|
|
$rsm = new ResultSetMappingBuilder(self::$em);
|
|
|
|
foreach ($entities as $alias => $entity) {
|
|
|
|
$rsm->addRootEntityFromClassMetadata($entity, $alias);
|
|
|
|
}
|
|
|
|
$query = preg_replace('/{select}/', $rsm->generateSelectClause(), $query);
|
|
|
|
$q = self::$em->createNativeQuery($query, $rsm);
|
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
$q->setParameter($k, $v);
|
|
|
|
}
|
|
|
|
return $q->getResult();
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* A list of possible operations needed in self::buildExpression
|
|
|
|
*/
|
|
|
|
private static array $find_by_ops = [
|
|
|
|
'or', 'and', 'eq', 'neq', 'lt', 'lte',
|
2020-07-25 02:55:39 +01:00
|
|
|
'gt', 'gte', 'is_null', 'in', 'not_in',
|
2020-11-06 19:47:15 +00:00
|
|
|
'contains', 'member_of', 'starts_with', 'ends_with',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a Doctrine Criteria expression from the given $criteria.
|
|
|
|
*
|
|
|
|
* @see self::findBy for the syntax
|
|
|
|
*/
|
2021-05-11 22:04:15 +01:00
|
|
|
private static function buildExpression(ExpressionBuilder $eb, array $criteria): array
|
2020-07-21 22:32:21 +01:00
|
|
|
{
|
|
|
|
$expressions = [];
|
|
|
|
foreach ($criteria as $op => $exp) {
|
|
|
|
if ($op == 'or' || $op == 'and') {
|
|
|
|
$method = "{$op}X";
|
2021-04-16 16:55:50 +01:00
|
|
|
$expr = self::buildExpression($eb, $exp);
|
|
|
|
if (is_array($expr)) {
|
2021-05-11 22:04:15 +01:00
|
|
|
$expressions[] = $eb->{$method}(...$expr);
|
2021-04-16 16:55:50 +01:00
|
|
|
} else {
|
2021-05-11 22:04:15 +01:00
|
|
|
$expressions[] = $eb->{$method}($expr);
|
2021-04-16 16:55:50 +01:00
|
|
|
}
|
2020-07-21 22:32:21 +01:00
|
|
|
} elseif ($op == 'is_null') {
|
|
|
|
$expressions[] = $eb->isNull($exp);
|
|
|
|
} else {
|
|
|
|
if (in_array($op, self::$find_by_ops)) {
|
2021-05-11 22:04:15 +01:00
|
|
|
foreach ($exp as $field => $value) {
|
|
|
|
$expressions[] = $eb->{$op}($field, $value);
|
|
|
|
}
|
2020-07-21 22:32:21 +01:00
|
|
|
} else {
|
|
|
|
$expressions[] = $eb->eq($op, $exp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expressions;
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Query $table according to $criteria. If $criteria's keys are
|
|
|
|
* one of self::$find_by_ops (and, or, etc), build a subexpression
|
|
|
|
* with that operator and recurse. Examples of $criteria are
|
|
|
|
* `['and' => ['lt' => ['foo' => 4], 'gte' => ['bar' => 2]]]` or
|
|
|
|
* `['in' => ['foo', 'bar']]`
|
|
|
|
*/
|
2020-07-25 18:49:57 +01:00
|
|
|
public static function findBy(string $table, array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
|
2020-07-21 22:32:21 +01:00
|
|
|
{
|
2021-08-04 21:04:18 +01:00
|
|
|
$criteria = array_change_key_case($criteria, CASE_LOWER);
|
2020-07-21 22:32:21 +01:00
|
|
|
$ops = array_intersect(array_keys($criteria), self::$find_by_ops);
|
2020-07-25 18:49:57 +01:00
|
|
|
$repo = self::getRepository($table);
|
2020-07-21 22:32:21 +01:00
|
|
|
if (empty($ops)) {
|
|
|
|
return $repo->findBy($criteria, $orderBy, $limit, $offset);
|
|
|
|
} else {
|
2021-05-11 22:04:15 +01:00
|
|
|
$eb = Criteria::expr();
|
|
|
|
$criteria = new Criteria($eb->andX(...self::buildExpression($eb, $criteria)), $orderBy, $offset, $limit);
|
2020-07-25 18:49:57 +01:00
|
|
|
return $repo->matching($criteria)->toArray(); // Always work with array or it becomes really complicated
|
2020-07-21 22:32:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Return the first element of the result of @see self::findBy
|
|
|
|
*/
|
2020-07-21 22:32:21 +01:00
|
|
|
public static function findOneBy(string $table, array $criteria, ?array $orderBy = null, ?int $offset = null)
|
|
|
|
{
|
2021-08-16 17:07:00 +01:00
|
|
|
$res = self::findBy($table, $criteria, $orderBy, 2, $offset); // Use limit 2 to check for consistency
|
2021-07-28 22:09:12 +01:00
|
|
|
switch (count($res)) {
|
|
|
|
case 0:
|
|
|
|
throw new NotFoundException("No value in table {$table} matches the requested criteria");
|
|
|
|
case 1:
|
2020-07-25 18:49:57 +01:00
|
|
|
return $res[0];
|
2021-07-28 22:09:12 +01:00
|
|
|
default:
|
|
|
|
throw new DuplicateFoundException("Multiple values in table {$table} match the requested criteria");
|
2020-07-25 02:55:39 +01:00
|
|
|
}
|
2020-07-21 22:32:21 +01:00
|
|
|
}
|
|
|
|
|
2021-04-11 12:03:32 +01:00
|
|
|
public static function count(string $table, array $criteria)
|
|
|
|
{
|
|
|
|
$repo = self::getRepository($table);
|
2021-05-11 22:04:15 +01:00
|
|
|
return $repo->count($criteria);
|
2021-04-11 12:03:32 +01:00
|
|
|
}
|
|
|
|
|
2021-04-23 13:54:25 +01:00
|
|
|
/**
|
|
|
|
* Insert all given objects with the generated ID of the first one
|
|
|
|
*/
|
|
|
|
public static function persistWithSameId(object $owner, object | array $others, ?callable $extra = null)
|
|
|
|
{
|
|
|
|
$conn = self::getConnection();
|
|
|
|
$metadata = self::getClassMetadata(get_class($owner));
|
|
|
|
$seqName = $metadata->getSequenceName($conn->getDatabasePlatform());
|
|
|
|
self::persist($owner);
|
|
|
|
$id = $conn->lastInsertId($seqName);
|
|
|
|
F\map(is_array($others) ? $others : [$others], function ($o) use ($id) { $o->setId($id); self::persist($o); });
|
|
|
|
if (!is_null($extra)) {
|
|
|
|
$extra($id);
|
|
|
|
}
|
|
|
|
self::flush();
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Intercept static function calls to allow refering to entities
|
|
|
|
* without writing the namespace (which is deduced from the call
|
|
|
|
* context)
|
|
|
|
*/
|
2020-05-14 22:55:04 +01:00
|
|
|
public static function __callStatic(string $name, array $args)
|
2020-05-13 13:10:24 +01:00
|
|
|
{
|
2021-04-15 18:01:52 +01:00
|
|
|
if (in_array($name, ['find', 'getReference', 'getPartialReference', 'getRepository'])
|
2021-07-21 17:40:48 +01:00
|
|
|
&& !str_contains($args[0], '\\')) {
|
2021-04-15 18:01:52 +01:00
|
|
|
$args[0] = self::$table_map[$args[0]];
|
2020-08-13 02:23:22 +01:00
|
|
|
}
|
2020-07-18 03:16:18 +01:00
|
|
|
|
2020-05-14 22:55:04 +01:00
|
|
|
return self::$em->{$name}(...$args);
|
2020-05-13 13:10:24 +01:00
|
|
|
}
|
|
|
|
}
|