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\Traits\Clause; |
16
|
|
|
|
17
|
|
|
use Phossa2\Query\Interfaces\BuilderInterface; |
18
|
|
|
use Phossa2\Query\Interfaces\Clause\UnionInterface; |
19
|
|
|
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* UnionTrait |
23
|
|
|
* |
24
|
|
|
* Implementation of UnionInterface |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Query |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see UnionInterface |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
trait UnionTrait |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* 0 NO, 1 YES, 2 UNION ALL |
36
|
|
|
* @var int |
37
|
|
|
* @access protected |
38
|
|
|
*/ |
39
|
|
|
protected $is_union = UnionInterface::UNION_NOT; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Previous statement used in UNION/UNION ALL |
43
|
|
|
* |
44
|
|
|
* @var SelectStatementInterface |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected $previous; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
public function union()/*# : SelectStatementInterface */ |
53
|
|
|
{ |
54
|
|
|
$this->is_union = UnionInterface::UNION_YES; |
55
|
|
|
return $this->getBuilder()->select()->table('')->setPrevious($this); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
|
|
public function unionAll()/*# : SelectStatementInterface */ |
62
|
|
|
{ |
63
|
|
|
$this->is_union = UnionInterface::UNION_ALL; |
64
|
|
|
return $this->getBuilder()->select()->table('')->setPrevious($this); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Override `getStatement()` in StatementAbstract |
69
|
|
|
* |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function getStatement(array $settings = [])/*# : string */ |
73
|
|
|
{ |
74
|
|
|
// combine settings |
75
|
|
|
$settings = $this->combineSettings($settings); |
76
|
|
|
|
77
|
|
|
// statements |
78
|
|
|
$sql = []; |
79
|
|
|
|
80
|
|
|
// build previous statement |
81
|
|
|
if ($this->hasPrevious()) { |
82
|
|
|
$sql[] = $this->getPrevious()->getStatement($settings); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// build current sql |
86
|
|
|
$sql[] = $this->buildSql($settings); |
87
|
|
|
|
88
|
|
|
// replace with ?, :name or real values |
89
|
|
|
return $this->bindValues(join($settings['seperator'], $sql), $settings); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Build UNION/UNION ALL |
94
|
|
|
* |
95
|
|
|
* @param string $prefix |
96
|
|
|
* @param array $settings |
97
|
|
|
* @return string |
98
|
|
|
* @access protected |
99
|
|
|
*/ |
100
|
|
|
protected function buildUnion( |
101
|
|
|
/*# string */ $prefix, |
102
|
|
|
array $settings |
103
|
|
|
)/*# : string */ { |
104
|
|
|
switch ($this->is_union) { |
105
|
|
|
case UnionInterface::UNION_YES : |
|
|
|
|
106
|
|
|
return $settings['seperator'] . 'UNION'; |
107
|
|
|
case UnionInterface::UNION_ALL : |
|
|
|
|
108
|
|
|
return $settings['seperator'] . 'UNION ALL'; |
109
|
|
|
default : |
|
|
|
|
110
|
|
|
return $prefix; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set previous SELECT |
116
|
|
|
* |
117
|
|
|
* @param SelectStatementInterface $select |
118
|
|
|
* @return $this |
119
|
|
|
* @access protected |
120
|
|
|
*/ |
121
|
|
|
protected function setPrevious(SelectStatementInterface $select) |
122
|
|
|
{ |
123
|
|
|
$this->previous = $select; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Has previous SELECT ? |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
* @access protected |
132
|
|
|
*/ |
133
|
|
|
protected function hasPrevious()/*# : bool */ |
134
|
|
|
{ |
135
|
|
|
return null !== $this->previous; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get previous SELECT |
140
|
|
|
* |
141
|
|
|
* @return SelectStatementInterface |
142
|
|
|
* @access protected |
143
|
|
|
*/ |
144
|
|
|
protected function getPrevious()/*# : SelectStatementInterface */ |
145
|
|
|
{ |
146
|
|
|
return $this->previous; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return the builder |
151
|
|
|
* |
152
|
|
|
* @return BuilderInterface |
153
|
|
|
* @access public |
154
|
|
|
*/ |
155
|
|
|
abstract public function getBuilder()/*# : BuilderInterface */; |
156
|
|
|
abstract protected function combineSettings(array $settings)/*# : array */; |
157
|
|
|
abstract protected function buildSql(array $settings)/*# : string */; |
158
|
|
|
abstract protected function bindValues(/*# string */ $sql, array $settings)/*# : string */; |
159
|
|
|
} |
160
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.