diff --git a/README.md b/README.md index cb44baf..fc28b46 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,15 @@ BlogArticle::select([ ->get(); ``` +#### Math +```php +use Tpetry\QueryExpressions\Function\Math\{ + Abs, +}; + +new Abs(string|Expression $expression); +``` + #### String ```php use Tpetry\QueryExpressions\Function\String\{ diff --git a/src/Function/Math/Abs.php b/src/Function/Math/Abs.php new file mode 100644 index 0000000..3a2f3d8 --- /dev/null +++ b/src/Function/Math/Abs.php @@ -0,0 +1,31 @@ +stringize($grammar, $this->expression); + + return match ($this->identify($grammar)) { + 'mysql', 'sqlite' => "(abs({$expression}))", + 'pgsql', 'sqlsrv' => "abs({$expression})", + }; + } +} diff --git a/tests/Function/Math/AbsTest.php b/tests/Function/Math/AbsTest.php new file mode 100644 index 0000000..19fea08 --- /dev/null +++ b/tests/Function/Math/AbsTest.php @@ -0,0 +1,25 @@ +expect(new Abs('val')) + ->toBeExecutable(function (Blueprint $table) { + $table->integer('val'); + }) + ->toBeMysql('(abs(`val`))') + ->toBePgsql('abs("val")') + ->toBeSqlite('(abs("val"))') + ->toBeSqlsrv('abs([val])'); + +it('can abs an expression') + ->expect(new Abs(new Expression('sum(1)'))) + ->toBeExecutable() + ->toBeMysql('(abs(sum(1)))') + ->toBePgsql('abs(sum(1))') + ->toBeSqlite('(abs(sum(1)))') + ->toBeSqlsrv('abs(sum(1))');