Skip to content

Commit 789bdfb

Browse files
committed
Add basic details for Condition
Adds leftOperand, operator and rightOperand properties to the Condition component. It only splits comparison operators for now. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 1f0c925 commit 789bdfb

69 files changed

Lines changed: 775 additions & 185 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Components/Condition.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ final class Condition implements Component
2727
*/
2828
public string $expr;
2929

30+
public string $leftOperand = '';
31+
public string $operator = '';
32+
public string $rightOperand = '';
33+
3034
/** @param string $expr the condition or the operator */
3135
public function __construct(string|null $expr = null)
3236
{

src/Parsers/Conditions.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ final class Conditions implements Parseable
5757
'XOR',
5858
];
5959

60+
private const COMPARISON_OPERATORS = ['=', '>=', '>', '<=', '<', '<>', '!='];
61+
6062
/**
6163
* @param Parser $parser the parser that serves as context
6264
* @param TokensList $list the list of tokens that are being parsed
@@ -84,6 +86,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
8486
*/
8587
$betweenBefore = false;
8688

89+
$hasSubQuery = false;
90+
$subQueryBracket = 0;
91+
8792
for (; $list->idx < $list->count; ++$list->idx) {
8893
/**
8994
* Token parsed at this moment.
@@ -104,9 +109,25 @@ public static function parse(Parser $parser, TokensList $list, array $options =
104109
// space character.
105110
if ($token->type === TokenType::Whitespace) {
106111
$expr->expr .= ' ';
112+
if ($expr->operator === '') {
113+
$expr->leftOperand .= ' ';
114+
} else {
115+
$expr->rightOperand .= ' ';
116+
}
117+
107118
continue;
108119
}
109120

121+
if (
122+
! $hasSubQuery
123+
&& $token->keyword !== null && $token->type === TokenType::Keyword
124+
&& $brackets > 0
125+
&& (Parser::STATEMENT_PARSERS[$token->keyword] ?? '') !== ''
126+
) {
127+
$hasSubQuery = true;
128+
$subQueryBracket = $brackets;
129+
}
130+
110131
// Conditions are delimited by logical operators.
111132
if (in_array($token->value, self::DELIMITERS, true)) {
112133
if ($betweenBefore && ($token->value === 'AND')) {
@@ -115,7 +136,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
115136
} else {
116137
// The expression ended.
117138
$expr->expr = trim($expr->expr);
118-
if (! empty($expr->expr)) {
139+
if ($expr->expr !== '') {
140+
$expr->leftOperand = trim($expr->leftOperand);
141+
$expr->rightOperand = trim($expr->rightOperand);
119142
$ret[] = $expr;
120143
}
121144

@@ -152,11 +175,23 @@ public static function parse(Parser $parser, TokensList $list, array $options =
152175
break;
153176
}
154177

178+
if ($subQueryBracket === $brackets) {
179+
$hasSubQuery = false;
180+
}
181+
155182
--$brackets;
183+
} elseif (! $hasSubQuery && in_array($token->value, self::COMPARISON_OPERATORS, true)) {
184+
$expr->operator = $token->value;
156185
}
157186
}
158187

159188
$expr->expr .= $token->token;
189+
if ($expr->operator === '') {
190+
$expr->leftOperand .= $token->token;
191+
} elseif ($expr->rightOperand !== '' || $expr->operator !== $token->value) {
192+
$expr->rightOperand .= $token->token;
193+
}
194+
160195
if (
161196
($token->type !== TokenType::None)
162197
&& (($token->type !== TokenType::Keyword)
@@ -176,7 +211,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
176211

177212
// Last iteration was not processed.
178213
$expr->expr = trim($expr->expr);
179-
if (! empty($expr->expr)) {
214+
if ($expr->expr !== '') {
215+
$expr->leftOperand = trim($expr->leftOperand);
216+
$expr->rightOperand = trim($expr->rightOperand);
180217
$ret[] = $expr;
181218
}
182219

tests/data/bugs/gh202.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@
321321
"id"
322322
],
323323
"isOperator": false,
324-
"expr": "t.id=1"
324+
"expr": "t.id=1",
325+
"leftOperand": "t.id",
326+
"operator": "=",
327+
"rightOperand": "1"
325328
}
326329
],
327330
"order": null,

tests/data/bugs/gh492.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@
274274
"orderid"
275275
],
276276
"isOperator": false,
277-
"expr": "orderid = ?"
277+
"expr": "orderid = ?",
278+
"leftOperand": "orderid",
279+
"operator": "=",
280+
"rightOperand": "?"
278281
}
279282
],
280283
"order": null,

tests/data/bugs/gh496.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,10 @@
479479
"i"
480480
],
481481
"isOperator": false,
482-
"expr": "io.id = i.id"
482+
"expr": "io.id = i.id",
483+
"leftOperand": "io.id",
484+
"operator": "=",
485+
"rightOperand": "i.id"
483486
}
484487
],
485488
"using": null

tests/data/bugs/gh498.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,10 @@
452452
"uno"
453453
],
454454
"isOperator": false,
455-
"expr": "dos.id = uno.id"
455+
"expr": "dos.id = uno.id",
456+
"leftOperand": "dos.id",
457+
"operator": "=",
458+
"rightOperand": "uno.id"
456459
}
457460
],
458461
"using": null

tests/data/bugs/gh511.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,10 @@
794794
"id_users_type"
795795
],
796796
"isOperator": false,
797-
"expr": "id_users_type = 19"
797+
"expr": "id_users_type = 19",
798+
"leftOperand": "id_users_type",
799+
"operator": "=",
800+
"rightOperand": "19"
798801
}
799802
],
800803
"order": null,

tests/data/bugs/pma11836.out

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,19 @@
568568
"nombre"
569569
],
570570
"isOperator": false,
571-
"expr": "id = IF(id = 1, id, nombre)"
571+
"expr": "id = IF(id = 1, id, nombre)",
572+
"leftOperand": "id",
573+
"operator": "=",
574+
"rightOperand": "IF(id = 1, id, nombre)"
572575
},
573576
{
574577
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
575578
"identifiers": [],
576579
"isOperator": true,
577-
"expr": "AND"
580+
"expr": "AND",
581+
"leftOperand": "",
582+
"operator": "",
583+
"rightOperand": ""
578584
},
579585
{
580586
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
@@ -583,7 +589,10 @@
583589
"alumnos"
584590
],
585591
"isOperator": false,
586-
"expr": "id not in (SELECT id FROM alumnos)"
592+
"expr": "id not in (SELECT id FROM alumnos)",
593+
"leftOperand": "id not in (SELECT id FROM alumnos)",
594+
"operator": "",
595+
"rightOperand": ""
587596
}
588597
],
589598
"group": null,

tests/data/parser/parseCreateOrReplaceView1.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,10 @@
682682
"1990-01-19"
683683
],
684684
"isOperator": false,
685-
"expr": "(mytable.birth > '1990-01-19')"
685+
"expr": "(mytable.birth > '1990-01-19')",
686+
"leftOperand": "(mytable.birth",
687+
"operator": ">",
688+
"rightOperand": "'1990-01-19')"
686689
}
687690
],
688691
"group": [

tests/data/parser/parseCreateView3.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@
473473
"one space"
474474
],
475475
"isOperator": false,
476-
"expr": "`one space` > 3.0"
476+
"expr": "`one space` > 3.0",
477+
"leftOperand": "`one space`",
478+
"operator": ">",
479+
"rightOperand": "3.0"
477480
}
478481
],
479482
"group": null,

0 commit comments

Comments
 (0)