| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Database\Query; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
|
| 7 |
class JoinClause extends Builder |
| 8 |
{ |
| 9 |
/** |
| 10 |
* The type of join being performed. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
public $type; |
| 15 |
|
| 16 |
/** |
| 17 |
* The table the join clause is joining to. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
public $table; |
| 22 |
|
| 23 |
/** |
| 24 |
* The connection of the parent query builder. |
| 25 |
* |
| 26 |
* @var \FluentBoards\Framework\Database\ConnectionInterface |
| 27 |
*/ |
| 28 |
protected $parentConnection; |
| 29 |
|
| 30 |
/** |
| 31 |
* The grammar of the parent query builder. |
| 32 |
* |
| 33 |
* @var \FluentBoards\Framework\Database\Query\Grammar |
| 34 |
*/ |
| 35 |
protected $parentGrammar; |
| 36 |
|
| 37 |
/** |
| 38 |
* The processor of the parent query builder. |
| 39 |
* |
| 40 |
* @var \FluentBoards\Framework\Database\Query\Processor |
| 41 |
*/ |
| 42 |
protected $parentProcessor; |
| 43 |
|
| 44 |
/** |
| 45 |
* The class name of the parent query builder. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
protected $parentClass; |
| 50 |
|
| 51 |
/** |
| 52 |
* Create a new join clause instance. |
| 53 |
* |
| 54 |
* @param \FluentBoards\Framework\Database\Query\Builder $parentQuery |
| 55 |
* @param string $type |
| 56 |
* @param string $table |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function __construct(Builder $parentQuery, $type, $table) |
| 60 |
{ |
| 61 |
$this->type = $type; |
| 62 |
$this->table = $table; |
| 63 |
$this->parentClass = get_class($parentQuery); |
| 64 |
$this->parentGrammar = $parentQuery->getGrammar(); |
| 65 |
$this->parentProcessor = $parentQuery->getProcessor(); |
| 66 |
$this->parentConnection = $parentQuery->getConnection(); |
| 67 |
|
| 68 |
parent::__construct( |
| 69 |
$this->parentConnection, $this->parentGrammar, $this->parentProcessor |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add an "on" clause to the join. |
| 75 |
* |
| 76 |
* On clauses can be chained, e.g. |
| 77 |
* |
| 78 |
* $join->on('contacts.user_id', '=', 'users.id') |
| 79 |
* ->on('contacts.info_id', '=', 'info.id') |
| 80 |
* |
| 81 |
* will produce the following SQL: |
| 82 |
* |
| 83 |
* on `contacts`.`user_id` = `users`.`id` and `contacts`.`info_id` = `info`.`id` |
| 84 |
* |
| 85 |
* @param \Closure|string $first |
| 86 |
* @param string|null $operator |
| 87 |
* @param \FluentBoards\Framework\Database\Query\Expression|string|null $second |
| 88 |
* @param string $boolean |
| 89 |
* @return self |
| 90 |
* |
| 91 |
* @throws \InvalidArgumentException |
| 92 |
*/ |
| 93 |
public function on($first, $operator = null, $second = null, $boolean = 'and') |
| 94 |
{ |
| 95 |
if ($first instanceof Closure) { |
| 96 |
return $this->whereNested($first, $boolean); |
| 97 |
} |
| 98 |
|
| 99 |
return $this->whereColumn($first, $operator, $second, $boolean); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Add an "or on" clause to the join. |
| 104 |
* |
| 105 |
* @param \Closure|string $first |
| 106 |
* @param string|null $operator |
| 107 |
* @param \FluentBoards\Framework\Database\Query\Expression|string|null $second |
| 108 |
* @return \FluentBoards\Framework\Database\Query\JoinClause |
| 109 |
*/ |
| 110 |
public function orOn($first, $operator = null, $second = null) |
| 111 |
{ |
| 112 |
return $this->on($first, $operator, $second, 'or'); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get a new instance of the join clause builder. |
| 117 |
* |
| 118 |
* @return \FluentBoards\Framework\Database\Query\JoinClause |
| 119 |
*/ |
| 120 |
public function newQuery() |
| 121 |
{ |
| 122 |
return new static($this->newParentQuery(), $this->type, $this->table); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Create a new query instance for sub-query. |
| 127 |
* |
| 128 |
* @return \FluentBoards\Framework\Database\Query\Builder |
| 129 |
*/ |
| 130 |
protected function forSubQuery() |
| 131 |
{ |
| 132 |
return $this->newParentQuery()->newQuery(); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Create a new parent query instance. |
| 137 |
* |
| 138 |
* @return \FluentBoards\Framework\Database\Query\Builder |
| 139 |
*/ |
| 140 |
protected function newParentQuery() |
| 141 |
{ |
| 142 |
$class = $this->parentClass; |
| 143 |
|
| 144 |
return new $class($this->parentConnection, $this->parentGrammar, $this->parentProcessor); |
| 145 |
} |
| 146 |
} |
| 147 |
|