Skip to content

Commit

Permalink
feat: add the return of queries executed in QFFQL
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Dec 11, 2021
1 parent 483c8ad commit 6c35f6d
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 69 deletions.
81 changes: 71 additions & 10 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
class Request extends RequestHandler
{
use ValueToString;

/**
* Tous les champs utilisés.
*
Expand Down Expand Up @@ -75,12 +77,17 @@ public function __construct(Schema $schema)
*/
public function __toString(): string
{
$output = '';
if ($this->execute) {
$output .= strtoupper($this->execute) . ' ';
} else {
$output .= sprintf('SELECT %s ', $this->columnNames ? addslashes(implode(', ', $this->columnNames)) : '*');
if ($this->execute === self::INSERT) {
return $this->insertIntoToString();
}
if ($this->execute === self::UPDATE) {
return $this->updateToString();
}
if ($this->execute === self::DELETE) {
return $this->deleteToString();
}

$output = sprintf('SELECT %s ', $this->columnNames ? addslashes(implode(', ', $this->columnNames)) : '*');
if ($this->from !== '') {
$output .= sprintf('FROM %s ', addslashes($this->from));
}
Expand All @@ -92,13 +99,10 @@ public function __toString(): string
(string) $value[ 'where' ]
);
}
if ($this->where !== null) {
$output .= sprintf('WHERE %s ', (string) $this->where);
}
$output .= $this->whereToString();
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);
$output .= sprintf('%s %s ', $type, trim((string) $union[ 'request' ], ';'));
}
if ($this->orderBy) {
$output .= 'ORDER BY ';
Expand Down Expand Up @@ -535,6 +539,63 @@ protected function executeDelete(): void
$this->tableData = array_values($this->tableData);
}

private function insertIntoToString(): string
{
$output = sprintf('INSERT INTO %s ', addslashes($this->from));

if ($this->columnNames) {
$output .= sprintf('(%s) VALUES%s', addslashes(implode(', ', $this->columnNames)), PHP_EOL);
}
$data = array_map(
function ($values) {
$data = array_map(
function ($item) {
return self::getValueToString($item);
},
$values
);

return sprintf('(%s)', implode(', ', $data));
},
$this->values
);
$output .= implode(',' . PHP_EOL, $data);

return trim($output) . ';';
}

private function deleteToString(): string
{
$output = sprintf('DELETE %s ', addslashes($this->from));
$output .= $this->whereToString();

return trim($output) . ';';
}

private function updateToString(): string
{
$output = sprintf('UPDATE %s SET ', addslashes($this->from));
$data = [];
foreach ($this->values[ 0 ] as $key => $value) {
$data[] = sprintf(
'%s = %s',
addslashes($key),
self::getValueToString($value)
);
}
$output .= implode(', ', $data) . ' ';
$output .= $this->whereToString();

return trim($output) . ';';
}

private function whereToString(): string
{
return $this->where === null
? ''
: sprintf('WHERE %s ', (string) $this->where);
}

/**
* Charge les colonnes de la table courante et des tables de jointure.
*/
Expand Down
45 changes: 45 additions & 0 deletions src/ValueToString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* Queryflatfile
*
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Queryflatfile;

trait ValueToString
{
/**
* @param array|null|scalar|Where $value
*
* @return string
*/
protected static function getValueToString($value): string
{
if (is_int($value)) {
return (string) $value;
}
if (is_string($value)) {
return sprintf('\'%s\'', addslashes($value));
}
if ($value instanceof Where) {
return (string) $value;
}
if (is_array($value)) {
return implode(
', ',
array_map(
function ($item): string {
return self::getValueToString($item);
},
$value
)
);
}

return 'null';
}
}
49 changes: 15 additions & 34 deletions src/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class Where extends WhereHandler
{
use ValueToString;

/**
* Retourne les paramètre des clauses en format pseudo SQL.
*
Expand Down Expand Up @@ -66,11 +68,13 @@ public function __toString(): string

break;
case 'between':
/** @var Between $value */
$value = $where['value'];
$output .= sprintf(
'%s %sBETWEEN %s ',
addslashes($whereColumn),
$not,
self::getValueToString($where['value'])
self::getBetweenToString($value)
);

break;
Expand Down Expand Up @@ -255,42 +259,19 @@ protected static function getColumn(string $value): string
: $value;
}

/**
* @param array|null|scalar|Where $value
/*
* @param array $between
*
* @phpstan-param Between $between
*
* @return string
*/
protected static function getValueToString($value): string
protected static function getBetweenToString(array $between): string
{
if (is_int($value)) {
return (string) $value;
}
if (is_string($value)) {
return sprintf('\'%s\'', addslashes($value));
}
if ($value instanceof Where) {
return (string) $value;
}
if (is_array($value)) {
if (isset($value[ 'min' ], $value[ 'max' ]) && is_scalar($value[ 'min' ]) && is_scalar($value[ 'max' ])) {
return sprintf(
'%s AND %s',
self::getValueToString($value[ 'min' ]),
self::getValueToString($value[ 'max' ])
);
}

return implode(
', ',
array_map(
function ($item): string {
return self::getValueToString($item);
},
$value
)
);
}

return 'null';
return sprintf(
'%s AND %s',
self::getValueToString($between[ 'min' ]),
self::getValueToString($between[ 'max' ])
);
}
}
111 changes: 86 additions & 25 deletions tests/unit/RequestExecuteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,81 @@ public function testCreateTableIfNotExists(): void

