JoinQueryBuilder::buildCrossJoinQuery()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The JOIN Query builder API.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 * @since v0.0.1 <Date: 16th April, 2019>
7
 */
8
9
namespace Dolphin\Builders;
10
11
/**
12
 * This class provides the mechanism to build the Join Queries.
13
 */
14
class JoinQueryBuilder extends QueryBuilder
15
{
16
    public function buildJoinQuery($conditions = [])
17
    {
18
        $prefix = $this->getPrefix();
19
        $query = [];
20
21
        if (!count($conditions)) {
22
            return $query;
23
        }
24
25
        foreach ($conditions as $join) {
26
            list($tableName, $tableAlias) = $this->addAlias($join[0]);
27
28
            if ($join[2] == '') {
29
                $query[] = 'JOIN '.$this->quote($prefix.$tableName).$tableAlias.' USING (`'.$join[1].'`)';
30
            } else {
31
                $query[] = 'JOIN '.$this->quote($prefix.$tableName).$tableAlias.' ON ('.$this->quote($join[1]).' '.$join[2].' '.$this->quote($join[3]).')';
32
            }
33
        }
34
35
        return $query;
36
    }
37
38
    public function buildLeftJoinQuery($conditions = [])
39
    {
40
        $prefix = $this->getPrefix();
41
        $query = [];
42
43
        if (!count($conditions)) {
44
            return $query;
45
        }
46
47
        foreach ($conditions as $leftJoin) {
48
            list($tableName, $tableAlias) = $this->addAlias($leftJoin[0]);
49
50
            if ($leftJoin[2] == '') {
51
                $query[] = 'LEFT JOIN '.$this->quote($prefix.$tableName).$tableAlias.' USING (`'.$leftJoin[1].'`)';
52
            } else {
53
                $query[] = 'LEFT JOIN '.$this->quote($prefix.$tableName).$tableAlias.' ON ('.$this->quote($leftJoin[1]).' '.$leftJoin[2].' '.$this->quote($leftJoin[3]).')';
54
            }
55
        }
56
57
        return $query;
58
    }
59
60
    public function buildRightJoinQuery($conditions = [])
61
    {
62
        $prefix = $this->getPrefix();
63
        $query = [];
64
65
        if (!count($conditions)) {
66
            return $query;
67
        }
68
69
        foreach ($conditions as $rightJoin) {
70
            list($tableName, $tableAlias) = $this->addAlias($rightJoin[0]);
71
72
            if ($rightJoin[2] == '') {
73
                $query[] = 'RIGHT JOIN '.$this->quote($prefix.$tableName).$tableAlias.' USING (`'.$rightJoin[1].'`)';
74
            } else {
75
                $query[] = 'RIGHT JOIN '.$this->quote($prefix.$tableName).$tableAlias.' ON ('.$this->quote($rightJoin[1]).' '.$rightJoin[2].' '.$this->quote($rightJoin[3]).')';
76
            }
77
        }
78
79
        return $query;
80
    }
81
82
    public function buildCrossJoinQuery($conditions = [])
83
    {
84
        $prefix = $this->getPrefix();
85
        $query = [];
86
87
        if (!count($conditions)) {
88
            return $query;
89
        }
90
91
        foreach ($conditions as $crossJoin) {
92
            list($tableName, $tableAlias) = $this->addAlias($crossJoin[0]);
93
94
            $query[] = 'CROSS JOIN '.$this->quote($prefix.$tableName).$tableAlias;
95
        }
96
97
        return $query;
98
    }
99
100
    public function buildAllJoinQuery($join, $leftJoin, $rightJoin, $crossJoin, $mainQuery = [])
101
    {
102
        $query = [];
103
104
        $joinQuery = $this->buildJoinQuery($join);
105
        if (count($joinQuery)) {
106
            $query = array_merge($query, $joinQuery);
107
        }
108
109
        $leftJoinQuery = $this->buildLeftJoinQuery($leftJoin);
110
        if (count($leftJoinQuery)) {
111
            $query = array_merge($query, $leftJoinQuery);
112
        }
113
114
        $rightJoinQuery = $this->buildRightJoinQuery($rightJoin);
115
        if (count($rightJoinQuery)) {
116
            $query = array_merge($query, $rightJoinQuery);
117
        }
118
119
        $crossJoinQuery = $this->buildCrossJoinQuery($crossJoin);
120
        if (count($crossJoinQuery)) {
121
            $query = array_merge($query, $crossJoinQuery);
122
        }
123
        if (count($query)) {
124
            $mainQuery = array_merge($mainQuery, $query);
125
        }
126
        return $mainQuery;
127
    }
128
}
129