-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: tpetry <github@tpetry.me>
- Loading branch information
1 parent
ab94832
commit 7d15482
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tpetry\QueryExpressions\Function\Math; | ||
|
||
use Illuminate\Contracts\Database\Query\Expression; | ||
use Illuminate\Database\Grammar; | ||
use Tpetry\QueryExpressions\Concerns\IdentifiesDriver; | ||
use Tpetry\QueryExpressions\Concerns\StringizeExpression; | ||
|
||
class Abs implements Expression | ||
{ | ||
use IdentifiesDriver; | ||
use StringizeExpression; | ||
|
||
public function __construct( | ||
private readonly string|Expression $expression, | ||
) { | ||
} | ||
|
||
public function getValue(Grammar $grammar): string | ||
{ | ||
$expression = $this->stringize($grammar, $this->expression); | ||
|
||
return match ($this->identify($grammar)) { | ||
'mysql', 'sqlite' => "(abs({$expression}))", | ||
'pgsql', 'sqlsrv' => "abs({$expression})", | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Query\Expression; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Tpetry\QueryExpressions\Function\Math\Abs; | ||
|
||
it('can abs a column') | ||
->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))'); |