Skip to content

Commit 3ba2ec9

Browse files
gdebrauwertpetry
andcommitted
lower and upper (resolves tpetry#12)
Co-authored-by: tpetry <[email protected]>
1 parent 31d9e69 commit 3ba2ec9

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,12 @@ BlogArticle::select([
256256
#### String
257257
```php
258258
use Tpetry\QueryExpressions\Function\String\{
259-
Concat, Uuid4
259+
Concat, Lower, Upper, Uuid4
260260
};
261261

262262
new Concat(array $expressions);
263+
new Lower(string|Expression $expression);
264+
new Upper(string|Expression $expression);
263265
new Uuid4();
264266

265267
Schema::table('users', function (Blueprint $table): void {

src/Function/String/Lower.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tpetry\QueryExpressions\Function\String;
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 Lower 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' => "(lower({$expression}))",
28+
'pgsql', 'sqlsrv' => "lower({$expression})",
29+
};
30+
}
31+
}

src/Function/String/Upper.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tpetry\QueryExpressions\Function\String;
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 Upper 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' => "(upper({$expression}))",
28+
'pgsql', 'sqlsrv' => "upper({$expression})",
29+
};
30+
}
31+
}

tests/Function/String/LowerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Query\Expression;
6+
use Tpetry\QueryExpressions\Function\String\Lower;
7+
8+
it('can lowercase a column')
9+
->expect(new Lower('val'))
10+
->toBeExecutable(['val varchar(255)'])
11+
->toBeMysql('(lower(`val`))')
12+
->toBePgsql('lower("val")')
13+
->toBeSqlite('(lower("val"))')
14+
->toBeSqlsrv('lower([val])');
15+
16+
it('can lowercase an expression')
17+
->expect(new Lower(new Expression("'foo'")))
18+
->toBeExecutable()
19+
->toBeMysql("(lower('foo'))")
20+
->toBePgsql("lower('foo')")
21+
->toBeSqlite("(lower('foo'))")
22+
->toBeSqlsrv("lower('foo')");

tests/Function/String/UpperTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Query\Expression;
6+
use Tpetry\QueryExpressions\Function\String\Upper;
7+
8+
it('can uppercase a column')
9+
->expect(new Upper('val'))
10+
->toBeExecutable(['val varchar(255)'])
11+
->toBeMysql('(upper(`val`))')
12+
->toBePgsql('upper("val")')
13+
->toBeSqlite('(upper("val"))')
14+
->toBeSqlsrv('upper([val])');
15+
16+
it('can uppercase an expression')
17+
->expect(new Upper(new Expression("'foo'")))
18+
->toBeExecutable()
19+
->toBeMysql("(upper('foo'))")
20+
->toBePgsql("upper('foo')")
21+
->toBeSqlite("(upper('foo'))")
22+
->toBeSqlsrv("upper('foo')");

0 commit comments

Comments
 (0)