Completed
Push — master ( 9de5b8...ddab96 )
by Matteo
03:46
created

Connection::getErrorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mattbit\MysqlCompat;
4
5
use PDO;
6
use PDOStatement;
7
use Mattbit\MysqlCompat\Exception\QueryException;
8
9
class Connection
10
{
11
    /**
12
     * The status of the connection.
13
     *
14
     * @var bool
15
     */
16
    protected $open = false;
17
18
    /**
19
     * The PDO instance which represents the actual connection.
20
     *
21
     * @var PDO
22
     */
23
    protected $pdo;
24
25
    /**
26
     * The last executed query.
27
     *
28
     * @var PDOStatement
29
     */
30
    protected $lastQuery;
31
32
    /**
33
     * Create a new Connection instance.
34
     *
35
     * @param PDO $pdo
36
     */
37
    public function __construct(PDO $pdo)
38
    {
39
        $this->pdo = $pdo;
40
        $this->open = true;
41
    }
42
43
    public function query($query)
44
    {
45
        $statement = $this->pdo->query($query);
46
47
        if ($statement === false) {
48
            throw new QueryException("Error executing the query.");
49
        }
50
51
        $this->lastQuery = $statement;
52
53
        if ($statement === null) {
54
            return;
55
        }
56
57
        return new Result($statement, $this);
58
    }
59
60
    public function quote($string)
61
    {
62
        $escaped = $this->pdo->quote($string);
63
64
        return $escaped;
65
    }
66
67
    public function useDatabase($database)
68
    {
69
        return $this->query("USE {$database}");
70
    }
71
72
    public function getServerInfo()
73
    {
74
        return $this->pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
75
    }
76
77
    public function close()
78
    {
79
        $this->pdo = null;
80
        $this->open = false;
81
82
        return true;
83
    }
84
85
    public function isOpen()
86
    {
87
        return $this->open;
88
    }
89
90
    public function getPdo()
91
    {
92
        return $this->pdo;
93
    }
94
95
    public function getRowCount()
96
    {
97
        if ($this->lastQuery) {
98
            return $this->lastQuery->rowCount();
99
        }
100
101
        return 0;
102
    }
103
    
104
    public function getCharset()
105
    {
106
        $statement = $this->pdo->query("SELECT COLLATION('c')");
107
108
        return $statement->fetch(PDO::FETCH_NUM)[0];
109
    }
110
    
111
    public function getErrorInfo()
112
    {
113
        return $this->pdo->errorInfo();
114
    }
115
116
    public function getAttribute($attribute)
117
    {
118
        return $this->pdo->getAttribute($attribute);
119
    }
120
121
    public function getLastInsertId()
122
    {
123
        return $this->pdo->lastInsertId();
124
    }
125
126
    /**
127
     * @param $query
128
     * @param array $params
129
     * @return bool|Result
130
     */
131
    public function parametrizedQuery($query, array $params = [])
132
    {
133
        $statement = $this->pdo->prepare($query);
134
135
        $success = $statement->execute($params);
136
        if ($success === false) {
137
            return false;
138
        }
139
140
        return new Result($statement, $this);
141
    }
142
}
143