| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Rdbms; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
abstract class JoinGroupBase { |
| 13 |
|
| 14 |
protected $tables = []; |
| 15 |
|
| 16 |
|
| 17 |
protected $joinConds = []; |
| 18 |
|
| 19 |
|
| 20 |
protected $lastAlias; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function leftJoin( $table, $alias = null, $conds = [] ) { |
| 71 |
$this->addJoin( 'LEFT JOIN', $table, $alias, $conds ); |
| 72 |
return $this; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function join( $table, $alias = null, $conds = [] ) { |
| 87 |
$this->addJoin( 'JOIN', $table, $alias, $conds ); |
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function straightJoin( $table, $alias = null, $conds = [] ) { |
| 103 |
$this->addJoin( 'STRAIGHT_JOIN', $table, $alias, $conds ); |
| 104 |
return $this; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
} |