Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\DataProcessor\DataFlow\CombinedDataFlow;
use Civi\DataProcessor\DataFlow\EndOfFlowException;
use Civi\DataProcessor\DataFlow\InvalidFlowException;
use Civi\DataProcessor\DataFlow\MultipleDataFlows\DataFlowDescription;
use Civi\DataProcessor\DataFlow\MultipleDataFlows\MultipleSourceDataFlows;
use Civi\DataProcessor\DataFlow\SqlDataFlow;
use Civi\DataProcessor\DataFlow\SqlTableDataFlow;
use Civi\DataProcessor\DataSpecification\DataSpecification;
use Civi\DataProcessor\DataSpecification\SqlFieldSpecification;
class UnionQueryDataFlow extends SqlDataFlow implements MultipleSourceDataFlows {
/**
* @var \Civi\DataProcessor\DataFlow\MultipleDataFlows\DataFlowDescription[]
*/
protected $sourceDataFlowDescriptions = array();
/**
* @var String
*/
protected $name;
public function __construct($name) {
parent::__construct();
$this->name = $name;
}
/**
* Adds a source data flow
*
* @param \Civi\DataProcessor\DataFlow\MultipleDataFlows\DataFlowDescription $dataFlowDescription
* @return void
* @throws \Civi\DataProcessor\DataFlow\InvalidFlowException
*/
public function addSourceDataFlow(DataFlowDescription $dataFlowDescription) {
if (!$dataFlowDescription->getDataFlow() instanceof SqlDataFlow) {
throw new InvalidFlowException();
}
$this->sourceDataFlowDescriptions[] = $dataFlowDescription;
}
public function getName() {
return $this->name;
}
/**
* Returns the From Statement.
*
* @return string
*/
public function getFromStatement() {
return "FROM {$this->getTableStatement()}";
}
/**
* Returns the Table part in the from statement.
*
* @return string
*/
public function getTableStatement() {
$selectStatements = array();
foreach($this->sourceDataFlowDescriptions as $sourceDataFlowDescription) {
$sourceDataFlow = $sourceDataFlowDescription->getDataFlow();
if ($sourceDataFlow instanceof SqlDataFlow) {
$selectAndFrom = $sourceDataFlow->getSelectQueryStatement();
$where = $sourceDataFlow->getWhereStatement();
$groupBy = $sourceDataFlow->getGroupByStatement();
$selectStatements[] = "{$selectAndFrom} {$where} {$groupBy}";
}
}
$sql = implode(" UNION ", $selectStatements);
return "({$sql}) `{$this->getName()}`";
}
/**
* Returns an array with the fields for in the select statement in the sql query.
*
* @return string[]
* @throws \Civi\DataProcessor\DataSpecification\FieldExistsException
*/
public function getFieldsForSelectStatement() {
$fields = array();
foreach($this->getDataSpecification()->getFields() as $field) {
if ($field instanceof SqlFieldSpecification) {
$fields[] = $field->getSqlSelectStatement($this->getName());
} else {
$fields[] = "`{$this->getName()}`.`{$field->name}` AS `{$field->alias}`";
}
}
return $fields;
}
}