Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Allow column names to contain any character #249

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions src/Sql/AbstractSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Zend\Db\Adapter\Platform\PlatformInterface;
use Zend\Db\Sql\Platform\PlatformDecoratorInterface;
use Zend\Db\Adapter\Platform\Sql92 as DefaultAdapterPlatform;
use Zend\Db\Exception\RuntimeException;

abstract class AbstractSql implements SqlInterface
{
Expand Down Expand Up @@ -405,9 +406,31 @@ protected function resolveColumnValue(
if ($column === null) {
return 'NULL';
}
return $isIdentifier
? $fromTable . $platform->quoteIdentifierInFragment($column)
: $platform->quoteValue($column);

if ($isIdentifier) {
$matches = [];
preg_match(
'#(?:(?<table>[^\s].*?)\.(?=\S)){0,1}(?:(?<star>\.{0,1}\*(?=\s*$))|(?<column>[^\s].*?)(?:\s*$|(?:\s+as\s+(?<alias>.*(?:\S)))))#i',
$column,
$matches
);
if (!empty($matches['table'])) {
$fromTable = $platform->quoteIdentifier($matches['table']) . $platform->getIdentifierSeparator();
}
if (Select::SQL_STAR === $matches['star']) {
return $fromTable . Select::SQL_STAR;
}
if (array_key_exists('column', $matches) && !empty($matches['column'])) {
$column = $platform->quoteIdentifier($matches['column']);
if (array_key_exists('alias', $matches)) {
$column .= ' AS ' . $platform->quoteIdentifier($matches['alias']);
}
return $fromTable . $column;
}
throw new RuntimeException('Invalid column name');
}

return $platform->quoteValue($column);
}

/**
Expand Down