Commit 995f2bf5 authored by François Agneray's avatar François Agneray
Browse files

In progress...

parent 634082d8
......@@ -10,11 +10,15 @@ services:
METADATA_DOCTRINE_PATH_PROXY: "/tmp/doctrine_proxy"
METADATA_DOCTRINE_DEV_MODE: "true"
METADATA_DB_DRIVER: "pdo_pgsql"
METADATA_DB_HOST: "db"
#METADATA_DB_HOST: "db"
METADATA_DB_HOST: "cesamsidb.lam.fr"
METADATA_DB_PORT: 5432
METADATA_DB_DBNAME: "anis_metamodel"
METADATA_DB_USER: "anis"
METADATA_DB_PASSWORD: "anis"
#METADATA_DB_DBNAME: "anis_metamodel"
METADATA_DB_DBNAME: "svom_sdb_dc1_metamodel"
#METADATA_DB_USER: "anis"
#METADATA_DB_PASSWORD: "anis"
METADATA_DB_USER: "svom"
METADATA_DB_PASSWORD: "svom#$$"
MAILER_HOST: "mailer"
MAILER_PORT: 25
LOGGER_NAME: "anis-v3-server"
......
......@@ -12,6 +12,7 @@ namespace App\Action\Search;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Psr\Http\Message\ServerRequestInterface as Request;
......@@ -131,8 +132,9 @@ final class SearchAction
$database = $dataset->getProject()->getDatabase();
$decryptedPassword = $this->decryptData($database->getPassword());
$connection = $this->dcf->create($database, $decryptedPassword);
$this->operatorFactory->setDatabasePlatform($connection->getDatabasePlatform());
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->from($dataset->getTableRef());
try {
......@@ -183,7 +185,7 @@ final class SearchAction
$this->logger->info('SQL: ' . $queryBuilder->getSQL());
return $response->withJson($result, 200, JSON_NUMERIC_CHECK);
return $response->withJson($result, 200, JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES);
}
private function getAndVerifySearchType(string $type)
......
<?php declare(strict_types=1);
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Utils\Operator;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class JsonPostgres extends Operator
{
private $path;
private $operator;
private $value;
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $path, string $operator, string $value)
{
parent::__construct($expr, $column, $columnType);
$this->path = explode(',', $path);
$this->operator = $operator;
$this->value = $value;
}
public function getExpression(): string
{
switch ($this->operator) {
case 'eq':
return $this->expr->eq($this->getColumn(), $this->getValue());
break;
case 'gt':
return $this->expr->gt($this->getColumn(), $this->getValue());
break;
}
}
private function getColumn(): string
{
return $this->column . '->' . implode('->', array_map(array($this, 'getSqlValue'), $this->path));
}
private function getValue()
{
if ($this->value === 'true' || $this->value === 'false') {
return $this->getSqlValue($this->value);
} else {
return $this->getSqlValue(json_encode($this->value));
}
}
}
......@@ -38,4 +38,14 @@ class OperatorException extends SearchException
{
return new self("You passed a string value '" . $value . "' or the criterion value required a numeric value");
}
public static function operatorJsonNotSupported()
{
return new self("Operator json is not supported by the database platform used");
}
public static function operatorJsonNeedColumnTypeJson()
{
return new self("Operator js need a json attribute type");
}
}
......@@ -10,10 +10,19 @@
*/
namespace App\Utils\Operator;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class OperatorFactory implements IOperatorFactory
{
private $databasePlatform;
public function setDatabasePlatform(AbstractPlatform $databasePlatform)
{
$this->databasePlatform = $databasePlatform;
}
public function create(string $type, ExpressionBuilder $expr, string $column, string $columnType, array $parameters): IOperator
{
switch ($type) {
......@@ -85,18 +94,30 @@ class OperatorFactory implements IOperatorFactory
case 'nl':
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
throw OperatorException::operatorBadNumberOfParameters($type, 0);
}
return new OperatorNull($expr, $column, $columnType);
case 'nnl':
if (count($parameters) != 0) {
throw OperatorException::operatorBadNumberOfParameters($type, 1);
throw OperatorException::operatorBadNumberOfParameters($type, 0);
}
return new OperatorNotNull($expr, $column, $columnType);
case 'js':
if (!($this->databasePlatform instanceof PostgreSQL94Platform)) {
throw OperatorException::operatorJsonNotSupported();
}
if ($columnType !== 'json') {
throw OperatorException::operatorJsonNeedColumnTypeJson();
}
if (count($parameters) != 3) {
throw OperatorException::operatorBadNumberOfParameters($type, 3);
}
return new JsonPostgres($expr, $column, $columnType, $parameters[0], $parameters[1], $parameters[2]);
default:
throw OperatorException::unknownOperator($type);
throw OperatorException::unknownOperator();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment