Skip to content

Commit

Permalink
refactor: rename column names with more precision
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Dec 8, 2021
1 parent 1fdf5a4 commit c1d1952
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 197 deletions.
48 changes: 24 additions & 24 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __toString(): string
if ($this->execute) {
$output .= strtoupper($this->execute) . ' ';
} else {
$output .= sprintf('SELECT %s ', $this->columns ? addslashes(implode(', ', $this->columns)) : '*');
$output .= sprintf('SELECT %s ', $this->columnNames ? addslashes(implode(', ', $this->columnNames)) : '*');
}
if ($this->from) {
$output .= sprintf('FROM %s ', addslashes($this->from));
Expand Down Expand Up @@ -206,7 +206,7 @@ public function fetchAll(): array
/* Le pointeur en cas de limite de résultat. */
$i = 0;

$column = array_flip($this->columns);
$columnNameKeys = array_flip($this->columnNames);

/*
* Exécution des jointures.
Expand Down Expand Up @@ -234,8 +234,8 @@ public function fetchAll(): array
}

/* SELECT */
$return[] = $this->columns
? array_intersect_key($row, $column)
$return[] = $this->columnNames
? array_intersect_key($row, $columnNameKeys)
: $row;
}

Expand Down Expand Up @@ -444,27 +444,27 @@ protected function executeInsert(): void
$increment = $this->table->getIncrement();
/* Je charge les colonnes de mon schéma. */
$fields = $this->table->getFields();
$count = count($this->columns);
$count = count($this->columnNames);

