Skip to content

Commit

Permalink
Create OrderSortKeyword
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Tekiela <[email protected]>
  • Loading branch information
kamil-tekiela committed Nov 5, 2024
1 parent 5c4dc47 commit 913804c
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/Components/OrderKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ final class OrderKeyword implements Component
/**
* The order type.
*/
public string $type;
public OrderSortKeyword $type;

/**
* @param Expression|null $expr the expression that we are sorting by
* @param string $type the sorting type
* @param Expression|null $expr the expression that we are sorting by
* @param OrderSortKeyword $type the sorting type
*/
public function __construct(Expression|null $expr = null, string $type = 'ASC')
public function __construct(Expression|null $expr = null, OrderSortKeyword $type = OrderSortKeyword::Asc)
{
$this->expr = $expr;
$this->type = $type;
}

public function build(): string
{
return $this->expr . ' ' . $this->type;
return $this->expr . ' ' . $this->type->value;
}

public function __toString(): string
Expand Down
11 changes: 11 additions & 0 deletions src/Components/OrderSortKeyword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\SqlParser\Components;

enum OrderSortKeyword: string
{
case Asc = 'ASC';
case Desc = 'DESC';
}
3 changes: 2 additions & 1 deletion src/Parsers/OrderKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpMyAdmin\SqlParser\Parsers;

use PhpMyAdmin\SqlParser\Components\OrderKeyword;
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
use PhpMyAdmin\SqlParser\Parseable;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\TokensList;
Expand Down Expand Up @@ -66,7 +67,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
($token->type === TokenType::Keyword)
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
) {
$expr->type = $token->keyword;
$expr->type = OrderSortKeyword::from($token->keyword);
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
if (! empty($expr->expr)) {
$ret[] = $expr;
Expand Down
5 changes: 3 additions & 2 deletions tests/Components/OrderKeywordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\SqlParser\Components\Expression;
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
use PhpMyAdmin\SqlParser\Parsers\OrderKeywords;
use PhpMyAdmin\SqlParser\Tests\TestCase;

Expand All @@ -17,8 +18,8 @@ public function testBuildAll(): void
'a ASC, b DESC',
OrderKeywords::buildAll(
[
new OrderKeyword(new Expression('a'), 'ASC'),
new OrderKeyword(new Expression('b'), 'DESC'),
new OrderKeyword(new Expression('a'), OrderSortKeyword::Asc),
new OrderKeyword(new Expression('b'), OrderSortKeyword::Desc),
],
),
);
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete.out
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete4.out
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete5.out
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete6.out
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDelete7.out
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDeleteErr5.out
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseDeleteErr7.out
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
12 changes: 10 additions & 2 deletions tests/data/parser/parseSelect.out
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,11 @@
"function": null,
"subquery": null
},
"type": "DESC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Desc",
"value": "DESC"
}
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
Expand All @@ -1051,7 +1055,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelect13.out
Original file line number Diff line number Diff line change
Expand Up @@ -11992,7 +11992,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelect9.out
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
12 changes: 10 additions & 2 deletions tests/data/parser/parseSelectErr1.out
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,11 @@
"function": null,
"subquery": null
},
"type": "DESC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Desc",
"value": "DESC"
}
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
Expand All @@ -997,7 +1001,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelectGroupBy.out
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": {
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelectOrderByComment.out
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
6 changes: 5 additions & 1 deletion tests/data/parser/parseSelectOrderByIsNull.out
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down
12 changes: 10 additions & 2 deletions tests/data/parser/parseSelectUnion2.out
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
},
{
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderKeyword",
Expand All @@ -1303,7 +1307,11 @@
"function": null,
"subquery": null
},
"type": "ASC"
"type": {
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
"name": "Asc",
"value": "ASC"
}
}
],
"limit": null,
Expand Down

0 comments on commit 913804c

Please sign in to comment.