Completed
Branch feature/pre-split (92bc7d)
by Anton
04:30
created

PDOResult::bind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Query;
10
11
use PDOStatement;
12
use Spiral\Database\Helpers\QueryInterpolator;
13
use Spiral\Database\ResultInterface;
14
15
/**
16
 * Works as prepared PDOStatement.
17
 */
18
class PDOResult extends PDOStatement implements ResultInterface, \JsonSerializable
19
{
20
    /**
21
     * Limits after which no records will be dumped in __debugInfo.
22
     */
23
    const DUMP_LIMIT = 500;
24
25
    /**
26
     * @var array
27
     */
28
    private $parameters = [];
29
30
    /**
31
     * @param array $parameters
32
     */
33
    protected function __construct(array $parameters)
34
    {
35
        $this->parameters = $parameters;
36
    }
37
38
    /**
39
     * Bind a column value to a PHP variable. Aliased to bindParam.
40
     *
41
     * @param int|string $fieldID Column number (0 - first column)
42
     * @param mixed      $variable
43
     * @return self
44
     */
45
    public function bind($fieldID, &$variable): PDOResult
46
    {
47
        if (is_numeric($fieldID)) {
48
            //PDO columns are 1-indexed
49
            $fieldID = $fieldID + 1;
50
        }
51
52
        $this->bindColumn($fieldID, $variable);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Just an alias.
59
     *
60
     * @return int
61
     */
62
    public function countColumns(): int
63
    {
64
        return $this->columnCount();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function queryString(): string
71
    {
72
        return QueryInterpolator::interpolate($this->queryString, $this->parameters);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * Attention, this method will return 0 for SQLite databases.
79
     *
80
     * @link http://php.net/manual/en/pdostatement.rowcount.php
81
     * @link http://stackoverflow.com/questions/15003232/pdo-returns-wrong-rowcount-after-select-statement
82
     *
83
     * @return int
84
     */
85
    public function count(): int
86
    {
87
        return $this->rowCount();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function close()
94
    {
95
        $this->closeCursor();
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function jsonSerialize(): array
102
    {
103
        return $this->fetchAll(\PDO::FETCH_ASSOC);
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function __debugInfo()
110
    {
111
        return [
112
            'query' => $this->queryString(),
113
            'count' => $this->count(),
114
            'rows'  => $this->count() > static::DUMP_LIMIT ? '[TOO MANY ROWS]' : $this->fetchAll(\PDO::FETCH_ASSOC)
115
        ];
116
    }
117
}
118