Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
- added the ability to pass parameters into Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Feb 18, 2020
1 parent aa41484 commit d5cf2d4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG for 0.9.0 RC
======================

v2.7.9 (18.02.2020)
-----
- added the ability to pass parameters into Expression in operators and values

v2.7.8 (18.02.2020)
-----
- added the ability to pass parameters into Expression
Expand Down
1 change: 1 addition & 0 deletions src/Driver/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Spiral\Database\Driver;

use Spiral\Database\Exception\CompilerException;
use Spiral\Database\Injection\Expression;
use Spiral\Database\Injection\FragmentInterface;
use Spiral\Database\Injection\Parameter;
use Spiral\Database\Injection\ParameterInterface;
Expand Down
24 changes: 24 additions & 0 deletions src/Driver/CompilerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,22 @@ protected function hashWhere(QueryParameters $params, array $where): string
} elseif ($context[0] instanceof ParameterInterface) {
$hash .= $this->hashParam($params, $context[0]);
} else {
if ($context[0] instanceof Expression) {
foreach ($context[0]->getTokens()['parameters'] as $param) {
$params->push($param);
}
}

$hash .= $context[0];
}

// operator
if ($context[1] instanceof Expression) {
foreach ($context[1]->getTokens()['parameters'] as $param) {
$params->push($param);
}
}

$hash .= $context[1];

if ($context[2] instanceof QueryInterface) {
Expand All @@ -249,6 +261,12 @@ protected function hashWhere(QueryParameters $params, array $where): string
} elseif ($context[2] instanceof ParameterInterface) {
$hash .= $this->hashParam($params, $context[2]);
} else {
if ($context[2] instanceof Expression) {
foreach ($context[2]->getTokens()['parameters'] as $param) {
$params->push($param);
}
}

$hash .= $context[2];
}

Expand All @@ -259,6 +277,12 @@ protected function hashWhere(QueryParameters $params, array $where): string
} elseif ($context[3] instanceof ParameterInterface) {
$hash .= $this->hashParam($params, $context[3]);
} else {
if ($context[3] instanceof Expression) {
foreach ($context[3]->getTokens()['parameters'] as $param) {
$params->push($param);
}
}

$hash .= $context[3];
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/Database/SelectQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2377,4 +2377,57 @@ public function testSelectWithParametricExpression(): void
$select
);
}

public function testSelectWithParametricExpression2(): void
{
$select = $this->database->select()
->from(['users'])
->where(
new Expression('RANGE(?, ?)', 101, 102),
'&&',
new Expression('RANGE(?, ?)', 103, 104)
);

$this->assertSameQuery(
'SELECT * FROM {users} WHERE RANGE(?, ?) && RANGE(?, ?)',
$select
);

$this->assertSameParameters(
[
101,
102,
103,
104
],
$select
);
}

public function testSelectWithParametricExpression3(): void
{
$select = $this->database->select()
->from(['users'])
->where(
new Expression('RANGE(?, ?)', 101, 102),
new Expression('RANGE(name, ?)', 600),
new Expression('RANGE(?, ?)', 103, 104)
);

$this->assertSameQuery(
'SELECT * FROM {users} WHERE RANGE(?, ?) RANGE({name}, ?) RANGE(?, ?)',
$select
);

$this->assertSameParameters(
[
101,
102,
600,
103,
104
],
$select
);
}
}

0 comments on commit d5cf2d4

Please sign in to comment.