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 Jun 3, 2016
2 parents 808da50 + f118e8d commit 01e8bb9
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 122 deletions.
72 changes: 45 additions & 27 deletions src/BaseQuery.php
Expand Up @@ -11,20 +11,6 @@

class BaseQuery
{
/**
* The database the query should be executed on
*
* @var string
*/
protected $database = null;

/**
* The table the query should be executed on
*
* @var string
*/
protected $table = null;

/**
* Query builder callback macros
*
Expand All @@ -37,21 +23,43 @@ class BaseQuery
*
* @var callable
*/
private $resultFetcher = null;
protected $resultFetcher = null;

/**
* Construct new query object and inherit properties
*
* @param BaseQuery $parent
* @return void
*/
final public function __construct(BaseQuery $parent = null)
{
if (!is_null($parent))
{
$this->inheritFromParent($parent);
}
}

/**
* Inherit property values from parent query
*
* @param BaseQuery $parent
* @return void
*/
protected function inheritFromParent(BaseQuery $parent)
{
$this->macros = $parent->macros;
$this->resultFetcher = $parent->resultFetcher;
}

/**
* Construct new query object
* Set the result fetcher of the query
*
* @param callable $resultFetcher
* @param string $table
* @param string $database
* @return void
*/
public function __construct($resultFetcher = null, $table = null, $database = null)
public function setResultFetcher($resultFetcher = null)
{
$this->resultFetcher = $resultFetcher;
$this->table = $table;
$this->database = $database;
}

/**
Expand Down Expand Up @@ -132,14 +140,24 @@ final public function attributes()
}

/**
* Creates another query instance with the current parameters
*
* @param string $className
* @return ClanCats\Hydrahon\BaseQuery
* Overwrite the query attributes
*
* Jesuz only use this if you really really know what your are doing
* otherwise you might break stuff add sql injection and all other bad stuff..
*
* @return array
*/
final protected function createSubQuery($className)
final public function overwriteAttributes($attributes)
{
return new $className($this->resultFetcher, $this->table, $this->database);
foreach($attributes as $key => $attribute)
{
if (isset($this->{$key}))
{
$this->{$key} = $attribute;
}
}

return $attributes;
}

/**
Expand Down
60 changes: 28 additions & 32 deletions src/Builder.php
Expand Up @@ -100,62 +100,58 @@ public function __construct($grammarKey, $executionCallback)
$this->executionCallback = $executionCallback;

// prepare the current grammar
list($this->queryClass, $translatorClass) = static::$grammar[$grammarKey];
list($queryBuilderClass, $translatorClass) = static::$grammar[$grammarKey];

// create the query builder specific instances
$this->queryTranslator = new $translatorClass;
$this->queryBuilder = new $queryBuilderClass;

// assign the result fetcher
$this->queryBuilder->setResultFetcher(array($this, 'executeQuery'));

// check if the translator is valid
if (!$this->queryTranslator instanceof TranslatorInterface)
{
throw new Exception('A query translator must implement the "TranslatorInterface" interface.');
}

// check if the query builder is an instance of Base Query
if (!$this->queryBuilder instanceof BaseQuery)
{
throw new Exception('A query builder must be an instance of the "BaseQuery".');
}
}

/**
* Creates a new query object with the given table and database and
* sets the query table and optinal the database seperated by a dott
* Forwards calls to the current query builder
*
* @param string $table
* @return ClanCats\Hydrahon\BaseQuery
*/
public function table($table)
public function __call($method, $arguments)
{
$database = null;

if (is_string($table) && strpos($table, '.') !== false)
{
$selection = explode('.', $table);

if (count($selection) !== 2)
{
throw new Exception( 'Invalid argument given. You can only define one seperator.' );
}

list($database, $table) = $selection;
}

// the table might include an alias we need to parse that one out
if (is_string($table) && strpos($table, ' as ') !== false)
{
$tableParts = explode(' as ', $table);
$table = array($tableParts[0] => $tableParts[1]);
unset($tableParts);
}
return call_user_func_array(array($this->queryBuilder, $method), $arguments);
}

// create and return new query instance
return new $this->queryClass(array($this, 'executeQuery'), $table, $database);
/**
* Translate the given query
*
* @param BaseQuery $query
* @return array
*/
public function translateQuery(BaseQuery $query)
{
return $this->queryTranslator->translate($query);
}

/**
* Translate a query and run the current execution callback
*
* @param ClanCats\Hydrahon\BaseQuery $query
* @param BaseQuery $query
* @return mixed
*/
public function executeQuery(BaseQuery $query)
{
$translatedArguments = $this->queryTranslator->translate($query);
$translatedArguments = array_merge(array($query), $translatedArguments);

return call_user_func_array($this->executionCallback, $translatedArguments);
return call_user_func_array($this->executionCallback, array_merge(array($query), $this->translateQuery($query)));
}
}
69 changes: 45 additions & 24 deletions src/Query/Sql.php
Expand Up @@ -9,80 +9,101 @@

