Skip to content

Commit 97802d9

Browse files
authored
Merge pull request #66 from homersimpsons/fix/sql-hints
sql hints
2 parents 10c68ca + 7a9e7f7 commit 97802d9

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

src/SQLParser/Node/Hint.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
4+
namespace SQLParser\Node;
5+
6+
7+
/**
8+
* This class represent a SQL hint, for example USE INDEX (my_index)
9+
*/
10+
class Hint
11+
{
12+
/**
13+
* @var string The hint type (example 'USE INDEX')
14+
*/
15+
private $type;
16+
/**
17+
* @var string The hint list (example '(my_index)')
18+
*/
19+
private $list;
20+
21+
public function __construct(string $type, string $list)
22+
{
23+
$this->type = $type;
24+
$this->list = $list;
25+
}
26+
27+
public function getType(): string
28+
{
29+
return $this->type;
30+
}
31+
32+
public function getList(): string
33+
{
34+
return $this->list;
35+
}
36+
}

src/SQLParser/Node/NodeFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ public static function toObject(array $desc)
191191
$expr->setRefClause(self::simplify($subTreeNodes));
192192
}
193193

194+
if (isset($desc['hints']) && $desc['hints']) {
195+
$expr->setHints(array_map(static function (array $hint) {
196+
return new Hint($hint['hint_type'], $hint['hint_list']);
197+
}, $desc['hints']));
198+
}
199+
194200
// Debug:
195201
unset($desc['base_expr']);
196202
unset($desc['expr_type']);

src/SQLParser/Node/Table.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,31 @@ public function setRefClause($refClause)
163163
$this->refClause = $refClause;
164164
}
165165

166+
/**
167+
* @var Hint[]
168+
*/
169+
private $hints;
170+
171+
/**
172+
* Return the list of table hints
173+
*
174+
* @return Hint[]
175+
*/
176+
public function getHints(): array
177+
{
178+
return $this->hints;
179+
}
180+
181+
/**
182+
* Set a list of table hints
183+
*
184+
* @param Hint[] $hints
185+
*/
186+
public function setHints(array $hints): void
187+
{
188+
$this->hints = $hints;
189+
}
190+
166191
/**
167192
* Returns a Mouf instance descriptor describing this object.
168193
*
@@ -206,6 +231,11 @@ public function toSql(array $parameters, AbstractPlatform $platform, int $indent
206231
if ($this->alias) {
207232
$sql .= ' AS '.$platform->quoteSingleIdentifier($this->alias);
208233
}
234+
if ($this->hints) {
235+
foreach ($this->hints as $hint) {
236+
$sql .= ' ' . $hint->getType() . ' ' . $hint->getList();
237+
}
238+
}
209239
if ($this->refClause) {
210240
$sql .= ' ON ';
211241
$sql .= NodeFactory::toSql($this->refClause, $platform, $parameters, ' ', true, $indent, $conditionsMode, $extrapolateParameters);

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public function testStandardSelect()
8888
$sql = 'SELECT * FROM users WHERE email not like :email';
8989
$this->assertEquals("SELECT * FROM users WHERE email NOT LIKE '%@example.com'", self::simplifySql($magicQuery->build($sql, ['email' => '%@example.com'])));
9090

91+
$sql = 'SELECT * FROM users USE INDEX (idx_users_email) ORDER BY email';
92+
$this->assertEquals("SELECT * FROM users USE INDEX (idx_users_email) ORDER BY email ASC", self::simplifySql($magicQuery->build($sql)));
93+
9194
$sql = 'SELECT * FROM myTable where someField BETWEEN :value1 AND :value2';
9295
$this->assertEquals("SELECT * FROM myTable WHERE someField BETWEEN '2' AND '4'", self::simplifySql($magicQuery->build($sql, ['value1' => 2, 'value2' => 4])));
9396
$this->assertEquals("SELECT * FROM myTable WHERE someField >= '2'", self::simplifySql($magicQuery->build($sql, ['value1' => 2])));

0 commit comments

Comments
 (0)