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
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
70 public function leftJoin( $table, $alias = null, $conds = [] ) {
71 $this->addJoin( 'LEFT JOIN', $table, $alias, $conds );
72 return $this;
73 }
74
86 public function join( $table, $alias = null, $conds = [] ) {
87 $this->addJoin( 'JOIN', $table, $alias, $conds );
88 return $this;
89 }
90
102 public function straightJoin( $table, $alias = null, $conds = [] ) {
103 $this->addJoin( 'STRAIGHT_JOIN', $table, $alias, $conds );
104 return $this;
105 }
106
114 private function addJoin( $type, $table, $alias, $joinConds ) {
115 if ( !$this->tables ) {
116 throw new \LogicException( __METHOD__ .
117 ': cannot add a join unless a regular table is added first' );
118 }
119 if ( $alias === null ) {
120 if ( is_string( $table ) ) {
121 $alias = $table;
122 } else {
123 $alias = $this->getAutoAlias();
124 }
125 }
126 if ( isset( $this->joinConds[$alias] ) ) {
127 throw new \LogicException( __METHOD__ .
128 ": a join with alias \"$alias\" has already been added" );
129 }
130 if ( $table instanceof JoinGroup ) {
131 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
132 if ( $conflicts ) {
133 $conflict = reset( $conflicts );
134 throw new \LogicException( __METHOD__ .
135 ": a join with alias \"$conflict\" has already been added" );
136 }
137 $this->tables[$alias] = $table->getRawTables();
138 $this->joinConds += $table->getRawJoinConds();
139 } elseif ( $table instanceof SelectQueryBuilder ) {
140 $this->tables[$alias] = new Subquery( $table->getSQL() );
141 } elseif ( is_string( $table ) ) {
142 $this->tables[$alias] = $table;
143 } else {
144 throw new InvalidArgumentException( __METHOD__ .
145 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
146 }
147 $this->joinConds[$alias] = [ $type, $joinConds ];
148 $this->lastAlias = $alias;
149 }
150
151 abstract protected function getAutoAlias();
152}
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.