1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Query |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Dialect\Mysql; |
16
|
|
|
|
17
|
|
|
use Phossa2\Query\Traits\StatementAbstract; |
18
|
|
|
use Phossa2\Query\Traits\Clause\LimitTrait; |
19
|
|
|
use Phossa2\Query\Traits\Clause\ClauseTrait; |
20
|
|
|
use Phossa2\Query\Traits\Clause\OrderByTrait; |
21
|
|
|
use Phossa2\Query\Interfaces\Statement\UnionStatementInterface; |
22
|
|
|
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface; |
23
|
|
|
use Phossa2\Query\Interfaces\BuilderInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Union |
27
|
|
|
* |
28
|
|
|
* @package Phossa2\Query |
29
|
|
|
* @author Hong Zhang <[email protected]> |
30
|
|
|
* @see StatementAbstract |
31
|
|
|
* @see UnionStatementInterface |
32
|
|
|
* @version 2.0.0 |
33
|
|
|
* @since 2.0.0 added |
34
|
|
|
*/ |
35
|
|
|
class Union extends StatementAbstract implements UnionStatementInterface |
36
|
|
|
{ |
37
|
|
|
use ClauseTrait, OrderByTrait, LimitTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
protected $configs = [ |
43
|
|
|
'UNION' => '', |
44
|
|
|
'ORDERBY' => 'ORDER BY', |
45
|
|
|
'LIMIT' => 'LIMIT', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param BuilderInterface $builder |
50
|
|
|
* @access public |
51
|
|
|
*/ |
52
|
|
|
public function __construct(BuilderInterface $builder) |
53
|
|
|
{ |
54
|
|
|
parent::__construct($builder); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function union(SelectStatementInterface $select) |
61
|
|
|
{ |
62
|
|
|
return $this->addUnion('UNION', $select); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
|
|
public function unionAll(SelectStatementInterface $select) |
69
|
|
|
{ |
70
|
|
|
return $this->addUnion('UNION ALL', $select); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Build unioned SELECT |
75
|
|
|
* |
76
|
|
|
* @param string $prefix |
77
|
|
|
* @param array $settings |
78
|
|
|
* @return string |
79
|
|
|
* @access protected |
80
|
|
|
*/ |
81
|
|
|
protected function buildUnion( |
82
|
|
|
/*# string */ $prefix, |
83
|
|
|
array $settings |
84
|
|
|
)/*# : string */ { |
85
|
|
|
$clause = &$this->getClause('UNION'); |
86
|
|
|
$flat = $this->flatSettings($settings); |
87
|
|
|
|
88
|
|
|
$parts = []; |
89
|
|
|
foreach ($clause as $idx => $field) { |
90
|
|
|
if ($idx) { // prepend type UNION or UNION ALL |
91
|
|
|
$parts[] = $field[1]; |
92
|
|
|
} |
93
|
|
|
$parts[] = $this->quoteItem($field[0], $flat); |
94
|
|
|
} |
95
|
|
|
return ltrim($this->joinClause($prefix, '', $parts, $settings)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $type |
100
|
|
|
* @param SelectStatementInterface $select |
101
|
|
|
* @return $this |
102
|
|
|
* @access protected |
103
|
|
|
*/ |
104
|
|
|
protected function addUnion( |
105
|
|
|
/*# string */ $type, |
106
|
|
|
SelectStatementInterface $select |
107
|
|
|
) { |
108
|
|
|
$clause = &$this->getClause('UNION'); |
109
|
|
|
$clause[] = [$select, $type]; |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
*/ |
116
|
|
|
protected function getType()/*# : string */ |
117
|
|
|
{ |
118
|
|
|
return ''; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|