MediaWiki 1.42.1
JoinGroupBase.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
10abstract class JoinGroupBase {
12 protected $tables = [];
13
15 protected $joinConds = [];
16
17 protected $lastAlias;
18
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
68 public function leftJoin( $table, $alias = null, $conds = [] ) {
69 $this->addJoin( 'LEFT JOIN', $table, $alias, $conds );
70 return $this;
71 }
72
84 public function join( $table, $alias = null, $conds = [] ) {
85 $this->addJoin( 'JOIN', $table, $alias, $conds );
86 return $this;
87 }
88
100 public function straightJoin( $table, $alias = null, $conds = [] ) {
101 $this->addJoin( 'STRAIGHT_JOIN', $table, $alias, $conds );
102 return $this;
103 }
104
112 private function addJoin( $type, $table, $alias, $joinConds ) {
113 if ( !$this->tables ) {
114 throw new \LogicException( __METHOD__ .
115 ': cannot add a join unless a regular table is added first' );
116 }
117 if ( $alias === null ) {
118 if ( is_string( $table ) ) {
119 $alias = $table;
120 } else {
121 $alias = $this->getAutoAlias();
122 }
123 }
124 if ( isset( $this->joinConds[$alias] ) ) {
125 throw new \LogicException( __METHOD__ .
126 ": a join with alias \"$alias\" has already been added" );
127 }
128 if ( $table instanceof JoinGroup ) {
129 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
130 if ( $conflicts ) {
131 $conflict = reset( $conflicts );
132 throw new \LogicException( __METHOD__ .
133 ": a join with alias \"$conflict\" has already been added" );
134 }
135 $this->tables[$alias] = $table->getRawTables();
136 $this->joinConds += $table->getRawJoinConds();
137 } elseif ( $table instanceof SelectQueryBuilder ) {
138 $this->tables[$alias] = new Subquery( $table->getSQL() );
139 } elseif ( is_string( $table ) ) {
140 $this->tables[$alias] = $table;
141 } else {
142 throw new \InvalidArgumentException( __METHOD__ .
143 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
144 }
145 $this->joinConds[$alias] = [ $type, $joinConds ];
146 $this->lastAlias = $alias;
147 }
148
149 abstract protected function getAutoAlias();
150}
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.