Completed
Pull Request — master (#451)
by Anton
13:12
created

From::leftJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 4
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Db\Query\Traits;
12
13
use Bluz\Proxy\Db;
14
15
/**
16
 * From Trait
17
 *
18
 * Required for:
19
 *  - Select Builder
20
 *  - Delete Builder
21
 *
22
 * @package  Bluz\Db\Query\Traits
23
 * @author   Anton Shevchuk
24
 */
25
trait From
26
{
27
    /**
28
     * <code>
29
     * [
30
     *     'table' => 'users',
31
     *     'alias' => 'u'
32
     * ]
33
     * </code>
34
     *
35
     * @var array
36
     */
37
    protected $from = [];
38
39
    /**
40
     * <code>
41
     * [
42
     *     'u' => [
43
     *         'joinType' => 'inner',
44
     *         'joinTable' => $join,
45
     *         'joinAlias' => $alias,
46
     *         'joinCondition' => $condition
47
     * ]
48
     * </code>
49 8
     *
50
     * @var array[]
51 8
     */
52
    protected $join = [];
53 8
54 8
    /**
55
     * Set FROM
56 8
     *
57 8
     * Create and add a query root corresponding to the table identified by the
58
     * given alias, forming a cartesian product with any existing query roots
59 8
     *
60
     * <code>
61
     *     $sb = new SelectBuilder();
62
     *     $sb
63
     *         ->select('u.id')
64
     *         ->from('users', 'u')
65
     * </code>
66
     *
67
     * @param  string $from  The table
68
     * @param  string $alias The alias of the table
69
     *
70
     * @return $this
71
     */
72
    public function from($from, $alias)
73
    {
74
        $this->aliases[] = $alias;
0 ignored issues
show
Bug introduced by
The property aliases does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
76
        $this->from[] = [
77
            'table' => $from,
78
            'alias' => $alias
79
        ];
80
81
        return $this;
82
    }
83
84
    /**
85
     * Creates and adds a join to the query
86
     *
87
     * Example
88
     * <code>
89
     *     $sb = new Select();
90
     *     $sb
91
     *         ->select('u.name')
92
     *         ->from('users', 'u')
93
     *         ->join('u', 'phone', 'p', 'p.is_primary = 1');
94
     * </code>
95
     *
96
     * @param  string $fromAlias the alias that points to a from clause
97
     * @param  string $join      the table name to join
98
     * @param  string $alias     the alias of the join table
99
     * @param  string $condition the condition for the join
100
     *
101
     * @return $this
102
     */
103
    public function join($fromAlias, $join, $alias, $condition = null)
104
    {
105
        return $this->innerJoin($fromAlias, $join, $alias, $condition);
106
    }
107
108
    /**
109
     * Creates and adds a join to the query
110
     *
111
     * Example
112
     * <code>
113
     *     $sb = new Select();
114
     *     $sb
115
     *         ->select('u.name')
116
     *         ->from('users', 'u')
117
     *         ->innerJoin('u', 'phone', 'p', 'p.is_primary = 1');
118
     * </code>
119
     *
120
     * @param  string $fromAlias the alias that points to a from clause
121
     * @param  string $join      the table name to join
122
     * @param  string $alias     the alias of the join table
123
     * @param  string $condition the condition for the join
124
     *
125
     * @return $this
126
     */
127
    public function innerJoin($fromAlias, $join, $alias, $condition = null)
128
    {
129
        $this->aliases[] = $alias;
130
131
        $this->join[$fromAlias][] = [
132
            'joinType' => 'inner',
133
            'joinTable' => $join,
134
            'joinAlias' => $alias,
135
            'joinCondition' => $condition
136
        ];
137
        return $this;
138
    }
139
140
    /**
141
     * Creates and adds a left join to the query.
142
     *
143
     * Example
144
     * <code>
145
     *     $sb = new Select();
146
     *     $sb
147
     *         ->select('u.name')
148
     *         ->from('users', 'u')
149
     *         ->leftJoin('u', 'phone', 'p', 'p.is_primary = 1');
150
     * </code>
151
     *
152
     * @param  string $fromAlias the alias that points to a from clause
153
     * @param  string $join      the table name to join
154
     * @param  string $alias     the alias of the join table
155
     * @param  string $condition the condition for the join
156
     *
157
     * @return $this
158
     */
159
    public function leftJoin($fromAlias, $join, $alias, $condition = null)
160
    {
161
        $this->aliases[] = $alias;
162
163
        $this->join[$fromAlias][] = [
164
            'joinType' => 'left',
165
            'joinTable' => $join,
166
            'joinAlias' => $alias,
167
            'joinCondition' => $condition
168
        ];
169
        return $this;
170
    }
171
172
    /**
173
     * Creates and adds a right join to the query.
174
     *
175
     * Example
176
     * <code>
177
     *     $sb = new Select();
178
     *     $sb
179
     *         ->select('u.name')
180
     *         ->from('users', 'u')
181
     *         ->rightJoin('u', 'phone', 'p', 'p.is_primary = 1');
182
     * </code>
183
     *
184
     * @param  string $fromAlias the alias that points to a from clause
185
     * @param  string $join      the table name to join
186
     * @param  string $alias     the alias of the join table
187
     * @param  string $condition the condition for the join
188
     *
189
     * @return $this
190
     */
191
    public function rightJoin($fromAlias, $join, $alias, $condition = null)
192
    {
193
        $this->aliases[] = $alias;
194
195
        $this->join[$fromAlias][] = [
196
            'joinType' => 'right',
197
            'joinTable' => $join,
198
            'joinAlias' => $alias,
199
            'joinCondition' => $condition
200
        ];
201
        return $this;
202
    }
203
204
    /**
205
     * setFromQueryPart
206
     *
207
     * @param  string $table
208
     *
209
     * @return self
210
     */
211
    protected function setFromQueryPart($table)
212
    {
213
        return $this->from($table, $table);
214
    }
215
216
    /**
217
     * Prepare From query part
218
     *
219
     * @return string
220
     */
221
    protected function prepareFrom() : string
222
    {
223
        $fromClauses = [];
224
        // Loop through all FROM clauses
225
        foreach ($this->from as $from) {
226
            $fromClause = Db::quoteIdentifier($from['table']) . ' AS ' . $from['alias']
227
                . $this->getSQLForJoins($from['alias']);
228
229
            $fromClauses[$from['alias']] = $fromClause;
230
        }
231
232
        return ' FROM ' . implode(', ', $fromClauses);
233
    }
234
235
    /**
236
     * Generate SQL string for JOINs
237
     *
238
     * @param  string $fromAlias alias of table
239
     *
240
     * @return string
241
     */
242
    protected function getSQLForJoins($fromAlias) : string
243
    {
244
        if (!isset($this->join[$fromAlias])) {
245
            return '';
246
        }
247
248
        $query = '';
249
250
        foreach ($this->join[$fromAlias] as $join) {
251
            $query .= ' ' . strtoupper($join['joinType'])
252
                . ' JOIN ' . Db::quoteIdentifier($join['joinTable']) . ' AS ' . $join['joinAlias']
253
                . ' ON ' . $join['joinCondition'];
254
            $query .= $this->getSQLForJoins($join['joinAlias']);
255
        }
256
257
        return $query;
258
    }
259
}
260