diff --git a/src/Clause/Conditional.php b/src/Clause/Conditional.php index d9c1a86..c7354a4 100644 --- a/src/Clause/Conditional.php +++ b/src/Clause/Conditional.php @@ -47,7 +47,8 @@ public function getValues(): array if ($values[$i] instanceof QueryInterface) { $value = $values[$i]->getValues(); array_splice($values, $i, 1, $value); - $i += count($value); + $i += count($value) - 1; + $count += count($value) - 1; } } diff --git a/src/Clause/Join.php b/src/Clause/Join.php index df78f77..0cf545e 100644 --- a/src/Clause/Join.php +++ b/src/Clause/Join.php @@ -38,6 +38,17 @@ public function __construct($subject, ConditionalInterface $on, string $type = ' */ public function getValues(): array { + if (is_array($this->subject)) { + reset($this->subject); + $alias = key($this->subject); + if (!is_string($alias)) { + trigger_error('Invalid subject array, use string keys for alias', E_USER_ERROR); + } + $table = $this->subject[$alias]; + if ($table instanceof SelectInterface) { + return array_merge($table->getValues(), $this->on->getValues()); + } + } return $this->on->getValues(); } diff --git a/src/Statement/Delete.php b/src/Statement/Delete.php index 4f150c5..9b0e234 100644 --- a/src/Statement/Delete.php +++ b/src/Statement/Delete.php @@ -77,10 +77,6 @@ public function getValues(): array $values = array_merge($values, $this->where->getValues()); } - if (!empty($this->orderBy)) { - $values = array_merge($values, $this->orderBy); - } - if ($this->limit != null) { $values = array_merge($values, $this->limit->getValues()); } diff --git a/tests/Clause/ConditionalTest.php b/tests/Clause/ConditionalTest.php index fb5e1fe..fc274bc 100644 --- a/tests/Clause/ConditionalTest.php +++ b/tests/Clause/ConditionalTest.php @@ -75,6 +75,7 @@ public function testGetValues() $this->assertIsArray($subject->getValues()); $this->assertCount(1, $subject->getValues()); + $this->assertEquals($subject->getValues(), ['val']); } public function testGetValuesWithQuery() @@ -92,5 +93,15 @@ public function testGetValuesWithQueryAndArgs() $this->assertIsArray($subject->getValues()); $this->assertCount(2, $subject->getValues()); + $this->assertEquals($subject->getValues(), [1, 2]); + } + + public function testGetValuesWithMultipleValues() + { + $subject = new Conditional('col', 'BETWEEN', [new Method('test', 1, 2), new Method('test', 3, 4)]); + + $this->assertIsArray($subject->getValues()); + $this->assertCount(4, $subject->getValues()); + $this->assertEquals($subject->getValues(), [1, 2, 3, 4]); } } diff --git a/tests/Clause/JoinTest.php b/tests/Clause/JoinTest.php index f72ec09..f2029b3 100644 --- a/tests/Clause/JoinTest.php +++ b/tests/Clause/JoinTest.php @@ -107,4 +107,21 @@ public function testGetValues() $this->assertIsArray($subject->getValues()); $this->assertCount(1, $subject->getValues()); } + + public function testGetValuesWithSubselect() + { + $db = $this->createMock(Database::class); + $subject = new Join( + ['alias' => (new Select($db))->from('table')->where(new Conditional('column1', '=', 'value1'))], + new Conditional('column2', '=', 'value2') + ); + + $this->assertStringMatchesFormat( + 'JOIN (SELECT * FROM table WHERE column1 = ?) AS alias ON column2 = ?', + $subject->__toString() + ); + $this->assertIsArray($subject->getValues()); + $this->assertCount(2, $subject->getValues()); + $this->assertEquals(['value1', 'value2'], $subject->getValues()); + } } diff --git a/tests/Statement/DeleteTest.php b/tests/Statement/DeleteTest.php index c9c04e8..c4bad74 100644 --- a/tests/Statement/DeleteTest.php +++ b/tests/Statement/DeleteTest.php @@ -162,9 +162,8 @@ public function testGetValuesWithOrderBy() ->orderBy('id', 'ASC') ->orderBy('name', 'DESC'); - // FIXME This seems broken... $this->assertIsArray($this->subject->getValues()); - $this->assertCount(2, $this->subject->getValues()); + $this->assertCount(0, $this->subject->getValues()); } public function testGetValuesWithLimit() diff --git a/tests/Statement/UpdateTest.php b/tests/Statement/UpdateTest.php index b9a5491..e16c575 100644 --- a/tests/Statement/UpdateTest.php +++ b/tests/Statement/UpdateTest.php @@ -242,7 +242,6 @@ public function testGetValuesWithOrderBy() ->orderBy('id', 'ASC') ->orderBy('name', 'DESC'); - // FIXME This seems broken... $this->assertIsArray($this->subject->getValues()); $this->assertCount(1, $this->subject->getValues()); }