Failed Conditions
Pull Request — develop (#3553)
by Sergei
100:55 queued 35:51
created

Driver::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\PDOSqlite;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
9
use Doctrine\DBAL\Driver\PDOConnection;
10
use Doctrine\DBAL\Driver\PDOException;
11
use Doctrine\DBAL\Platforms\SqlitePlatform;
12
use function array_merge;
13
14
/**
15
 * The PDO Sqlite driver.
16
 */
17
class Driver extends AbstractSQLiteDriver
18
{
19
    /** @var mixed[] */
20
    protected $_userDefinedFunctions = [
21
        'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
22
        'mod'  => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2],
23
        'locate'  => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1],
24
    ];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 462
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
30
    {
31 462
        if (isset($driverOptions['userDefinedFunctions'])) {
32
            $this->_userDefinedFunctions = array_merge(
33
                $this->_userDefinedFunctions,
34
                $driverOptions['userDefinedFunctions']
35
            );
36
            unset($driverOptions['userDefinedFunctions']);
37
        }
38
39
        try {
40 462
            $connection = new PDOConnection(
41 462
                $this->_constructPdoDsn($params),
42 32
                $username,
43 32
                $password,
44 32
                $driverOptions
45
            );
46 19
        } catch (PDOException $ex) {
47 19
            throw DBALException::driverException($this, $ex);
48
        }
49
50 462
        $pdo = $connection->getWrappedConnection();
51
52 462
        foreach ($this->_userDefinedFunctions as $fn => $data) {
53 462
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
54
        }
55
56 462
        return $connection;
57
    }
58
59
    /**
60
     * Constructs the Sqlite PDO DSN.
61
     *
62
     * @param mixed[] $params
63
     *
64
     * @return string The DSN.
65
     */
66 462
    protected function _constructPdoDsn(array $params)
67
    {
68 462
        $dsn = 'sqlite:';
69 462
        if (isset($params['path'])) {
70 19
            $dsn .= $params['path'];
71 462
        } elseif (isset($params['memory'])) {
72 462
            $dsn .= ':memory:';
73
        }
74
75 462
        return $dsn;
76
    }
77
}
78