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

Merge branch '19-gestion-du-type-pour-les-criterions' into 'develop'

Resolve "Gestion du type pour les criterions"

Closes #19

See merge request !18
parents 1c675079 789deb24
Pipeline #1131 failed with stages
in 2 minutes and 35 seconds
......@@ -5,7 +5,7 @@ require 'vendor/autoload.php';
$settings = require './src/settings.php';
$metadata = $settings['settings']['metadata'];
$c = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($metadata['entity_path'], $metadata['dev_mode']);
$c = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array('src/Entity'), $metadata['dev_mode']);
$c->setProxyDir(getcwd() . '/' . $metadata['path_proxy']);
if ($metadata['dev_mode']) {
$c->setAutoGenerateProxyClasses(true);
......
......@@ -28,12 +28,21 @@ final class DatasetListAction
private $logger;
private $em;
private $dcf;
/**
* The encryption key used by anis to encrypt and decrypt sensitive data like passwords
* This key is provided by configuration (see the config file)
*
* @var string
*/
private $encryptionKey;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em, DBALConnectionFactory $dcf)
public function __construct(LoggerInterface $logger, EntityManagerInterface $em, DBALConnectionFactory $dcf, string $encryptionKey)
{
$this->logger = $logger;
$this->em = $em;
$this->dcf = $dcf;
$this->encryptionKey = $encryptionKey;
}
public function __invoke(Request $request, Response $response, array $args): Response
......@@ -111,7 +120,8 @@ final class DatasetListAction
private function postAttributes(Dataset $dataset): void
{
$database = $dataset->getProject()->getDatabase();
$connection = $this->dcf->create($database);
$decryptedPassword = $this->decryptData($database->getPassword());
$connection = $this->dcf->create($database, $decryptedPassword);
$sm = $connection->getSchemaManager();
$columns = $sm->listTableColumns($dataset->getTableRef());
$i = 10;
......
......@@ -15,6 +15,8 @@ use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\Entity\Dataset;
use App\Entity\Attribute;
use App\Utils\ActionTrait;
use App\Utils\DBALConnectionFactory;
use App\utils\AnisQueryBuilderFactory;
......@@ -122,6 +124,25 @@ final class SearchMetaAction
)->withStatus(400);
}
$queryParams = $request->getQueryParams();
if (!array_key_exists('a', $queryParams)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param a is required for this request'
)->withStatus(400);
}
$listOfIds = explode(';', $queryParams['a']);
$attributesSelected = array();
foreach($listOfIds as $id) {
$attribute = $this->getAttribute($dataset, (int) $id);
$attributesSelected[] = array(
'name' => $attribute->getName(),
'label' => $attribute->getLabel()
);
}
$database = $dataset->getProject()->getDatabase();
$decryptedPassword = $this->decryptData($database->getPassword());
$connection = $this->dcf->create($database, $decryptedPassword);
......@@ -137,10 +158,22 @@ final class SearchMetaAction
$result = $anisQueryBuilder->fetchAll();
$meta = array();
$meta['dataset-selected'] = $dataset->getLabel();
$meta['total-items'] = $result[0]['nb'];
$meta['url'] = $dataset->getName() . '?' . $request->getUri()->getQuery();
$meta['dataset_selected'] = $dataset->getLabel();
$meta['attributes_selected'] = $attributesSelected;
$meta['total_items'] = $result[0]['nb'];
// $meta['url'] = $dataset->getName() . '?' . $request->getUri()->getQuery();
return $response->withJson($meta);
}
private function getAttribute(Dataset $dataset, int $id): Attribute
{
$attributes = $dataset->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getId() === $id) {
return $attribute;
}
}
throw SearchException::attributeNotFound($id, $dataset->getLabel());
}
}
......@@ -59,12 +59,13 @@ class AnisQueryBuilder
$params = $this->getCriterionParams($criterion);
$attribute = $this->getAttribute((int) $params[0]);
$column = $attribute->getTableName() . '.' . $attribute->getName();
$columnType = $attribute->getType();
if (array_key_exists(2, $params)) {
$values = explode('|', $params[2]);
} else {
$values = array();
}
$operator = $this->operatorFactory->create($params[1], $this->queryBuilder->expr(), $column, $values);
$operator = $this->operatorFactory->create($params[1], $this->queryBuilder->expr(), $column, $columnType, $values);
$expressions[] = $operator->getExpression();
}
$this->queryBuilder->where(new CompositeExpression(CompositeExpression::TYPE_AND, $expressions));
......
......@@ -18,9 +18,9 @@ class Between extends Operator
private $value1;
private $value2;
public function __construct(ExpressionBuilder $expr, string $column, float $value1, float $value2)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value1, string $value2)
{
parent::__construct($expr, $column);
parent::__construct($expr, $column, $columnType);
$this->value1 = $value1;
$this->value2 = $value2;
}
......@@ -28,8 +28,8 @@ class Between extends Operator
public function getExpression()
{
$args = array();
$args[] = $this->expr->gte($this->column, $this->value1);
$args[] = $this->expr->lte($this->column, $this->value2);
$args[] = $this->expr->gte($this->column, $this->getSqlValue($this->value1));
$args[] = $this->expr->lte($this->column, $this->getSqlValue($this->value2));
return new CompositeExpression(CompositeExpression::TYPE_AND, $args);
}
}
......@@ -16,19 +16,15 @@ class Equal extends Operator
{
private $value;
public function __construct(ExpressionBuilder $expr, string $column, string $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
if (is_numeric($value)) {
$this->value = $value;
} else {
$this->value = $this->expr->literal($value);
}
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression()
{
$expr = $this->expr->eq($this->column, $this->value);
$expr = $this->expr->eq($this->column, $this->getSqlValue($this->value));
return $expr;
}
}
......@@ -14,19 +14,16 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class GreaterThan extends Operator
{
/**
* @var number
*/
private $value;
public function __construct(ExpressionBuilder $expr, string $column, float $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression() : string
{
return $this->expr->gt($this->column, $this->value);
return $this->expr->gt($this->column, $this->getSqlValue($this->value));
}
}
......@@ -14,19 +14,16 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class GreaterThanEqual extends Operator
{
/**
* @var number
*/
private $value;
public function __construct(ExpressionBuilder $expr, string $column, float $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression() : string
{
return $this->expr->gte($this->column, $this->value);
return $this->expr->gte($this->column, $this->getSqlValue($this->value));
}
}
......@@ -14,5 +14,5 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
interface IOperatorFactory
{
public function create(string $type, ExpressionBuilder $expr, string $column, array $parameters): IOperator;
public function create(string $type, ExpressionBuilder $expr, string $column, string $columnType, array $parameters): IOperator;
}
......@@ -16,20 +16,14 @@ class In extends Operator
{
private $values;
public function __construct(ExpressionBuilder $expr, string $column, array $values)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, array $values)
{
parent::__construct($expr, $column);
if (is_numeric($this->values[0])) {
$this->values = $values;
} else {
$this->values = array_map(function (string $value): string {
return $this->expr->literal($value);
}, $values);
}
parent::__construct($expr, $column, $columnType);
$this->values = $values;
}
public function getExpression()
{
return $this->expr->in($this->column, $this->values);
return $this->expr->in($this->column, array_map(array($this, 'getSqlValue'), $this->values));
}
}
......@@ -14,19 +14,16 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class LessThan extends Operator
{
/**
* @var number
*/
private $value;
public function __construct(ExpressionBuilder $expr, string $column, float $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression() : string
{
return $this->expr->lt($this->column, $this->value);
return $this->expr->lt($this->column, $this->getSqlValue($this->value));
}
}
......@@ -14,19 +14,16 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class LessThanEqual extends Operator
{
/**
* @var number
*/
private $value;
public function __construct(ExpressionBuilder $expr, string $column, float $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression() : string
{
return $this->expr->lte($this->column, $this->value);
return $this->expr->lte($this->column, $this->getSqlValue($this->value));
}
}
......@@ -16,14 +16,14 @@ class Like extends Operator
{
private $value;
public function __construct(ExpressionBuilder $expr, string $column, string $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
$this->value = $this->expr->literal($value);
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression()
{
return $this->expr->like($this->column, $this->value);
return $this->expr->like($this->column, $this->expr->literal($this->value));
}
}
......@@ -16,18 +16,14 @@ class NotEqual extends Operator
{
private $value;
public function __construct(ExpressionBuilder $expr, string $column, string $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
if (is_numeric($value)) {
$this->value = $value;
} if (is_string($value)) {
$this->value = $this->expr->literal($value);
}
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression()
{
return $this->expr->neq($this->column, $this->value);
return $this->expr->neq($this->column, $this->getSqlValue($this->value));
}
}
......@@ -16,20 +16,14 @@ class NotIn extends Operator
{
private $values;
public function __construct(ExpressionBuilder $expr, string $column, array $values)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, array $values)
{
parent::__construct($expr, $column);
if (is_numeric($this->values[0])) {
$this->values = $values;
} else {
$this->values = array_map(function (string $value): string {
return $this->expr->literal($value);
}, $values);
}
parent::__construct($expr, $column, $columnType);
$this->values = $values;
}
public function getExpression()
{
return $this->expr->notIn($this->column, $this->values);
return $this->expr->notIn($this->column, array_map(array($this, 'getSqlValue'), $this->values));
}
}
......@@ -16,14 +16,14 @@ class NotLike extends Operator
{
private $value;
public function __construct(ExpressionBuilder $expr, string $column, string $value)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType, string $value)
{
parent::__construct($expr, $column);
$this->value = $this->expr->literal($value);
parent::__construct($expr, $column, $columnType);
$this->value = $value;
}
public function getExpression()
{
return $this->expr->notLike($this->column, $this->value);
return $this->expr->notLike($this->column, $this->expr->literal($this->value));
}
}
......@@ -11,6 +11,7 @@
namespace App\Search\Operator;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Types\Type;
abstract class Operator implements IOperator
{
......@@ -24,9 +25,27 @@ abstract class Operator implements IOperator
*/
protected $column;
public function __construct(ExpressionBuilder $expr, string $column)
/**
* @var string
*/
protected $columnType;
public function __construct(ExpressionBuilder $expr, string $column, string $columnType)
{
$this->expr = $expr;
$this->column = $column;
$this->columnType = $columnType;
}
protected function getSqlValue(string $value): string
{
switch ($this->columnType) {
case Type::STRING:
case Type::TEXT:
case Type::DATE:
return $this->expr->literal($value);
default:
return $value;
}
}
}
......@@ -14,47 +14,47 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class OperatorFactory implements IOperatorFactory
{
public function create(string $type, ExpressionBuilder $expr, string $column, array $parameters): IOperator
public function create(string $type, ExpressionBuilder $expr, string $column, string $columnType, array $parameters): IOperator
{
switch ($type) {
case 'bw':
return new Between($expr, $column, (float) $parameters[0], (float) $parameters[1]);
return new Between($expr, $column, $columnType, $parameters[0], $parameters[1]);
case 'eq':
return new Equal($expr, $column, $parameters[0]);
return new Equal($expr, $column, $columnType, $parameters[0]);
case 'neq':
return new NotEqual($expr, $column, $parameters[0]);
return new NotEqual($expr, $column, $columnType, $parameters[0]);
case 'gt':
return new GreaterThan($expr, $column, (float) $parameters[0]);
return new GreaterThan($expr, $column, $columnType, $parameters[0]);
case 'gte':
return new GreaterThanEqual($expr, $column, (float) $parameters[0]);
return new GreaterThanEqual($expr, $column, $columnType, $parameters[0]);
case 'lt':
return new LessThan($expr, $column, (float) $parameters[0]);
return new LessThan($expr, $column, $columnType, $parameters[0]);
case 'lte':
return new LessThanEqual($expr, $column, (float) $parameters[0]);
return new LessThanEqual($expr, $column, $columnType, $parameters[0]);
case 'lk':
return new Like($expr, $column, $parameters[0]);
return new Like($expr, $column, $columnType, $parameters[0]);
case 'nlk':
return new NotLike($expr, $column, $parameters[0]);
return new NotLike($expr, $column, $columnType, $parameters[0]);
case 'in':
return new In($expr, $column, $parameters);
return new In($expr, $column, $columnType, $parameters);
case 'nin':
return new NotIn($expr, $column, $parameters);
return new NotIn($expr, $column, $columnType, $parameters);
case 'nl':
return new OperatorNull($expr, $column);
return new OperatorNull($expr, $column, $columnType);
case 'nnl':
return new OperatorNotNull($expr, $column);
return new OperatorNotNull($expr, $column, $columnType);
default:
throw OperatorException::unknownOperator($type);
......
......@@ -14,9 +14,9 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class OperatorNotNull extends Operator
{
public function __construct(ExpressionBuilder $expr, string $column)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType)
{
parent::__construct($expr, $column);
parent::__construct($expr, $column, $columnType);
}
public function getExpression()
......
......@@ -14,9 +14,9 @@ use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
class OperatorNull extends Operator
{
public function __construct(ExpressionBuilder $expr, string $column)
public function __construct(ExpressionBuilder $expr, string $column, string $columnType)
{
parent::__construct($expr, $column);
parent::__construct($expr, $column, $columnType);
}
public function getExpression()
......
Supports Markdown
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