Skip to content

Commit 857ccd9

Browse files
committed
refactor join() method
1 parent 5a473d8 commit 857ccd9

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/QueryBuilder.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class QueryBuilder
1111
private const COND_OPERATORS = ['=', '>', '<', '>=', '<=', '!=', '<>', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN'];
1212
private const LOGICS = ['AND', 'OR', 'NOT'];
1313
private const SORT_TYPES = ['ASC', 'DESC'];
14-
private const JOIN_TYPES = ['INNER', 'LEFT OUTER', 'RIGHT OUTER', 'FULL OUTER', 'CROSS'];
14+
private const JOIN_TYPES = ['INNER', 'LEFT', 'LEFT OUTER', 'RIGHT OUTER', 'FULL OUTER', 'CROSS'];
1515
private const SQLITE_JOIN_TYPES = ['INNER', 'LEFT', 'LEFT OUTER', 'CROSS'];
1616
private const FIELD_SPEC_CHARS = ['+', '-', '*', '/', '%', '(', ')', '||'];
1717
private const NO_FETCH = 0;
@@ -221,7 +221,7 @@ public function count($table, string $field = '')
221221

222222
/**
223223
* @param string $column
224-
* @return $this|string|array
224+
* @return QueryBuilder|string|array
225225
*/
226226
public function column(string $column = 'id')
227227
{
@@ -922,18 +922,26 @@ public function join($table, $on, string $join_type = 'INNER'): QueryBuilder
922922
{
923923
$join_type = strtoupper($join_type);
924924

925-
if (empty($join_type) || !in_array($join_type, self::JOIN_TYPES)) {
926-
$this->setError('Empty $join_type or is not allowed in ' . __METHOD__);
927-
return $this;
928-
}
925+
if (empty($table)) {
926+
$this->setError('Empty $table in ' . __METHOD__);
927+
return $this;
928+
}
929929

930-
if (empty($table)) {
931-
$this->setError('Empty $table in ' . __METHOD__);
932-
return $this;
933-
}
930+
if (empty($join_type)) {
931+
$this->setError('Empty $join_type in ' . __METHOD__);
932+
return $this;
933+
}
934+
935+
if ($this->getDriver() == 'sqlite' && !in_array($join_type, self::SQLITE_JOIN_TYPES)) {
936+
$this->setError('$join_type is not allowed in SQLite in ' . __METHOD__);
937+
return $this;
938+
} else if (!in_array($join_type, self::JOIN_TYPES)) {
939+
$this->setError('$join_type is not allowed in ' . __METHOD__);
940+
return $this;
941+
}
934942

935943
if (is_array($table) || is_string($table)) {
936-
$this->sql .= " {$join_type} JOIN {$this->prepareAliases($table)}";
944+
$this->sql .= " {$join_type} JOIN {$this->prepareTables($table)}";
937945
} else {
938946
$this->setError('Incorrect type of $table in ' . __METHOD__ . '. $table must be a string or an array.');
939947
return $this;
@@ -950,8 +958,6 @@ public function join($table, $on, string $join_type = 'INNER'): QueryBuilder
950958
}
951959
}
952960

953-
$this->setError();
954-
955961
return $this;
956962
}
957963

0 commit comments

Comments
 (0)