Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Function/String/Concat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tpetry\QueryExpressions\Function\String;

use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Grammar;
use Tpetry\QueryExpressions\Concerns\IdentifiesDriver;
use Tpetry\QueryExpressions\Function\Conditional\ManyArgumentsExpression;

class Concat extends ManyArgumentsExpression implements Expression
{
use IdentifiesDriver;

public function getValue(Grammar $grammar)
{
$expressions = $this->getExpressions($grammar);
$expressionsStr = implode(', ', $expressions);

return match ($this->identify($grammar)) {
'mysql', 'pgsql', 'sqlsrv' => "concat({$expressionsStr})",
'sqlite' => implode(' || ', $expressions),
};
}
}
13 changes: 13 additions & 0 deletions tests/Function/String/ConcatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Tpetry\QueryExpressions\Function\String\Concat;

it('can concatenate string values')
->expect(new Concat(['Hello', ' ', 'World']))
->toBeExecutable()
->toBeMysql('concat(`Hello`, ` `, `World`)')
->toBePgsql('concat("Hello", " ", "World")')
->toBeSqlite('"Hello" || " " || "World"')
->toBeSqlsrv('concat([Hello], [ ], [World])');