33 public function table( $table, $alias =
null ) {
35 $alias ??= $table->getAlias();
36 $table = $table->getRawTables();
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.' );
45 } elseif ( !is_string( $table ) ) {
46 throw new InvalidArgumentException( __METHOD__ .
47 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
49 if ( $alias ===
null ) {
50 $this->tables[] = $table;
51 $this->lastAlias = $table;
53 $this->tables[$alias] = $table;
54 $this->lastAlias = $alias;
73 public function leftJoin( $table, $alias =
null, $conds = [] ) {
74 $this->addJoin(
'LEFT JOIN', $table, $alias, $conds );
92 public function join( $table, $alias =
null, $conds = [] ) {
93 $this->addJoin(
'JOIN', $table, $alias, $conds );
112 $this->addJoin(
'STRAIGHT_JOIN', $table, $alias, $conds );
123 private function addJoin( $type, $table, $alias,
$joinConds ) {
124 if ( !$this->tables ) {
125 throw new \LogicException( __METHOD__ .
126 ': cannot add a join unless a regular table is added first' );
128 if ( $alias ===
null ) {
129 if ( is_string( $table ) ) {
135 if ( isset( $this->joinConds[$alias] ) ) {
136 throw new \LogicException( __METHOD__ .
137 ": a join with alias \"$alias\" has already been added" );
139 if ( $table instanceof JoinGroup ) {
140 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
142 $conflict = reset( $conflicts );
143 throw new \LogicException( __METHOD__ .
144 ": a join with alias \"$conflict\" has already been added" );
146 $this->tables[$alias] = $table->getRawTables();
147 $this->joinConds += $table->getRawJoinConds();
148 } elseif ( $table instanceof SelectQueryBuilder ) {
149 $this->tables[$alias] =
new Subquery( $table->getSQL() );
150 } elseif ( is_string( $table ) ) {
151 $this->tables[$alias] = $table;
153 throw new InvalidArgumentException( __METHOD__ .
154 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
156 $this->joinConds[$alias] = [ $type,
$joinConds ];
157 $this->lastAlias = $alias;