Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ public function join(string $table, $cond, string $type = '', ?bool $escape = nu
$cond = ' ON ' . $cond;
} else {
// Split multiple conditions
// @TODO This does not parse `BETWEEN a AND b` correctly.
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) {
$conditions = [];
$joints = $joints[0];
Expand All @@ -676,6 +677,13 @@ public function join(string $table, $cond, string $type = '', ?bool $escape = nu
foreach ($conditions as $i => $condition) {
$operator = $this->getOperator($condition);

// Workaround for BETWEEN
if ($operator === false) {
$cond .= $joints[$i] . $condition;

continue;
}

$cond .= $joints[$i];
$cond .= preg_match('/(\(*)?([\[\]\w\.\'-]+)' . preg_quote($operator, '/') . '(.*)/i', $condition, $match) ? $match[1] . $this->db->protectIdentifiers($match[2]) . $operator . $this->db->protectIdentifiers($match[3]) : $condition;
}
Expand Down
7 changes: 7 additions & 0 deletions system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ public function join(string $table, $cond, string $type = '', ?bool $escape = nu
foreach ($conditions as $i => $condition) {
$operator = $this->getOperator($condition);

// Workaround for BETWEEN
if ($operator === false) {
$cond .= $joints[$i] . $condition;

continue;
}

$cond .= $joints[$i];
$cond .= preg_match('/(\(*)?([\[\]\w\.\'-]+)' . preg_quote($operator, '/') . '(.*)/i', $condition, $match) ? $match[1] . $this->db->protectIdentifiers($match[2]) . $operator . $this->db->protectIdentifiers($match[3]) : $condition;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/system/Database/Builder/JoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ public function testJoinMultipleConditions(): void
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/8791
*/
public function testJoinMultipleConditionsBetween(): void
{
$builder = new BaseBuilder('table1', $this->db);

$builder->join(
'leases',
'units.unit_id = leases.unit_id AND CURDATE() BETWEEN lease_start_date AND lease_exp_date',
'LEFT'
);

// @TODO Should be `... CURDATE() BETWEEN "lease_start_date" AND "lease_exp_date"`
$expectedSQL = 'SELECT * FROM "table1" LEFT JOIN "leases" ON "units"."unit_id" = "leases"."unit_id" AND CURDATE() BETWEEN lease_start_date AND lease_exp_date';

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3832
*/
Expand Down