Skip to content

Commit

Permalink
fix: ad hoc fix for join() with BETWEEN
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Apr 15, 2024
1 parent 6a65664 commit b19a785
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
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

0 comments on commit b19a785

Please sign in to comment.