Skip to content

Commit

Permalink
Merge branch 'bugfix/mariadb-tests' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
StulE-ru committed Aug 30, 2023
2 parents f84949b + 68b6ef1 commit 056c0c0
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 85 deletions.
20 changes: 20 additions & 0 deletions src/Drivers/Escapers/MySQLEscaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class MySQLEscaper extends Escaper
*/
public function table(string $name): string
{
$pieces = explode('.', $name);

if (\count($pieces) > 1) {
foreach ($pieces as $key => $piece) {
$pieces[$key] = self::table($piece);
}

return implode('.', $pieces);
}

return self::QUOTE . parent::table($name) . self::QUOTE;
}

Expand All @@ -27,6 +37,16 @@ public function table(string $name): string
*/
public function column(string $name): string
{
$pieces = explode('.', $name);

if (\count($pieces) > 1) {
foreach ($pieces as $key => $piece) {
$pieces[$key] = self::table($piece);
}

return implode('.', $pieces);
}

return self::QUOTE . parent::column($name) . self::QUOTE;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function columnCount(): int
public function fetchColumn(int $column = 0): mixed
{
try {
/** @var mixed */
/** @var mixed Comment for PSalm and CSFixer */
$result = $this->statement?->fetchColumn($column) ?? false;
} catch (\Throwable $th) {
$result = false;
Expand Down
19 changes: 2 additions & 17 deletions src/Traits/JoinsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function joinsQuery(Escaper $escaper): string
return '';
}

$query = $this->joinsType . ' ?';
$query = $this->joinsType . ' ' . $escaper->table($this->joinsTable);

if (!empty($this->on)) {
$query .= ' ' . $this->onQuery($escaper);
Expand All @@ -50,9 +50,7 @@ protected function joinsPlaceholders(): array
return [];
}

return array_merge([
$this->joinsTable,
], $this->onPlaceholders());
return $this->onPlaceholders();
}

/**
Expand Down Expand Up @@ -97,17 +95,4 @@ public function rightJoin(string $table): object

return $this;
}

/**
* Set full outer join.
*
* @return $this
*/
public function fullJoin(string $table): object
{
$this->joinsTable = $table;
$this->joinsType = 'FULL OUTER JOIN';

return $this;
}
}
28 changes: 16 additions & 12 deletions src/Traits/OnTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
trait OnTrait
{
/**
* @var list<array{type: string, name: string, sign: string, value: scalar|null|ScalarFunction|Condition}> on conditions
* @var list<array{type: string, name: string, sign: string, value: scalar|ScalarFunction|Condition|null}> on conditions
*/
protected array $on = [];

Expand All @@ -41,7 +41,7 @@ protected function onQuery(Escaper $escaper): string
$value = '?';
}*/

$query .= $type . ' ' . $escaper->column($name) . $sign . $escaper->column(strval($value)) . ' ';
$query .= $type . ' ' . $escaper->column($name) . $sign . $escaper->column((string) $value) . ' ';
}

