Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Enh #477, #478: Improve performance of `ArrayParser::parse()` method (@Tigrov)
- Enh #478: Improve performance of `StructuredParser::parse()` method (@Tigrov)
- Bug #481: Fix quoting array column names(@Tigrov)
Comment thread
Tigrov marked this conversation as resolved.
Outdated
- Enh #479: Explicitly import functions and constants in "use" section (@mspirkov)
- Enh #480: Remove unnecessary files from Composer package (@mspirkov)

Expand Down
1 change: 0 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Db\Pgsql\Column\ColumnFactory;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\Quoter;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Transaction\TransactionInterface;
Expand Down
42 changes: 42 additions & 0 deletions src/Quoter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql;

use function str_contains;
use function strpos;
use function strrpos;
use function substr;

final class Quoter extends \Yiisoft\Db\Schema\Quoter
{
public function quoteColumnName(string $name): string
{
if (str_contains($name, '(') || str_contains($name, '[[')) {
return $name;
}

$dotPos = strrpos($name, '.');
if ($dotPos !== false) {
$prefix = $this->quoteTableName(substr($name, 0, $dotPos)) . '.';
$name = substr($name, $dotPos + 1);
} else {
$prefix = '';
}

if (str_contains($name, '{{')) {
return $name;
}

$bracketPos = strpos($name, '[');
if ($bracketPos !== false) {
$suffix = substr($name, $bracketPos);
$name = substr($name, 0, $bracketPos);
} else {
$suffix = '';
}

return $prefix . $this->quoteSimpleColumnName($name) . $suffix;
}
}
12 changes: 12 additions & 0 deletions tests/Provider/QuoterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

final class QuoterProvider extends \Yiisoft\Db\Tests\Provider\QuoterProvider
{
public static function columnNames(): array
{
return [
...parent::columnNames(),
['array_col[1]', '"array_col"[1]'],
['multi_array_col[1][2]', '"multi_array_col"[1][2]'],
['table_name.array_col[1]', '"table_name"."array_col"[1]'],
['[[array_col]][1]', '[[array_col]][1]'],
['(array_col[1])', '(array_col[1])'],
];
}

public static function tableNameParts(): array
{
return [
Expand Down
6 changes: 6 additions & 0 deletions tests/QuoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ final class QuoterTest extends CommonQuoterTest
{
use IntegrationTestTrait;

#[DataProviderExternal(QuoterProvider::class, 'columnNames')]
public function testQuoteColumnName(string $columnName, string $expected): void
{
parent::testQuoteColumnName($columnName, $expected);
}

#[DataProviderExternal(QuoterProvider::class, 'tableNameParts')]
public function testGetTableNameParts(string $tableName, array $expected): void
{
Expand Down
Loading