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