return trim($query);
Expand Down Expand Up @@ -74,10 +74,10 @@ protected function onPlaceholders(): array
/**
* Adds condition to list.
*
* @param string $type
* @param string $name
* @param string $sign
* @param scalar|null|ScalarFunction|Condition $value
* @param string $type condition type
* @param string $name column name
* @param string $sign condition sign
* @param scalar|ScalarFunction|Condition|null $value condition value
*/
protected function addOnCondition(string $type, string $name, string $sign, string|int|float|bool|null|ScalarFunction|Condition $value): void
{
Expand All @@ -92,9 +92,9 @@ protected function addOnCondition(string $type, string $name, string $sign, stri
/**
* Set first condition.
*
* @param string $name
* @param scalar|null|ScalarFunction|Condition $value
* @param string $sign
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* !!! ATTENTION !!!
* Method think that $value is name of table like 'join_table.id'
Expand All @@ -119,9 +119,9 @@ public function on(string $name, string|int|float|bool|null|ScalarFunction|Condi
/**
* Adds 'and' condition.
*
* @param string $name
* @param scalar|null|ScalarFunction|Condition $value
* @param string $sign
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* !!! ATTENTION !!!
* Method think that $value is name of table like 'join_table.id'
Expand Down Expand Up @@ -150,6 +150,10 @@ public function onAnd(string $name, string|int|float|bool|null|ScalarFunction|Co
/**
* Adds 'or' condition.
*
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* !!! ATTENTION !!!
* Method think that $value is name of table like 'join_table.id'
* and do not prepare it. So be careful with sql-injections.
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/SetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
trait SetTrait
{
/**
* @var array<string, scalar|null|ScalarFunction> values for SET
* @var array<string, scalar|ScalarFunction|null> values for SET
*/
protected array $set = [];

Expand Down Expand Up @@ -58,7 +58,7 @@ protected function setPlaceholders(): array
/**
* Set values for SET.
*
* @param array<string, scalar|null|ScalarFunction> $values array of values for SET
* @param array<string, scalar|ScalarFunction|null> $values array of values for SET
*
* @return $this
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/ValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
trait ValuesTrait
{
/**
* @var list<list<scalar|null|ScalarFunction>> values for VALUES
* @var list<list<scalar|ScalarFunction|null>> values for VALUES
*/
protected array $values = [];

Expand Down Expand Up @@ -63,7 +63,7 @@ protected function valuesPlaceholders(): array
/**
* Set values for VALUES.
*
* @param list<list<scalar|null|ScalarFunction>> $values values for VALUES
* @param list<list<scalar|ScalarFunction|null>> $values values for VALUES
*
* @return $this
*/
Expand Down
28 changes: 14 additions & 14 deletions src/Traits/WhereTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
trait WhereTrait
{
/**
* @var list<array{type: string, name: string, sign: string, value: scalar|null|ScalarFunction|Condition}> where conditions
* @var list<array{type: string, name: string, sign: string, value: scalar|ScalarFunction|Condition|null}> where conditions
*/
protected array $where = [];

Expand Down Expand Up @@ -71,10 +71,10 @@ protected function wherePlaceholders(): array
/**
* Adds condition to list.
*
* @param string $type
* @param string $name
* @param string $sign
* @param scalar|null|ScalarFunction|Condition $value
* @param string $type condition type
* @param string $name column name
* @param string $sign condition sign
* @param scalar|ScalarFunction|Condition|null $value condition value
*/
protected function addCondition(string $type, string $name, string $sign, string|int|float|bool|null|ScalarFunction|Condition $value): void
{
Expand All @@ -89,9 +89,9 @@ protected function addCondition(string $type, string $name, string $sign, string
/**
* Set first condition.
*
* @param string $name
* @param scalar|null|ScalarFunction|Condition $value
* @param string $sign
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* $value can be subclass of Condition (In, Beetween, etc.)
* If $value is subclass of Condition $sign will be ignored.
Expand All @@ -112,9 +112,9 @@ public function where(string $name, string|int|float|bool|null|ScalarFunction|Co
/**
* Adds 'and' condition.
*
* @param string $name
* @param scalar|null|ScalarFunction|Condition $value
* @param string $sign
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* $value can be subclass of Condition (In, Beetween, etc.)
* If $value is subclass of Condition $sign will be ignored.
Expand All @@ -139,9 +139,9 @@ public function and(string $name, string|int|float|bool|null|ScalarFunction|Cond
/**
* Adds 'or' condition.
*
* @param string $name
* @param scalar|null|ScalarFunction|Condition $value
* @param string $sign
* @param string $name column name
* @param scalar|ScalarFunction|Condition|null $value condition value
* @param string $sign condition sign
*
* $value can be subclass of Condition (In, Beetween, etc.)
* If $value is subclass of Condition $sign will be ignored.
Expand Down
33 changes: 8 additions & 25 deletions tests/Integration/CRUDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public function testSelect(): void
assertEquals(['len' => 5], $this->mpdo->select(self::TABLE)->columns(['len' => new Lenght('name')])->where('id', 1)->row());

// test conditions
assertEquals([['id' => 1, 'id' => 3]], $this->mpdo->select(self::TABLE)->columns(['id'])->where('id', new In([1, 3]))->rows());
assertEquals([['id' => 1, 'id' => 2, 'id' => 3]], $this->mpdo->select(self::TABLE)->columns(['id'])->where('id', new Between(1, 3))->rows());
assertEquals([['id' => 1], ['id' => 3]], $this->mpdo->select(self::TABLE)->columns(['id'])->where('id', new In([1, 3]))->rows());
assertEquals([['id' => 1], ['id' => 2], ['id' => 3]], $this->mpdo->select(self::TABLE)->columns(['id'])->where('id', new Between(1, 3))->rows());

// test get cell
assertEquals(9, $this->mpdo->select(self::TABLE)->columns([new Count()])->cell());
Expand Down Expand Up @@ -173,7 +173,6 @@ public function testJoins(): void
assertEquals(2, $this->mpdo->select(self::TABLE)->columns([new Count()])->innerJoin($table)->on(self::TABLE . '.id', $table . '.id')->cell());
assertEquals(4, $this->mpdo->select(self::TABLE)->columns([new Count()])->leftJoin($table)->on(self::TABLE . '.id', $table . '.id')->cell());
assertEquals(4, $this->mpdo->select(self::TABLE)->columns([new Count()])->rightJoin($table)->on(self::TABLE . '.id', $table . '.id')->cell());
assertEquals(6, $this->mpdo->select(self::TABLE)->columns([new Count()])->fullJoin($table)->on(self::TABLE . '.id', $table . '.id')->cell());

assertEquals(
[
Expand All @@ -182,8 +181,8 @@ public function testJoins(): void
],
$this->mpdo->select(self::TABLE)
->columns([
self::TABLE . '.id' => 'id',
self::TABLE . '.name' => 'name',
'id' => self::TABLE . '.id',
'name' => self::TABLE . '.name',
])->innerJoin($table)->on(self::TABLE . '.id', $table . '.id')->rows()
);

Expand All @@ -196,8 +195,8 @@ public function testJoins(): void
],
$this->mpdo->select(self::TABLE)
->columns([
self::TABLE . '.id' => 'id',
self::TABLE . '.name' => 'name',
'id' => self::TABLE . '.id',
'name' => self::TABLE . '.name',
])->leftJoin($table)->on(self::TABLE . '.id', $table . '.id')->rows()
);

Expand All @@ -210,26 +209,10 @@ public function testJoins(): void
],
$this->mpdo->select(self::TABLE)
->columns([
self::TABLE . '.id' => 'id',
self::TABLE . '.name' => 'name',
'id' => self::TABLE . '.id',
'name' => self::TABLE . '.name',
])->rightJoin($table)->on(self::TABLE . '.id', $table . '.id')->rows()
);

assertEquals(
[
['id' => 1, 'name' => 'l1'],
['id' => 2, 'name' => 'l2'],
['id' => 3, 'name' => 'l3'],
['id' => 4, 'name' => 'l4'],
['id' => null, 'name' => null],
['id' => null, 'name' => null],
],
$this->mpdo->select(self::TABLE)
->columns([
self::TABLE . '.id' => 'id',
self::TABLE . '.name' => 'name',
])->fullJoin($table)->on(self::TABLE . '.id', $table . '.id')->rows()
);
}

public function testUpdate(): void
Expand Down
21 changes: 9 additions & 12 deletions tests/Unit/Actions/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,40 +183,37 @@ public function testConditions(): void

public function testJoins(): void
{
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN ?', ['join_table'])
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN join_table', [])
->innerJoin('join_table')->rows();

$this->make('SELECT * FROM ' . self::TABLE . ' LEFT OUTER JOIN ?', ['join_table'])
$this->make('SELECT * FROM ' . self::TABLE . ' LEFT OUTER JOIN join_table', [])
->leftJoin('join_table')->rows();

$this->make('SELECT * FROM ' . self::TABLE . ' RIGHT OUTER JOIN ?', ['join_table'])
$this->make('SELECT * FROM ' . self::TABLE . ' RIGHT OUTER JOIN join_table', [])
->rightJoin('join_table')->rows();

$this->make('SELECT * FROM ' . self::TABLE . ' FULL OUTER JOIN ?', ['join_table'])
->fullJoin('join_table')->rows();
}

public function testJoinsOn(): void
{
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN ? ON ' . self::TABLE . '.id=join_table.id', ['join_table'])
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN join_table ON ' . self::TABLE . '.id=join_table.id', [])
->innerJoin('join_table')->on(self::TABLE . '.id', 'join_table.id')->rows();

$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN ? ON ' . self::TABLE . '.id=join_table.id AND ' . self::TABLE . '.id=join_table.id', ['join_table'])
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN join_table ON ' . self::TABLE . '.id=join_table.id AND ' . self::TABLE . '.id=join_table.id', [])
->innerJoin('join_table')->on(self::TABLE . '.id', 'join_table.id')->onAnd(self::TABLE . '.id', 'join_table.id')->rows();

$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN ? ON ' . self::TABLE . '.id=join_table.id OR ' . self::TABLE . '.id=join_table.id', ['join_table'])
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN join_table ON ' . self::TABLE . '.id=join_table.id OR ' . self::TABLE . '.id=join_table.id', [])
->innerJoin('join_table')->on(self::TABLE . '.id', 'join_table.id')->onOr(self::TABLE . '.id', 'join_table.id')->rows();
}

public function testJoinsOnWhere(): void
{
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN ? ON ' . self::TABLE . '.id=join_table.id WHERE id=?', ['join_table', 1])
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN join_table ON ' . self::TABLE . '.id=join_table.id WHERE id=?', [1])
->innerJoin('join_table')->on(self::TABLE . '.id', 'join_table.id')->where('id', 1)->rows();

$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN ? ON ' . self::TABLE . '.id=join_table.id WHERE id=? AND name=?', ['join_table', 1, 'name'])
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN join_table ON ' . self::TABLE . '.id=join_table.id WHERE id=? AND name=?', [1, 'name'])
->innerJoin('join_table')->on(self::TABLE . '.id', 'join_table.id')->where('id', 1)->and('name', 'name')->rows();

$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN ? ON ' . self::TABLE . '.id=join_table.id AND ' . self::TABLE . '.name=join_table.name WHERE id=? AND name=?', ['join_table', 1, 'name'])
$this->make('SELECT * FROM ' . self::TABLE . ' INNER JOIN join_table ON ' . self::TABLE . '.id=join_table.id AND ' . self::TABLE . '.name=join_table.name WHERE id=? AND name=?', [1, 'name'])
->innerJoin('join_table')->on(self::TABLE . '.id', 'join_table.id')->onAnd(self::TABLE . '.name', 'join_table.name')->where('id', 1)->and('name', 'name')->rows();
}

Expand Down

0 comments on commit 056c0c0

Please sign in to comment.