Connection::query()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 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
     *
130
     * @return bool|Result
131
     */
132
    public function parametrizedQuery($query, array $params = [])
133
    {
134
        $statement = $this->pdo->prepare($query);
135
136
        $success = $statement->execute($params);
137
        if ($success === false) {
138
            return false;
139
        }
140
141
        return new Result($statement, $this);
142
    }
143
}
144