MediaWiki REL1_37
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
92 private function addJoin( $type, $table, $alias, $joinConds ) {
93 if ( !$this->tables ) {
94 throw new \LogicException( __METHOD__ .
95 ': cannot add a join unless a regular table is added first' );
96 }
97 if ( $alias === null ) {
98 if ( is_string( $table ) ) {
99 $alias = $table;
100 } else {
101 $alias = $this->getAutoAlias();
102 }
103 }
104 if ( isset( $this->joinConds[$alias] ) ) {
105 throw new \LogicException( __METHOD__ .
106 ": a join with alias \"$alias\" has already been added" );
107 }
108 if ( $table instanceof JoinGroup ) {
109 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
110 if ( $conflicts ) {
111 $conflict = reset( $conflicts );
112 throw new \LogicException( __METHOD__ .
113 ": a join with alias \"$conflict\" has already been added" );
114 }
115 $this->tables[$alias] = $table->getRawTables();
116 $this->joinConds += $table->getRawJoinConds();
117 } elseif ( $table instanceof SelectQueryBuilder ) {
118 $this->tables[$alias] = new Subquery( $table->getSQL() );
119 } elseif ( is_string( $table ) ) {
120 $this->tables[$alias] = $table;
121 } else {
122 throw new \InvalidArgumentException( __METHOD__ .
123 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
124 }
125 $this->joinConds[$alias] = [ $type, $joinConds ];
126 $this->lastAlias = $alias;
127 }
128
129 abstract protected function getAutoAlias();
130}
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.
addJoin( $type, $table, $alias, $joinConds)
Private helper for functions that add joins.
leftJoin( $table, $alias=null, $conds=[])
Left 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