Skip to content

Commit da724f8

Browse files
authored
Fix quoting array column name (#481)
1 parent ea891ce commit da724f8

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Enh #477, #478: Improve performance of `ArrayParser::parse()` method (@Tigrov)
66
- Enh #478: Improve performance of `StructuredParser::parse()` method (@Tigrov)
7+
- Bug #481: Fix quoting array column names (@Tigrov)
78
- Enh #479: Explicitly import functions and constants in "use" section (@mspirkov)
89
- Enh #480: Remove unnecessary files from Composer package (@mspirkov)
910

src/Connection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Yiisoft\Db\Pgsql\Column\ColumnFactory;
1212
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
1313
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
14-
use Yiisoft\Db\Schema\Quoter;
1514
use Yiisoft\Db\Schema\QuoterInterface;
1615
use Yiisoft\Db\Schema\SchemaInterface;
1716
use Yiisoft\Db\Transaction\TransactionInterface;

src/Quoter.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Pgsql;
6+
7+
use function str_contains;
8+
use function strpos;
9+
use function strrpos;
10+
use function substr;
11+
12+
final class Quoter extends \Yiisoft\Db\Schema\Quoter
13+
{
14+
public function quoteColumnName(string $name): string
15+
{
16+
if (str_contains($name, '(') || str_contains($name, '[[')) {
17+
return $name;
18+
}
19+
20+
$dotPos = strrpos($name, '.');
21+
if ($dotPos !== false) {
22+
$prefix = $this->quoteTableName(substr($name, 0, $dotPos)) . '.';
23+
$name = substr($name, $dotPos + 1);
24+
} else {
25+
$prefix = '';
26+
}
27+
28+
if (str_contains($name, '{{')) {
29+
return $name;
30+
}
31+
32+
$bracketPos = strpos($name, '[');
33+
if ($bracketPos !== false) {
34+
$suffix = substr($name, $bracketPos);
35+
$name = substr($name, 0, $bracketPos);
36+
} else {
37+
$suffix = '';
38+
}
39+
40+
return $prefix . $this->quoteSimpleColumnName($name) . $suffix;
41+
}
42+
}

tests/Provider/QuoterProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
final class QuoterProvider extends \Yiisoft\Db\Tests\Provider\QuoterProvider
88
{
9+
public static function columnNames(): array
10+
{
11+
return [
12+
...parent::columnNames(),
13+
['array_col[1]', '"array_col"[1]'],
14+
['multi_array_col[1][2]', '"multi_array_col"[1][2]'],
15+
['table_name.array_col[1]', '"table_name"."array_col"[1]'],
16+
['[[array_col]][1]', '[[array_col]][1]'],
17+
['(array_col[1])', '(array_col[1])'],
18+
];
19+
}
20+
921
public static function tableNameParts(): array
1022
{
1123
return [

tests/QuoterTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ final class QuoterTest extends CommonQuoterTest
1616
{
1717
use IntegrationTestTrait;
1818

19+
#[DataProviderExternal(QuoterProvider::class, 'columnNames')]
20+
public function testQuoteColumnName(string $columnName, string $expected): void
21+
{
22+
parent::testQuoteColumnName($columnName, $expected);
23+
}
24+
1925
#[DataProviderExternal(QuoterProvider::class, 'tableNameParts')]
2026
public function testGetTableNameParts(string $tableName, array $expected): void
2127
{

0 commit comments

Comments
 (0)