Completed
Push — master ( 11b67b...3a0a1d )
by Sergei
19:09 queued 19:04
created

Driver::connect()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.8437

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
ccs 10
cts 16
cp 0.625
rs 9.7333
c 0
b 0
f 0
cc 4
nc 6
nop 4
crap 4.8437
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 464
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
28
    {
29 464
        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 464
            $pdo = new PDOConnection(
39 464
                $this->_constructPdoDsn($params),
40 32
                $username,
41 32
                $password,
42 32
                $driverOptions
43
            );
44
        } catch (PDOException $ex) {
45
            throw DBALException::driverException($this, $ex);
46
        }
47
48 464
        foreach ($this->_userDefinedFunctions as $fn => $data) {
49 464
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
50
        }
51
52 464
        return $pdo;
53
    }
54
55
    /**
56
     * Constructs the Sqlite PDO DSN.
57
     *
58
     * @param mixed[] $params
59
     *
60
     * @return string The DSN.
61
     */
62 464
    protected function _constructPdoDsn(array $params)
63
    {
64 464
        $dsn = 'sqlite:';
65 464
        if (isset($params['path'])) {
66
            $dsn .= $params['path'];
67 464
        } elseif (isset($params['memory'])) {
68 464
            $dsn .= ':memory:';
69
        }
70
71 464
        return $dsn;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 299
    public function getName()
78
    {
79 299
        return 'pdo_sqlite';
80
    }
81
}
82