Skip to content

Commit

Permalink
Merge pull request #9 from ClanCats/developer
Browse files Browse the repository at this point in the history
Feature: Query flags
  • Loading branch information
mario-deluna committed Feb 23, 2018
2 parents 6b22adc + 94f82a0 commit 29955bd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/BaseQuery.php
Expand Up @@ -18,6 +18,16 @@ class BaseQuery
*/
protected $macros = array();

/**
* Query flags
* These allow you to store data inside the query object.
* This data has no influence on the generated query string or parameters directly.
* But allow you to use the query a state mashine.
*
* @var array
*/
protected $flags = array();

/**
* The callback where we fetch the results from
*
Expand Down Expand Up @@ -48,6 +58,7 @@ final public function __construct(BaseQuery $parent = null)
protected function inheritFromParent(BaseQuery $parent)
{
$this->macros = $parent->macros;
$this->flags = $parent->flags;
$this->resultFetcher = $parent->resultFetcher;
}

Expand All @@ -62,6 +73,34 @@ public function setResultFetcher($resultFetcher = null)
$this->resultFetcher = $resultFetcher;
}

/**
* Set a flag on the query object
*
* @param string $key
* @param mixed $value
* @return void
*/
final public function setFlag($key, $value)
{
$this->flags[$key] = $value;
}

/**
* Gets a flag from the query object
*
* @param string $key
* @param mixed $default
* @return void
*/
final public function getFlag($key, $default = null)
{
if (!isset($this->flags[$key])) {
return $default;
}

return $this->flags[$key];
}

/**
* Register a macro on the current query object
*
Expand Down
41 changes: 41 additions & 0 deletions tests/BaseQueryTest.php
@@ -0,0 +1,41 @@
<?php namespace ClanCats\Hydrahon\Test;
/**
* Hydrahon base query test
**
*
* @package Hydrahon
* @copyright Mario D枚ring
*
* @group Hydrahon
* @group Hydrahon_BaseQuery
*/

use ClanCats\Hydrahon\BaseQuery;
use ClanCats\Hydrahon\Query\Sql\Table;
use ClanCats\Hydrahon\Query\Sql\Select;

class BaseQueryTest extends \PHPUnit_Framework_TestCase
{
public function testFlags()
{
$query = new BaseQuery;

$this->assertNull($query->getFlag('foo'));
$this->assertEquals('bar', $query->getFlag('foo', 'bar'));

$query->setFlag('number', 42);
$this->assertEquals(42, $query->getFlag('number'));
$this->assertEquals(42, $query->getFlag('number', 'nope'));
}

public function testFlagInheritence()
{
$query = new Table;
$query->setFlag('foo', 'bar');

$select = $query->select();
$this->assertInstanceOf("ClanCats\\Hydrahon\\Query\\Sql\\Select", $select);

$this->assertEquals('bar', $select->getFlag('foo'));
}
}
8 changes: 8 additions & 0 deletions tests/Query/Sql/Select.php
Expand Up @@ -162,6 +162,14 @@ public function testJoin()

// outer
$this->assertAttributes($this->createQuery()->outerJoin('avatars', 'users.id', '=', 'avatars.user_id'), array('joins' => array(array('outer', 'avatars', 'users.id', '=', 'avatars.user_id'))));

// join with raw values
$expression = new Expression("`avatars`.`user_id` and `avatars`.`active` = 1");

$this->assertAttributes(
$this->createQuery()->join('avatars', 'users.id', '=', new Expression("`avatars`.`user_id` and `avatars`.`active` = 1")),
array('joins' => array(array('left', 'avatars', 'users.id', '=', $expression)))
);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/Translator/Mysql.php
Expand Up @@ -413,6 +413,12 @@ public function testSelectJoins()
->rightJoin('db1.orders as o', 'u.id', '=', 'o.user_id')
->innerJoin('profiles as p', 'u.id', '=', 'p.user_id');
});

// with raw values
$this->assertQueryTranslation('select * from `db1`.`users` as `u` left join `db1`.`groups` as `g` on `u`.`id` = `g`.`user_id` AND `g`.active = 1', array(), function($q)
{
return $q->table('db1.users as u')->select()->join('db1.groups as g', 'u.id', '=', new Expression('`g`.`user_id` AND `g`.active = 1'));
});
}

/**
Expand Down

0 comments on commit 29955bd

Please sign in to comment.