Skip to content

Commit 48503a3

Browse files
author
Carl Sverre
authored
Merge pull request #28 from srdante/sort-key-improvement
Sort key direction support improvement
2 parents ab7fada + ca2ee04 commit 48503a3

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/Schema/Grammar.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
177177
return str_replace(sprintf('alter table %s add ', $this->wrapTable($blueprint)), '', $compiled);
178178
}
179179

180+
/**
181+
* Convert an array of column names into a delimited string (with direction parameter).
182+
*
183+
* @param array $columns
184+
* @return string
185+
*/
186+
protected function columnizeWithDirection(array $columns, string $direction)
187+
{
188+
$wrapped = array_map([$this, 'wrap'], $columns);
189+
190+
return implode(", ", array_map(function ($column) use ($direction) {
191+
return $column . ' ' . $direction;
192+
}, $wrapped));
193+
}
194+
180195
/**
181196
* Get the SQL for an auto-increment column modifier.
182197
*

src/Schema/Grammar/CompilesKeys.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function compileSortKey(Blueprint $blueprint, Fluent $command)
2222
return "{$variable}={$value}";
2323
})->join(',');
2424

25-
return "sort key({$this->columnize($command->columns)} {$command->direction}) with ({$compiled})";
25+
return "sort key({$this->columnizeWithDirection($command->columns, $command->direction)}) with ({$compiled})";
2626
}
2727

28-
return "sort key({$this->columnize($command->columns)} {$command->direction})";
28+
return "sort key({$this->columnizeWithDirection($command->columns, $command->direction)})";
2929
}
3030

3131
public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)

tests/Hybrid/CreateTable/SortKeysTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function it_adds_a_dual_sort_key()
7878

7979
$this->assertCreateStatement(
8080
$blueprint,
81-
'create table `test` (`f_name` varchar(255) not null, `l_name` varchar(255) not null, sort key(`f_name`, `l_name` asc))'
81+
'create table `test` (`f_name` varchar(255) not null, `l_name` varchar(255) not null, sort key(`f_name` asc, `l_name` asc))'
8282
);
8383
}
8484

0 commit comments

Comments
 (0)