Skip to content

Commit

Permalink
clean code of queries
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Aug 6, 2015
1 parent 693ebad commit 4acbe68
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 82 deletions.
3 changes: 2 additions & 1 deletion src/Queries/Mysql/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class Count extends BaseQuery
{
use WhereTrait;
use WhereExtendedTrait;
use LimitTrait;

/**
Expand Down Expand Up @@ -62,6 +62,7 @@ public function __toString()
{
$query = "SELECT COUNT(*) FROM `{$this->entity->table}`";

$query .= $this->fromToString();
$query .= $this->whereToString();
$query .= $this->limitToString();

Expand Down
3 changes: 1 addition & 2 deletions src/Queries/Mysql/LimitTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace SimpleCrud\Queries\Mysql;


/**
* Common function to manage LIMIT clause
*/
Expand Down Expand Up @@ -40,7 +39,7 @@ public function offset($offset)

/**
* Generate LIMIT clause
*
*
* @return string
*/
protected function limitToString()
Expand Down
76 changes: 4 additions & 72 deletions src/Queries/Mysql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use SimpleCrud\Queries\BaseQuery;
use SimpleCrud\RowCollection;
use SimpleCrud\Row;
use SimpleCrud\RowInterface;
use SimpleCrud\Entity;
use SimpleCrud\SimpleCrudException;
use PDOStatement;
Expand All @@ -15,75 +14,14 @@
*/
class Select extends BaseQuery
{
use WhereTrait;
use WhereExtendedTrait;
use LimitTrait;

protected $fields = [];
protected $from = [];
protected $leftJoin = [];
protected $orderBy = [];

protected $statement;

/**
* Adds new extra table to the query
*
* @param string $table
*
* @return self
*/
public function from($table)
{
$this->from[] = $table;

return $this;
}

/**
* Adds a WHERE according with the relation of other entity
*
* @param RowInterface $row
* @param string $through
*
* @return self
*/
public function relatedWith(RowInterface $row, $through = null)
{
$entity = $row->getEntity();

if ($through !== null) {
$through = $this->entity->getDb()->$through;

if (!$through->hasOne($entity)) {
throw new SimpleCrudException("The relationship between '{$through->table}' and '{$entity->table}' must be RELATION_HAS_ONE");
}
if (!$through->hasOne($this->entity)) {
throw new SimpleCrudException("The relationship between '{$through->table}' and '{$this->entity->table}' must be RELATION_HAS_ONE");
}

$this->from($through->table);
$this->from($entity->table);

$this->fields[] = "`{$through->table}`.`{$entity->foreignKey}`";

$this->where("`{$through->table}`.`{$this->entity->foreignKey}` = `{$this->entity->table}`.`id`");
$this->where("`{$through->table}`.`{$entity->foreignKey}` = `{$entity->table}`.`id`");
$this->where("`{$entity->table}`.`id` IN (:{$through->name})", [":{$through->name}" => $row->get('id')]);

return $this;
}

if ($this->entity->hasOne($entity)) {
return $this->by($entity->foreignKey, $row->get('id'));
}

if ($this->entity->hasMany($entity)) {
return $this->byId($row->get($this->entity->foreignKey));
}

throw new SimpleCrudException("The tables {$this->entity->table} and {$entity->table} are no related");
}

/**
* Adds an ORDER BY clause
*
Expand Down Expand Up @@ -159,7 +97,7 @@ public function run()

/**
* Run the query and return all values
*
*
* @param boolean $idAsKey
*
* @return RowCollection
Expand Down Expand Up @@ -226,15 +164,9 @@ public function __toString()
$query .= ', '.static::buildFields($join['entity']->table, array_keys($join['entity']->fields), $join['entity']->name);
}

foreach ($this->fields as $field) {
$query .= ', '.$field;
}

$query .= $this->fieldsToString();
$query .= ' FROM `'.$this->entity->table.'`';

if (!empty($this->from)) {
$query .= ', `'.implode('`, `', $this->from).'`';
}
$query .= $this->fromToString();

foreach ($this->leftJoin as $join) {
$query .= ' LEFT JOIN `'.$join['entity']->table.'`"';
Expand Down
5 changes: 3 additions & 2 deletions src/Queries/Mysql/Sum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
/**
* Manages a database select count query in Mysql databases
*/
class Count extends BaseQuery
class Sum extends BaseQuery
{
use WhereTrait;
use WhereExtendedTrait;
use LimitTrait;

protected $field;
Expand Down Expand Up @@ -92,6 +92,7 @@ public function __toString()
{
$query = "SELECT SUM(`{$this->field}`) FROM `{$this->entity->table}`";

$query .= $this->fromToString();
$query .= $this->whereToString();
$query .= $this->limitToString();

Expand Down
108 changes: 108 additions & 0 deletions src/Queries/Mysql/WhereExtendedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
namespace SimpleCrud\Queries\Mysql;

use SimpleCrud\RowInterface;

/**
* Common function to manage WHERE clause
*/
trait WhereExtendedTrait
{
use WhereTrait;

protected $from = [];
protected $fields = [];

/**
* Adds new extra table to the query
*
* @param string $table
*
* @return self
*/
public function from($table)
{
$this->from[] = $table;

return $this;
}

/**
* Adds a new extra field to the query
*
* @param string $field
*
* @return self
*/
public function field($field)
{
$this->fields[] = $field;

return $this;
}

/**
* Adds a WHERE according with the relation of other entity
*
* @param RowInterface $row
* @param string $through
*
* @return self
*/
public function relatedWith(RowInterface $row, $through = null)
{
$entity = $row->getEntity();

if ($through !== null) {
$through = $this->entity->getDb()->$through;

if (!$through->hasOne($entity)) {
throw new SimpleCrudException("The relationship between '{$through->table}' and '{$entity->table}' must be RELATION_HAS_ONE");
}
if (!$through->hasOne($this->entity)) {
throw new SimpleCrudException("The relationship between '{$through->table}' and '{$this->entity->table}' must be RELATION_HAS_ONE");
}

$this->from($through->table);
$this->from($entity->table);

$this->fields[] = "`{$through->table}`.`{$entity->foreignKey}`";

$this->where("`{$through->table}`.`{$this->entity->foreignKey}` = `{$this->entity->table}`.`id`");
$this->where("`{$through->table}`.`{$entity->foreignKey}` = `{$entity->table}`.`id`");
$this->where("`{$entity->table}`.`id` IN (:{$through->name})", [":{$through->name}" => $row->get('id')]);

return $this;
}

if ($this->entity->hasOne($entity)) {
return $this->by($entity->foreignKey, $row->get('id'));
}

if ($this->entity->hasMany($entity)) {
return $this->byId($row->get($this->entity->foreignKey));
}

throw new SimpleCrudException("The tables {$this->entity->table} and {$entity->table} are no related");
}

/**
* add extra fields to the code
*
* @return string
*/
protected function fieldsToString($prepend = ', ')
{
return $this->fields ? $prepend.implode(', ', $this->fields) : '';
}

/**
* add extra fields to the code
*
* @return string
*/
protected function fromToString($prepend = ', ')
{
return $this->from ? $prepend.'`'.implode('`, `', $this->from).'`' : '';
}
}
3 changes: 1 addition & 2 deletions src/Queries/Mysql/WhereTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace SimpleCrud\Queries\Mysql;


/**
* Common function to manage WHERE clause
*/
Expand Down Expand Up @@ -85,7 +84,7 @@ public function byId($id)

/**
* Generate WHERE clause
*
*
* @return string
*/
protected function whereToString()
Expand Down
1 change: 0 additions & 1 deletion src/Queries/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace SimpleCrud\Queries;

use SimpleCrud\Entity;
use SimpleCrud\SimpleCrudException;
use PDOStatement;

/**
Expand Down
3 changes: 2 additions & 1 deletion src/RowCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function __get($name)

/**
* Set whether or not use the id as key
*
*
* @param boolean $idAsKey
*
* @return self
Expand Down Expand Up @@ -135,6 +135,7 @@ public function offsetSet($offset, $value)

if ($this->idAsKey === false) {
$this->rows[] = $value;

return;
}

Expand Down
1 change: 0 additions & 1 deletion tests/RowTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
use SimpleCrud\SimpleCrud;
use SimpleCrud\Entity;

class RowTest extends PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit 4acbe68

Please sign in to comment.