Skip to content

Commit e193e10

Browse files
committed
Add multiple join processes and multiple DataStore's join methods
1 parent 0bdbad4 commit e193e10

File tree

8 files changed

+425
-2
lines changed

8 files changed

+425
-2
lines changed

src/KoolReport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class KoolReport
4848
*/
4949
public function version()
5050
{
51-
return "6.1.0";
51+
return "6.6.2";
5252
}
5353

5454
/**

src/core/DataStore.php

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,12 +1373,17 @@ public function join($secondStore, $matching)
13731373
return $dstore;
13741374
}
13751375

1376+
public function innerJoin($secondStore, $matching)
1377+
{
1378+
return $this->join($secondStore, $matching);
1379+
}
1380+
13761381
/**
13771382
* Left join with another datastore on matching keys
13781383
*
13791384
* Examples
13801385
*
1381-
* $store1->join($store2,array("id"=>"userId"));
1386+
* $store1->leftJoin($store2,array("id"=>"userId"));
13821387
*
13831388
* @param DataStore $secondStore Second datastore
13841389
* @param array $matching Matching keys
@@ -1431,6 +1436,143 @@ public function leftJoin($secondStore, $matching)
14311436
return $dstore;
14321437
}
14331438

1439+
/**
1440+
* Right join with another datastore on matching keys
1441+
*
1442+
* Examples
1443+
*
1444+
* $store1->rightJoin($store2,array("id"=>"userId"));
1445+
*
1446+
* @param DataStore $secondStore Second datastore
1447+
* @param array $matching Matching keys
1448+
*
1449+
* @return DataStore New datastore containing results.
1450+
*/
1451+
public function rightJoin($secondStore, $matching)
1452+
{
1453+
$dstore = new DataStore;
1454+
// join with other datasource to produce new one with above condition
1455+
$firstKeys = array_keys($matching);
1456+
$secondKeys = array_values($matching);
1457+
1458+
1459+
$firstMaps = $this->_mapKeyIndex($this->rows, $firstKeys);
1460+
$secondMaps = $this->_mapKeyIndex($secondStore->all(), $secondKeys);
1461+
1462+
$firstNullRow = array();
1463+
foreach ($this->first() as $k => $v) {
1464+
$firstNullRow[$k] = null;
1465+
}
1466+
1467+
foreach ($secondMaps as $key => $indices) {
1468+
foreach ($indices as $i) {
1469+
if (isset($firstMaps[$key])) {
1470+
foreach ($firstMaps[$key] as $j) {
1471+
$dstore->push(
1472+
array_merge(
1473+
$this->get($j),
1474+
$secondStore->get($i)
1475+
)
1476+
);
1477+
}
1478+
} else {
1479+
$dstore->push(
1480+
array_merge(
1481+
$firstNullRow,
1482+
$secondStore->get($i)
1483+
)
1484+
);
1485+
}
1486+
}
1487+
}
1488+
1489+
$columnMeta = array_merge(
1490+
$this->metaData["columns"],
1491+
$secondStore->meta()["columns"]
1492+
);
1493+
$dstore->meta(array("columns" => $columnMeta));
1494+
return $dstore;
1495+
}
1496+
1497+
/**
1498+
* Outer join with another datastore on matching keys
1499+
*
1500+
* Examples
1501+
*
1502+
* $store1->outerJoin($store2,array("id"=>"userId"));
1503+
*
1504+
* @param DataStore $secondStore Second datastore
1505+
* @param array $matching Matching keys
1506+
*
1507+
* @return DataStore New datastore containing results.
1508+
*/
1509+
public function outerJoin($secondStore, $matching)
1510+
{
1511+
$dstore = new DataStore;
1512+
// join with other datasource to produce new one with above condition
1513+
$firstKeys = array_keys($matching);
1514+
$secondKeys = array_values($matching);
1515+
1516+
$firstMaps = $this->_mapKeyIndex($this->rows, $firstKeys);
1517+
$secondMaps = $this->_mapKeyIndex($secondStore->all(), $secondKeys);
1518+
1519+
$firstNullRow = array();
1520+
foreach ($this->first() as $k => $v) {
1521+
$firstNullRow[$k] = null;
1522+
}
1523+
$secondNullRow = array();
1524+
foreach ($secondStore->first() as $k => $v) {
1525+
$secondNullRow[$k] = null;
1526+
}
1527+
1528+
foreach ($firstMaps as $key => $indices) {
1529+
foreach ($indices as $i) {
1530+
if (isset($secondMaps[$key])) {
1531+
foreach ($secondMaps[$key] as $j) {
1532+
$dstore->push(
1533+
array_merge(
1534+
$this->rows[$i],
1535+
$secondStore->get($j)
1536+
)
1537+
);
1538+
}
1539+
} else {
1540+
$dstore->push(
1541+
array_merge(
1542+
$this->rows[$i],
1543+
$secondNullRow
1544+
)
1545+
);
1546+
}
1547+
}
1548+
}
1549+
1550+
foreach ($secondMaps as $key => $indices) {
1551+
foreach ($indices as $i) {
1552+
if (!isset($firstMaps[$key])) {
1553+
$dstore->push(
1554+
array_merge(
1555+
$firstNullRow,
1556+
$secondStore->get($i)
1557+
)
1558+
);
1559+
}
1560+
}
1561+
}
1562+
1563+
$columnMeta = array_merge(
1564+
$this->metaData["columns"],
1565+
$secondStore->meta()["columns"]
1566+
);
1567+
$dstore->meta(array("columns" => $columnMeta));
1568+
return $dstore;
1569+
}
1570+
1571+
public function fullJoin($secondStore, $matching)
1572+
{
1573+
return $this->outerJoin($secondStore, $matching);
1574+
}
1575+
14341576
/**
14351577
* Return distinct value of a column
14361578
*

src/processes/FullJoin.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* This file contains class to join two data flow on a condition.
4+
*
5+
* @category Core
6+
* @package KoolReport
7+
* @author KoolPHP Inc <support@koolphp.net>
8+
* @copyright 2017-2028 KoolPHP Inc
9+
* @license MIT License https://www.koolreport.com/license#mit-license
10+
* @link https://www.koolphp.net
11+
*/
12+
13+
/* Usage
14+
* (new Join(array($source1,source2,array("id1"=>"id2"))))
15+
*
16+
*
17+
*/
18+
namespace koolreport\processes;
19+
20+
use \koolreport\core\Process;
21+
22+
/**
23+
* This file contains class to join two data flow on a condition.
24+
*
25+
* @category Core
26+
* @package KoolReport
27+
* @author KoolPHP Inc <support@koolphp.net>
28+
* @copyright 2017-2028 KoolPHP Inc
29+
* @license MIT License https://www.koolreport.com/license#mit-license
30+
* @link https://www.koolphp.net
31+
*/
32+
class FullJoin extends OuterJoin
33+
{
34+
}

