From db69fabcd2318f40aeb275822a94c7af5668cd53 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 29 Jan 2017 19:08:35 +0100 Subject: [PATCH] Bad implementation for count --- DataStorage/Database/GrammarAbstract.php | 24 +++++++++++++++++++++++- DataStorage/Database/Query/Builder.php | 4 +++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/DataStorage/Database/GrammarAbstract.php b/DataStorage/Database/GrammarAbstract.php index 4f73360bf..b8c9676c2 100644 --- a/DataStorage/Database/GrammarAbstract.php +++ b/DataStorage/Database/GrammarAbstract.php @@ -17,6 +17,8 @@ declare(strict_types=1); namespace phpOMS\DataStorage\Database; +use phpOMS\Utils\StringUtils; + /** * Grammar. * @@ -78,6 +80,16 @@ abstract class GrammarAbstract */ protected $tablePrefix = ''; + /** + * Special keywords. + * + * @var string + * @since 1.0.0 + */ + protected $specialKeywords = [ + 'COUNT(' + ]; + /** * Compile to query. * @@ -198,6 +210,16 @@ abstract class GrammarAbstract */ protected function compileSystem($system, string $prefix = '') : string { + // todo: this is a bad way to handle select count(*) which doesn't need a prefix. Maybe remove prefixes in total? + $identifier = $this->systemIdentifier; + + foreach($this->specialKeywords as $keyword) { + if(StringUtils::startsWith($system, $keyword)) { + $prefix = ''; + $identifier = ''; + } + } + // todo: move remaining * test also here not just if .* but also if * (should be done in else?) if (count($split = explode('.', $system)) == 2) { if ($split[1] === '*') { @@ -208,7 +230,7 @@ abstract class GrammarAbstract return $this->compileSystem($prefix . $split[0]) . '.' . $system; } else { - return $this->systemIdentifier . $prefix . $system . $this->systemIdentifier; + return $identifier . $prefix . $system . $identifier; } } diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 6f8962f5a..ce07ca2d7 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -752,8 +752,10 @@ class Builder extends BuilderAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public function count() + public function count(string $table = '*') { + // todo: don't do this as string, create new object new Count(); this can get handled by the grammar parser WAY better + return $this->select('COUNT(' . $table . ')'); } /**