Skip to content

Commit

Permalink
[update] - update query builder updated
Browse files Browse the repository at this point in the history
  • Loading branch information
iloElias committed Oct 9, 2024
1 parent 2fcca9b commit 534c41a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/Database/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function set(string|array $column, $value = null): Update
$column = Utils::sanitizeForPostgres($column);
$paramName = ":$column";
$this->set[$column] = $paramName;
$this->parameters[$paramName] = $value;
$this->storeParameter($paramName, $value);
}
return $this;
}
Expand All @@ -53,6 +53,5 @@ public function getSql(): string
$whereClause = implode(" AND ", $this->where);

return "UPDATE {$this->table} SET $setClause" . ($whereClause ? " WHERE $whereClause" : "");
;
}
}
22 changes: 13 additions & 9 deletions tests/unit/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testUpdate()
$update->table($table::tableName())->set('nickname', 'updated_nickname')->where($conditions);

$expectedSql = "UPDATE {$table::tableName()} SET nickname = :nickname WHERE email = :where_email";
$expectedParams = [':nickname' => 'updated_nickname', ':where_email' => "'email@example.com'"];
$expectedParams = [':nickname' => "'updated_nickname'", ':where_email' => "'email@example.com'"];

$this->assertEquals($expectedSql, $update->getSql());
$this->assertEquals($expectedParams, $update->getParameters());
Expand All @@ -36,7 +36,7 @@ public function testUpdateWithInClause()
$update->table($table::tableName())->set('nickname', 'updated_nickname')->in($conditions);

$expectedSql = "UPDATE {$table::tableName()} SET nickname = :nickname WHERE id IN(:in_id_0,:in_id_1,:in_id_2)";
$expectedParams = [':nickname' => 'updated_nickname', ':in_id_0' => 1, ':in_id_1' => 2, ':in_id_2' => 3];
$expectedParams = [':nickname' => "'updated_nickname'", ':in_id_0' => 1, ':in_id_1' => 2, ':in_id_2' => 3];

$this->assertEquals($expectedSql, $update->getSql());
$this->assertEquals($expectedParams, $update->getParameters());
Expand All @@ -52,7 +52,7 @@ public function testUpdateWithMultipleConditions()
$update->table($table::tableName())->set('nickname', 'updated_nickname')->where($conditions);

$expectedSql = "UPDATE {$table::tableName()} SET nickname = :nickname WHERE email = :where_email AND active = :where_active";
$expectedParams = [':nickname' => 'updated_nickname', ':where_email' => "'email@example.com'", ':where_active' => true];
$expectedParams = [':nickname' => "'updated_nickname'", ':where_email' => "'email@example.com'", ':where_active' => "TRUE"];

$this->assertEquals($expectedSql, $update->getSql());
$this->assertEquals($expectedParams, $update->getParameters());
Expand All @@ -72,9 +72,13 @@ public function testMultipleInConditions()

$expectedSql = "UPDATE {$table::tableName()} SET nickname = :nickname WHERE id IN(:in_id_0,:in_id_1,:in_id_2) AND group_id IN(:in_group_id_0,:in_group_id_1,:in_group_id_2)";
$expectedParams = [
':nickname' => 'updated_nickname',
':in_id_0' => 1, ':in_id_1' => 2, ':in_id_2' => 3,
':in_group_id_0' => 10, ':in_group_id_1' => 20, ':in_group_id_2' => 30
':nickname' => "'updated_nickname'",
':in_id_0' => 1,
':in_id_1' => 2,
':in_id_2' => 3,
':in_group_id_0' => 10,
':in_group_id_1' => 20,
':in_group_id_2' => 30
];

$this->assertEquals($expectedSql, $update->getSql());
Expand All @@ -90,7 +94,7 @@ public function testUpdateWithoutConditions()
$update->table($table::tableName())->set('nickname', 'updated_nickname');

$expectedSql = "UPDATE {$table::tableName()} SET nickname = :nickname";
$expectedParams = [':nickname' => 'updated_nickname'];
$expectedParams = [':nickname' => "'updated_nickname'"];

$this->assertEquals($expectedSql, $update->getSql());
$this->assertEquals($expectedParams, $update->getParameters());
Expand All @@ -107,7 +111,7 @@ public function testUpdateWithTimestamp()
$update->table($table::tableName())->set('updated_at', $date)->where($conditions);

$expectedSql = "UPDATE {$table::tableName()} SET updated_at = :updated_at WHERE email = :where_email";
$expectedParams = [':updated_at' => $date, ':where_email' => "'email@example.com'"];
$expectedParams = [':updated_at' => "'{$date}'", ':where_email' => "'email@example.com'"];

$this->assertEquals($expectedSql, $update->getSql());
$this->assertEquals($expectedParams, $update->getParameters());
Expand All @@ -123,7 +127,7 @@ public function testUpdateMultipleSetClauses()
$update->table($table::tableName())->set('nickname', 'updated_nickname')->set('active', false)->where($conditions);

$expectedSql = "UPDATE {$table::tableName()} SET nickname = :nickname, active = :active WHERE email = :where_email";
$expectedParams = [':nickname' => 'updated_nickname', ':active' => false, ':where_email' => '\'email@example.com\''];
$expectedParams = [':nickname' => "'updated_nickname'", ':active' => 'FALSE', ':where_email' => "'email@example.com'"];

$this->assertEquals($expectedSql, $update->getSql());
$this->assertEquals($expectedParams, $update->getParameters());
Expand Down

0 comments on commit 534c41a

Please sign in to comment.