Skip to content

Commit

Permalink
Merge pull request #549 from MauricioFauth/condition-details
Browse files Browse the repository at this point in the history
Add basic details for Condition
  • Loading branch information
MauricioFauth authored Feb 8, 2024
2 parents c4efadf + 789bdfb commit 4ae21ca
Show file tree
Hide file tree
Showing 69 changed files with 775 additions and 185 deletions.
4 changes: 4 additions & 0 deletions src/Components/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ final class Condition implements Component
*/
public string $expr;

public string $leftOperand = '';
public string $operator = '';
public string $rightOperand = '';

/** @param string $expr the condition or the operator */
public function __construct(string|null $expr = null)
{
Expand Down
41 changes: 39 additions & 2 deletions src/Parsers/Conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ final class Conditions implements Parseable
'XOR',
];

private const COMPARISON_OPERATORS = ['=', '>=', '>', '<=', '<', '<>', '!='];

/**
* @param Parser $parser the parser that serves as context
* @param TokensList $list the list of tokens that are being parsed
Expand Down Expand Up @@ -84,6 +86,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
*/
$betweenBefore = false;

$hasSubQuery = false;
$subQueryBracket = 0;

for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
Expand All @@ -104,9 +109,25 @@ public static function parse(Parser $parser, TokensList $list, array $options =
// space character.
if ($token->type === TokenType::Whitespace) {
$expr->expr .= ' ';
if ($expr->operator === '') {
$expr->leftOperand .= ' ';
} else {
$expr->rightOperand .= ' ';
}

continue;
}

if (
! $hasSubQuery
&& $token->keyword !== null && $token->type === TokenType::Keyword
&& $brackets > 0
&& (Parser::STATEMENT_PARSERS[$token->keyword] ?? '') !== ''
) {
$hasSubQuery = true;
$subQueryBracket = $brackets;
}

// Conditions are delimited by logical operators.
if (in_array($token->value, self::DELIMITERS, true)) {
if ($betweenBefore && ($token->value === 'AND')) {
Expand All @@ -115,7 +136,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
} else {
// The expression ended.
$expr->expr = trim($expr->expr);
if (! empty($expr->expr)) {
if ($expr->expr !== '') {
$expr->leftOperand = trim($expr->leftOperand);
$expr->rightOperand = trim($expr->rightOperand);
$ret[] = $expr;
}

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

if ($subQueryBracket === $brackets) {
$hasSubQuery = false;
}

--$brackets;
} elseif (! $hasSubQuery && in_array($token->value, self::COMPARISON_OPERATORS, true)) {
$expr->operator = $token->value;
}
}

$expr->expr .= $token->token;
if ($expr->operator === '') {
$expr->leftOperand .= $token->token;
} elseif ($expr->rightOperand !== '' || $expr->operator !== $token->value) {
$expr->rightOperand .= $token->token;
}

if (
($token->type !== TokenType::None)
&& (($token->type !== TokenType::Keyword)
Expand All @@ -176,7 +211,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =

// Last iteration was not processed.
$expr->expr = trim($expr->expr);
if (! empty($expr->expr)) {
if ($expr->expr !== '') {
$expr->leftOperand = trim($expr->leftOperand);
$expr->rightOperand = trim($expr->rightOperand);
$ret[] = $expr;
}

Expand Down
5 changes: 4 additions & 1 deletion tests/data/bugs/gh202.out
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@
"id"
],
"isOperator": false,
"expr": "t.id=1"
"expr": "t.id=1",
"leftOperand": "t.id",
"operator": "=",
"rightOperand": "1"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/bugs/gh492.out
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@
"orderid"
],
"isOperator": false,
"expr": "orderid = ?"
"expr": "orderid = ?",
"leftOperand": "orderid",
"operator": "=",
"rightOperand": "?"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/bugs/gh496.out
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,10 @@
"i"
],
"isOperator": false,
"expr": "io.id = i.id"
"expr": "io.id = i.id",
"leftOperand": "io.id",
"operator": "=",
"rightOperand": "i.id"
}
],
"using": null
Expand Down
5 changes: 4 additions & 1 deletion tests/data/bugs/gh498.out
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@
"uno"
],
"isOperator": false,
"expr": "dos.id = uno.id"
"expr": "dos.id = uno.id",
"leftOperand": "dos.id",
"operator": "=",
"rightOperand": "uno.id"
}
],
"using": null
Expand Down
5 changes: 4 additions & 1 deletion tests/data/bugs/gh511.out
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,10 @@
"id_users_type"
],
"isOperator": false,
"expr": "id_users_type = 19"
"expr": "id_users_type = 19",
"leftOperand": "id_users_type",
"operator": "=",
"rightOperand": "19"
}
],
"order": null,
Expand Down
15 changes: 12 additions & 3 deletions tests/data/bugs/pma11836.out
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,19 @@
"nombre"
],
"isOperator": false,
"expr": "id = IF(id = 1, id, nombre)"
"expr": "id = IF(id = 1, id, nombre)",
"leftOperand": "id",
"operator": "=",
"rightOperand": "IF(id = 1, id, nombre)"
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": true,
"expr": "AND"
"expr": "AND",
"leftOperand": "",
"operator": "",
"rightOperand": ""
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
Expand All @@ -583,7 +589,10 @@
"alumnos"
],
"isOperator": false,
"expr": "id not in (SELECT id FROM alumnos)"
"expr": "id not in (SELECT id FROM alumnos)",
"leftOperand": "id not in (SELECT id FROM alumnos)",
"operator": "",
"rightOperand": ""
}
],
"group": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseCreateOrReplaceView1.out
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,10 @@
"1990-01-19"
],
"isOperator": false,
"expr": "(mytable.birth > '1990-01-19')"
"expr": "(mytable.birth > '1990-01-19')",
"leftOperand": "(mytable.birth",
"operator": ">",
"rightOperand": "'1990-01-19')"
}
],
"group": [
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseCreateView3.out
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,10 @@
"one space"
],
"isOperator": false,
"expr": "`one space` > 3.0"
"expr": "`one space` > 3.0",
"leftOperand": "`one space`",
"operator": ">",
"rightOperand": "3.0"
}
],
"group": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseCreateViewWithUnion.out
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,10 @@
"M"
],
"isOperator": false,
"expr": "`employees`.`gender` = 'M'"
"expr": "`employees`.`gender` = 'M'",
"leftOperand": "`employees`.`gender`",
"operator": "=",
"rightOperand": "'M'"
}
],
"group": null,
Expand Down
25 changes: 20 additions & 5 deletions tests/data/parser/parseDelete.out
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,19 @@
"id"
],
"isOperator": false,
"expr": "`id`<3"
"expr": "`id`<3",
"leftOperand": "`id`",
"operator": "<",
"rightOperand": "3"
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": true,
"expr": "AND"
"expr": "AND",
"leftOperand": "",
"operator": "",
"rightOperand": ""
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
Expand All @@ -501,13 +507,19 @@
"Dan"
],
"isOperator": false,
"expr": "(username=\"Dan\""
"expr": "(username=\"Dan\"",
"leftOperand": "(username",
"operator": "=",
"rightOperand": "\"Dan\""
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": true,
"expr": "OR"
"expr": "OR",
"leftOperand": "",
"operator": "",
"rightOperand": ""
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
Expand All @@ -516,7 +528,10 @@
"Paul"
],
"isOperator": false,
"expr": "username=\"Paul\")"
"expr": "username=\"Paul\")",
"leftOperand": "username",
"operator": "=",
"rightOperand": "\"Paul\")"
}
],
"order": [
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDelete10.out
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,10 @@
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": false,
"expr": "1=1"
"expr": "1=1",
"leftOperand": "1",
"operator": "=",
"rightOperand": "1"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDelete11.out
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": false,
"expr": "1=1"
"expr": "1=1",
"leftOperand": "1",
"operator": "=",
"rightOperand": "1"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDelete12.out
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": false,
"expr": "1=1"
"expr": "1=1",
"leftOperand": "1",
"operator": "=",
"rightOperand": "1"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDelete13.out
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@
"salary"
],
"isOperator": false,
"expr": "x.salary = 20"
"expr": "x.salary = 20",
"leftOperand": "x.salary",
"operator": "=",
"rightOperand": "20"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDelete3.out
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": false,
"expr": "1=1"
"expr": "1=1",
"leftOperand": "1",
"operator": "=",
"rightOperand": "1"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDelete4.out
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": false,
"expr": "1=1"
"expr": "1=1",
"leftOperand": "1",
"operator": "=",
"rightOperand": "1"
}
],
"order": [
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDelete5.out
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": false,
"expr": "1=1"
"expr": "1=1",
"leftOperand": "1",
"operator": "=",
"rightOperand": "1"
}
],
"order": [
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDeleteErr11.out
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@
"a"
],
"isOperator": false,
"expr": "a = 1"
"expr": "a = 1",
"leftOperand": "a",
"operator": "=",
"rightOperand": "1"
}
],
"order": null,
Expand Down
5 changes: 4 additions & 1 deletion tests/data/parser/parseDeleteErr4.out
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@
"@type": "PhpMyAdmin\\SqlParser\\Components\\Condition",
"identifiers": [],
"isOperator": false,
"expr": "1=1"
"expr": "1=1",
"leftOperand": "1",
"operator": "=",
"rightOperand": "1"
}
],
"order": null,
Expand Down
Loading

0 comments on commit 4ae21ca

Please sign in to comment.