Relations
3 weeks ago
Collection.php
3 weeks ago
EagerLoader.php
3 weeks ago
Expression.php
3 weeks ago
JoinClause.php
3 weeks ago
Model.php
3 weeks ago
Paginator.php
3 weeks ago
QueryBuilder.php
3 weeks ago
QueryCompiler.php
3 weeks ago
JoinClause.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Represents a JOIN clause sub-builder for building join conditions fluently. |
| 5 | * This class extends the QueryBuilder and is used internally for building complex JOIN ON conditions, |
| 6 | * including nested and dynamic join clauses, for SQL queries. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Database\Query |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Database\Query; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Closure; |
| 16 | class JoinClause extends QueryBuilder |
| 17 | { |
| 18 | /** |
| 19 | * The type of join (e.g., INNER, LEFT, RIGHT). |
| 20 | * |
| 21 | * @var string |
| 22 | * |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | public $type; |
| 26 | /** |
| 27 | * The table to be joined. |
| 28 | * |
| 29 | * @var string |
| 30 | * |
| 31 | * @since 1.0.0 |
| 32 | */ |
| 33 | public $table; |
| 34 | /** |
| 35 | * The parent QueryBuilder instance. |
| 36 | * |
| 37 | * @var \Framework\Database\Query\QueryBuilder |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public $parent; |
| 42 | /** |
| 43 | * The query compiler from the parent builder. |
| 44 | * |
| 45 | * @var \Framework\Database\Query\QueryCompiler |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | protected $parent_compiler; |
| 50 | /** |
| 51 | * The database connection from the parent builder. |
| 52 | * |
| 53 | * @var \Framework\Database\Connection\Connection |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | protected $parent_connection; |
| 58 | /** |
| 59 | * Create a new JoinClause instance. |
| 60 | * |
| 61 | * @param QueryBuilder $parent The parent query builder instance. |
| 62 | * @param string $type The join type (INNER, LEFT, etc.). |
| 63 | * @param string $table The table being joined. |
| 64 | * |
| 65 | * @return void |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | public function __construct(QueryBuilder $parent, $type, $table) |
| 70 | { |
| 71 | $this->type = $type; |
| 72 | $this->table = $table; |
| 73 | $this->parent = $parent; |
| 74 | $this->parent_compiler = $parent->get_compiler(); |
| 75 | $this->parent_connection = $parent->get_connection(); |
| 76 | parent::__construct($this->parent_connection, $this->parent_compiler, $this->parent->model); |
| 77 | } |
| 78 | /** |
| 79 | * Add an ON condition to the join. |
| 80 | * |
| 81 | * Accepts either simple column comparison or a closure for nested join conditions. |
| 82 | * |
| 83 | * @param string|\Closure $first The first column (or a closure for nesting). |
| 84 | * @param string|null $operator The comparison operator (=, >, etc.). |
| 85 | * @param string|null $second The second column for the ON condition. |
| 86 | * @param string $boolean The boolean operator (AND/OR) for the condition. |
| 87 | * |
| 88 | * @return JoinClause |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | */ |
| 92 | public function on($first, $operator = null, $second = null, $boolean = 'and') |
| 93 | { |
| 94 | if ($first instanceof Closure) { |
| 95 | return $this->where_nested($first, $boolean); |
| 96 | } |
| 97 | return $this->where_column($first, $operator, $second, $boolean); |
| 98 | } |
| 99 | /** |
| 100 | * Add an OR ON condition to the join. |
| 101 | * |
| 102 | * @param string|\Closure $first The first column or closure. |
| 103 | * @param string|null $operator The operator (optional). |
| 104 | * @param string|null $second The second column (optional). |
| 105 | * |
| 106 | * @return JoinClause |
| 107 | * |
| 108 | * @since 1.0.0 |
| 109 | */ |
| 110 | public function or_on($first, $operator = null, $second = null) |
| 111 | { |
| 112 | return $this->on($first, $operator, $second, 'or'); |
| 113 | } |
| 114 | /** |
| 115 | * Create a new JoinClause instance for building nested join conditions. |
| 116 | * |
| 117 | * @return static |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | */ |
| 121 | public function new_query() |
| 122 | { |
| 123 | return new static($this->new_parent_query(), $this->type, $this->table); |
| 124 | } |
| 125 | /** |
| 126 | * Create a new parent QueryBuilder instance. |
| 127 | * |
| 128 | * Used to generate a fresh parent builder for subqueries or nested clauses. |
| 129 | * |
| 130 | * @return QueryBuilder |
| 131 | * |
| 132 | * @since 1.0.0 |
| 133 | */ |
| 134 | protected function new_parent_query() |
| 135 | { |
| 136 | $class = \get_class($this->parent); |
| 137 | return new $class($this->parent_connection, $this->parent_compiler, $this->parent->model); |
| 138 | } |
| 139 | /** |
| 140 | * Create a new parent query instance for subqueries within join clauses. |
| 141 | * |
| 142 | * @return QueryBuilder |
| 143 | * |
| 144 | * @since 1.0.0 |
| 145 | */ |
| 146 | protected function for_subquery() |
| 147 | { |
| 148 | return $this->new_parent_query()->new_query(); |
| 149 | } |
| 150 | } |
| 151 |