1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
<?php
namespace Wikimedia\Rdbms;
/**
* A class for code shared between SelectQueryBuilder and JoinGroup.
* Represents tables and join conditions.
*/
abstract class JoinGroupBase {
/** @var array */
protected $tables = [];
/** @var array */
protected $joinConds = [];
protected $lastAlias;
/**
* Add a single table or a single parenthesized group.
*
* @param string|JoinGroup|SelectQueryBuilder $table The table to add. If
* this is a string, it is the table name. If it is a JoinGroup created
* by SelectQueryBuilder::newJoinGroup(), the group will be added. If it
* is a SelectQueryBuilder, a table subquery will be added.
* @param string|null $alias The table alias, or null for no alias
* @return $this
*/
public function table( $table, $alias = null ) {
if ( $table instanceof JoinGroup ) {
if ( $alias === null ) {
$alias = $table->getAlias();
}
$table = $table->getRawTables();
} elseif ( $table instanceof SelectQueryBuilder ) {
if ( $alias === null ) {
$alias = $this->getAutoAlias();
}
$table = new Subquery( $table->getSQL() );
} elseif ( !is_string( $table ) ) {
throw new \InvalidArgumentException( __METHOD__ .
': $table must be either string, JoinGroup or SelectQueryBuilder' );
}
if ( $alias === null ) {
$this->tables[] = $table;
$this->lastAlias = $table;
} else {
$this->tables[$alias] = $table;
$this->lastAlias = $alias;
}
return $this;
}
/**
* Left join a table or group of tables. This should be called after table().
*
* @param string|JoinGroup|SelectQueryBuilder $table The table name, or a
* JoinGroup containing multiple tables, or a SelectQueryBuilder
* representing a subquery.
* @param string|null $alias The alias name, or null to automatically
* generate an alias which will be unique to this builder
* @param string|array $conds The conditions for the ON clause
* @return $this
*/
public function leftJoin( $table, $alias = null, $conds = [] ) {
$this->addJoin( 'LEFT JOIN', $table, $alias, $conds );
return $this;
}
/**
* Inner join a table or group of tables. This should be called after table().
*
* @param string|JoinGroup|SelectQueryBuilder $table The table name, or a
* JoinGroup containing multiple tables, or a SelectQueryBuilder
* representing a subquery.
* @param string|null $alias The alias name, or null to automatically
* generate an alias which will be unique to this builder
* @param string|array $conds The conditions for the ON clause
* @return $this
*/
public function join( $table, $alias = null, $conds = [] ) {
$this->addJoin( 'JOIN', $table, $alias, $conds );
return $this;
}
/**
* Straight join a table or group of tables. This should be called after table().
*
* @param string|JoinGroup|SelectQueryBuilder $table The table name, or a
* JoinGroup containing multiple tables, or a SelectQueryBuilder
* representing a subquery.
* @param string|null $alias The alias name, or null to automatically
* generate an alias which will be unique to this builder
* @param string|array $conds The conditions for the ON clause
* @return $this
*/
public function straightJoin( $table, $alias = null, $conds = [] ) {
$this->addJoin( 'STRAIGHT_JOIN', $table, $alias, $conds );
return $this;
}
/**
* Private helper for functions that add joins
* @param string $type
* @param string|JoinGroup|SelectQueryBuilder $table
* @param string|null $alias
* @param string|array $joinConds
*/
private function addJoin( $type, $table, $alias, $joinConds ) {
if ( !$this->tables ) {
throw new \LogicException( __METHOD__ .
': cannot add a join unless a regular table is added first' );
}
if ( $alias === null ) {
if ( is_string( $table ) ) {
$alias = $table;
} else {
$alias = $this->getAutoAlias();
}
}
if ( isset( $this->joinConds[$alias] ) ) {
throw new \LogicException( __METHOD__ .
": a join with alias \"$alias\" has already been added" );
}
if ( $table instanceof JoinGroup ) {
$conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
if ( $conflicts ) {
$conflict = reset( $conflicts );
throw new \LogicException( __METHOD__ .
": a join with alias \"$conflict\" has already been added" );
}
$this->tables[$alias] = $table->getRawTables();
$this->joinConds += $table->getRawJoinConds();
} elseif ( $table instanceof SelectQueryBuilder ) {
$this->tables[$alias] = new Subquery( $table->getSQL() );
} elseif ( is_string( $table ) ) {
$this->tables[$alias] = $table;
} else {
throw new \InvalidArgumentException( __METHOD__ .
': $table must be either string, JoinGroup or SelectQueryBuilder' );
}
$this->joinConds[$alias] = [ $type, $joinConds ];
$this->lastAlias = $alias;
}
abstract protected function getAutoAlias();
}
|