Union::union()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\OrderTrait;
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, OrderTrait, LimitTrait;
38
39
    /**
40
     * @param  BuilderInterface $builder
41
     * @access public
42
     */
43
    public function __construct(BuilderInterface $builder)
44
    {
45
        parent::__construct($builder);
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function union(SelectStatementInterface $select)
52
    {
53
        return $this->addUnion('UNION', $select);
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function unionAll(SelectStatementInterface $select)
60
    {
61
        return $this->addUnion('UNION ALL', $select);
62
    }
63
64
    /**
65
     * Build unioned SELECT
66
     *
67
     * @param  string $prefix
68
     * @param  array $settings
69
     * @return string
70
     * @access protected
71
     */
72 View Code Duplication
    protected function buildUnion(
73
        /*# string */ $prefix,
74
        array $settings
75
    )/*# : string */ {
76
        $clause = &$this->getClause('UNION');
77
        $flat = $this->flatSettings($settings);
78
79
        $res = '';
80
        foreach ($clause as $idx => $field) {
81
            $parts = [];
82
            $prefix = $idx ? $field[1] : '';
83
            $parts[] = $this->quoteItem($field[0], $flat);
84
            $res .= $this->joinClause($prefix, '', $parts, $settings);
85
        }
86
        return ltrim($res);
87
    }
88
89
    /**
90
     * @param  string $type
91
     * @param  SelectStatementInterface $select
92
     * @return $this
93
     * @access protected
94
     */
95
    protected function addUnion(
96
        /*# string */ $type,
97
        SelectStatementInterface $select
98
    ) {
99
        $clause = &$this->getClause('UNION');
100
        $clause[] = [$select, $type];
101
        return $this;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    protected function getConfigs()/*# : array */
108
    {
109
        return [
110
            'UNION' => '',
111
            'ORDER' => 'ORDER BY',
112
            'LIMIT' => 'LIMIT',
113
        ];
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    protected function getType()/*# : string */
120
    {
121
        return '';
122
    }
123
}
124