foreach ($this->values as $values) {
/* Pour chaque ligne je vérifie si le nombre de colonne correspond au nombre valeur insérée. */
if ($count !== count($values)) {
throw new ColumnsNotFoundException(
sprintf(
'The number of fields in the selections are different: %s != %s',
implode(', ', $this->columns),
implode(', ', $this->columnNames),
implode(', ', $values)
)
);
}

/* Je prépare l'association clé=>valeur pour chaque ligne à insérer. */
$row = array_combine($this->columns, $values);
$row = array_combine($this->columnNames, $values);
if ($row == false) {
throw new ColumnsNotFoundException(
sprintf(
'The number of fields in the selections are different: %s != %s',
implode(', ', $this->columns),
implode(', ', $this->columnNames),
implode(', ', $values)
)
);
Expand Down Expand Up @@ -567,8 +567,8 @@ private function filterFrom(): void
*/
private function filterSelect(): void
{
if ($this->columns) {
$this->diffColumns($this->columns);
if ($this->columnNames) {
$this->diffColumnNames($this->columnNames);
}
}

Expand All @@ -593,19 +593,19 @@ private function filterLimit(): void
*/
private function filterWhere(): void
{
$columns = [];
$columnNames = [];
/* Merge toutes les colonnes des conditions de chaque jointure. */
foreach ($this->joins as $value) {
$columns = array_merge($columns, $value[ 'where' ]->getColumns());
$columnNames = array_merge($columnNames, $value[ 'where' ]->getColumnNames());
}

/* Merge les colonnes des conditions de la requête courante. */
if ($this->where) {
$columns = array_merge($columns, $this->where->getColumns());
$columnNames = array_merge($columnNames, $this->where->getColumnNames());
}

if ($columns) {
$this->diffColumns($columns);
if ($columnNames) {
$this->diffColumnNames($columnNames);
}
}

Expand All @@ -620,8 +620,8 @@ private function filterOrderBy(): void
return;
}

$columns = array_keys($this->orderBy);
$this->diffColumns($columns);
$columnNames = array_keys($this->orderBy);
$this->diffColumnNames($columnNames);

foreach ($this->orderBy as $field => $order) {
if ($order !== SORT_ASC && $order !== SORT_DESC) {
Expand All @@ -639,17 +639,17 @@ private function filterOrderBy(): void
*/
private function filterUnion(): void
{
$count = count($this->columns);
$count = count($this->columnNames);
foreach ($this->unions as $union) {
if ($count === count($union[ 'request' ]->getColumns())) {
if ($count === count($union[ 'request' ]->getColumnNames())) {
continue;
}

throw new ColumnsNotFoundException(
sprintf(
'The number of fields in the selections are different: %s != %s',
implode(', ', $this->columns),
implode(', ', $union[ 'request' ]->getColumns())
implode(', ', $this->columnNames),
implode(', ', $union[ 'request' ]->getColumnNames())
)
);
}
Expand All @@ -659,14 +659,14 @@ private function filterUnion(): void
* Déclenche une exception si l'un des champs passés en paramètre diffère
* des champs disponibles dans les tables.
*
* @param string[] $columns Liste des champs.
* @param string[] $columnNames Liste des nom des champs.
*
* @throws ColumnsNotFoundException
*/
private function diffColumns(array $columns): void
private function diffColumnNames(array $columnNames): void
{
$diff = array_diff_key(
array_flip($columns),
array_flip($columnNames),
$this->allFieldsSchema
);

Expand Down
106 changes: 53 additions & 53 deletions src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
*
* @author Mathieu NOËL <mathieu@soosyze.com>
*
* @method Request where(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notWhere(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orWhere(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotWhere(string $column, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request where(string $columnName, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notWhere(string $columnName, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orWhere(string $columnName, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotWhere(string $columnName, string $operator, null|scalar $value) Alias de la fonction de l'objet Queryflatfile\Where
*
* @method Request between(string $column, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orBetween(string $column, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notBetween(string $column, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotBetween(string $column, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request between(string $columnName, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orBetween(string $columnName, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notBetween(string $columnName, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotBetween(string $columnName, numeric|string $min, numeric|string $max) Alias de la fonction de l'objet Queryflatfile\Where
*
* @method Request in(string $column, array $values) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orIn(string $column, array $values) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notIn(string $column, array $values) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotIn(string $column, array $values) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request in(string $columnName, array $values) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orIn(string $columnName, array $values) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notIn(string $columnName, array $values) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotIn(string $columnName, array $values) Alias de la fonction de l'objet Queryflatfile\Where
*
* @method Request isNull(string $column) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orIsNull(string $column) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request isNotNull(string $column) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orIsNotNull(string $column) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request isNull(string $columnName) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orIsNull(string $columnName) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request isNotNull(string $columnName) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orIsNotNull(string $columnName) Alias de la fonction de l'objet Queryflatfile\Where
*
* @method Request regex(string $column, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orRegex(string $column, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notRegex(string $column, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotRegex(string $column, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request regex(string $columnName, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orRegex(string $columnName, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notRegex(string $columnName, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request orNotRegex(string $columnName, string $pattern) Alias de la fonction de l'objet Queryflatfile\Where
*
* @method Request whereGroup(\Closure $callable) Alias de la fonction de l'objet Queryflatfile\Where
* @method Request notWhereGroup(\Closure $callable) Alias de la fonction de l'objet Queryflatfile\Where
Expand Down Expand Up @@ -135,7 +135,7 @@ abstract class RequestHandler implements RequestInterface
*
* @var string[]
*/
protected $columns = [];
protected $columnNames = [];

/**
* Les valeurs à insérer ou mettre à jour.
Expand Down Expand Up @@ -202,18 +202,18 @@ public function from(string $tableName)
/**
* {@inheritdoc}
*/
public function getColumns(): array
public function getColumnNames(): array
{
return $this->columns;
return $this->columnNames;
}

/**
* {@inheritdoc}
*/
public function insertInto(string $tableName, array $columns)
public function insertInto(string $tableName, array $columnNames)
{
$this->execute = self::INSERT;
$this->from($tableName)->select(...$columns);
$this->from($tableName)->select(...$columnNames);

return $this;
}
Expand Down Expand Up @@ -248,9 +248,9 @@ public function limit(int $limit, int $offset = 0)
/**
* {@inheritdoc}
*/
public function orderBy(string $column, int $order = SORT_ASC)
public function orderBy(string $columnName, int $order = SORT_ASC)
{
$this->orderBy[ $column ] = $order;
$this->orderBy[ $columnName ] = $order;

return $this;
}
Expand All @@ -273,10 +273,10 @@ public function rightJoin(string $tableName, $column, string $operator = '', str
/**
* {@inheritdoc}
*/
public function select(string ...$columns)
public function select(string ...$columnNames)
{
foreach ($columns as $column) {
$this->columns[] = $column;
foreach ($columnNames as $columnName) {
$this->columnNames[] = $columnName;
}

return $this;
Expand Down Expand Up @@ -305,21 +305,21 @@ public function unionAll(RequestInterface $request)
/**
* {@inheritdoc}
*/
public function update(string $tableName, array $columns)
public function update(string $tableName, array $row)
{
$this->execute = self::UPDATE;
$this->from($tableName)->select(...array_keys($columns));
$this->values[ 0 ] = $columns;
$this->execute = self::UPDATE;
$this->from($tableName)->select(...array_keys($row));
$this->values[ 0 ] = $row;

return $this;
}

/**
* {@inheritdoc}
*/
public function values(array $columns)
public function values(array $rowValues)
{
$this->values[] = $columns;
$this->values[] = $rowValues;

return $this;
}
Expand All @@ -331,33 +331,33 @@ public function values(array $columns)
*/
protected function init()
{
$this->columns = [];
$this->execute = null;
$this->from = '';
$this->joins = [];
$this->limit = self::ALL;
$this->offset = 0;
$this->orderBy = [];
$this->sumLimit = 0;
$this->unions = [];
$this->values = [];
$this->columnNames = [];
$this->execute = null;
$this->from = '';
$this->joins = [];
$this->limit = self::ALL;
$this->offset = 0;
$this->orderBy = [];
$this->sumLimit = 0;
$this->unions = [];
$this->values = [];

return $this;
}

/**
* Enregistre une jointure.
*
* @param string $type Type de la jointure.
* @param string $tableName Nom de la table à joindre
* @param string $column Nom de la colonne d'une des tables précédentes.
* @param string $operator Opérateur logique ou null pour une closure.
* @param string $value Valeur ou une colonne de la table jointe (au format nom_table.colonne)
* @param string $type Type de la jointure.
* @param string $tableName Nom de la table à joindre
* @param string $columnName Nom de la colonne d'une des tables précédentes.
* @param string $operator Opérateur logique ou null pour une closure.
* @param string $value Valeur ou une colonne de la table jointe (au format nom_table.colonne)
*/
private function join(string $type, string $tableName, string $column, string $operator, string $value): void
private function join(string $type, string $tableName, string $columnName, string $operator, string $value): void
{
$where = new Where();
$where->where($column, $operator, $value);
$where->where($columnName, $operator, $value);

$this->joins[] = [ 'type' => $type, 'table' => $tableName, 'where' => $where ];
}
Expand Down

0 comments on commit c1d1952

Please sign in to comment.