From ae7861344388d986d3b95e3179c9919cae7cbb3c Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 3 Jun 2015 14:13:15 +0200 Subject: [PATCH] lib: Add sub query mode to the DbQuery refs #9009 --- library/Icinga/Data/Db/DbQuery.php | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Data/Db/DbQuery.php b/library/Icinga/Data/Db/DbQuery.php index 4fa7f132d..42937147b 100644 --- a/library/Icinga/Data/Db/DbQuery.php +++ b/library/Icinga/Data/Db/DbQuery.php @@ -23,6 +23,15 @@ class DbQuery extends SimpleQuery */ protected $db; + /** + * Whether or not the query is a sub query + * + * Sub queries are automatically wrapped in parentheses + * + * @var bool + */ + protected $isSubQuery = false; + /** * Select query * @@ -71,6 +80,27 @@ class DbQuery extends SimpleQuery parent::init(); } + /** + * Get whether or not the query is a sub query + */ + public function getIsSubQuery() + { + return $this->isSubQuery; + } + + /** + * Set whether or not the query is a sub query + * + * @param bool $isSubQuery + * + * @return $this + */ + public function setIsSubQuery($isSubQuery = true) + { + $this->isSubQuery = (bool) $isSubQuery; + return $this; + } + public function setUseSubqueryCount($useSubqueryCount = true) { $this->useSubqueryCount = $useSubqueryCount; @@ -331,7 +361,8 @@ class DbQuery extends SimpleQuery */ public function __toString() { - return (string) $this->getSelectQuery(); + $select = (string) $this->getSelectQuery(); + return $this->getIsSubQuery() ? ('(' . $select . ')') : $select; } /**