Skip to content

Commit 2d0784f

Browse files
czoIgrobert.kubac
andauthored
fix #125 (#359)
Co-authored-by: robert.kubac <[email protected]>
1 parent 930734a commit 2d0784f

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

src/PHPSQLParser/processors/FromProcessor.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ public function process($tokens) {
190190
if ($token_category === 'LEFT' || $token_category === 'RIGHT' || $token_category === 'NATURAL') {
191191
$token_category = '';
192192
$parseInfo['next_join_type'] = strtoupper(trim($prevToken)); // it seems to be a join
193+
} elseif ($token_category === 'IDX_HINT') {
194+
$parseInfo['expression'] .= $token;
195+
if ($parseInfo['ref_type'] !== false) { // all after ON / USING
196+
$parseInfo['ref_expr'] .= $token;
197+
}
193198
}
194199
break;
195200

@@ -276,6 +281,12 @@ public function process($tokens) {
276281
break;
277282

278283
case 'FOR':
284+
if ($token_category === 'IDX_HINT') {
285+
$cur_hint = (count($parseInfo['hints']) - 1);
286+
$parseInfo['hints'][$cur_hint]['hint_type'] .= " " . $upper;
287+
continue 2;
288+
}
289+
279290
$parseInfo['token_count']++;
280291
$skip_next = true;
281292
break;
@@ -295,6 +306,12 @@ public function process($tokens) {
295306
$parseInfo['next_join_type'] = 'CROSS';
296307

297308
case 'JOIN':
309+
if ($token_category === 'IDX_HINT') {
310+
$cur_hint = (count($parseInfo['hints']) - 1);
311+
$parseInfo['hints'][$cur_hint]['hint_type'] .= " " . $upper;
312+
continue 2;
313+
}
314+
298315
if ($parseInfo['subquery']) {
299316
$parseInfo['sub_tree'] = $this->parse($this->removeParenthesisFromStart($parseInfo['subquery']));
300317
$parseInfo['expression'] = $parseInfo['subquery'];
@@ -304,6 +321,13 @@ public function process($tokens) {
304321
$parseInfo = $this->initParseInfo($parseInfo);
305322
break;
306323

324+
case 'GROUP BY':
325+
if ($token_category === 'IDX_HINT') {
326+
$cur_hint = (count($parseInfo['hints']) - 1);
327+
$parseInfo['hints'][$cur_hint]['hint_type'] .= " " . $upper;
328+
continue 2;
329+
}
330+
307331
default:
308332
// TODO: enhance it, so we can have base_expr to calculate the position of the keywords
309333
// build a subtree under "hints"

src/PHPSQLParser/processors/SQLProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public function process($tokens) {
400400
break;
401401

402402
case 'FOR':
403-
if ($prev_category === 'SHOW') {
403+
if ($prev_category === 'SHOW' || $token_category === 'FROM') {
404404
break;
405405
}
406406
$skip_next = 1;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PHPSQLParser\Test\Creator;
4+
use PHPSQLParser\PHPSQLParser;
5+
use PHPSQLParser\PHPSQLCreator;
6+
7+
class issue125Test extends \PHPUnit\Framework\TestCase
8+
{
9+
/**
10+
* @dataProvider indexHintsDataProvider
11+
* @param string $hintType
12+
*/
13+
public function testIssue125FromIndexHint($hintType)
14+
{
15+
$sql = sprintf(
16+
'SELECT start_date FROM users %s INDEX (vacation_idx, users_idx) INNER JOIN vacation ON start_date = end_date',
17+
$hintType
18+
);
19+
20+
$parser = new PHPSQLParser();
21+
$creator = new PHPSQLCreator();
22+
23+
$parser->parse($sql, true);
24+
25+
$this->assertEquals($sql, $creator->create($parser->parsed));
26+
}
27+
28+
/**
29+
* @dataProvider indexHintsDataProvider
30+
* @param string $hintType
31+
*/
32+
public function testIssue125JoinIndexHint($hintType)
33+
{
34+
$sql = sprintf(
35+
'SELECT start_date FROM users %s INDEX FOR JOIN (vacation_idx, users_idx) INNER JOIN vacation ON start_date = end_date',
36+
$hintType
37+
);
38+
39+
$parser = new PHPSQLParser();
40+
$creator = new PHPSQLCreator();
41+
42+
$parser->parse($sql, true);
43+
44+
$this->assertEquals($sql, $creator->create($parser->parsed));
45+
}
46+
47+
public function indexHintsDataProvider()
48+
{
49+
return array(
50+
array('USE'),
51+
array('FORCE'),
52+
array('IGNORE')
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)