Completed
Push — master ( b71ad6...2211ef )
by Ivan
08:51
created

Driver   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 102
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A __destruct() 0 4 1
A connect() 0 11 3
A disconnect() 0 6 2
B prepare() 0 20 5
A begin() 0 14 2
A commit() 0 12 2
A rollback() 0 12 2
A isTransaction() 0 4 1
1
<?php
2
3
namespace vakata\database\driver\sqlite;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\DriverAbstract;
8
use \vakata\database\StatementInterface;
9
use \vakata\database\schema\Table;
10
use \vakata\database\schema\TableRelation;
11
12
class Driver extends DriverAbstract implements DriverInterface
13
{
14
    protected $connection;
15
    protected $lnk = null;
16
17
    public function __construct(array $connection)
18
    {
19
        $temp = explode('://', $connection['orig'], 2)[1];
20
        $temp = array_pad(explode('?', $temp, 2), 2, '');
21
        $connection = [];
22
        parse_str($temp[1], $connection['opts']);
23
        $connection['name'] = $temp[0];
24
        if (!is_file($connection['name']) && is_file('/'.$connection['name'])) {
25
           $connection['name'] = '/'.$connection['name'];
26
        }
27
        $this->connection = $connection;
28
    }
29
    public function __destruct()
30
    {
31
        $this->disconnect();
32
    }
33
    protected function connect()
34
    {
35
        if ($this->lnk === null) {
36
            try {
37
                $this->lnk = new \SQLite3($this->connection['name']);
38
                $this->lnk->exec('PRAGMA encoding = "'.$this->option('charset', 'utf-8'));
39
            } catch (\Exception $e) {
40
                throw new DBException('Connect error: '.$this->lnk->lastErrorMsg());
41
            }
42
        }
43
    }
44
    protected function disconnect()
45
    {
46
        if ($this->lnk !== null) {
47
            @$this->lnk->close();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
        }
49
    }
50
    public function prepare(string $sql) : StatementInterface
51
    {
52
        $this->connect();
53
        $binder = '?';
54
        if (strpos($sql, $binder) !== false) {
55
            $tmp = explode($binder, $sql);
56
            $sql = '';
57
            foreach ($tmp as $i => $v) {
58
                $sql .= $v;
59
                if (isset($tmp[($i + 1)])) {
60
                    $sql .= ':i'.$i;
61
                }
62
            }
63
        }
64
        $temp = $this->lnk->prepare($sql);
65
        if (!$temp) {
66
            throw new DBException('Could not prepare : '.$this->lnk->lastErrorMsg().' <'.$sql.'>');
67
        }
68
        return new Statement($temp, $this->lnk);
69
    }
70
71
    public function begin()
72
    {
73
        $this->connect();
74
        try {
75
            $this->transaction = true;
0 ignored issues
show
Bug introduced by
The property transaction does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76
            $this->query('BEGIN TRANSACTION');
77
        } catch (DBException $e) {
78
            $this->transaction = false;
79
80
            return false;
81
        }
82
83
        return true;
84
    }
85
    public function commit()
86
    {
87
        $this->connect();
88
        $this->transaction = false;
89
        try {
90
            $this->query('COMMIT');
91
        } catch (DBException $e) {
92
            return false;
93
        }
94
95
        return true;
96
    }
97
    public function rollback()
98
    {
99
        $this->connect();
100
        $this->transaction = false;
101
        try {
102
            $this->query('ROLLBACK');
103
        } catch (DBException $e) {
104
            return false;
105
        }
106
107
        return true;
108
    }
109
    public function isTransaction()
110
    {
111
        return $this->transaction;
112
    }
113
}