From f515f036a9b04a9a7173af55e1c5de621289be6e Mon Sep 17 00:00:00 2001 From: metalinspired Date: Sun, 28 May 2017 23:21:36 +0200 Subject: [PATCH 1/4] Allow column names to contain any character In regards to issues #8 and #208 I've created a regex that will allow user to use any character as column name. It will also keep functionality of allowing user to enter 'column as alias' as column name. I've made an assumption that no one in right mind will have spaces on beginning or end of column name so they are ignored by regex. --- src/Sql/AbstractSql.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index 1c5c8b6383..3ab0691557 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -405,9 +405,21 @@ protected function resolveColumnValue( if ($column === null) { return 'NULL'; } - return $isIdentifier - ? $fromTable . $platform->quoteIdentifierInFragment($column) - : $platform->quoteValue($column); + + if ($isIdentifier) { + $matches = []; + preg_match('#(?P[^\s].+?)(?:\s*$|(?:\s+as\s+(?P.*(?:\S))))#i', $column, $matches); + if (array_key_exists('column', $matches)) { + $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); } /** From 78613276870beabcd59e867d7e3a6def01675b69 Mon Sep 17 00:00:00 2001 From: metalinspired Date: Sun, 28 May 2017 23:26:49 +0200 Subject: [PATCH 2/4] Update AbstractSql.php --- src/Sql/AbstractSql.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index 3ab0691557..c41a1d59e4 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -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 { From 3a02d97ce506134b585137f906be281666a4fc48 Mon Sep 17 00:00:00 2001 From: metalinspired Date: Sun, 28 May 2017 23:43:13 +0200 Subject: [PATCH 3/4] Update AbstractSql.php --- src/Sql/AbstractSql.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index c41a1d59e4..63f31b4eb9 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -409,8 +409,15 @@ protected function resolveColumnValue( if ($isIdentifier) { $matches = []; - preg_match('#(?P[^\s].+?)(?:\s*$|(?:\s+as\s+(?P.*(?:\S))))#i', $column, $matches); - if (array_key_exists('column', $matches)) { + preg_match( + '#(?P\.{0,1}\*(?=\s*$))|(?P[^\s].+?)(?:\s*$|(?:\s+as\s+(?P.*(?:\S))))#i', + $column, + $matches + ); + 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']); From 6b03837baa263c3dea183216892ba5283bc00b81 Mon Sep 17 00:00:00 2001 From: metalinspired Date: Mon, 29 May 2017 00:34:44 +0200 Subject: [PATCH 4/4] Update AbstractSql.php --- src/Sql/AbstractSql.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index 63f31b4eb9..a014a32a3d 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -410,10 +410,13 @@ protected function resolveColumnValue( if ($isIdentifier) { $matches = []; preg_match( - '#(?P\.{0,1}\*(?=\s*$))|(?P[^\s].+?)(?:\s*$|(?:\s+as\s+(?P.*(?:\S))))#i', + '#(?:(?[^\s].*?)\.(?=\S)){0,1}(?:(?\.{0,1}\*(?=\s*$))|(?[^\s].*?)(?:\s*$|(?:\s+as\s+(?.*(?:\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; }