Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends WordPressGrammar from MySqlGrammar #92

Merged
merged 2 commits into from
Oct 12, 2024
Merged
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
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