MediaWiki  master
JoinGroupBase.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
9 abstract class JoinGroupBase {
11  protected $tables = [];
12 
14  protected $joinConds = [];
15 
16  protected $lastAlias;
17 
28  public function table( $table, $alias = null ) {
29  if ( $table instanceof JoinGroup ) {
30  $alias ??= $table->getAlias();
31  $table = $table->getRawTables();
32  } elseif ( $table instanceof SelectQueryBuilder ) {
33  $alias ??= $this->getAutoAlias();
34  $table = new Subquery( $table->getSQL() );
35  } elseif ( $table instanceof Subquery ) {
36  if ( $alias === null ) {
37  throw new \InvalidArgumentException( __METHOD__ .
38  ': Subquery as table must provide an alias.' );
39  }
40  } elseif ( !is_string( $table ) ) {
41  throw new \InvalidArgumentException( __METHOD__ .
42  ': $table must be either string, JoinGroup or SelectQueryBuilder' );
43  }
44  if ( $alias === null ) {
45  $this->tables[] = $table;
46  $this->lastAlias = $table;
47  } else {
48  $this->tables[$alias] = $table;
49  $this->lastAlias = $alias;
50  }
51  return $this;
52  }
53 
65  public function leftJoin( $table, $alias = null, $conds = [] ) {
66  $this->addJoin( 'LEFT JOIN', $table, $alias, $conds );
67  return $this;
68  }
69 
81  public function join( $table, $alias = null, $conds = [] ) {
82  $this->addJoin( 'JOIN', $table, $alias, $conds );
83  return $this;
84  }
85 
97  public function straightJoin( $table, $alias = null, $conds = [] ) {
98  $this->addJoin( 'STRAIGHT_JOIN', $table, $alias, $conds );
99  return $this;
100  }
101 
109  private function addJoin( $type, $table, $alias, $joinConds ) {
110  if ( !$this->tables ) {
111  throw new \LogicException( __METHOD__ .
112  ': cannot add a join unless a regular table is added first' );
113  }
114  if ( $alias === null ) {
115  if ( is_string( $table ) ) {
116  $alias = $table;
117  } else {
118  $alias = $this->getAutoAlias();
119  }
120  }
121  if ( isset( $this->joinConds[$alias] ) ) {
122  throw new \LogicException( __METHOD__ .
123  ": a join with alias \"$alias\" has already been added" );
124  }
125  if ( $table instanceof JoinGroup ) {
126  $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
127  if ( $conflicts ) {
128  $conflict = reset( $conflicts );
129  throw new \LogicException( __METHOD__ .
130  ": a join with alias \"$conflict\" has already been added" );
131  }
132  $this->tables[$alias] = $table->getRawTables();
133  $this->joinConds += $table->getRawJoinConds();
134  } elseif ( $table instanceof SelectQueryBuilder ) {
135  $this->tables[$alias] = new Subquery( $table->getSQL() );
136  } elseif ( is_string( $table ) ) {
137  $this->tables[$alias] = $table;
138  } else {
139  throw new \InvalidArgumentException( __METHOD__ .
140  ': $table must be either string, JoinGroup or SelectQueryBuilder' );
141  }
142  $this->joinConds[$alias] = [ $type, $joinConds ];
143  $this->lastAlias = $alias;
144  }
145 
146  abstract protected function getAutoAlias();
147 }
A class for code shared between SelectQueryBuilder and JoinGroup.
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.
An object representing a parenthesized group of tables and their join types and conditions.
Definition: JoinGroup.php:9
A query builder for SELECT queries with a fluent interface.