public function testInsertInto(): void
{
$this->request->insertInto('user', [ 'id', 'name', 'firstname' ])
$request1 = $this->request
->insertInto('user', [ 'id', 'name', 'firstname' ])
->values([ 0, 'NOEL', 'Mathieu' ])
->values([ 1, 'DUPOND', 'Jean' ])
->execute();
->values([ 1, 'DUPOND', 'Jean' ]);

self::assertEquals(
'INSERT INTO user (id, name, firstname) VALUES' . PHP_EOL .
'(0, \'NOEL\', \'Mathieu\'),' . PHP_EOL .
'(1, \'DUPOND\', \'Jean\');',
(string) $request1
);
$request1->execute();

$this->request->insertInto('user', [ 'name', 'firstname' ])
$request2 = $this->request
->insertInto('user', [ 'name', 'firstname' ])
->values([ 'MARTIN', 'Manon' ])
->values([ null, 'Marie' ])
->values([ 'DUPOND', 'Pierre' ])
->execute();
->values([ 'DUPOND', 'Pierre' ]);

$this->request->insertInto('user', [ 'id', 'name', 'firstname' ])
self::assertEquals(
'INSERT INTO user (name, firstname) VALUES' . PHP_EOL .
'(\'MARTIN\', \'Manon\'),' . PHP_EOL .
'(null, \'Marie\'),' . PHP_EOL .
'(\'DUPOND\', \'Pierre\');',
(string) $request2
);
$request2->execute();

$request3 = $this->request
->insertInto('user', [ 'id', 'name', 'firstname' ])
->values([ 5, 'MEYER', 'Eva' ])
->values([ 6, 'ROBERT', null ])
->execute();
->values([ 6, 'ROBERT', null ]);

self::assertEquals(
'INSERT INTO user (id, name, firstname) VALUES' . PHP_EOL .
'(5, \'MEYER\', \'Eva\'),' . PHP_EOL .
'(6, \'ROBERT\', null);',
(string) $request3
);
$request3->execute();

$this->request->insertInto('role', [ 'id_role', 'labelle' ])
$request4 = $this->request
->insertInto('role', [ 'id_role', 'labelle' ])
->values([ 0, 'Admin' ])
->values([ 1, 'Author' ])
->values([ 2, 'User' ])
->execute();
->values([ 2, 'User' ]);

self::assertEquals(
'INSERT INTO role (id_role, labelle) VALUES' . PHP_EOL .
'(0, \'Admin\'),' . PHP_EOL .
'(1, \'Author\'),' . PHP_EOL .
'(2, \'User\');',
(string) $request4
);
$request4->execute();

$this->request->insertInto('user_role', [ 'id_user', 'id_role' ])
$request5 = $this->request->insertInto('user_role', [ 'id_user', 'id_role' ])
->values([ 0, 0 ])
->values([ 1, 0 ])
->values([ 2, 1 ])
->values([ 3, 1 ])
->values([ 4, 2 ])
->values([ 5, 2 ])
->execute();
->values([ 5, 2 ]);

self::assertEquals(
'INSERT INTO user_role (id_user, id_role) VALUES' . PHP_EOL .
'(0, 0),' . PHP_EOL .
'(1, 0),' . PHP_EOL .
'(2, 1),' . PHP_EOL .
'(3, 1),' . PHP_EOL .
'(4, 2),' . PHP_EOL .
'(5, 2);',
(string) $request5
);
$request5->execute();

self::assertFileExists(self::ROOT . 'user.' . $this->bdd->getExtension());
}
Expand Down Expand Up @@ -176,10 +221,15 @@ public function testInsertIntoExceptionValue(): void

public function testUpdateData(): void
{
$this->request
->update('user', [ 'name' => 'PETIT' ])
->where('id', '=', 0)
->execute();
$request = $this->request
->update('user', [ 'name' => 'PETIT', 'firstname' => 'Mathieu' ])
->where('id', '=', 0);

self::assertEquals(
'UPDATE user SET name = \'PETIT\', firstname = \'Mathieu\' WHERE id = 0;',
(string) $request
);
$request->execute();

$data = $this->request
->from('user')
Expand All @@ -191,9 +241,14 @@ public function testUpdateData(): void

public function testUpdateDataFull(): void
{
$this->request
->update('user', [ 'name' => 'PETIT' ])
->execute();
$request = $this->request
->update('user', [ 'name' => 'PETIT' ]);

self::assertEquals(
'UPDATE user SET name = \'PETIT\';',
(string) $request
);
$request->execute();

$data = $this->request
->from('user')
Expand All @@ -205,10 +260,16 @@ public function testUpdateDataFull(): void

public function testDeleteData(): void
{
$this->request->from('user')
$request = $this->request
->from('user')
->delete()
->between('id', 1, 4)
->execute();
->between('id', 1, 4);

self::assertEquals(
'DELETE user WHERE id BETWEEN 1 AND 4;',
(string) $request
);
$request->execute();

$data = $this->request
->from('user')
Expand Down

0 comments on commit 6c35f6d

Please sign in to comment.