Skip to content

Commit

Permalink
Refactor query components for 63c8ad9
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Oct 22, 2017
1 parent accfca2 commit 4c6c6fa
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

namespace Lampager\Query\Conditions;

use Lampager\Query\Direction;
use Lampager\Query\Order;
namespace Lampager\Query;

/**
* Class Condition
Expand Down
45 changes: 38 additions & 7 deletions src/Query/Conditions/Group.php → src/Query/ConditionGroup.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
<?php

namespace Lampager\Query\Conditions;

use Lampager\Query\Direction;
use Lampager\Query\Order;
namespace Lampager\Query;

/**
* Class Group
* Class ConditionGroup
*
* Conditions are concatenated with "AND".
*/
class Group implements \IteratorAggregate
class ConditionGroup implements \IteratorAggregate
{
/**
* @var Condition[]
*/
protected $conditions = [];

/**
* @param Order[] $orders
* @param int[]|string[] $cursor
* @param Direction $direction
* @param bool $exclusive
* @param bool $isSupportQuery
* @return static[]
*/
public static function createMany(array $orders, array $cursor, Direction $direction, $exclusive, $isSupportQuery = false)
{
$groups = [];
$count = count($orders);
for ($i = 0; $i < $count; ++$i) {
/*
* Slice orders for building conditions.
*
* e.g.
*
* 1st: updated_at = ? AND created_at = ? AND id > ?
* 2nd: updated_at = ? AND created_at > ?
* 3rd: updated_at > ?
*/
$groups[] = static::create(
array_slice($orders, 0, $count - $i),
$cursor,
$direction,
$exclusive,
$i === 0, // First row has a primary key
$isSupportQuery
);
}
return $groups;
}

/**
* @param Order[] $orders
* @param int[]|string[] $cursor
Expand All @@ -26,7 +57,7 @@ class Group implements \IteratorAggregate
* @param bool $isSupportQuery
* @return static
*/
public static function create(array $orders, array $cursor, Direction $direction, $exclusive, $hasPrimaryKey, $isSupportQuery)
public static function create(array $orders, array $cursor, Direction $direction, $exclusive, $hasPrimaryKey, $isSupportQuery = false)
{
$conditions = [];
$i = 0;
Expand Down
28 changes: 20 additions & 8 deletions src/Query/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Select extends SelectOrUnionAll
{
/**
* @var null|Where
* @var ConditionGroup[]
*/
protected $where;

Expand All @@ -25,11 +25,11 @@ class Select extends SelectOrUnionAll
/**
* Select constructor.
*
* @param null|Where $where
* @param ConditionGroup[] $where
* @param Order[] $orders
* @param Limit $limit
*/
public function __construct($where, array $orders, Limit $limit)
public function __construct(array $where, array $orders, Limit $limit)
{
$this->where = $where;
$this->orders = static::validate(...$orders);
Expand All @@ -46,13 +46,23 @@ protected static function validate(Order ...$orders)
}

/**
* @return null|Where
* @return ConditionGroup[]
*/
public function where()
{
return $this->where;
}

/**
* An alias of where().
*
* @return ConditionGroup[]
*/
public function conditionGroups()
{
return $this->where;
}

/**
* @return Order[]
*/
Expand All @@ -75,7 +85,9 @@ public function limit()
public function inverse()
{
return new static(
$this->where ? $this->where->inverse() : null,
array_map(static function (ConditionGroup $group) {
return $group->inverse();
}, $this->where),
array_map(static function (Order $order) {
return $order->inverse();
}, $this->orders),
Expand All @@ -88,9 +100,9 @@ public function inverse()
*/
public function __clone()
{
if ($this->where) {
$this->where = clone $this->where;
}
$this->where = array_map(static function (ConditionGroup $group) {
return clone $group;
}, $this->where);
$this->orders = array_map(static function (Order $order) {
return clone $order;
}, $this->orders);
Expand Down
2 changes: 1 addition & 1 deletion src/Query/SelectOrUnionAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class SelectOrUnionAll implements \IteratorAggregate
public static function create(array $orders, array $cursor, Limit $limit, Direction $direction, $exclusive, $seekable)
{
$mainQuery = new Select(
$cursor ? Where::create($orders, $cursor, $direction, $exclusive) : null,
$cursor ? ConditionGroup::createMany($orders, $cursor, $direction, $exclusive) : [],
$direction->backward()
? array_map(static function (Order $order) {
return $order->inverse();
Expand Down
109 changes: 0 additions & 109 deletions src/Query/Where.php

This file was deleted.

0 comments on commit 4c6c6fa

Please sign in to comment.