From 534c41aa8ddbd0db78d534987ef008887fc0d71b Mon Sep 17 00:00:00 2001 From: Murilo Elias Date: Wed, 9 Oct 2024 18:51:30 -0300 Subject: [PATCH] [update] - update query builder updated --- src/Database/Update.php | 3 +-- tests/unit/UpdateTest.php | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Database/Update.php b/src/Database/Update.php index f3eed07..5b9d8ca 100644 --- a/src/Database/Update.php +++ b/src/Database/Update.php @@ -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; } @@ -53,6 +53,5 @@ public function getSql(): string $whereClause = implode(" AND ", $this->where); return "UPDATE {$this->table} SET $setClause" . ($whereClause ? " WHERE $whereClause" : ""); - ; } } diff --git a/tests/unit/UpdateTest.php b/tests/unit/UpdateTest.php index ab58d40..bf54777 100644 --- a/tests/unit/UpdateTest.php +++ b/tests/unit/UpdateTest.php @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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());