1
0
mirror of synced 2025-01-30 03:51:43 +03:00

[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.

This commit is contained in:
beberlei 2010-02-11 21:38:58 +00:00
parent 13bf8a760f
commit 371f3d5ecc
3 changed files with 40 additions and 5 deletions

View File

@ -28,6 +28,14 @@ namespace Doctrine\DBAL\Driver\PDOSqlite;
*/
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.
*
@ -39,12 +47,24 @@ class Driver implements \Doctrine\DBAL\Driver
*/
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),
$username,
$password,
$driverOptions
);
foreach ($this->_userDefinedFunctions AS $fn => $data) {
$pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
}
return $pdo;
}
/**

View File

@ -384,4 +384,23 @@ class SqlitePlatform extends AbstractPlatform
{
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);
}
}

View File

@ -125,8 +125,6 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase
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")
->getArrayResult();
@ -139,8 +137,6 @@ class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase
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")
->getArrayResult();