diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php index f83637535..cee851e80 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php @@ -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; } /** diff --git a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php index 9ab6a0f4e..4dcef06e3 100644 --- a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php @@ -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); + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php index 2b2a4cfae..ad0f9ecae 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php @@ -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();