Skip to content

Commit

Permalink
Make atomic updateAll and deleteAll return the affected rows instead …
Browse files Browse the repository at this point in the history
…of bool.
  • Loading branch information
euromark committed Dec 10, 2014
1 parent 6f9131a commit a73ec6a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Database/Statement/SqliteStatement.php
Expand Up @@ -32,7 +32,7 @@ public function rowCount() {
$changes->execute();
$count = $changes->fetch()[0];
$changes->closeCursor();
return $count;
return (int)$count;
}
return parent::rowCount();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Datasource/RepositoryInterface.php
Expand Up @@ -76,7 +76,7 @@ public function query();
* @param array $fields A hash of field => new value.
* @param mixed $conditions Conditions to be used, accepts anything Query::where()
* can take.
* @return bool Success Returns true if one or more rows are affected.
* @return int Count Returns the affected rows.
*/
public function updateAll($fields, $conditions);

Expand All @@ -94,7 +94,7 @@ public function updateAll($fields, $conditions);
*
* @param mixed $conditions Conditions to be used, accepts anything Query::where()
* can take.
* @return bool Success Returns true if one or more rows are affected.
* @return int Count Returns the affected rows.
* @see RepositoryInterface::delete()
*/
public function deleteAll($conditions);
Expand Down
6 changes: 2 additions & 4 deletions src/ORM/Table.php
Expand Up @@ -1024,9 +1024,8 @@ public function updateAll($fields, $conditions) {
->set($fields)
->where($conditions);
$statement = $query->execute();
$success = $statement->rowCount() > 0;
$statement->closeCursor();
return $success;
return $statement->rowCount();
}

/**
Expand Down Expand Up @@ -1104,9 +1103,8 @@ public function deleteAll($conditions) {
->delete()
->where($conditions);
$statement = $query->execute();
$success = $statement->rowCount() > 0;
$statement->closeCursor();
return $success;
return $statement->rowCount();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -571,7 +571,7 @@ public function testUpdateAll() {
]);
$fields = ['username' => 'mark'];
$result = $table->updateAll($fields, ['id <' => 4]);
$this->assertTrue($result);
$this->assertSame(3, $result);

$result = $table->find('all')
->select(['username'])
Expand Down Expand Up @@ -617,7 +617,7 @@ public function testDeleteAll() {
'connection' => $this->connection,
]);
$result = $table->deleteAll(['id <' => 4]);
$this->assertTrue($result);
$this->assertSame(3, $result);

$result = $table->find('all')->toArray();
$this->assertCount(1, $result, 'Only one record should remain');
Expand All @@ -636,7 +636,7 @@ public function testDeleteAllAliasedConditions() {
'connection' => $this->connection,
]);
$result = $table->deleteAll(['Managers.id <' => 4]);
$this->assertTrue($result);
$this->assertSame(3, $result);

$result = $table->find('all')->toArray();
$this->assertCount(1, $result, 'Only one record should remain');
Expand Down

0 comments on commit a73ec6a

Please sign in to comment.