Completed
Push — master ( 5924b0...d78793 )
by Ivan
02:02
created

Driver::test()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
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 $lnk = null;
15
    protected $transaction = false;
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
    public function test() : bool
45
    {
46
        if ($this->lnk) {
47
            return true;
48
        }
49
        try {
50
            $this->connect();
51
            return true;
52
        } catch (\Exception $e) {
53
            return false;
54
        }
55
    }
56
    protected function disconnect()
57
    {
58
        if ($this->lnk !== null) {
59
            $this->lnk->close();
60
        }
61
    }
62
    public function prepare(string $sql) : StatementInterface
63
    {
64
        $this->connect();
65
        $binder = '?';
66
        if (strpos($sql, $binder) !== false) {
67
            $tmp = explode($binder, $sql);
68
            $sql = '';
69
            foreach ($tmp as $i => $v) {
70
                $sql .= $v;
71
                if (isset($tmp[($i + 1)])) {
72
                    $sql .= ':i'.$i;
73
                }
74
            }
75
        }
76
        $temp = $this->lnk->prepare($sql);
77
        if (!$temp) {
78
            throw new DBException('Could not prepare : '.$this->lnk->lastErrorMsg().' <'.$sql.'>');
79
        }
80
        return new Statement($temp, $this->lnk);
81
    }
82
83
    public function begin()
84
    {
85
        $this->connect();
86
        try {
87
            $this->transaction = true;
88
            $this->query('BEGIN TRANSACTION');
89
        } catch (DBException $e) {
90
            $this->transaction = false;
91
92
            return false;
93
        }
94
95
        return true;
96
    }
97
    public function commit()
98
    {
99
        $this->connect();
100
        $this->transaction = false;
101
        try {
102
            $this->query('COMMIT');
103
        } catch (DBException $e) {
104
            return false;
105
        }
106
107
        return true;
108
    }
109
    public function rollback()
110
    {
111
        $this->connect();
112
        $this->transaction = false;
113
        try {
114
            $this->query('ROLLBACK');
115
        } catch (DBException $e) {
116
            return false;
117
        }
118
119
        return true;
120
    }
121
    public function isTransaction()
122
    {
123
        return $this->transaction;
124
    }
125
}