use ClanCats\Hydrahon\BaseQuery;

use ClanCats\Hydrahon\Query\Sql\Select;
use ClanCats\Hydrahon\Query\Sql\Insert;
use ClanCats\Hydrahon\Query\Sql\Update;
use ClanCats\Hydrahon\Query\Sql\Delete;
use ClanCats\Hydrahon\Query\Sql\Drop;
use ClanCats\Hydrahon\Query\Sql\Truncate;
use ClanCats\Hydrahon\Query\Sql\Table;

class Sql extends BaseQuery
{
/**
* Create a new table instance
*
* $h->table('users')
*
* @param string|array $fields
* @return Select
*/
public function table($table = null, $alias = null)
{
$query = new Table($this); return $query->table($table, $alias);
}

/**
* Create a new select query builder
*
* $h->table('users')->select(['name', 'age'])
* $h->select('users', ['name', 'age'])
*
* @param string|array $fields
* @return ClanCats\Hydrahon\Query\Sql\Select
* @return Select
*/
public function select($fields = null)
public function select($table = null, $fields = null)
{
return $this->createSubQuery(__NAMESPACE__ . '\\Sql\\Select')->fields($fields);
return $this->table($table)->select($fields);
}

/**
* Create a new insert query builder
*
* $h->table('users')->insert(['name' => 'Lucas', 'age' => 21])
* $h->insert('users', ['name' => 'Lucas', 'age' => 21])
*
* @param array $values
* @return ClanCats\Hydrahon\Query\Sql\Insert
* @return Insert
*/
public function insert(array $values = array())
public function insert($table = null, array $values = array())
{
return $this->createSubQuery(__NAMESPACE__ . '\\Sql\\Insert')->values($values);
return $this->table($table)->insert($values);
}

/**
* Create a new update query builder
*
* $h->table('users')->update(['age' => 25])->where('name', 'Johanna')
* $h->update('users', ['age' => 25])->where('name', 'Johanna')
*
* @param array $values
* @return ClanCats\Hydrahon\Query\Sql\Update
* @return Update
*/
public function update(array $values = array())
public function update($table = null, array $values = array())
{
return $this->createSubQuery(__NAMESPACE__ . '\\Sql\\Update')->set($values);
return $this->table($table)->update($values);
}

/**
* Create a new delete sql builder
*
* $h->table('users')->delete()->where('age', '<', '18')
* $h->delete('users')->where('age', '<', '18')
*
* @return ClanCats\Hydrahon\Query\Sql\Delete
* @return Delete
*/
public function delete()
public function delete($table = null)
{
return $this->createSubQuery(__NAMESPACE__ . '\\Sql\\Delete');
return $this->table($table)->delete();
}

/**
* Create a new drop table query
*
* $h->table('users')->drop()
* $h->drop('users')
*
* @return ClanCats\Hydrahon\Query\Sql\Drop
* @return Drop
*/
public function drop()
public function drop($table = null)
{
return $this->createSubQuery(__NAMESPACE__ . '\\Sql\\Drop');
return $this->table($table)->drop();
}

/**
* Create a new truncate table query
*
* $h->table('users')->truncate()
* $h->truncate('users')
*
* @return ClanCats\Hydrahon\Query\Sql\Truncate
* @return Truncate
*/
public function truncate()
public function truncate($table = null)
{
return $this->createSubQuery(__NAMESPACE__ . '\\Sql\\Truncate');
return $this->table($table)->truncate();
}
}

0 comments on commit 01e8bb9

Please sign in to comment.