Skip to content

Commit

Permalink
Merge a114aff into 77288e5
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Dec 8, 2021
2 parents 77288e5 + a114aff commit 4229b2c
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 266 deletions.
28 changes: 19 additions & 9 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
* Pattern fluent pour la création et configuration des types de données.
*
* @author Mathieu NOËL <mathieu@soosyze.com>
*
* @phpstan-type FieldToArray array{
* _comment?: string,
* default?: null|scalar,
* length?: int,
* nullable?: bool,
* opt?: string,
* type: string,
* unsigned?: bool,
* }
*/
abstract class Field
{
Expand All @@ -33,7 +43,7 @@ abstract class Field
protected const INVALID_ARGUMENT_MESSAGE = 'The value of the %s field must be of type %s: %s given.';

/**
* @var bool|null|numeric|string
* @var null|scalar
*/
protected $valueDefault;

Expand Down Expand Up @@ -92,19 +102,19 @@ public function nullable(): self
* Enregistre une valeur par défaut au champ précédent.
* Lève une exception si la valeur par défaut ne correspond pas au type de valeur passée en paramètre.
*
* @param bool|null|numeric|string $value Valeur à tester.
* @param null|scalar $value Valeur à tester.
*
* @throws ColumnsValueException
*
* @return bool|null|numeric|string
* @return null|scalar
*/
abstract public function filterValue($value);

/**
* Enregistre une valeur par défaut au champ précédent.
* Lève une exception si la valeur par défaut ne correspond pas au type de valeur passée en paramètre.
*
* @param bool|null|numeric|string $value Valeur à tester.
* @param null|scalar $value Valeur à tester.
*
* @throws TableBuilderException
*
Expand All @@ -122,7 +132,7 @@ public function valueDefault($value)
*
* @throws ColumnsValueException
*
* @return bool|null|numeric|string Valeur par defaut.
* @return null|scalar Valeur par defaut.
*/
public function getValueDefault()
{
Expand Down Expand Up @@ -179,13 +189,13 @@ public function setName(string $name): self
* Retourne les données du champ.
*
* @return array
*
* @phpstan-return FieldToArray
*/
public function toArray(): array
{
$data = [];
if (static::TYPE !== '') {
$data[ 'type' ] = static::TYPE;
}
$data[ 'type' ] = static::TYPE;

if ($this->isNullable) {
$data[ 'nullable' ] = $this->isNullable;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Field/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace Queryflatfile\Field;

use Queryflatfile\Field;

/**
* @author Mathieu NOËL <mathieu@soosyze.com>
*/
Expand Down
107 changes: 59 additions & 48 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@
* Les requêtes se construisent avec le pattern fluent.
*
* @author Mathieu NOËL <mathieu@soosyze.com>
*
* @phpstan-import-type RowData from Schema
* @phpstan-import-type TableData from Schema
*/
class Request extends RequestHandler
{
/**
* Toutes les configurations du schéma des champs utilisés.
* Tous les champs utilisés.
*
* @var array<Field>
* @var Field[]
*/
private $allColumnsSchema;
private $allFieldsSchema;

/**
* Les données de la table.
*
* @var array
*
* @phpstan-var TableData
*/
private $tableData = [];

Expand Down Expand Up @@ -90,7 +95,7 @@ public function __toString(): string
if ($this->where) {
$output .= sprintf('WHERE %s ', (string) $this->where);
}
foreach ($this->union as $union) {
foreach ($this->unions as $union) {
$type = $union[ 'type' ] === self::UNION_SIMPLE ? 'UNION' : 'UNION ALL';
$subRequest = trim((string) $union[ 'request' ], ';');
$output .= sprintf('%s %s ', $type, $subRequest);
Expand Down Expand Up @@ -133,23 +138,23 @@ public function setSchema(Schema $schema): self
/**
* Lit les données d'une table.
*
* @param string $name Nom de la table.
* @param string $tableName Nom de la table.
*
* @return array Données de la table.
*/
public function getTableData(string $name): array
public function getTableData(string $tableName): array
{
return $this->schema->read($name);
return $this->schema->read($tableName);
}

/**
* {@inheritdoc}
*/
public function from(string $table): self
public function from(string $tableName): self
{
parent::from($table);
$this->table = $this->schema->getTableSchema($table);
$this->tableData = $this->getTableData($table);
parent::from($tableName);
$this->table = $this->schema->getTableSchema($tableName);
$this->tableData = $this->getTableData($tableName);

return $this;
}
Expand All @@ -162,7 +167,7 @@ public function from(string $table): self
public function execute(): void
{
$this->filterFrom();
$this->loadAllColumnsSchema();
$this->loadAllFieldsSchema();
$this->filterSelect();
$this->filterWhere();

Expand All @@ -184,11 +189,13 @@ public function execute(): void
* Retourne tous les résultats de la requête.
*
* @return array les données
*
* @phpstan-return TableData
*/
public function fetchAll(): array
{
$this->filterFrom();
$this->loadAllColumnsSchema();
$this->loadAllFieldsSchema();
$this->filterSelect();
$this->filterWhere();
$this->filterUnion();
Expand All @@ -209,7 +216,7 @@ public function fetchAll(): array
$this->executeJoins($value[ 'type' ], $value[ 'table' ], $value[ 'where' ]);
}

$limitHandel = $this->orderBy || $this->union;
$limitHandel = $this->orderBy || $this->unions;
foreach ($this->tableData as $row) {
/* WHERE */
if ($this->where && !$this->where->execute($row)) {
Expand All @@ -233,7 +240,7 @@ public function fetchAll(): array
}

/* UNION */
foreach ($this->union as $union) {
foreach ($this->unions as $union) {
/* Si le retour est demandé en liste. */
$fetchAll = $union[ 'request' ]->fetchAll();

Expand Down Expand Up @@ -269,6 +276,8 @@ public function fetchAll(): array
* Retourne le premier résultat de la requête.
*
* @return array Résultat de la requête.
*
* @phpstan-return RowData
*/
public function fetch(): array
{
Expand All @@ -283,18 +292,18 @@ public function fetch(): array
* Retourne les résultats de la requête sous forme de tableau simple,
* composé uniquement du champ passé en paramètre ou du premier champ sélectionné.
*
* @param string $name Nom du champ.
* @param string|null $key Clé des valeurs de la liste
* @param string $columnName Nom du champ.
* @param string|null $key Clé des valeurs de la liste
*
* @throws ColumnsNotFoundException
*
* @return array Liste du champ passé en paramètre.
* @return array<null|scalar> Liste du champ passé en paramètre.
*/
public function lists(string $name, ?string $key = null): array
public function lists(string $columnName, ?string $key = null): array
{
$data = $this->fetchAll();

return array_column($data, $name, $key);
return array_column($data, $columnName, $key);
}

/**
Expand All @@ -303,10 +312,10 @@ public function lists(string $name, ?string $key = null): array
public function init(): self
{
parent::init();
$this->allColumnsSchema = [];
$this->execute = null;
$this->tableData = [];
$this->where = null;
$this->allFieldsSchema = [];
$this->execute = null;
$this->tableData = [];
$this->where = null;

return $this;
}
Expand Down Expand Up @@ -337,21 +346,21 @@ protected static function arrayUniqueMultidimensional(array &$input): void
* Execute les jointures.
*
* @param string $type
* @param string $table
* @param string $tableName
* @param Where $where
*
* @return void
*/
protected function executeJoins(string $type, string $table, Where $where): void
protected function executeJoins(string $type, string $tableName, Where $where): void
{
$result = [];
$rowTableNull = $this->getRowTableNull($table);
$rowTableNull = $this->getRowTableNull($tableName);
$left = $type === self::JOIN_LEFT;
$tableData = $left
? $this->tableData
: $this->getTableData($table);
: $this->getTableData($tableName);
$tableJoin = $left
? $this->getTableData($table)
? $this->getTableData($tableName)
: $this->tableData;

foreach ($tableData as $row) {
Expand Down Expand Up @@ -505,7 +514,7 @@ protected function executeUpdate(): void
if ($this->where && !$this->where->execute($row)) {
continue;
}
$row = array_merge($row, $this->values);
$row = array_merge($row, $this->values[0]);
}
unset($row);
}
Expand All @@ -529,13 +538,13 @@ protected function executeDelete(): void
/**
* Charge les colonnes de la table courante et des tables de jointure.
*/
private function loadAllColumnsSchema(): void
private function loadAllFieldsSchema(): void
{
$this->allColumnsSchema = $this->table->getFields();
$this->allFieldsSchema = $this->table->getFields();

foreach ($this->joins as $value) {
$this->allColumnsSchema = array_merge(
$this->allColumnsSchema,
$this->allFieldsSchema = array_merge(
$this->allFieldsSchema,
$this->schema->getTableSchema($value[ 'table' ])->getFields()
);
}
Expand Down Expand Up @@ -631,32 +640,34 @@ private function filterOrderBy(): void
private function filterUnion(): void
{
$count = count($this->columns);
foreach ($this->union as $request) {
if ($count != count($request[ 'request' ]->columns)) {
throw new ColumnsNotFoundException(
sprintf(
'The number of fields in the selections are different: %s != %s',
implode(', ', $this->columns),
implode(', ', $request[ 'request' ]->columns)
)
);
foreach ($this->unions as $union) {
if ($count === count($union[ 'request' ]->getColumns())) {
continue;
}

throw new ColumnsNotFoundException(
sprintf(
'The number of fields in the selections are different: %s != %s',
implode(', ', $this->columns),
implode(', ', $union[ 'request' ]->getColumns())
)
);
}
}

/**
* Déclenche une exception si l'un des champs passés en paramètre diffère
* des champs disponibles dans les tables.
*
* @param array $columns Liste des champs.
* @param string[] $columns Liste des champs.
*
* @throws ColumnsNotFoundException
*/
private function diffColumns(array $columns): void
{
$diff = array_diff_key(
array_flip($columns),
$this->allColumnsSchema
$this->allFieldsSchema
);

if ($diff) {
Expand All @@ -677,12 +688,12 @@ private function diffColumns(array $columns): void
* Si des champs existent dans le schéma ils seront rajouté. Fonction utilisée
* pour les jointures en cas d'absence de résultat.
*
* @param string $table Nom de la table.
* @param string $tableName Nom de la table.
*/
private function getRowTableNull(string $table): array
private function getRowTableNull(string $tableName): array
{
/* Prend les noms des champs de la table à joindre. */
$rowTableKey = $this->schema->getTableSchema($table)->getFieldsName();
$rowTableKey = $this->schema->getTableSchema($tableName)->getFieldsName();
/* Prend les noms des champs dans la requête précédente. */
if ($this->table->getFields() !== []) {
$rowTableAllKey = $this->table->getFieldsName();
Expand Down

0 comments on commit 4229b2c

Please sign in to comment.