Skip to content

Commit 72a170a

Browse files
committed
Merge #443 - Fix #260 - implement support for LEFT JOIN, JOIN,INNER JOIN on UpdateStatement
Pull-request: #443 Fixes: #260 Signed-off-by: William Desportes <[email protected]>
2 parents 7b1f1e5 + 204d34a commit 72a170a

12 files changed

Lines changed: 52 additions & 24 deletions

src/Statements/UpdateStatement.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\SqlParser\Components\Condition;
88
use PhpMyAdmin\SqlParser\Components\Expression;
9+
use PhpMyAdmin\SqlParser\Components\JoinKeyword;
910
use PhpMyAdmin\SqlParser\Components\Limit;
1011
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
1112
use PhpMyAdmin\SqlParser\Components\SetOperation;
@@ -15,6 +16,7 @@
1516
* `UPDATE` statement.
1617
*
1718
* UPDATE [LOW_PRIORITY] [IGNORE] table_reference
19+
* [INNER JOIN | LEFT JOIN | JOIN] T1 ON T1.C1 = T2.C1
1820
* SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
1921
* [WHERE where_condition]
2022
* [ORDER BY ...]
@@ -62,6 +64,18 @@ class UpdateStatement extends Statement
6264
'UPDATE',
6365
1,
6466
],
67+
'JOIN' => [
68+
'JOIN',
69+
1,
70+
],
71+
'LEFT JOIN' => [
72+
'LEFT JOIN',
73+
1,
74+
],
75+
'INNER JOIN' => [
76+
'INNER JOIN',
77+
1,
78+
],
6579
'SET' => [
6680
'SET',
6781
3,
@@ -114,4 +128,11 @@ class UpdateStatement extends Statement
114128
* @var Limit|null
115129
*/
116130
public $limit;
131+
132+
/**
133+
* Joins.
134+
*
135+
* @var JoinKeyword[]|null
136+
*/
137+
public $join;
117138
}

tests/Builder/UpdateStatementTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public function testBuilder(): void
1717
);
1818
$stmt = $parser->statements[0];
1919
$this->assertEquals(
20-
'UPDATE user AS `u` SET ud.ip = \'33\' WHERE u.id = 1',
20+
'UPDATE user AS `u` LEFT JOIN user_detail AS `ud` ON u.id = ud.user_id SET ud.ip = \'33\' WHERE u.id = 1',
2121
$stmt->build()
2222
);
2323
/* Assertion 2 */
2424
$parser = new Parser('update user u join user_detail ud on u.id = ud.user_id set ud.ip =\'33\' where u.id = 1');
2525
$stmt = $parser->statements[0];
2626
$this->assertEquals(
27-
'UPDATE user AS `u` SET ud.ip = \'33\' WHERE u.id = 1',
27+
'UPDATE user AS `u` JOIN user_detail AS `ud` ON u.id = ud.user_id SET ud.ip = \'33\' WHERE u.id = 1',
2828
$stmt->build()
2929
);
3030
/* Assertion 3 */
@@ -33,7 +33,7 @@ public function testBuilder(): void
3333
);
3434
$stmt = $parser->statements[0];
3535
$this->assertEquals(
36-
'UPDATE user AS `u` SET ud.ip = \'33\' WHERE u.id = 1',
36+
'UPDATE user AS `u` INNER JOIN user_detail AS `ud` ON u.id = ud.user_id SET ud.ip = \'33\' WHERE u.id = 1',
3737
$stmt->build()
3838
);
3939
}

tests/data/parser/parseTransaction.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@
502502
],
503503
"order": null,
504504
"limit": null,
505+
"join": null,
505506
"options": {
506507
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
507508
"options": []

tests/data/parser/parseTransaction2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@
502502
],
503503
"order": null,
504504
"limit": null,
505+
"join": null,
505506
"options": {
506507
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
507508
"options": []

tests/data/parser/parseUpdate1.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
"where": null,
215215
"order": null,
216216
"limit": null,
217+
"join": null,
217218
"options": {
218219
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
219220
"options": []

tests/data/parser/parseUpdate2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@
372372
"offset": 2,
373373
"rowCount": 1
374374
},
375+
"join": null,
375376
"options": {
376377
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
377378
"options": []

tests/data/parser/parseUpdate3.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
],
222222
"order": null,
223223
"limit": null,
224+
"join": null,
224225
"options": {
225226
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
226227
"options": []

tests/data/parser/parseUpdate4.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
],
358358
"order": null,
359359
"limit": null,
360+
"join": null,
360361
"options": {
361362
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
362363
"options": []

tests/data/parser/parseUpdate5.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,6 @@
444444
],
445445
"order": null,
446446
"limit": null,
447-
"options": {
448-
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
449-
"options": []
450-
},
451-
"first": 0,
452-
"last": 41,
453447
"join": [
454448
{
455449
"@type": "PhpMyAdmin\\SqlParser\\Components\\JoinKeyword",
@@ -479,7 +473,13 @@
479473
],
480474
"using": null
481475
}
482-
]
476+
],
477+
"options": {
478+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
479+
"options": []
480+
},
481+
"first": 0,
482+
"last": 41
483483
}
484484
],
485485
"brackets": 0,

tests/data/parser/parseUpdate6.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,6 @@
625625
],
626626
"order": null,
627627
"limit": null,
628-
"options": {
629-
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
630-
"options": []
631-
},
632-
"first": 0,
633-
"last": 60,
634628
"join": [
635629
{
636630
"@type": "PhpMyAdmin\\SqlParser\\Components\\JoinKeyword",
@@ -686,7 +680,13 @@
686680
],
687681
"using": null
688682
}
689-
]
683+
],
684+
"options": {
685+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
686+
"options": []
687+
},
688+
"first": 0,
689+
"last": 60
690690
}
691691
],
692692
"brackets": 0,

0 commit comments

Comments
 (0)