Failed Conditions
Pull Request — 2.11.x (#3864)
by Benjamin
51:24 queued 47:47
created

Driver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
wmc 8
eloc 27
dl 0
loc 67
ccs 15
cts 22
cp 0.6818
rs 10
c 0
b 0
f 0

3 Methods

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