Completed
Push — master ( 2e871f...9de5b8 )
by Matteo
02:28
created

Result::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
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
7
class Result
8
{
9
    const FETCH_ASSOC = 1;
10
    const FETCH_NUM = 2;
11
    const FETCH_BOTH = 3;
12
    const FETCH_OBJ = PDO::FETCH_OBJ;
13
14
    protected $connection;
15
16
    protected $statement;
17
18
    protected $cursor;
19
20
    public function __construct(\PDOStatement $statement, Connection $connection)
21
    {
22
        $this->statement = $statement;
23
        $this->connection = $connection;
24
    }
25
26
    public function getStatement()
27
    {
28
        return $this->statement;
29
    }
30
31
    public function toArray()
32
    {
33
        return $this->statement->fetchAll();
34
    }
35
36
    public function count()
37
    {
38
        return $this->statement->rowCount();
39
    }
40
41
    public function column($column, $row)
42
    {
43
        $row = $this->statement->fetch(PDO::FETCH_BOTH, PDO::FETCH_ORI_ABS, $row);
44
45
        return $row[$column];
46
    }
47
48
    public function fetch($style = self::FETCH_BOTH, $orientation = PDO::FETCH_ORI_NEXT, $offset = 0)
49
    {
50
        $fetchMode = $this->convertFetchStyle($style);
51
52
        if ($this->cursor !== null) {
53
            $result = $this->statement->fetch($fetchMode, PDO::FETCH_ORI_ABS, $this->cursor);
54
            $this->cursor++;
55
56
            return $result;
57
        }
58
59
        return $this->statement->fetch($fetchMode, $orientation, $offset);
60
    }
61
62
    public function fetchObject($class = 'stdClass', array $params = [])
63
    {
64
        $this->statement->setFetchMode(PDO::FETCH_CLASS, $class, $params);
65
66
        return $this->fetch(static::FETCH_OBJ);
67
    }
68
69
    public function fetchAll()
70
    {
71
        return $this->statement->fetchAll();
72
    }
73
74
    public function free()
75
    {
76
        return $this->statement->closeCursor();
77
    }
78
79
    public function convertFetchStyle($style)
80
    {
81
        switch ($style) {
82
            case static::FETCH_ASSOC:
83
                return PDO::FETCH_ASSOC;
84
85
            case static::FETCH_NUM:
86
                return PDO::FETCH_NUM;
87
88
            case static::FETCH_BOTH:
89
                return PDO::FETCH_BOTH;
90
91
            case static::FETCH_OBJ:
92
                return PDO::FETCH_CLASS;
93
94
            default:
95
                return $style;
96
        }
97
    }
98
99
    public function setCursor($rowNumber)
100
    {
101
        $this->cursor = $rowNumber;
102
    }
103
104
    public function getColumnMeta($columnNumber)
105
    {
106
        return $this->statement->getColumnMeta($columnNumber);
107
    }
108
109
    public function getColumnCount()
110
    {
111
        return $this->statement->columnCount();
112
    }
113
114
    public function getConnection()
115
    {
116
        return $this->connection;
117
    }
118
}
119