File tree Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Original file line number Diff line number Diff line change @@ -253,6 +253,15 @@ BlogArticle::select([
253
253
->get();
254
254
```
255
255
256
+ #### Math
257
+ ``` php
258
+ use Tpetry\QueryExpressions\Function\Math\{
259
+ Abs,
260
+ };
261
+
262
+ new Abs(string|Expression $expression);
263
+ ```
264
+
256
265
#### String
257
266
``` php
258
267
use Tpetry\QueryExpressions\Function\String\{
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Tpetry \QueryExpressions \Function \Math ;
6
+
7
+ use Illuminate \Contracts \Database \Query \Expression ;
8
+ use Illuminate \Database \Grammar ;
9
+ use Tpetry \QueryExpressions \Concerns \IdentifiesDriver ;
10
+ use Tpetry \QueryExpressions \Concerns \StringizeExpression ;
11
+
12
+ class Abs implements Expression
13
+ {
14
+ use IdentifiesDriver;
15
+ use StringizeExpression;
16
+
17
+ public function __construct (
18
+ private readonly string |Expression $ expression ,
19
+ ) {
20
+ }
21
+
22
+ public function getValue (Grammar $ grammar ): string
23
+ {
24
+ $ expression = $ this ->stringize ($ grammar , $ this ->expression );
25
+
26
+ return match ($ this ->identify ($ grammar )) {
27
+ 'mysql ' , 'sqlite ' => "(abs( {$ expression })) " ,
28
+ 'pgsql ' , 'sqlsrv ' => "abs( {$ expression }) " ,
29
+ };
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ use Illuminate \Database \Query \Expression ;
6
+ use Illuminate \Database \Schema \Blueprint ;
7
+ use Tpetry \QueryExpressions \Function \Math \Abs ;
8
+
9
+ it ('can abs a column ' )
10
+ ->expect (new Abs ('val ' ))
11
+ ->toBeExecutable (function (Blueprint $ table ) {
12
+ $ table ->integer ('val ' );
13
+ })
14
+ ->toBeMysql ('(abs(`val`)) ' )
15
+ ->toBePgsql ('abs("val") ' )
16
+ ->toBeSqlite ('(abs("val")) ' )
17
+ ->toBeSqlsrv ('abs([val]) ' );
18
+
19
+ it ('can abs an expression ' )
20
+ ->expect (new Abs (new Expression ('sum(1) ' )))
21
+ ->toBeExecutable ()
22
+ ->toBeMysql ('(abs(sum(1))) ' )
23
+ ->toBePgsql ('abs(sum(1)) ' )
24
+ ->toBeSqlite ('(abs(sum(1))) ' )
25
+ ->toBeSqlsrv ('abs(sum(1)) ' );
You can’t perform that action at this time.
0 commit comments