Bad implementation for count

This commit is contained in:
Dennis Eichhorn 2017-01-29 19:08:35 +01:00
parent 7faae589c3
commit db69fabcd2
2 changed files with 26 additions and 2 deletions

View File

@ -17,6 +17,8 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database; namespace phpOMS\DataStorage\Database;
use phpOMS\Utils\StringUtils;
/** /**
* Grammar. * Grammar.
* *
@ -78,6 +80,16 @@ abstract class GrammarAbstract
*/ */
protected $tablePrefix = ''; protected $tablePrefix = '';
/**
* Special keywords.
*
* @var string
* @since 1.0.0
*/
protected $specialKeywords = [
'COUNT('
];
/** /**
* Compile to query. * Compile to query.
* *
@ -198,6 +210,16 @@ abstract class GrammarAbstract
*/ */
protected function compileSystem($system, string $prefix = '') : string 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?) // todo: move remaining * test also here not just if .* but also if * (should be done in else?)
if (count($split = explode('.', $system)) == 2) { if (count($split = explode('.', $system)) == 2) {
if ($split[1] === '*') { if ($split[1] === '*') {
@ -208,7 +230,7 @@ abstract class GrammarAbstract
return $this->compileSystem($prefix . $split[0]) . '.' . $system; return $this->compileSystem($prefix . $split[0]) . '.' . $system;
} else { } else {
return $this->systemIdentifier . $prefix . $system . $this->systemIdentifier; return $identifier . $prefix . $system . $identifier;
} }
} }

View File

@ -752,8 +752,10 @@ class Builder extends BuilderAbstract
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
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 . ')');
} }
/** /**