Commit 371f3d5e authored by beberlei's avatar beberlei

[2.0] DDC-321, DDC-323, DDC-324 - Implemented way to define UDFs from PHP in...

[2.0] DDC-321, DDC-323, DDC-324 - Implemented way to define UDFs from PHP in Sqlite Driver and registered to required default callbacks for SQRT and MOD, allowing functional tests for DQL MOD and SQRT to pass for all platforms.
parent 13bf8a76
...@@ -28,6 +28,14 @@ namespace Doctrine\DBAL\Driver\PDOSqlite; ...@@ -28,6 +28,14 @@ namespace Doctrine\DBAL\Driver\PDOSqlite;
*/ */
class Driver implements \Doctrine\DBAL\Driver class Driver implements \Doctrine\DBAL\Driver
{ {
/**
* @var array
*/
protected $_userDefinedFunctions = array(
'sqrt' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfSqrt'), 'numArgs' => 1),
'mod' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfMod'), 'numArgs' => 2),
);
/** /**
* Tries to establish a database connection to SQLite. * Tries to establish a database connection to SQLite.
* *
...@@ -39,12 +47,24 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -39,12 +47,24 @@ class Driver implements \Doctrine\DBAL\Driver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{ {
return new \Doctrine\DBAL\Driver\PDOConnection( if (isset($driverOptions['userDefinedFunctions'])) {
$this->_userDefinedFunctions = array_merge(
$this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
unset($driverOptions['userDefinedFunctions']);
}
$pdo = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
foreach ($this->_userDefinedFunctions AS $fn => $data) {
$pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
}
return $pdo;
} }
/** /**
......
...@@ -384,4 +384,23 @@ class SqlitePlatform extends AbstractPlatform ...@@ -384,4 +384,23 @@ class SqlitePlatform extends AbstractPlatform
{ {
return 'DELETE FROM '.$tableName; return 'DELETE FROM '.$tableName;
} }
/**
* User-defined function for Sqlite that is used with PDO::sqliteCreateFunction()
*
* @param int|float $value
* @return float
*/
static public function udfSqrt($value)
{
return sqrt($value);
}
/**
* User-defined function for Sqlite that implements MOD(a, b)
*/
static public function udfMod($a, $b)
{
return ($a % $b);
}
} }
...@@ -125,8 +125,6 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -125,8 +125,6 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase
public function testFunctionMod() public function testFunctionMod()
{ {
$this->markTestSkipped('MOD does not exist on SqLite');
$result = $this->_em->createQuery("SELECT m, MOD(m.salary, 3500) AS amod FROM Doctrine\Tests\Models\Company\CompanyManager m") $result = $this->_em->createQuery("SELECT m, MOD(m.salary, 3500) AS amod FROM Doctrine\Tests\Models\Company\CompanyManager m")
->getArrayResult(); ->getArrayResult();
...@@ -139,8 +137,6 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -139,8 +137,6 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase
public function testFunctionSqrt() public function testFunctionSqrt()
{ {
$this->markTestSkipped('SQRT does not exist on SqLite');
$result = $this->_em->createQuery("SELECT m, SQRT(m.salary) AS sqrtsalary FROM Doctrine\Tests\Models\Company\CompanyManager m") $result = $this->_em->createQuery("SELECT m, SQRT(m.salary) AS sqrtsalary FROM Doctrine\Tests\Models\Company\CompanyManager m")
->getArrayResult(); ->getArrayResult();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment