Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a DB::execute($query) to execute unprepared queries #54

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ public function statement($query, $bindings = array())
});
}

/**
* Execute an unprepared SQL statement and return the boolean result.
*
* @param string $query
* @return bool
*/
public function execute($query)
{
return $this->run($query, array(), function($me, $query, $bindings)
{
// Since this is an unprepared statement, $bindings was kept in closure list
// of parameters just for compatibility with run()'s callback call.
if ($me->pretending()) return true;

return $me->getPdo()->exec($query);
});
}

/**
* Run an SQL statement and get the number of rows affected.
*
Expand Down Expand Up @@ -664,4 +682,4 @@ public function withTablePrefix(Grammar $grammar)
return $grammar;
}

}
}
11 changes: 10 additions & 1 deletion tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ public function testSchemaBuilderCanBeCreated()
}


public function testExecuteMethod()
{
$connection = $this->getMockConnection(array('execute'));
$connection->expects($this->once())->method('execute')->with($this->equalTo('foo'))->will($this->returnValue(true));
$results = $connection->execute('foo');
$this->assertTrue($results);
}


protected function getMockConnection($methods = array(), $pdo = null)
{
$pdo = $pdo ?: new DatabaseConnectionTestMockPDO;
Expand All @@ -212,4 +221,4 @@ protected function getMockConnection($methods = array(), $pdo = null)

}

class DatabaseConnectionTestMockPDO extends PDO { public function __construct() {} }
class DatabaseConnectionTestMockPDO extends PDO { public function __construct() {} }