Skip to content

Commit

Permalink
Merge pull request #92 from dimitriBouteille/v4/update-query-grammars
Browse files Browse the repository at this point in the history
Extends WordPressGrammar from MySqlGrammar
  • Loading branch information
dimitriBouteille authored Oct 12, 2024
2 parents 7beb917 + 1a2eb34 commit 91c289e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 47 deletions.
36 changes: 1 addition & 35 deletions src/Orm/Query/Grammars/WordPressGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,8 @@

namespace Dbout\WpOrm\Orm\Query\Grammars;

use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Grammars\MySqlGrammar;

/**
* @todo Extend from MySqlGrammar next major version
* @see MySqlGrammar
*/
class WordPressGrammar extends Grammar
class WordPressGrammar extends MySqlGrammar
{
/**
* @inheritDoc
*/
public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update): string
{
// @phpstan-ignore-next-line
$useUpsertAlias = $query->connection->getConfig('use_upsert_alias');

$sql = $this->compileInsert($query, $values);

if ($useUpsertAlias) {
$sql .= ' as laravel_upsert_alias';
}

$sql .= ' on duplicate key update ';

$columns = collect($update)->map(function ($value, $key) use ($useUpsertAlias) {
if (! is_numeric($key)) {
return $this->wrap($key).' = '.$this->parameter($value);
}

return $useUpsertAlias
? $this->wrap($value).' = '.$this->wrap('laravel_upsert_alias').'.'.$this->wrap($value)
: $this->wrap($value).' = values('.$this->wrap($value).')';
})->implode(', ');

return $sql.$columns;
}
}
21 changes: 9 additions & 12 deletions tests/WordPress/Orm/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
use Dbout\WpOrm\Orm\Database;
use Dbout\WpOrm\Tests\WordPress\TestCase;

/**
* @coversDefaultClass \Dbout\WpOrm\Orm\Database
*/
class DatabaseTest extends TestCase
{
private Database $database;
Expand All @@ -29,7 +26,7 @@ public function setUp(): void

/**
* @return void
* @covers ::getTablePrefix
* @covers Database::getTablePrefix
*/
public function testGetTablePrefix(): void
{
Expand All @@ -39,8 +36,8 @@ public function testGetTablePrefix(): void

/**
* @return void
* @covers ::getDatabaseName
* @covers ::getConfig
* @covers Database::getDatabaseName
* @covers Database::getConfig
*/
public function testGetDatabaseName(): void
{
Expand All @@ -49,8 +46,8 @@ public function testGetDatabaseName(): void

/**
* @return void
* @covers ::getName
* @covers ::getConfig
* @covers Database::getName
* @covers Database::getConfig
*/
public function testGetName(): void
{
Expand All @@ -62,7 +59,7 @@ public function testGetName(): void
* @param string|null $alias
* @param string $expectedQuery
* @return void
* @covers ::table
* @covers Database::table
* @dataProvider providerTestTable
*/
public function testTable(string $table, ?string $alias, string $expectedQuery): void
Expand All @@ -79,19 +76,19 @@ protected function providerTestTable(): \Generator
yield 'Without alias' => [
'options',
null,
sprintf('select * from "%s"', $this->getTable('options')),
sprintf('select * from `%s`', $this->getTable('options')),
];

yield 'With alias' => [
'options',
'opts',
sprintf('select * from "%s" as "opts"', $this->getTable('options')),
sprintf('select * from `%s` as `opts`', $this->getTable('options')),
];
}

/**
* @return void
* @covers ::lastInsertId
* @covers Database::lastInsertId
*/
public function testLastInsertId(): void
{
Expand Down

0 comments on commit 91c289e

Please sign in to comment.