Skip to content

Commit 97e90e7

Browse files
committed
Merge #350 - Fix #296 - Support KEY order (ASC/DESC)
Fixes: #296 Pull-request: #350 Signed-off-by: William Desportes <williamdes@wdes.fr>
2 parents 1eb7d53 + 70894e4 commit 97e90e7

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

src/Components/Key.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ class Key extends Component
5555
public $name;
5656

5757
/**
58-
* Columns.
58+
* The key columns
5959
*
60-
* @var array
60+
* @var array[]
61+
* @phpstan-var array{name?: string, length?: int, order?: string}[]
6162
*/
6263
public $columns;
6364

@@ -116,7 +117,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
116117
/**
117118
* Last parsed column.
118119
*
119-
* @var array
120+
* @var array<string,mixed>
120121
*/
121122
$lastColumn = array();
122123

@@ -186,6 +187,16 @@ public static function parse(Parser $parser, TokensList $list, array $options =
186187
$lastColumn = array();
187188
}
188189
}
190+
} elseif (
191+
(
192+
$token->type === Token::TYPE_KEYWORD
193+
)
194+
&&
195+
(
196+
($token->keyword === 'ASC') || ($token->keyword === 'DESC')
197+
)
198+
) {
199+
$lastColumn['order'] = $token->keyword;
189200
} else {
190201
$lastColumn['name'] = $token->value;
191202
}
@@ -244,6 +255,11 @@ public static function build($component, array $options = array())
244255
if (isset($column['length'])) {
245256
$tmp .= '(' . $column['length'] . ')';
246257
}
258+
259+
if (isset($column['order'])) {
260+
$tmp .= ' ' . $column['order'];
261+
}
262+
247263
$columns[] = $tmp;
248264
}
249265

tests/Builder/CreateStatementTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,51 @@ public function testBuildSelect()
617617
);
618618
}
619619

620+
public function testBuildCreateTableSortedIndex()
621+
{
622+
$parser = new Parser(
623+
<<<'SQL'
624+
CREATE TABLE `entries` (
625+
`id` int(11) NOT NULL AUTO_INCREMENT,
626+
`fk_ug_id` int(11) DEFAULT NULL,
627+
`amount` decimal(10,2) DEFAULT NULL,
628+
PRIMARY KEY (`id`),
629+
KEY `entries__ug` (`fk_ug_id` DESC),
630+
KEY `entries__ug2` (`fk_ug_id` ASC),
631+
KEY `33` (`id` ASC, `fk_ug_id` DESC)
632+
) /*!50100 TABLESPACE `innodb_system` */ ENGINE=InnoDB AUTO_INCREMENT=4465 DEFAULT CHARSET=utf8
633+
SQL
634+
);
635+
636+
/** @var CreateStatement $stmt */
637+
$stmt = $parser->statements[0];
638+
639+
$tableBody = <<<'SQL'
640+
(
641+
`id` int(11) NOT NULL AUTO_INCREMENT,
642+
`fk_ug_id` int(11) DEFAULT NULL,
643+
`amount` decimal(10,2) DEFAULT NULL,
644+
PRIMARY KEY (`id`),
645+
KEY `entries__ug` (`fk_ug_id` DESC),
646+
KEY `entries__ug2` (`fk_ug_id` ASC),
647+
KEY `33` (`id` ASC,`fk_ug_id` DESC)
648+
)
649+
SQL;
650+
651+
$this->assertEquals(
652+
$tableBody,
653+
CreateDefinition::build($stmt->fields)
654+
);
655+
656+
$this->assertEquals(
657+
'CREATE TABLE `entries` '
658+
. $tableBody
659+
. ' ENGINE=InnoDB AUTO_INCREMENT=4465 DEFAULT CHARSET=utf8 TABLESPACE `innodb_system`',
660+
$stmt->build()
661+
);
662+
663+
}
664+
620665
public function testBuildCreateTableComplexIndexes()
621666
{
622667
$parser = new Parser(

tests/Components/KeyTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,61 @@ public function testParseKeyWithLengthWithoutOptions()
5858
), $component->columns);
5959
}
6060

61+
public function testParseKeyWithLengthWithoutOptionsWithOrder()
62+
{
63+
$component = Key::parse(
64+
new Parser(),
65+
$this->getTokensList('KEY `alias_type_idx` (`alias_type`(10) ASC),')
66+
);
67+
$this->assertEquals('KEY', $component->type);
68+
$this->assertEquals('alias_type_idx', $component->name);
69+
$this->assertEquals(new OptionsArray(), $component->options);
70+
$this->assertNull($component->expr);
71+
$this->assertSame(array(
72+
array(
73+
'name' => 'alias_type',
74+
'length' => 10,
75+
'order' => 'ASC',
76+
)
77+
), $component->columns);
78+
}
79+
80+
public function testParseKeyWithoutOptionsWithOrderLowercase()
81+
{
82+
$component = Key::parse(
83+
new Parser(),
84+
$this->getTokensList('KEY `alias_type_idx` (`alias_type` desc),')
85+
);
86+
$this->assertEquals('KEY', $component->type);
87+
$this->assertEquals('alias_type_idx', $component->name);
88+
$this->assertEquals(new OptionsArray(), $component->options);
89+
$this->assertNull($component->expr);
90+
$this->assertSame(array(
91+
array(
92+
'name' => 'alias_type',
93+
'order' => 'DESC',
94+
)
95+
), $component->columns);
96+
}
97+
98+
public function testParseKeyWithoutOptionsWithOrder()
99+
{
100+
$component = Key::parse(
101+
new Parser(),
102+
$this->getTokensList('KEY `alias_type_idx` (`alias_type` DESC),')
103+
);
104+
$this->assertEquals('KEY', $component->type);
105+
$this->assertEquals('alias_type_idx', $component->name);
106+
$this->assertEquals(new OptionsArray(), $component->options);
107+
$this->assertNull($component->expr);
108+
$this->assertSame(array(
109+
array(
110+
'name' => 'alias_type',
111+
'order' => 'DESC',
112+
)
113+
), $component->columns);
114+
}
115+
61116
public function testParseKeyWithLengthWithOptions()
62117
{
63118
$component = Key::parse(

0 commit comments

Comments
 (0)