31 public function table( $table, $alias =
null ) {
33 $alias ??= $table->getAlias();
34 $table = $table->getRawTables();
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.' );
43 } elseif ( !is_string( $table ) ) {
44 throw new InvalidArgumentException( __METHOD__ .
45 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
47 if ( $alias ===
null ) {
48 $this->tables[] = $table;
49 $this->lastAlias = $table;
51 $this->tables[$alias] = $table;
52 $this->lastAlias = $alias;
67 public function leftJoin( $table, $alias =
null, $conds = [] ) {
68 $this->addJoin(
'LEFT JOIN', $table, $alias, $conds );
82 public function join( $table, $alias =
null, $conds = [] ) {
83 $this->addJoin(
'JOIN', $table, $alias, $conds );
97 public function straightJoin( $table, $alias =
null, $conds = [] ) {
98 $this->addJoin(
'STRAIGHT_JOIN', $table, $alias, $conds );
109 private function addJoin( $type, $table, $alias,
$joinConds ) {
110 if ( !$this->tables ) {
111 throw new \LogicException( __METHOD__ .
112 ': cannot add a join unless a regular table is added first' );
114 if ( $alias ===
null ) {
115 if ( is_string( $table ) ) {
121 if ( isset( $this->joinConds[$alias] ) ) {
122 throw new \LogicException( __METHOD__ .
123 ": a join with alias \"$alias\" has already been added" );
125 if ( $table instanceof JoinGroup ) {
126 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
128 $conflict = reset( $conflicts );
129 throw new \LogicException( __METHOD__ .
130 ": a join with alias \"$conflict\" has already been added" );
132 $this->tables[$alias] = $table->getRawTables();
133 $this->joinConds += $table->getRawJoinConds();
134 } elseif ( $table instanceof SelectQueryBuilder ) {
135 $this->tables[$alias] =
new Subquery( $table->getSQL() );
136 } elseif ( is_string( $table ) ) {
137 $this->tables[$alias] = $table;
139 throw new InvalidArgumentException( __METHOD__ .
140 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
142 $this->joinConds[$alias] = [ $type,
$joinConds ];
143 $this->lastAlias = $alias;