Completed
Push — master ( 5ab9f6...ab12c8 )
by Hong
07:19
created

Union::buildUnion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
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
24
/**
25
 * Union
26
 *
27
 * @package Phossa2\Query
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     StatementAbstract
30
 * @see     UnionStatementInterface
31
 * @version 2.0.0
32
 * @since   2.0.0 added
33
 */
34
class Union extends StatementAbstract implements UnionStatementInterface
35
{
36
    use ClauseTrait, OrderByTrait, LimitTrait;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    protected $configs = [
42
        'UNION' => '',
43
        'ORDERBY' => 'ORDER BY',
44
        'LIMIT' => 'LIMIT',
45
    ];
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function union(SelectStatementInterface $select)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
51
    {
52
        return $this->addUnion('UNION', $select);
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function unionAll(SelectStatementInterface $select)
59
    {
60
        return $this->addUnion('UNION ALL', $select);
61
    }
62
63
    /**
64
     * Build unioned SELECT
65
     *
66
     * @param  string $prefix
67
     * @param  array $settings
68
     * @return string
69
     * @access protected
70
     */
71
    protected function buildUnion(
72
        /*# string */ $prefix,
73
        array $settings
74
    )/*# : string */ {
75
        $clause = &$this->getClause('UNION');
76
        $flat = $this->flatSettings($settings);
77
78
        $parts = [];
79
        foreach ($clause as $idx => $field) {
80
            if ($idx) { // prepend type UNION or UNION ALL
81
                $parts[] = $field[1];
82
            }
83
            $parts[] = $this->quoteItem($field[0], $flat);
84
        }
85
        return trim($this->joinClause($prefix, '', $parts, $settings));
86
    }
87
88
    /**
89
     * @param  string $type
90
     * @param  SelectStatementInterface $select
91
     * @return $this
92
     * @access protected
93
     */
94
    protected function addUnion(
95
        /*# string */ $type,
96
        SelectStatementInterface $select
97
    ) {
98
        $clause = &$this->getClause('UNION');
99
        $clause[] = [$select, $type];
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    protected function getType()/*# : string */
107
    {
108
        return '';
109
    }
110
}
111