Skip to content

Commit

Permalink
Merge branch 'developer'
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Aug 25, 2016
2 parents f08ce66 + cb48863 commit 4aab7bf
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Query/Sql/Exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php namespace ClanCats\Hydrahon\Query\Sql;

/**
* Exists query
*
* Allows building queries like "SELECT EXISTS(select * from showtimes) as hasShows"
**
* @package Hydrahon
* @copyright 2015 Mario Döring
*/

use ClanCats\Hydrahon\BaseQuery;

class Exists extends BaseQuery
{
/**
* The select query we want to check if
* any results exists
*
* @var Select
*/
protected $select = null;

/**
* Sets the select query
*
* @param Select $select
* @return void
*/
public function setSelect(Select $select)
{
$this->select = $select;
}
}
23 changes: 23 additions & 0 deletions src/Query/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
26 changes: 26 additions & 0 deletions src/Translator/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -86,6 +87,10 @@ public function translate(BaseQuery $query)
{
$queryString = $this->translateTruncate();
}
elseif ($query instanceof Exists)
{
$queryString = $this->translateExists();
}
// everything else is wrong
else
{
Expand Down Expand Up @@ -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`';
}
}
30 changes: 30 additions & 0 deletions tests/Translator/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}

0 comments on commit 4aab7bf

Please sign in to comment.