Completed
Push — master ( 85d2a2...f0e67d )
by Ivan
04:12
created

Driver::raw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 $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
        $connection['opts'] = [];
23
        parse_str($temp[1], $connection['opts']);
24
        $connection['name'] = $temp[0];
25
        if (!is_file($connection['name']) && is_file('/'.$connection['name'])) {
26
            $connection['name'] = '/'.$connection['name'];
27
        }
28
        $this->connection = $connection;
29
    }
30
    public function __destruct()
31
    {
32
        $this->disconnect();
33
    }
34
    public function connect()
35
    {
36
        if ($this->lnk === null) {
37
            try {
38
                $this->lnk = new \SQLite3($this->connection['name']);
39
                $this->lnk->exec('PRAGMA encoding = "'.$this->option('charset', 'utf-8'));
40
            } catch (\Exception $e) {
41
                if ($this->lnk !== null) {
42
                    throw new DBException('Connect error: '.$this->lnk->lastErrorMsg());
43
                } else {
44
                    throw new DBException('Connect error');
45
                }
46
            }
47
        }
48
    }
49
    public function test() : bool
50
    {
51
        if ($this->lnk) {
52
            return true;
53
        }
54
        try {
55
            @$this->connect();
56
            return true;
57
        } catch (\Exception $e) {
58
            $this->lnk = null;
59
            return false;
60
        }
61
    }
62
    public function disconnect()
63
    {
64
        if ($this->lnk !== null && $this->lnk !== false) {
65
            $this->lnk->close();
66
        }
67
    }
68
    public function raw(string $sql)
69
    {
70
        return $this->lnk->query($sql);
71
    }
72
    public function prepare(string $sql) : StatementInterface
73
    {
74
        $this->connect();
75
        $binder = '?';
76
        if (strpos($sql, $binder) !== false) {
77
            $tmp = explode($binder, $sql);
78
            $sql = '';
79
            foreach ($tmp as $i => $v) {
80
                $sql .= $v;
81
                if (isset($tmp[($i + 1)])) {
82
                    $sql .= ':i'.$i;
83
                }
84
            }
85
        }
86
        $temp = $this->lnk->prepare($sql);
87
        if (!$temp) {
88
            throw new DBException('Could not prepare : '.$this->lnk->lastErrorMsg().' <'.$sql.'>');
89
        }
90
        return new Statement($temp, $this->lnk);
91
    }
92
93
    public function begin() : bool
94
    {
95
        $this->connect();
96
        try {
97
            $this->transaction = true;
98
            $this->query('BEGIN TRANSACTION');
99
        } catch (DBException $e) {
100
            $this->transaction = false;
101
102
            return false;
103
        }
104
105
        return true;
106
    }
107
    public function commit() : bool
108
    {
109
        $this->connect();
110
        $this->transaction = false;
111
        try {
112
            $this->query('COMMIT');
113
        } catch (DBException $e) {
114
            return false;
115
        }
116
117
        return true;
118
    }
119
    public function rollback() : bool
120
    {
121
        $this->connect();
122
        $this->transaction = false;
123
        try {
124
            $this->query('ROLLBACK');
125
        } catch (DBException $e) {
126
            return false;
127
        }
128
129
        return true;
130
    }
131
    public function isTransaction()
132
    {
133
        return $this->transaction;
134
    }
135
}
136