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