Skip to content

Commit bbaf573

Browse files
committed
Explain query
1 parent 4946886 commit bbaf573

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Tests/Unit/ExplainTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
class ExplainTest extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testExplainGroupBy() : void
8+
{
9+
$table = new \Tests\App\Table\InventoryTransaction();
10+
$table->addSelect('category');
11+
$table->addSelect(new \PHPFUI\ORM\Literal('count(*)'), 'count');
12+
$table->addJoin('product');
13+
$table->addGroupBy('category');
14+
$table->addOrderBy('count', 'desc');
15+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
16+
}
17+
18+
public function testExplainIn() : void
19+
{
20+
$orderDetailTable = new \Tests\App\Table\OrderDetail();
21+
$orderDetailTable->setWhere(new \PHPFUI\ORM\Condition('quantity', 10));
22+
$orderDetailTable->addSelect('order_id');
23+
$this->assertGreaterThan(0, \count($orderDetailTable->getExplainRows()));
24+
$orderTable = new \Tests\App\Table\Order();
25+
$orderTable->setWhere(new \PHPFUI\ORM\Condition('order_id', $orderDetailTable, new \PHPFUI\ORM\Operator\In()));
26+
$this->assertGreaterThan(0, \count($orderTable->getExplainRows()));
27+
}
28+
29+
public function testExplainJoin() : void
30+
{
31+
$table = new \Tests\App\Table\InventoryTransaction();
32+
$table->addJoin('inventory_transaction_type');
33+
$table->addJoin('order');
34+
$table->addJoin('product');
35+
$table->addJoin('purchase_order');
36+
37+
$table->setLimit(10);
38+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
39+
}
40+
41+
public function testExplainNotIn() : void
42+
{
43+
$orderDetailTable = new \Tests\App\Table\OrderDetail();
44+
$orderDetailTable->setWhere(new \PHPFUI\ORM\Condition('quantity', 10));
45+
$orderDetailTable->addSelect('order_id');
46+
$this->assertGreaterThan(0, \count($orderDetailTable->getExplainRows()));
47+
$orderTable = new \Tests\App\Table\Order();
48+
$orderTable->setWhere(new \PHPFUI\ORM\Condition('order_id', $orderDetailTable, new \PHPFUI\ORM\Operator\NotIn()));
49+
$this->assertGreaterThan(0, \count($orderTable->getExplainRows()));
50+
}
51+
52+
public function testExplainUnion() : void
53+
{
54+
$table = new \Tests\App\Table\InventoryTransactionType();
55+
$table->addSelect('inventory_transaction_type_id', 'id');
56+
$table->addSelect('inventory_transaction_type_name', 'name');
57+
$table->addUnion(new \Tests\App\Table\OrderDetailStatus());
58+
$table->addUnion(new \Tests\App\Table\OrderStatus());
59+
$table->addUnion(new \Tests\App\Table\OrderTaxStatus());
60+
$table->addUnion(new \Tests\App\Table\PurchaseOrderStatus());
61+
$table->addOrderBy('name');
62+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
63+
}
64+
65+
public function testExplainWhere() : void
66+
{
67+
$table = new \Tests\App\Table\Customer();
68+
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', 'Purchasing Manager'));
69+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
70+
71+
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\Like()));
72+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
73+
74+
$table->setWhere(new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\NotLike()));
75+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
76+
77+
$condition = new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\Like());
78+
$condition->and(new \PHPFUI\ORM\Condition('state_province', 'NY'));
79+
$table->setWhere($condition);
80+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
81+
82+
$condition = new \PHPFUI\ORM\Condition('job_title', '%Purchasing%', new \PHPFUI\ORM\Operator\NotLike());
83+
$condition->or(new \PHPFUI\ORM\Condition('state_province', 'NY'));
84+
$table->setWhere($condition);
85+
$this->assertGreaterThan(0, \count($table->getExplainRows()));
86+
}
87+
}

src/PHPFUI/ORM/Table.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,17 @@ public function getDataObjectCursor() : \PHPFUI\ORM\DataObjectCursor
397397
return \PHPFUI\ORM::getDataObjectCursor($this->lastSql, $this->lastInput)->setCountSQL($this->getCountSQL($totalInput))->setTotalCountSQL($this->getTotalSQL($totalInput));
398398
}
399399

400+
/**
401+
* Return an array of the explain query
402+
*/
403+
public function getExplainRows() : array
404+
{
405+
$this->lastInput = [];
406+
$this->lastSql = 'explain ' . $this->getSQL($this->lastInput);
407+
408+
return \PHPFUI\ORM::getRows($this->lastSql, $this->lastInput);
409+
}
410+
400411
/**
401412
* @return array<string,array<mixed>>
402413
*/

0 commit comments

Comments
 (0)