Passed
Branch master (d023ff)
by Biao
06:15
created

SwoolePDO::inTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Illuminate\Database;
4
5
use Illuminate\Database\QueryException;
6
use Swoole\Coroutine\MySQL as SwooleMySQL;
7
8
class SwoolePDO extends \PDO
9
{
10
    protected $sm;
11
    protected $isInTransaction = false;
12
13
    public function __construct()
14
    {
15
        $this->sm = new SwooleMySQL();
16
    }
17
18
    public function connect(array $serverInfo)
19
    {
20
        $this->sm->connect($serverInfo);
21
    }
22
23
    public function prepare($statement, $options = null)
24
    {
25
        $oldStatement = $this->sm->prepare($statement);
26
        if ($oldStatement === false) {
0 ignored issues
show
introduced by
The condition $oldStatement === false is always false.
Loading history...
27
            throw new QueryException($statement, [], new \Exception($this->sm->error, $this->sm->errno));
28
        }
29
        return new SwoolePDOStatement($oldStatement);
30
    }
31
32
    public function beginTransaction()
33
    {
34
        $this->isInTransaction = true;
35
        $this->sm->begin();
36
    }
37
38
    public function commit()
39
    {
40
        $this->sm->commit();
41
        $this->isInTransaction = false;
42
    }
43
44
    public function rollBack()
45
    {
46
        $this->sm->rollback();
47
        $this->isInTransaction = false;
48
    }
49
50
    public function query($statement, $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, $arg3 = null, array $ctorargs = [])
51
    {
52
        return $this->sm->query($statement, array_get($ctorargs, 'timeout', 0.0));
53
    }
54
55
    public function exec($statement)
56
    {
57
        return $this->sm->query($statement);
58
    }
59
60
    public function lastInsertId($name = null)
61
    {
62
        return $this->sm->insert_id;
63
    }
64
65
    public function rowCount()
66
    {
67
        return $this->sm->affected_rows;
68
    }
69
70
    public function quote($string, $parameter_type = \PDO::PARAM_STR)
71
    {
72
        //TODO
73
        return $string;
74
    }
75
76
    public function errorCode()
77
    {
78
        return $this->sm->errno;
79
    }
80
81
    public function errorInfo()
82
    {
83
        return [
84
            $this->sm->errno,
85
            $this->sm->errno,
86
            $this->sm->error,
87
        ];
88
    }
89
90
    public function inTransaction()
91
    {
92
        return $this->isInTransaction;
93
    }
94
95
    public function getAttribute($attribute)
96
    {
97
        return isset($this->sm->serverInfo[$attribute]) ? $this->sm->serverInfo[$attribute] : null;
98
    }
99
100
    public function setAttribute($attribute, $value)
101
    {
102
        // TODO
103
        return false;
104
    }
105
106
    public static function getAvailableDrivers()
107
    {
108
        return ['mysql'];
109
    }
110
}
111