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
27 changes: 23 additions & 4 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function array_key_exists;
use function array_keys;
use function array_unshift;
use function array_map;

Check failure on line 20 in src/Query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Use statements should be sorted alphabetically. The first wrong one is array_map.
use function count;
use function func_get_arg;
use function func_get_args;
Expand Down Expand Up @@ -82,6 +83,7 @@
'orderBy' => [],
'values' => [],
'for_update' => null,
'comment' => [],
];

/**
Expand Down Expand Up @@ -411,21 +413,23 @@
return $this->sql;
}

$sql = $this->getComments();

switch ($this->type) {
case self::INSERT:
$sql = $this->getSQLForInsert();
$sql .= $this->getSQLForInsert();
break;

case self::DELETE:
$sql = $this->getSQLForDelete();
$sql .= $this->getSQLForDelete();
break;

case self::UPDATE:
$sql = $this->getSQLForUpdate();
$sql .= $this->getSQLForUpdate();
break;

case self::SELECT:
$sql = $this->getSQLForSelect();
$sql .= $this->getSQLForSelect();
break;
}

Expand Down Expand Up @@ -636,6 +640,7 @@
|| $sqlPartName === 'groupBy'
|| $sqlPartName === 'select'
|| $sqlPartName === 'set'
|| $sqlPartName === 'comment'
) {
foreach ($sqlPart as $part) {
$this->sqlParts[$sqlPartName][] = $part;
Expand Down Expand Up @@ -1756,4 +1761,18 @@

return $this;
}

public function withComment(string $comment): self
{
$this->add('comment', $comment, true);

return $this;
}

private function getComments(): string
{
return implode('', array_map(function ($comment) {

Check failure on line 1774 in src/Query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Closure not using "$this" should be declared static.
return sprintf('/* %s */ ', $comment);

Check failure on line 1775 in src/Query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Function sprintf() should not be referenced via a fallback global name, but via a use statement.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all database engines support this syntax for comments?

}, $this->sqlParts['comment']));
}
}
55 changes: 55 additions & 0 deletions tests/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,29 @@ public function testSelectMultipleFrom(): void
self::assertEquals('SELECT u.*, p.* FROM users u, phonenumbers p', (string) $qb);
}

public function testSelectWithComment(): void
{
$qb = new QueryBuilder($this->conn);

$qb->select('u.id')
->from('users', 'u')
->withComment('Test comment');

self::assertEquals('/* Test comment */ SELECT u.id FROM users u', (string) $qb);
}

public function testSelectWithCommentMultiple(): void
{
$qb = new QueryBuilder($this->conn);

$qb->select('u.id')
->from('users', 'u')
->withComment('Test comment')
->withComment('Second comment');

self::assertEquals('/* Test comment */ /* Second comment */ SELECT u.id FROM users u', (string) $qb);
}

public function testUpdate(): void
{
$qb = new QueryBuilder($this->conn);
Expand All @@ -432,6 +455,18 @@ public function testUpdate(): void
self::assertEquals('UPDATE users u SET u.foo = ?, u.bar = ?', (string) $qb);
}

public function testUpdateWithComment(): void
{
$qb = new QueryBuilder($this->conn);
$qb->update('users', 'u')
->set('u.foo', '?')
->set('u.bar', '?')
->withComment('Test comment');

self::assertEquals(QueryBuilder::UPDATE, $qb->getType());
self::assertEquals('/* Test comment */ UPDATE users u SET u.foo = ?, u.bar = ?', (string) $qb);
}

public function testUpdateWithoutAlias(): void
{
$qb = new QueryBuilder($this->conn);
Expand Down Expand Up @@ -470,6 +505,16 @@ public function testDelete(): void
self::assertEquals('DELETE FROM users u', (string) $qb);
}

public function testDeleteWithComment(): void
{
$qb = new QueryBuilder($this->conn);
$qb->delete('users', 'u')
->withComment('Test comment');

self::assertEquals(QueryBuilder::DELETE, $qb->getType());
self::assertEquals('/* Test comment */ DELETE FROM users u', (string) $qb);
}

public function testDeleteWithoutAlias(): void
{
$qb = new QueryBuilder($this->conn);
Expand Down Expand Up @@ -512,6 +557,16 @@ public function testInsertValues(): void
self::assertEquals('INSERT INTO users (foo, bar) VALUES(?, ?)', (string) $qb);
}

public function testInsertWithComment(): void
{
$qb = new QueryBuilder($this->conn);
$qb->insert('users')
->withComment('Test comment');

self::assertEquals(QueryBuilder::INSERT, $qb->getType());
self::assertEquals('/* Test comment */ INSERT INTO users () VALUES()', (string) $qb);
}

public function testInsertReplaceValues(): void
{
$qb = new QueryBuilder($this->conn);
Expand Down
Loading