Skip to content

Commit

Permalink
Merge pull request doctrine#5759 from derrabus/remove/sqlite-udf
Browse files Browse the repository at this point in the history
Remove user defined SQLite functions
  • Loading branch information
derrabus committed Oct 13, 2022
2 parents d8a5b7c + 7959ea4 commit 7289470
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 130 deletions.
42 changes: 28 additions & 14 deletions UPGRADE.md
Expand Up @@ -8,20 +8,34 @@ awareness about deprecated code.

# Upgrade to 4.0

## BC Break: Removed the `userDefinedFunctions` driver option for `pdo_sqlite`

To register a custom function, use `getNativeConnection()` to access the
wrapped PDO connection and register your custom function directly.

```php
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'path' => '/path/to/file.db',
]);

$connection->getNativeConnection()
->sqliteCreateFunction('my_function', MyClass::myMethod(...), 2);
```
## BC Break: Removed registration of user defined functions for SQLite

DBAL does not register functions for SQLite anymore. The following functions
which were previously provided by DBAL have been removed:

* `locate()`: SQLite provides the function `instr()` that behaves similarly.
Use `AbstractPlatform::getLocateExpression()` if you need a portable solution.
* `mod()`: SQLite provides a `%` operator for modulo calculations.
Use `AbstractPlatform::getModExpression()` if you need a portable solution.
Since version 3.35.0 SQLite also provides a `mod()` function if math
functions have been enabled.
* `sqrt()`: Upgrade to SQLite 3.35.0 and compile SQLite with math functions to
get a native `sqrt()` function. If you need a `sqrt()` implementation for an
earlier release of SQLite, you can polyfill it.

```php
// pdo_sqlite driver
$connection->getNativeConnection()
->sqliteCreateFunction('sqrt', \sqrt(...), 1);

// sqlite3 driver
$connection->getNativeConnection()
->createFunction('sqrt', \sqrt(...), 1);
```

The `userDefinedFunctions` driver option has also been removed. If you want
to register your own functions, do so by calling `sqliteCreateFunction()`
or `createFunction()` on the PDO or SQLite3 connection.

## BC BREAK: Removed `Table` methods

Expand Down
67 changes: 0 additions & 67 deletions src/Driver/API/SQLite/UserDefinedFunctions.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Driver/PDO/SQLite/Driver.php
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\DBAL\Driver\PDO\SQLite;

use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use Doctrine\DBAL\Driver\API\SQLite\UserDefinedFunctions;
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use PDO;
Expand All @@ -29,8 +28,6 @@ public function connect(array $params): Connection
throw Exception::new($exception);
}

UserDefinedFunctions::register($pdo->sqliteCreateFunction(...));

return new Connection($pdo);
}

Expand Down
3 changes: 0 additions & 3 deletions src/Driver/SQLite3/Driver.php
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\DBAL\Driver\SQLite3;

use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use Doctrine\DBAL\Driver\API\SQLite\UserDefinedFunctions;
use SQLite3;

final class Driver extends AbstractSQLiteDriver
Expand Down Expand Up @@ -41,8 +40,6 @@ public function connect(array $params): Connection

$connection->enableExceptions(true);

UserDefinedFunctions::register($connection->createFunction(...));

return new Connection($connection);
}
}
43 changes: 0 additions & 43 deletions tests/Functional/DataAccessTest.php
Expand Up @@ -13,9 +13,7 @@
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Types;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;

use function array_change_key_case;
use function date;
Expand All @@ -26,8 +24,6 @@

class DataAccessTest extends FunctionalTestCase
{
use VerifyDeprecations;

protected function setUp(): void
{
$table = new Table('fetch_table');
Expand Down Expand Up @@ -698,45 +694,6 @@ public function testLocateExpression(): void
], $row);
}

public function testSqliteLocateEmulation(): void
{
if (! TestUtil::isDriverOneOf('pdo_sqlite', 'sqlite3')) {
self::markTestSkipped('test is for SQLite only');
}

$sql = <<< 'SQL'
SELECT
LOCATE(test_string, 'oo') AS locate1,
LOCATE(test_string, 'foo') AS locate2,
LOCATE(test_string, 'bar') AS locate3,
LOCATE(test_string, test_string) AS locate4,
LOCATE('foo', test_string) AS locate5,
LOCATE('barfoobaz', test_string) AS locate6,
LOCATE('bar', test_string) AS locate7,
LOCATE(test_string, 'oo', 2) AS locate8,
LOCATE(test_string, 'oo', 3) AS locate9,
LOCATE(test_string, 'foo', 1) AS locate10,
LOCATE(test_string, 'oo', 1 + 1) AS locate11
FROM fetch_table
SQL;

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/5749');

self::assertEquals([
'locate1' => 2,
'locate2' => 1,
'locate3' => 0,
'locate4' => 1,
'locate5' => 1,
'locate6' => 4,
'locate7' => 0,
'locate8' => 2,
'locate9' => 0,
'locate10' => 1,
'locate11' => 2,
], $this->connection->fetchAssociative($sql));
}

/** @dataProvider substringExpressionProvider */
public function testSubstringExpression(string $string, string $start, ?string $length, string $expected): void
{
Expand Down

0 comments on commit 7289470

Please sign in to comment.