Aggregate.php
3 years ago
CRUD.php
3 years ago
FromClause.php
4 years ago
GroupByStatement.php
4 years ago
HavingClause.php
3 years ago
JoinClause.php
4 years ago
LimitStatement.php
4 years ago
MetaQuery.php
3 years ago
OffsetStatement.php
4 years ago
OrderByStatement.php
4 years ago
SelectStatement.php
4 years ago
TablePrefix.php
4 years ago
UnionOperator.php
4 years ago
WhereClause.php
3 years ago
JoinClause.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\QueryBuilder\Concerns; |
| 4 | |
| 5 | use Closure; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 8 | use Give\Framework\QueryBuilder\Clauses\Join; |
| 9 | use Give\Framework\QueryBuilder\Clauses\RawSQL; |
| 10 | |
| 11 | /** |
| 12 | * @since 2.19.0 |
| 13 | */ |
| 14 | trait JoinClause |
| 15 | { |
| 16 | |
| 17 | /** |
| 18 | * @var Closure[]|RawSQL[] |
| 19 | */ |
| 20 | protected $joins = []; |
| 21 | |
| 22 | /** |
| 23 | * Method used to build advanced JOIN queries, Check README.md for more info. |
| 24 | * If you need to perform only simple JOINs with only one JOIN condition, then you don't need this method. |
| 25 | * |
| 26 | * @param Closure $callback The closure will receive a Give\Framework\QueryBuilder\JoinQueryBuilder instance |
| 27 | * |
| 28 | * @return $this |
| 29 | */ |
| 30 | public function join($callback) |
| 31 | { |
| 32 | $this->joins[] = $callback; |
| 33 | |
| 34 | return $this; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param string|RawSQL $table |
| 39 | * @param string $column1 |
| 40 | * @param string $column2 |
| 41 | * @param string|null $alias |
| 42 | * |
| 43 | * @return $this |
| 44 | */ |
| 45 | public function leftJoin($table, $column1, $column2, $alias = null) |
| 46 | { |
| 47 | $this->join( |
| 48 | function (JoinQueryBuilder $builder) use ($table, $column1, $column2, $alias) { |
| 49 | $builder |
| 50 | ->leftJoin($table, $alias) |
| 51 | ->on($column1, $column2); |
| 52 | } |
| 53 | ); |
| 54 | |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param string|RawSQL $table |
| 60 | * @param string $column1 |
| 61 | * @param string $column2 |
| 62 | * @param string|null $alias |
| 63 | * |
| 64 | * @return $this |
| 65 | */ |
| 66 | public function innerJoin($table, $column1, $column2, $alias = null) |
| 67 | { |
| 68 | $this->join( |
| 69 | function (JoinQueryBuilder $builder) use ($table, $column1, $column2, $alias) { |
| 70 | $builder |
| 71 | ->innerJoin($table, $alias) |
| 72 | ->on($column1, $column2); |
| 73 | } |
| 74 | ); |
| 75 | |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string|RawSQL $table |
| 81 | * @param string $column1 |
| 82 | * @param string $column2 |
| 83 | * @param string|null $alias |
| 84 | * |
| 85 | * @return $this |
| 86 | */ |
| 87 | public function rightJoin($table, $column1, $column2, $alias = null) |
| 88 | { |
| 89 | $this->join( |
| 90 | function (JoinQueryBuilder $builder) use ($table, $column1, $column2, $alias) { |
| 91 | $builder |
| 92 | ->rightJoin($table, $alias) |
| 93 | ->on($column1, $column2); |
| 94 | } |
| 95 | ); |
| 96 | |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Add raw SQL JOIN clause |
| 103 | * |
| 104 | * @param string $sql |
| 105 | * @param ...$args |
| 106 | * |
| 107 | * @return $this; |
| 108 | */ |
| 109 | public function joinRaw($sql, ...$args) |
| 110 | { |
| 111 | $this->joins[] = new RawSQL($sql, $args); |
| 112 | |
| 113 | return $this; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * @return string[] |
| 119 | */ |
| 120 | protected function getJoinSQL() |
| 121 | { |
| 122 | return array_map(function ($callback) { |
| 123 | if ($callback instanceof RawSQL) { |
| 124 | return $callback->sql; |
| 125 | } |
| 126 | |
| 127 | $builder = new JoinQueryBuilder(); |
| 128 | |
| 129 | call_user_func($callback, $builder); |
| 130 | |
| 131 | $joins = array_map(function ($join) { |
| 132 | if ($join instanceof RawSQL) { |
| 133 | return $join->sql; |
| 134 | } |
| 135 | |
| 136 | if ($join instanceof Join) { |
| 137 | if ($join->alias) { |
| 138 | return DB::prepare( |
| 139 | '%1s JOIN %2s %3s', |
| 140 | $join->joinType, |
| 141 | $join->table, |
| 142 | $join->alias |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | return DB::prepare( |
| 147 | '%1s JOIN %2s', |
| 148 | $join->joinType, |
| 149 | $join->table |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | // JoinCondition |
| 154 | return DB::prepare( |
| 155 | $join->quote |
| 156 | ? ' %1s %2s = %s' |
| 157 | : ' %1s %2s = %3s', |
| 158 | $join->logicalOperator, |
| 159 | $join->column1, |
| 160 | $join->column2 |
| 161 | ); |
| 162 | }, $builder->getDefinedJoins()); |
| 163 | |
| 164 | return implode(' ', $joins); |
| 165 | }, $this->joins); |
| 166 | } |
| 167 | } |
| 168 |