diff --git a/src/Drivers/Escapers/MySQLEscaper.php b/src/Drivers/Escapers/MySQLEscaper.php index 9589826..106af45 100644 --- a/src/Drivers/Escapers/MySQLEscaper.php +++ b/src/Drivers/Escapers/MySQLEscaper.php @@ -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; } @@ -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; } diff --git a/src/Statement.php b/src/Statement.php index b84155b..9253477 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -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; diff --git a/src/Traits/JoinsTrait.php b/src/Traits/JoinsTrait.php index 9e7a96d..a2acd81 100644 --- a/src/Traits/JoinsTrait.php +++ b/src/Traits/JoinsTrait.php @@ -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); @@ -50,9 +50,7 @@ protected function joinsPlaceholders(): array return []; } - return array_merge([ - $this->joinsTable, - ], $this->onPlaceholders()); + return $this->onPlaceholders(); } /** @@ -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; - } } diff --git a/src/Traits/OnTrait.php b/src/Traits/OnTrait.php index 064123a..0c17031 100644 --- a/src/Traits/OnTrait.php +++ b/src/Traits/OnTrait.php @@ -15,7 +15,7 @@ trait OnTrait { /** - * @var list on conditions + * @var list on conditions */ protected array $on = []; @@ -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); @@ -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 { @@ -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' @@ -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' @@ -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. diff --git a/src/Traits/SetTrait.php b/src/Traits/SetTrait.php index c600d01..7c86d71 100644 --- a/src/Traits/SetTrait.php +++ b/src/Traits/SetTrait.php @@ -11,7 +11,7 @@ trait SetTrait { /** - * @var array values for SET + * @var array values for SET */ protected array $set = []; @@ -58,7 +58,7 @@ protected function setPlaceholders(): array /** * Set values for SET. * - * @param array $values array of values for SET + * @param array $values array of values for SET * * @return $this */ diff --git a/src/Traits/ValuesTrait.php b/src/Traits/ValuesTrait.php index 71ba2b1..81c7b38 100644 --- a/src/Traits/ValuesTrait.php +++ b/src/Traits/ValuesTrait.php @@ -8,7 +8,7 @@ trait ValuesTrait { /** - * @var list> values for VALUES + * @var list> values for VALUES */ protected array $values = []; @@ -63,7 +63,7 @@ protected function valuesPlaceholders(): array /** * Set values for VALUES. * - * @param list> $values values for VALUES + * @param list> $values values for VALUES * * @return $this */ diff --git a/src/Traits/WhereTrait.php b/src/Traits/WhereTrait.php index bb1bb18..6ac5713 100644 --- a/src/Traits/WhereTrait.php +++ b/src/Traits/WhereTrait.php @@ -12,7 +12,7 @@ trait WhereTrait { /** - * @var list where conditions + * @var list where conditions */ protected array $where = []; @@ -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 { @@ -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. @@ -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. @@ -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. diff --git a/tests/Integration/CRUDTest.php b/tests/Integration/CRUDTest.php index 1dfb425..666b6b1 100644 --- a/tests/Integration/CRUDTest.php +++ b/tests/Integration/CRUDTest.php @@ -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()); @@ -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( [ @@ -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() ); @@ -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() ); @@ -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 diff --git a/tests/Unit/Actions/SelectTest.php b/tests/Unit/Actions/SelectTest.php index 4c4650a..8fe8f4d 100644 --- a/tests/Unit/Actions/SelectTest.php +++ b/tests/Unit/Actions/SelectTest.php @@ -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(); }