diff --git a/src/Query/Sql/Exists.php b/src/Query/Sql/Exists.php new file mode 100644 index 0000000..0280494 --- /dev/null +++ b/src/Query/Sql/Exists.php @@ -0,0 +1,34 @@ +select = $select; + } +} diff --git a/src/Query/Sql/Select.php b/src/Query/Sql/Select.php index c50b215..32291f8 100644 --- a/src/Query/Sql/Select.php +++ b/src/Query/Sql/Select.php @@ -630,4 +630,27 @@ public function avg($field) { return $this->column(new Func('avg', $field)); } + + /** + * Do any results of this query exist? + * + * @return bool + */ + public function exists() + { + $existsQuery = new Exists($this); + + // set the current select for the exists query + $existsQuery->setSelect($this); + + // run the callbacks to retirve the results + $result = $existsQuery->executeResultFetcher(); + + if (isset($result[0]['exists'])) + { + return (bool) $result[0]['exists']; + } + + return false; + } } diff --git a/src/Translator/Mysql.php b/src/Translator/Mysql.php index 2e9a3e2..35be27d 100644 --- a/src/Translator/Mysql.php +++ b/src/Translator/Mysql.php @@ -19,6 +19,7 @@ use ClanCats\Hydrahon\Query\Sql\Drop; use ClanCats\Hydrahon\Query\Sql\Truncate; use ClanCats\Hydrahon\Query\Sql\Func; +use ClanCats\Hydrahon\Query\Sql\Exists; class Mysql implements TranslatorInterface { @@ -86,6 +87,10 @@ public function translate(BaseQuery $query) { $queryString = $this->translateTruncate(); } + elseif ($query instanceof Exists) + { + $queryString = $this->translateExists(); + } // everything else is wrong else { @@ -683,4 +688,25 @@ protected function translateTruncate() { return 'truncate table ' . $this->escapeTable() .';'; } + + /** + * Translate the exists querry + * + * @return string + */ + protected function translateExists() + { + $translator = new static; + + // translate the subselect + list($subQuery, $subQueryParameters) = $translator->translate($this->attr('select')); + + // merge the parameters + foreach($subQueryParameters as $parameter) + { + $this->addParameter($parameter); + } + + return 'select exists(' . $subQuery .') as `exists`'; + } } diff --git a/tests/Translator/Mysql.php b/tests/Translator/Mysql.php index 0f8803f..3025c8e 100644 --- a/tests/Translator/Mysql.php +++ b/tests/Translator/Mysql.php @@ -679,4 +679,34 @@ public function testAvg() return $q->table('test')->select()->avg('views'); }); } + + /** + * mysql grammar tests + */ + public function testExists() + { + $this->assertQueryExecution(array(array('exists' => 1)), true, 'select exists(select * from `test`) as `exists`', array(), function($q) + { + return $q->table('test')->select()->exists(); + }); + + // no results + $this->assertQueryExecution(array(array('exists' => 0)), false, 'select exists(select * from `test`) as `exists`', array(), function($q) + { + return $q->table('test')->select()->exists(); + }); + + // with some statements + $this->assertQueryExecution(array(array('exists' => 0)), false, 'select exists(select * from `test` where ( `a` = ? or `c` = ? )) as `exists`', array('b', 'd'), function($q) + { + return $q->table('test') + ->select() + ->where(function( $q ) + { + $q->where('a', 'b'); + $q->orWhere('c', 'd'); + }) + ->exists(); + }); + } } \ No newline at end of file