MediaWiki master
JoinGroupBase.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use InvalidArgumentException;
6
12abstract class JoinGroupBase {
14 protected $tables = [];
15
17 protected $joinConds = [];
18
20 protected $lastAlias;
21
33 public function table( $table, $alias = null ) {
34 if ( $table instanceof JoinGroup ) {
35 $alias ??= $table->getAlias();
36 $table = $table->getRawTables();
37 } elseif ( $table instanceof SelectQueryBuilder ) {
38 $alias ??= $this->getAutoAlias();
39 $table = new Subquery( $table->getSQL() );
40 } elseif ( $table instanceof Subquery ) {
41 if ( $alias === null ) {
42 throw new InvalidArgumentException( __METHOD__ .
43 ': Subquery as table must provide an alias.' );
44 }
45 } elseif ( !is_string( $table ) ) {
46 throw new InvalidArgumentException( __METHOD__ .
47 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
48 }
49 if ( $alias === null ) {
50 $this->tables[] = $table;
51 $this->lastAlias = $table;
52 } else {
53 $this->tables[$alias] = $table;
54 $this->lastAlias = $alias;
55 }
56 return $this;
57 }
58
73 public function leftJoin( $table, $alias = null, $conds = [] ) {
74 $this->addJoin( 'LEFT JOIN', $table, $alias, $conds );
75 return $this;
76 }
77
92 public function join( $table, $alias = null, $conds = [] ) {
93 $this->addJoin( 'JOIN', $table, $alias, $conds );
94 return $this;
95 }
96
111 public function straightJoin( $table, $alias = null, $conds = [] ) {
112 $this->addJoin( 'STRAIGHT_JOIN', $table, $alias, $conds );
113 return $this;
114 }
115
123 private function addJoin( $type, $table, $alias, $joinConds ) {
124 if ( !$this->tables ) {
125 throw new \LogicException( __METHOD__ .
126 ': cannot add a join unless a regular table is added first' );
127 }
128 if ( $alias === null ) {
129 if ( is_string( $table ) ) {
130 $alias = $table;
131 } else {
132 $alias = $this->getAutoAlias();
133 }
134 }
135 if ( isset( $this->joinConds[$alias] ) ) {
136 throw new \LogicException( __METHOD__ .
137 ": a join with alias \"$alias\" has already been added" );
138 }
139 if ( $table instanceof JoinGroup ) {
140 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
141 if ( $conflicts ) {
142 $conflict = reset( $conflicts );
143 throw new \LogicException( __METHOD__ .
144 ": a join with alias \"$conflict\" has already been added" );
145 }
146 $this->tables[$alias] = $table->getRawTables();
147 $this->joinConds += $table->getRawJoinConds();
148 } elseif ( $table instanceof SelectQueryBuilder ) {
149 $this->tables[$alias] = new Subquery( $table->getSQL() );
150 } elseif ( is_string( $table ) ) {
151 $this->tables[$alias] = $table;
152 } else {
153 throw new InvalidArgumentException( __METHOD__ .
154 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
155 }
156 $this->joinConds[$alias] = [ $type, $joinConds ];
157 $this->lastAlias = $alias;
158 }
159
163 abstract protected function getAutoAlias();
164}
Shared code between SelectQueryBuilder and JoinGroup to represent tables and join conditions.
join( $table, $alias=null, $conds=[])
Inner join a table or group of tables.
table( $table, $alias=null)
Add a single table or a single parenthesized group.
leftJoin( $table, $alias=null, $conds=[])
Left join a table or group of tables.
straightJoin( $table, $alias=null, $conds=[])
Straight join a table or group of tables.
Parenthesized group of table names and their join types and conditions.
Definition JoinGroup.php:10
Build SELECT queries with a fluent interface.