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
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* UnionTrait |
22
|
|
|
* |
23
|
|
|
* Implementation of UnionInterface |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Query |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see UnionInterface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
trait UnionTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* 0 NO, 1 YES, 2 UNION ALL |
35
|
|
|
* @var int |
36
|
|
|
* @access protected |
37
|
|
|
*/ |
38
|
|
|
protected $is_union = UnionInterface::UNION_NOT; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function union()/*# : SelectStatementInterface */ |
44
|
|
|
{ |
45
|
|
|
$this->is_union = UnionInterface::UNION_YES; |
46
|
|
|
return $this->getBuilder()->select()->table('')->setPrevious($this); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
public function unionAll()/*# : SelectStatementInterface */ |
53
|
|
|
{ |
54
|
|
|
$this->is_union = UnionInterface::UNION_ALL; |
55
|
|
|
return $this->getBuilder()->select()->table('')->setPrevious($this); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Override `getStatement()` in StatementAbstract |
60
|
|
|
* |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
public function getStatement(array $settings = [])/*# : string */ |
64
|
|
|
{ |
65
|
|
|
// combine settings |
66
|
|
|
$settings = $this->combineSettings($settings); |
67
|
|
|
|
68
|
|
|
// statements |
69
|
|
|
$sql = []; |
70
|
|
|
|
71
|
|
|
// build previous statement |
72
|
|
|
if ($this->hasPrevious()) { |
73
|
|
|
$sql[] = $this->getPrevious()->getStatement($settings); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// build current sql |
77
|
|
|
$sql[] = $this->buildSql($settings); |
78
|
|
|
|
79
|
|
|
// replace with ?, :name or real values |
80
|
|
|
return $this->bindValues(join($settings['seperator'], $sql), $settings); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Build UNION/UNION ALL |
85
|
|
|
* |
86
|
|
|
* @param string $prefix |
87
|
|
|
* @param array $settings |
88
|
|
|
* @return string |
89
|
|
|
* @access protected |
90
|
|
|
*/ |
91
|
|
|
protected function buildUnion( |
92
|
|
|
/*# string */ $prefix, |
93
|
|
|
array $settings |
94
|
|
|
)/*# : string */ { |
95
|
|
|
switch ($this->is_union) { |
96
|
|
|
case UnionInterface::UNION_YES : |
|
|
|
|
97
|
|
|
return $settings['seperator'] . 'UNION'; |
98
|
|
|
case UnionInterface::UNION_ALL : |
|
|
|
|
99
|
|
|
return $settings['seperator'] . 'UNION ALL'; |
100
|
|
|
default : |
|
|
|
|
101
|
|
|
return $prefix; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
abstract protected function hasPrevious()/*# : bool */; |
106
|
|
|
abstract protected function getPrevious()/*# : StatementInterface */; |
107
|
|
|
/** |
108
|
|
|
* Return the builder |
109
|
|
|
* |
110
|
|
|
* @return BuilderInterface |
111
|
|
|
* @access public |
112
|
|
|
*/ |
113
|
|
|
abstract public function getBuilder()/*# : BuilderInterface */; |
114
|
|
|
abstract protected function combineSettings(array $settings)/*# : array */; |
115
|
|
|
abstract protected function buildSql(array $settings)/*# : string */; |
116
|
|
|
abstract protected function bindValues(/*# string */ $sql, array $settings)/*# : string */; |
117
|
|
|
} |
118
|
|
|
|
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.