Completed
Push — master ( 68f317...226980 )
by Hong
03:41
created

JoinTrait::rightJoin()   A

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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 3
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\ExpressionInterface;
18
use Phossa2\Query\Interfaces\Clause\JoinInterface;
19
20
/**
21
 * JoinTrait
22
 *
23
 * Implementation of JoinInterface
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     JoinInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait JoinTrait
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function join($secondTable, $onClause = '', $firstTable = '')
37
    {
38
        return $this->realJoin('INNER JOIN', $secondTable, $onClause, $firstTable);
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function leftJoin($secondTable, $onClause = '', $firstTable = '')
45
    {
46
        return $this->realJoin('LEFT JOIN', $secondTable, $onClause, $firstTable);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function joinRaw(/*# string */ $joinType, /*# string */ $rawString)
53
    {
54
        return $this->realJoin($joinType, $rawString, '', '', true);
55
    }
56
57
    /**
58
     * The real join
59
     *
60
     * @param  string $joinType
61
     * @param  string|string[]|SelectStatementInterface $secondTable
62
     * @param  string|string[]|ExpressionInterface $onClause
63
     * @param  string $firstTable
64
     * @param  bool $rawMode
65
     * @return $this
66
     * @access protected
67
     */
68
    protected function realJoin(
69
        /*# string */ $joinType,
70
        $secondTable,
71
        $onClause = '',
72
        $firstTable = '',
73
        /*# bool */ $rawMode = false
74
    ) {
75
        $alias = 0; // no alias
76
77
        // totally raw
78
        if ($rawMode || '' === $onClause) {
79
            $rawMode = true;
80
        }
81
82
        // fix table, alias, on clause
83
        if (!$rawMode) {
84
            $onClause = $this->fixOnClause($onClause);
85
            list($secondTable, $alias) = $this->fixJoinTable($secondTable);
86
        }
87
        $clause = &$this->getClause('JOIN');
88
        $clause[] = [$rawMode, $joinType, $secondTable, $alias, $onClause, $firstTable];
89
        return $this;
90
    }
91
92
    /**
93
     * Fix join table
94
     *
95
     * @param  string|string[]|SelectStatementInterface $table
96
     * @return array [table, alias]
97
     * @access protected
98
     */
99
    protected function fixJoinTable($table)
100
    {
101
        if (is_object($table)) {
102
            return [$table, uniqid()]; // need an alias
103
        } elseif (is_string($table)) {
104
            return [$table, 0]; // alias set 0
105
        } else {
106
            return $table; // array
107
        }
108
    }
109
110
    /**
111
     * Fix 'ON' clause
112
     *
113
     * @param  mixed $onClause
114
     * @return array|ExpressionInterface
115
     * @access protected
116
     */
117
    protected function fixOnClause($onClause)
118
    {
119
        if (is_string($onClause)) {
120
            return [$onClause, '=', $onClause];
121
        } elseif (is_array($onClause) && !isset($onClause[2])) {
122
            return [$onClause[0], '=', $onClause[1]];
123
        } else {
124
            return $onClause;
125
        }
126
    }
127
128
    /**
129
     * Build join
130
     *
131
     * @param  string $prefix
132
     * @param  array $settings
133
     * @return string
134
     * @access protected
135
     */
136
    protected function buildJoin(
137
        /*# string */ $prefix,
1 ignored issue
show
Unused Code introduced by
The parameter $prefix is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
        array $settings
139
    )/*# : string */ {
140
        $string = '';
141
        $clause = &$this->getClause('JOIN');
142
        foreach ($clause as $cls) {
143
            $result = [];
144
            $prefix = $cls[1]; // join type
145
            if ($cls[0]) { // raw mode
146
                $result[] = $cls[2];
147
            } else {
148
                $result[] = $this->buildJoinTable($cls, $settings);
149
                $result[] = $this->buildJoinOn($cls, $settings);
150
            }
151
            $string .= $this->joinClause($prefix, '', $result, $settings);
152
        }
153
        return $string;
154
    }
155
156
    /**
157
     * Build TABLE part
158
     *
159
     * @param  array $cls
160
     * @param  array $settings
161
     * @return string
162
     * @access protected
163
     */
164
    protected function buildJoinTable(array $cls, array $settings)/*# : string */
165
    {
166
        $table = $cls[2];
167
        $alias = $cls[3];
168
        return $this->quoteItem($table, $settings) . $this->quoteAlias($alias, $settings);
169
    }
170
171
    /**
172
     * Build ON part
173
     *
174
     * @param  array $cls
175
     * @param  array $settings
176
     * @return string
177
     * @access protected
178
     */
179
    protected function buildJoinOn(array $cls, array $settings)/*# : string */
180
    {
181
        $on = $cls[4];
182
        $res = ['ON'];
183
184
        if (is_object($on)) {
185
            $res[] = $on->getStatement($settings);
186
        } else {
187
            // first
188
            $res[] = $this->quote($this->getFirstTableAlias($cls) . $on[0], $settings);
189
            // operator
190
            $res[] = $on[1];
191
            // second
192
            $res[] = $this->quote($this->getSecondTableAlias($cls) . $on[2], $settings);
193
        }
194
        return join(' ', $res);
195
    }
196
197
    /**
198
     * Get first table alias
199
     *
200
     * @param  array $cls
201
     * @return string
202
     * @access protected
203
     */
204
    protected function getFirstTableAlias(array $cls)/*# : string */
205
    {
206
        // first table specified
207
        if (!empty($cls[5])) {
208
            return $cls[5] . '.';
209
        } else {
210
            $tables = &$this->getClause('FROM');
211
            reset($tables);
212
            $alias = key($tables);
213
            return (is_int($alias) ? $tables[$alias][0] : $alias) . '.';
214
        }
215
    }
216
217
    /**
218
     * Get second table alias
219
     *
220
     * @param  array $cls
221
     * @return string
222
     * @access protected
223
     */
224
    protected function getSecondTableAlias(array $cls)/*# : string */
225
    {
226
        $alias = $cls[3];
227
        if (!is_string($alias)) {
228
            $alias = $cls[2];
229
        }
230
        return $alias . '.';
231
    }
232
233
    abstract protected function quote(/*# string */ $str, array $settings)/*# : string */;
234
    abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */;
235
    abstract protected function &getClause(/*# string */ $clauseName)/*# : array */;
236
    abstract protected function flatSettings(array $settings)/*# : array */;
237
    abstract protected function quoteItem(
238
        $item,
239
        array $settings,
240
        /*# bool */ $rawMode = false
241
    )/*# : string */;
242
    abstract protected function quoteAlias($alias, array $settings)/*# : string */;
243
    abstract protected function joinClause(
244
        /*# : string */ $prefix,
245
        /*# : string */ $seperator,
246
        array $clause,
247
        array $settings
248
    )/*# : string */;
249
}
250