orm refactoring

This commit is contained in:
Yuri Kuznetsov
2021-07-25 10:15:00 +03:00
parent bf943cc706
commit 49f12ac82f
12 changed files with 197 additions and 59 deletions

View File

@@ -29,6 +29,8 @@
namespace Espo\ORM\Query;
use Espo\ORM\Query\Part\WhereClause;
use RuntimeException;
/**
@@ -47,6 +49,22 @@ class Delete implements Query
return $this->params['from'];
}
/**
* Get a from-alias
*/
public function getFromAlias(): ?string
{
return $this->params['fromAlias'] ?? null;
}
/**
* Get a LIMIT.
*/
public function getLimit(): ?int
{
return $this->params['limit'] ?? null;
}
private function validateRawParams(array $params): void
{
$this->validateRawParamsSelecting($params);

View File

@@ -38,9 +38,9 @@ class LockTable implements Query
{
use BaseTrait;
const MODE_SHARE = 'SHARE';
public const MODE_SHARE = 'SHARE';
const MODE_EXCLUSIVE = 'EXCLUSIVE';
public const MODE_EXCLUSIVE = 'EXCLUSIVE';
protected function validateRawParams(array $params): void
{

View File

@@ -32,13 +32,14 @@ namespace Espo\ORM\Query;
use Espo\ORM\Query\Part\WhereClause;
use Espo\ORM\Query\Part\SelectExpression;
use Espo\ORM\Query\Part\OrderExpression;
use Espo\ORM\Query\Part\Expression;
use RuntimeException;
/**
* Select parameters.
*
* @todo Add validation and normalization (from ORM\DB\BaseQuery).
* @todo Add validation and normalization.
*/
class Select implements SelectingQuery
{
@@ -57,6 +58,48 @@ class Select implements SelectingQuery
return $this->params['from'] ?? null;
}
/**
* Get a from-alias
*/
public function getFromAlias(): ?string
{
return $this->params['fromAlias'] ?? null;
}
/**
* Get a from-query.
*/
public function getFromQuery(): ?SelectingQuery
{
return $this->params['fromQuery'] ?? null;
}
/**
* Get an OFFSET.
*/
public function getOffset(): ?int
{
return $this->params['offset'] ?? null;
}
/**
* Get a LIMIT.
*/
public function getLimit(): ?int
{
return $this->params['limit'] ?? null;
}
/**
* Get USE INDEX (list of indexes).
*
* @return string[]
*/
public function getUseIndex(): array
{
return $this->params['useIndex'] ?? [];
}
/**
* Get SELECT items.
*
@@ -81,32 +124,6 @@ class Select implements SelectingQuery
);
}
/**
* Get ORDER items.
*
* @return OrderExpression[]
*/
public function getOrder(): array
{
return array_map(
function ($item) {
if (is_array($item) && count($item)) {
$itemValue = is_int($item[0]) ? (string) $item[0] : $item[0];
return OrderExpression::fromString($itemValue)
->withDirection($item[1] ?? OrderExpression::ASC);
}
if (is_string($item)) {
return OrderExpression::fromString($item);
}
throw new RuntimeException("Bad order item.");
},
$this->params['orderBy'] ?? []
);
}
/**
* Whether DISTINCT is applied.
*/
@@ -116,25 +133,48 @@ class Select implements SelectingQuery
}
/**
* Get GROUP BY items.
* Whether a FOR SHARE lock mode is set.
*/
public function getGroupBy(): array
public function isForShare(): bool
{
return $this->params['orderBy'] ?? [];
return $this->params['forShare'] ?? false;
}
/**
* Get WHERE clause.
* Whether a FOR UPDATE lock mode is set.
*/
public function getWhere(): ?WhereClause
public function isForUpdate(): bool
{
$whereClause = $this->params['whereClause'] ?? null;
return $this->params['forUpdate'] ?? false;
}
if ($whereClause === null || $whereClause === []) {
/**
* Get GROUP BY items.
*
* @return Expression[]
*/
public function getGroupBy(): array
{
return array_map(
function (string $item) {
return Expression::create($item);
},
$this->params['groupBy'] ?? []
);
}
/**
* Get HAVING clause.
*/
public function getHaving(): ?WhereClause
{
$havingClause = $this->params['havingClause'] ?? null;
if ($havingClause === null || $havingClause === []) {
return null;
}
return WhereClause::fromRaw($whereClause);
return WhereClause::fromRaw($havingClause);
}
private function validateRawParams(array $params): void

View File

@@ -173,10 +173,10 @@ class SelectBuilder implements Builder
* Passing a string|Expression will append an item.
*
* Usage options:
* * `groupBy(Expression|string $expression)`
* * `groupBy([$expr1, $expr2, ...])`
* * `groupBy(string|Expression $expression)`
*
* @param string|Expression|array $groupBy
* @param Expression|Expression[]|string|string[] $groupBy
*/
public function groupBy($groupBy): self
{

View File

@@ -29,8 +29,53 @@
namespace Espo\ORM\Query;
use Espo\ORM\Query\Part\OrderExpression;
use Espo\ORM\Query\Part\WhereClause;
use RuntimeException;
trait SelectingTrait
{
/**
* Get ORDER items.
*
* @return OrderExpression[]
*/
public function getOrder(): array
{
return array_map(
function ($item) {
if (is_array($item) && count($item)) {
$itemValue = is_int($item[0]) ? (string) $item[0] : $item[0];
return OrderExpression::fromString($itemValue)
->withDirection($item[1] ?? OrderExpression::ASC);
}
if (is_string($item)) {
return OrderExpression::fromString($item);
}
throw new RuntimeException("Bad order item.");
},
$this->params['orderBy'] ?? []
);
}
/**
* Get WHERE clause.
*/
public function getWhere(): ?WhereClause
{
$whereClause = $this->params['whereClause'] ?? null;
if ($whereClause === null || $whereClause === []) {
return null;
}
return WhereClause::fromRaw($whereClause);
}
private static function validateRawParamsSelecting(array $params): void
{
}

View File

@@ -29,6 +29,8 @@
namespace Espo\ORM\Query;
use Espo\ORM\Query\Part\WhereClause;
use RuntimeException;
/**
@@ -39,6 +41,22 @@ class Update implements Query
use SelectingTrait;
use BaseTrait;
/**
* Get an entity type.
*/
public function getIn(): ?string
{
return $this->params['from'] ?? null;
}
/**
* Get a LIMIT.
*/
public function getLimit(): ?int
{
return $this->params['limit'] ?? null;
}
private function validateRawParams(array $params): void
{
$this->validateRawParamsSelecting($params);

View File

@@ -53,7 +53,6 @@ class UpdateBuilder implements Builder
return $this;
}
/**
* For what entity type to build a query.
*/

View File

@@ -305,13 +305,13 @@ class RDBRelation
/**
* Specify GROUP BY.
* Passing an array will reset previously set items.
* Passing a string will append an item.
* Passing a string|Expression will append an item.
*
* Usage options:
* * `groupBy(Expression|string $expression)`
* * `groupBy([$expr1, $expr2, ...])`
* * `groupBy(string|Expression $expression)`
*
* @param string|Expression|array $groupBy
* @param Expression|Expression[]|string|string[] $groupBy
*/
public function groupBy($groupBy): Builder
{

View File

@@ -388,13 +388,13 @@ class RDBRelationSelectBuilder
/**
* Specify GROUP BY.
* Passing an array will reset previously set items.
* Passing a string will append an item.
* Passing a string|Expression will append an item.
*
* Usage options:
* * `groupBy(Expression|string $expression)`
* * `groupBy([$expr1, $expr2, ...])`
* * `groupBy(string|Expression $expression)`
*
* @param string|Expression|array $groupBy
* @param Expression|Expression[]|string|string[] $groupBy
*/
public function groupBy($groupBy): self
{

View File

@@ -801,13 +801,13 @@ class RDBRepository extends Repository
/**
* Specify GROUP BY.
* Passing an array will reset previously set items.
* Passing a string will append an item.
* Passing a string|Expression will append an item.
*
* Usage options:
* * `groupBy(Expression|string $expression)`
* * `groupBy([$expr1, $expr2, ...])`
* * `groupBy(string|Expression $expression)`
*
* @param string|Expression|array $groupBy
* @param Expression|Expression[]|string|string[] $groupBy
*/
public function groupBy($groupBy): RDBSelectBuilder
{

View File

@@ -327,13 +327,13 @@ class RDBSelectBuilder
/**
* Specify GROUP BY.
* Passing an array will reset previously set items.
* Passing a string will append an item.
* Passing a string|Expression will append an item.
*
* Usage options:
* * `groupBy(Expression|string $expression)`
* * `groupBy([$expr1, $expr2, ...])`
* * `groupBy(string|Expression $expression)`
*
* @param string|Expression|array $groupBy
* @param Expression|Expression[]|string|string[] $groupBy
*/
public function groupBy($groupBy): self
{

View File

@@ -248,8 +248,6 @@ class SelectBuilderTest extends \PHPUnit\Framework\TestCase
$raw = $select->getRaw();
$this->assertEquals(['test=' => null], $raw['havingClause']);
$this->assertEquals(['test'], $raw['groupBy']);
}
@@ -257,15 +255,35 @@ class SelectBuilderTest extends \PHPUnit\Framework\TestCase
{
$select = $this->builder
->from('Test')
->having(Cond::equal(Expr::column('test'), null))
->groupBy([Expr::create('test')])
->groupBy([
Expr::create('test1'),
Expr::create('test2'),
])
->build();
$raw = $select->getRaw();
$this->assertEquals(['test=' => null], $raw['havingClause']);
$this->assertEquals(
[
Expr::create('test1'),
Expr::create('test2'),
],
$select->getGroupBy()
);
}
$this->assertEquals(['test'], $raw['groupBy']);
public function testHaving1()
{
$select = $this->builder
->from('Test')
->having(Cond::equal(Expr::column('test'), null))
->groupBy(Expr::create('test'))
->build();
$this->assertEquals(
Cond::equal(Expr::column('test'), null)->getRaw(),
$select->getHaving()->getRaw()
);
}
public function testOrder1()