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;
70 public function leftJoin( $table, $alias =
null, $conds = [] ) {
71 $this->addJoin(
'LEFT JOIN', $table, $alias, $conds );
86 public function join( $table, $alias =
null, $conds = [] ) {
87 $this->addJoin(
'JOIN', $table, $alias, $conds );
103 $this->addJoin(
'STRAIGHT_JOIN', $table, $alias, $conds );
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' );
119 if ( $alias ===
null ) {
120 if ( is_string( $table ) ) {
126 if ( isset( $this->joinConds[$alias] ) ) {
127 throw new \LogicException( __METHOD__ .
128 ": a join with alias \"$alias\" has already been added" );
130 if ( $table instanceof JoinGroup ) {
131 $conflicts = array_intersect_key( $this->joinConds, $table->getRawJoinConds() );
133 $conflict = reset( $conflicts );
134 throw new \LogicException( __METHOD__ .
135 ": a join with alias \"$conflict\" has already been added" );
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;
144 throw new InvalidArgumentException( __METHOD__ .
145 ': $table must be either string, JoinGroup or SelectQueryBuilder' );
147 $this->joinConds[$alias] = [ $type,
$joinConds ];
148 $this->lastAlias = $alias;