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
19 protected $lastAlias;
20
31 public function table( $table, $alias = null ) {
32 if ( $table instanceof JoinGroup ) {
33 $alias ??= $table->getAlias();
34 $table = $table->getRawTables();
35 } elseif ( $table instanceof SelectQueryBuilder ) {
36 $alias ??= $this->getAutoAlias();
37 $table = new Subquery( $table->getSQL() );
38 } elseif ( $table instanceof Subquery ) {
39 if ( $alias === null ) {
40 throw new InvalidArgumentException( __METHOD__ .
41 ': Subquery as table must provide an alias.' );
42 }
43 } elseif ( !is_string( $table ) ) {
44 throw new InvalidArgumentException( __METHOD__ .
45 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
46 }
47 if ( $alias === null ) {
48 $this->tables[] = $table;
49 $this->lastAlias = $table;
50 } else {
51 $this->tables[$alias] = $table;
52 $this->lastAlias = $alias;
53 }
54 return $this;
55 }
56
67 public function leftJoin( $table, $alias = null, $conds = [] ) {
68 $this->addJoin( 'LEFT JOIN', $table, $alias, $conds );
69 return $this;
70 }
71
82 public function join( $table, $alias = null, $conds = [] ) {
83 $this->addJoin( 'JOIN', $table, $alias, $conds );
84 return $this;
85 }
86
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}
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.