Driver.php 2.12 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Driver\PDOSqlite;
4

5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
Steve Müller's avatar
Steve Müller committed
7
use Doctrine\DBAL\Driver\PDOConnection;
8
use Doctrine\DBAL\Platforms\SqlitePlatform;
9
use PDOException;
10

11
use function array_merge;
12

13 14 15
/**
 * The PDO Sqlite driver.
 */
16
class Driver extends AbstractSQLiteDriver
17
{
18
    /** @var mixed[] */
19
    protected $_userDefinedFunctions = [
20 21 22
        'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
        'mod'  => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2],
        'locate'  => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1],
23
    ];
24

25
    /**
Benjamin Morel's avatar
Benjamin Morel committed
26
     * {@inheritdoc}
27
     */
28
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
29
    {
30 31
        if (isset($driverOptions['userDefinedFunctions'])) {
            $this->_userDefinedFunctions = array_merge(
32 33 34
                $this->_userDefinedFunctions,
                $driverOptions['userDefinedFunctions']
            );
35 36 37
            unset($driverOptions['userDefinedFunctions']);
        }

38
        try {
Steve Müller's avatar
Steve Müller committed
39
            $pdo = new PDOConnection(
40 41 42 43 44 45 46 47
                $this->_constructPdoDsn($params),
                $username,
                $password,
                $driverOptions
            );
        } catch (PDOException $ex) {
            throw DBALException::driverException($this, $ex);
        }
48

49
        foreach ($this->_userDefinedFunctions as $fn => $data) {
50 51 52 53
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
        }

        return $pdo;
54
    }
55

56 57 58
    /**
     * Constructs the Sqlite PDO DSN.
     *
59
     * @param mixed[] $params
Benjamin Morel's avatar
Benjamin Morel committed
60 61
     *
     * @return string The DSN.
62 63 64 65 66 67
     */
    protected function _constructPdoDsn(array $params)
    {
        $dsn = 'sqlite:';
        if (isset($params['path'])) {
            $dsn .= $params['path'];
Steve Müller's avatar
Steve Müller committed
68
        } elseif (isset($params['memory'])) {
69 70
            $dsn .= ':memory:';
        }
71

72 73
        return $dsn;
    }
74

Benjamin Morel's avatar
Benjamin Morel committed
75 76
    /**
     * {@inheritdoc}
77 78
     *
     * @deprecated
Benjamin Morel's avatar
Benjamin Morel committed
79
     */
80 81 82 83
    public function getName()
    {
        return 'pdo_sqlite';
    }
84
}