Issue #3120892 by Kumar Kundan, ravi.shankar, julienjoye, daffie: Replicate SQL LEAST() in SQLite
parent
e337176092
commit
b5343b93b7
|
@ -133,6 +133,7 @@ class Connection extends DatabaseConnection {
|
|||
// Create functions needed by SQLite.
|
||||
$pdo->sqliteCreateFunction('if', [__CLASS__, 'sqlFunctionIf']);
|
||||
$pdo->sqliteCreateFunction('greatest', [__CLASS__, 'sqlFunctionGreatest']);
|
||||
$pdo->sqliteCreateFunction('least', [__CLASS__, 'sqlFunctionLeast']);
|
||||
$pdo->sqliteCreateFunction('pow', 'pow', 2);
|
||||
$pdo->sqliteCreateFunction('exp', 'exp', 1);
|
||||
$pdo->sqliteCreateFunction('length', 'strlen', 1);
|
||||
|
@ -239,6 +240,16 @@ class Connection extends DatabaseConnection {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SQLite compatibility implementation for the LEAST() SQL function.
|
||||
*/
|
||||
public static function sqlFunctionLeast() {
|
||||
// Remove all NULL, FALSE and empty strings values but leaves 0 (zero) values.
|
||||
$values = array_filter(func_get_args(), 'strlen');
|
||||
|
||||
return count($values) < 1 ? NULL : min($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* SQLite compatibility implementation for the CONCAT() SQL function.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Database;
|
||||
|
||||
/**
|
||||
* Tests the SQL LEAST operator.
|
||||
*
|
||||
* @group Database
|
||||
*/
|
||||
class SelectLeastTest extends DatabaseTestBase {
|
||||
|
||||
/**
|
||||
* Tests the SQL LEAST operator.
|
||||
*
|
||||
* @dataProvider selectLeastProvider
|
||||
*/
|
||||
public function testSelectLeast($values, $expected) {
|
||||
$least = $this->connection->query("SELECT LEAST(:values[])", [':values[]' => $values])->fetchField();
|
||||
$this->assertEquals($expected, $least);
|
||||
}
|
||||
|
||||
public function selectLeastProvider() {
|
||||
return [
|
||||
[[1, 2, 3, 4, 5, 6], 1],
|
||||
[['A', 'B', 'C', 'NULL', 'F'], 'A'],
|
||||
[['NULL', 'NULL'], 'NULL'],
|
||||
[['TRUE', 'FALSE'], 'FALSE'],
|
||||
[['A', 'B', 'C', 'NULL'], 'A'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue