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;
68 public function leftJoin( $table, $alias =
null, $conds = [] ) {
69 $this->addJoin(
'LEFT JOIN', $table, $alias, $conds );
84 public function join( $table, $alias =
null, $conds = [] ) {
85 $this->addJoin(
'JOIN', $table, $alias, $conds );
101 $this->addJoin(
'STRAIGHT_JOIN', $table, $alias, $conds );
112 private function addJoin( $type, $table, $alias,
$joinConds ) {
113 if ( !$this->tables ) {
114 throw new \LogicException( __METHOD__ .
115 ': cannot add a join unless a regular table is added first' );
117 if ( $alias ===
null ) {
118 if ( is_string( $table ) ) {
124 if ( isset( $this->joinConds[$alias] ) ) {
125 throw new \LogicException( __METHOD__ .
126 ": a join with alias \"$alias\" has already been added" );
128 if ( $table instanceof JoinGroup ) {
129 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
131 $conflict = reset( $conflicts );
132 throw new \LogicException( __METHOD__ .
133 ": a join with alias \"$conflict\" has already been added" );
135 $this->tables[$alias] = $table->getRawTables();
136 $this->joinConds += $table->getRawJoinConds();
137 } elseif ( $table instanceof SelectQueryBuilder ) {
138 $this->tables[$alias] =
new Subquery( $table->getSQL() );
139 } elseif ( is_string( $table ) ) {
140 $this->tables[$alias] = $table;
142 throw new \InvalidArgumentException( __METHOD__ .
143 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
145 $this->joinConds[$alias] = [ $type,
$joinConds ];
146 $this->lastAlias = $alias;