src/processes/InnerJoin.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* This file contains class to join two data flow on a condition.
4+
*
5+
* @category Core
6+
* @package KoolReport
7+
* @author KoolPHP Inc <support@koolphp.net>
8+
* @copyright 2017-2028 KoolPHP Inc
9+
* @license MIT License https://www.koolreport.com/license#mit-license
10+
* @link https://www.koolphp.net
11+
*/
12+
13+
/* Usage
14+
* (new Join(array($source1,source2,array("id1"=>"id2"))))
15+
*
16+
*
17+
*/
18+
namespace koolreport\processes;
19+
20+
use \koolreport\core\Process;
21+
22+
/**
23+
* This file contains class to join two data flow on a condition.
24+
*
25+
* @category Core
26+
* @package KoolReport
27+
* @author KoolPHP Inc <support@koolphp.net>
28+
* @copyright 2017-2028 KoolPHP Inc
29+
* @license MIT License https://www.koolreport.com/license#mit-license
30+
* @link https://www.koolphp.net
31+
*/
32+
class InnerJoin extends Join
33+
{
34+
}

src/processes/Join.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
class Join extends Process
3333
{
3434
protected $container;
35+
protected $firstSideKeys = [];
36+
protected $secondSideKeys = [];
3537

3638
/**
3739
* The constructor
@@ -88,6 +90,12 @@ public function input($row, $source)
8890
break;
8991
}
9092
}
93+
if ($source === $this->container[0]["source"]) {
94+
$this->firstSideKeys = array_merge($this->firstSideKeys, array_keys($row));
95+
}
96+
if ($source === $this->container[1]["source"]) {
97+
$this->secondSideKeys = array_merge($this->secondSideKeys, array_keys($row));
98+
}
9199
}
92100

93101
/**

src/processes/LeftJoin.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* This file contains class to join two data flow on a condition.
4+
*
5+
* @category Core
6+
* @package KoolReport
7+
* @author KoolPHP Inc <support@koolphp.net>
8+
* @copyright 2017-2028 KoolPHP Inc
9+
* @license MIT License https://www.koolreport.com/license#mit-license
10+
* @link https://www.koolphp.net
11+
*/
12+
13+
/* Usage
14+
* (new Join(array($source1,source2,array("id1"=>"id2"))))
15+
*
16+
*
17+
*/
18+
namespace koolreport\processes;
19+
20+
use \koolreport\core\Process;
21+
22+
/**
23+
* This file contains class to join two data flow on a condition.
24+
*
25+
* @category Core
26+
* @package KoolReport
27+
* @author KoolPHP Inc <support@koolphp.net>
28+
* @copyright 2017-2028 KoolPHP Inc
29+
* @license MIT License https://www.koolreport.com/license#mit-license
30+
* @link https://www.koolphp.net
31+
*/
32+
class LeftJoin extends Join
33+
{
34+
/**
35+
* Handle on input end
36+
*
37+
* @return null
38+
*/
39+
protected function onInputEnd()
40+
{
41+
foreach ($this->container[0]["data"] as $key => $rows) {
42+
if (isset($this->container[1]["data"][$key])) {
43+
foreach ($rows as $first) {
44+
foreach ($this->container[1]["data"][$key] as $second) {
45+
$this->next(array_merge($first, $second));
46+
}
47+
}
48+
unset($this->container[1]["data"][$key]);
49+
} else {
50+
foreach ($rows as $first) {
51+
foreach ($rows as $first) {
52+
$mergedRow = $first;
53+
foreach ($this->secondSideKeys as $k) {
54+
if (!isset($mergedRow[$k])) $mergedRow[$k] = null;
55+
}
56+
$this->next($mergedRow);
57+
}
58+
}
59+
}
60+
unset($this->container[0]["data"][$key]);
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
 (0)