Skip to content

Commit

Permalink
Add method to execute an ORM\Query and get a statement back.
Browse files Browse the repository at this point in the history
It is sometimes useful to have access to the statement object. In the
case of updateAll/deleteAll, the row count is useful. Shoehorning the
rowcount into ORM\ResultSet felt more awkward.
  • Loading branch information
markstory committed Jun 23, 2013
1 parent b59c037 commit e6295f1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Database/Query.php
Expand Up @@ -180,7 +180,7 @@ public function connection($connection = null) {
* Resulting statement is traversable, so it can be used in any loop as you would
* with an array.
*
* @return Cake\Database\Statement
* @return Cake\Database\StatementInterface
*/
public function execute() {
$query = $this->_transformQuery();
Expand Down
12 changes: 11 additions & 1 deletion lib/Cake/ORM/Query.php
Expand Up @@ -307,7 +307,7 @@ public function normalizedContainments() {

/**
* Compiles the SQL representation of this query and executes it using the
* configured connection object. Returns a ResultSet iterator object
* provided connection object. Returns a ResultSet iterator object
*
* Resulting object is traversable, so it can be used in any loop as you would
* with an array.
Expand All @@ -318,6 +318,16 @@ public function execute() {
return new ResultSet($this, parent::execute());
}

/**
* Compiles the SQL representation of this query ane executes it using
* the provided connection object.
*
* @return Cake\Database\StatementInterface
*/
public function executeStatement() {
return parent::execute();
}

/**
* Returns an array representation of the results after executing the query.
*
Expand Down
12 changes: 6 additions & 6 deletions lib/Cake/ORM/Table.php
Expand Up @@ -495,15 +495,15 @@ protected function _buildQuery() {
*
* @param array $fields A hash of field => new value.
* @param array $conditions An array of conditions, similar to those used with find()
* @return boolean Success
* @return boolean Success Returns true if one or more rows are effected.
*/
public function updateAll($fields, $conditions) {
$query = $this->_buildQuery();
$query->update($this->table())
->set($fields)
->where($conditions);
$query->execute();
return true;
$statement = $query->executeStatement();
return $statement->rowCount() > 0;
}

/**
Expand All @@ -519,14 +519,14 @@ public function updateAll($fields, $conditions) {
* with this method.
*
* @param array $conditions An array of conditions, similar to those used with find()
* @return boolean Success
* @return boolean Success Returns true if one or more rows are effected.
*/
public function deleteAll($conditions) {
$query = $this->_buildQuery();
$query->delete($this->table())
->where($conditions);
$query->execute();
return true;
$statement = $query->executeStatement();
return $statement->rowCount() > 0;
}

}
8 changes: 4 additions & 4 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -402,12 +402,12 @@ public function testUpdateAllFailure() {
['_buildQuery'],
['table' => 'users', 'connection' => $this->connection]
);
$query = $this->getMock('Cake\ORM\Query', ['execute'], [$this->connection]);
$query = $this->getMock('Cake\ORM\Query', ['executeStatement'], [$this->connection]);
$table->expects($this->once())
->method('_buildQuery')
->will($this->returnValue($query));
$query->expects($this->once())
->method('execute')
->method('executeStatement')
->will($this->throwException(new \Cake\Database\Exception('Not good')));
$table->updateAll(['username' => 'mark'], []);
}
Expand Down Expand Up @@ -438,12 +438,12 @@ public function testDeleteAllFailure() {
['_buildQuery'],
['table' => 'users', 'connection' => $this->connection]
);
$query = $this->getMock('Cake\ORM\Query', ['execute'], [$this->connection]);
$query = $this->getMock('Cake\ORM\Query', ['executeStatement'], [$this->connection]);
$table->expects($this->once())
->method('_buildQuery')
->will($this->returnValue($query));
$query->expects($this->once())
->method('execute')
->method('executeStatement')
->will($this->throwException(new \Cake\Database\Exception('Not good')));
$table->deleteAll(['id >' => 4]);
}
Expand Down

4 comments on commit e6295f1

@renan
Copy link
Contributor

@renan renan commented on e6295f1 Jun 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoehorning!? What does it means?

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a north american colloquialism. It is used when you're trying to jam/stick something where it may not belong, or space might not allow it.

@lorenzo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@renan
Copy link
Contributor

@renan renan commented on e6295f1 Jun 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, cool!
Google had a few results but none made sense for me.

Thanks! 😄

Please sign in to comment.