Skip to content

Commit

Permalink
new Sum query
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Aug 6, 2015
1 parent 58aa13d commit 693ebad
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 258 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: php
sudo: false

php:
- 5.6
Expand Down
48 changes: 3 additions & 45 deletions src/Queries/Mysql/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,7 @@
class Count extends BaseQuery
{
use WhereTrait;

protected $limit;

/**
* @see QueryInterface
*
* $entity->count($where, $marks, $limit)
*
* {@inheritdoc}
*/
public static function execute(Entity $entity, array $args)
{
$count = self::getInstance($entity);

if (isset($args[0])) {
$count->where($args[0], isset($args[1]) ? $args[1] : null);
}

if (isset($args[2])) {
$count->limit($args[2]);
}

return $count->get();
}

/**
* Adds a LIMIT clause
*
* @param integer $limit
*
* @return self
*/
public function limit($limit)
{
$this->limit = $limit;

return $this;
}
use LimitTrait;

/**
* Adds new marks to the query
Expand Down Expand Up @@ -99,13 +62,8 @@ public function __toString()
{
$query = "SELECT COUNT(*) FROM `{$this->entity->table}`";

if (!empty($this->where)) {
$query .= ' WHERE ('.implode(') AND (', $this->where).')';
}

if (!empty($this->limit)) {
$query .= ' LIMIT '.$this->limit;
}
$query .= $this->whereToString();
$query .= $this->limitToString();

return $query;
}
Expand Down
69 changes: 3 additions & 66 deletions src/Queries/Mysql/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,7 @@
class Delete extends BaseQuery
{
use WhereTrait;

protected $limit;
protected $offset;

/**
* @see QueryInterface
*
* $entity->delete($where, $marks, $limit)
*
* {@inheritdoc}
*/
public static function execute(Entity $entity, array $args)
{
$delete = self::getInstance($entity);

if (isset($args[0])) {
$delete->where($args[0], isset($args[1]) ? $args[1] : null);
}

if (isset($args[2])) {
$delete->limit($args[2]);
}

return $delete->run();
}

/**
* Adds a LIMIT clause
*
* @param integer $limit
*
* @return self
*/
public function limit($limit)
{
$this->limit = $limit;

return $this;
}

/**
* Adds an offset to the LIMIT clause
*
* @param integer $offset
*
* @return self
*/
public function offset($offset)
{
$this->offset = $offset;

return $this;
}
use LimitTrait;

/**
* Adds new marks to the query
Expand Down Expand Up @@ -98,19 +46,8 @@ public function __toString()
{
$query = "DELETE FROM `{$this->entity->table}`";

if (!empty($this->where)) {
$query .= ' WHERE ('.implode(') AND (', $this->where).')';
}

if (!empty($this->limit)) {
$query .= ' LIMIT';

if (!empty($this->offset)) {
$query .= ' '.$this->offset.',';
}

$query .= ' '.$this->limit;
}
$query .= $this->whereToString();
$query .= $this->limitToString();

return $query;
}
Expand Down
12 changes: 0 additions & 12 deletions src/Queries/Mysql/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@
*/
class Fields extends BaseQuery
{
/**
* @see QueryInterface
*
* $entity->fields()
*
* {@inheritdoc}
*/
public static function execute(Entity $entity, array $args)
{
return self::getInstance($entity)->get();
}

/**
* Run the query and return all values
*
Expand Down
24 changes: 0 additions & 24 deletions src/Queries/Mysql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@ class Insert extends BaseQuery
protected $data = [];
protected $duplications;

/**
* @see QueryInterface
*
* $entity->insert($data, $duplications)
*
* {@inheritdoc}
*/
public static function execute(Entity $entity, array $args)
{
$insert = self::getInstance($entity);

if (isset($args[0])) {
$insert->data($args[0]);
}

if (isset($args[1])) {
$delete->duplications($args[1]);
}

$delete->run();

return $this->entity->getDb()->lastInsertId();
}

/**
* Set the data to update
*
Expand Down
60 changes: 60 additions & 0 deletions src/Queries/Mysql/LimitTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace SimpleCrud\Queries\Mysql;


/**
* Common function to manage LIMIT clause
*/
trait LimitTrait
{
protected $limit;
protected $offset;

/**
* Adds a LIMIT clause
*
* @param integer $limit
*
* @return self
*/
public function limit($limit)
{
$this->limit = $limit;

return $this;
}

/**
* Adds an offset to the LIMIT clause
*
* @param integer $offset
*
* @return self
*/
public function offset($offset)
{
$this->offset = $offset;

return $this;
}

/**
* Generate LIMIT clause
*
* @return string
*/
protected function limitToString()
{
if (!empty($this->limit)) {
$query = ' LIMIT';

if (!empty($this->offset)) {
$query .= ' '.$this->offset.',';
}

return $query.' '.$this->limit;
}

return '';
}
}
93 changes: 21 additions & 72 deletions src/Queries/Mysql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,14 @@
class Select extends BaseQuery
{
use WhereTrait;
use LimitTrait;

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

/**
* @see QueryInterface
*
* $entity->select($where, $marks, $orderBy, $limit)
*
* {@inheritdoc}
*/
public static function execute(Entity $entity, array $args)
{
$select = self::getInstance($entity);

if (isset($args[0])) {
$select->where($args[0], isset($args[1]) ? $args[1] : null);
}

if (isset($args[2])) {
$select->orderBy($args[2]);
}

if (isset($args[3])) {
if ($args[3] === true) {
return $select->one();
}

$select->limit($args[3]);
}

return $select->all();
}
protected $statement;

/**
* Adds new extra table to the query
Expand Down Expand Up @@ -132,34 +103,6 @@ public function orderBy($orderBy, $direction = null)
return $this;
}

/**
* Adds a LIMIT clause
*
* @param integer $limit
*
* @return self
*/
public function limit($limit)
{
$this->limit = $limit;

return $this;
}

/**
* Adds an offset to the LIMIT clause
*
* @param integer $offset
*
* @return self
*/
public function offset($offset)
{
$this->offset = $offset;

return $this;
}

/**
* Adds a LEFT JOIN clause
*
Expand Down Expand Up @@ -246,7 +189,23 @@ public function one()
$this->limit(1);
}

$row = $this->run()->fetch();
$this->statement = null;

return $this->fetch();
}

/**
* Run the query and return the first value
*
* @return RowCollection
*/
public function fetch()
{
if (!$this->statement) {
$this->statement = $this->run();
}

$row = $this->statement->fetch();

if ($row !== false) {
return $this->entity->create($this->entity->prepareDataFromDatabase($row));
Expand Down Expand Up @@ -285,23 +244,13 @@ public function __toString()
}
}

if (!empty($this->where)) {
$query .= ' WHERE ('.implode(') AND (', $this->where).')';
}
$query .= $this->whereToString();

if (!empty($this->orderBy)) {
$query .= ' ORDER BY '.implode(', ', $this->orderBy);
}

if (!empty($this->limit)) {
$query .= ' LIMIT';

if (!empty($this->offset)) {
$query .= ' '.$this->offset.',';
}

$query .= ' '.$this->limit;
}
$query .= $this->limitToString();

return $query;
}
Expand Down
Loading

0 comments on commit 693ebad

Please sign in to comment.