Join::join()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Author: Adrian Dumitru
5
 * Date: 5/12/2017 9:20 PM
6
 */
7
8
namespace Qpdb\QueryBuilder\Traits;
9
10
11
use Qpdb\QueryBuilder\Dependencies\QueryHelper;
12
use Qpdb\QueryBuilder\Dependencies\QueryStructure;
13
14
15
/**
16
 * Trait Join
17
 * @package Qpdb\QueryBuilder\Traits
18
 * @property QueryStructure $queryStructure
19
 */
20
trait Join
21
{
22
23
	/**
24
	 * @param string $tableJoin
25
	 * @param string $onLeft
26
	 * @param string $onRight
27
	 * @return $this
28
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
29
	 */
30
	public function innerJoin( $tableJoin, $onLeft, $onRight = null )
31
	{
32
		return $this->makeJoin( 'INNER JOIN', $tableJoin, $onLeft, $onRight );
33
	}
34
35
	/**
36
	 * @param string $tableJoin
37
	 * @param string $onLeft
38
	 * @param string $onRight
39
	 * @return $this
40
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
41
	 */
42
	public function leftJoin( $tableJoin, $onLeft, $onRight = null )
43
	{
44
		return $this->makeJoin( 'LEFT JOIN', $tableJoin, $onLeft, $onRight );
45
	}
46
47
	/**
48
	 * @param string $tableJoin
49
	 * @param string $onLeft
50
	 * @param string $onRight
51
	 * @return $this
52
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
53
	 */
54
	public function leftOuterJoin( $tableJoin, $onLeft, $onRight = null )
55
	{
56
		return $this->makeJoin( 'LEFT OUTER JOIN', $tableJoin, $onLeft, $onRight );
57
	}
58
59
	/**
60
	 * @param string $tableJoin
61
	 * @param string $onLeft
62
	 * @param string $onRight
63
	 * @return $this
64
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
65
	 */
66
	public function rightJoin( $tableJoin, $onLeft, $onRight = null )
67
	{
68
		return $this->makeJoin( 'RIGHT JOIN', $tableJoin, $onLeft, $onRight );
69
	}
70
71
	/**
72
	 * @param string $tableJoin
73
	 * @param string $onLeft
74
	 * @param string $onRight
75
	 * @return $this
76
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
77
	 */
78
	public function rightOuterJoin( $tableJoin, $onLeft, $onRight = null )
79
	{
80
		return $this->makeJoin( 'RIGHT OUTER JOIN', $tableJoin, $onLeft, $onRight );
81
	}
82
83
	/**
84
	 * @param string $tableJoin
85
	 * @param string $onLeft
86
	 * @param string $onRight
87
	 * @return $this
88
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
89
	 */
90
	public function fullJoin( $tableJoin, $onLeft, $onRight = null )
91
	{
92
		return $this->makeJoin( 'FULL JOIN', $tableJoin, $onLeft, $onRight );
93
	}
94
95
	/**
96
	 * @param string $tableJoin
97
	 * @param string $onLeft
98
	 * @param string $onRight
99
	 * @return $this
100
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
101
	 */
102
	public function fullOuterJoin( $tableJoin, $onLeft, $onRight = null )
103
	{
104
		return $this->makeJoin( 'FULL OUTER JOIN', $tableJoin, $onLeft, $onRight );
105
	}
106
107
	/**
108
	 * @param $stringJoin
109
	 * @return $this
110
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
111
	 */
112
	public function join( $stringJoin )
113
	{
114
		$this->queryStructure->setElement( QueryStructure::JOIN, $stringJoin );
115
116
		return $this;
117
	}
118
119
	/**
120
	 * @param string $typeJoin
121
	 * @param string $tableJoin
122
	 * @param string $onLeft
123
	 * @param string $onRight
124
	 * @return $this
125
	 * @throws \Qpdb\QueryBuilder\Dependencies\QueryException
126
	 */
127
	private function makeJoin( $typeJoin, $tableJoin, $onLeft, $onRight = null )
128
	{
129
		$join = $typeJoin . ' ' . $tableJoin;
130
131
		if ( is_null( $onRight ) )
132
			$join .= " USING ( $onLeft )";
133
		else
134
			$join .= " ON $onLeft = $onRight";
135
136
		$this->queryStructure->setElement( QueryStructure::JOIN, $join );
137
138
		return $this;
139
	}
140
141
	/**
142
	 * @return string
143
	 */
144
	private function getJoinSyntax()
145
	{
146
		$joinString = implode( ' ', $this->queryStructure->getElement( QueryStructure::JOIN ) );
147
148
		return QueryHelper::clearMultipleSpaces( $joinString );
149
	}
150
151
}