1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Database\Statement;
16:
17: use PDO;
18: use PDOStatement as Statement;
19:
20: /**
21: * Decorator for \PDOStatement class mainly used for converting human readable
22: * fetch modes into PDO constants.
23: */
24: class PDOStatement extends StatementDecorator
25: {
26:
27: /**
28: * Constructor
29: *
30: * @param \PDOStatement|null $statement Original statement to be decorated.
31: * @param \Cake\Database\Driver|null $driver Driver instance.
32: */
33: public function __construct(Statement $statement = null, $driver = null)
34: {
35: parent::__construct($statement, $driver);
36: }
37:
38: /**
39: * Assign a value to a positional or named variable in prepared query. If using
40: * positional variables you need to start with index one, if using named params then
41: * just use the name in any order.
42: *
43: * You can pass PDO compatible constants for binding values with a type or optionally
44: * any type name registered in the Type class. Any value will be converted to the valid type
45: * representation if needed.
46: *
47: * It is not allowed to combine positional and named variables in the same statement
48: *
49: * ### Examples:
50: *
51: * ```
52: * $statement->bindValue(1, 'a title');
53: * $statement->bindValue(2, 5, PDO::INT);
54: * $statement->bindValue('active', true, 'boolean');
55: * $statement->bindValue(5, new \DateTime(), 'date');
56: * ```
57: *
58: * @param string|int $column name or param position to be bound
59: * @param mixed $value The value to bind to variable in query
60: * @param string|int $type PDO type or name of configured Type class
61: * @return void
62: */
63: public function bindValue($column, $value, $type = 'string')
64: {
65: if ($type === null) {
66: $type = 'string';
67: }
68: if (!ctype_digit($type)) {
69: list($value, $type) = $this->cast($value, $type);
70: }
71: $this->_statement->bindValue($column, $value, $type);
72: }
73:
74: /**
75: * Returns the next row for the result set after executing this statement.
76: * Rows can be fetched to contain columns as names or positions. If no
77: * rows are left in result set, this method will return false
78: *
79: * ### Example:
80: *
81: * ```
82: * $statement = $connection->prepare('SELECT id, title from articles');
83: * $statement->execute();
84: * print_r($statement->fetch('assoc')); // will show ['id' => 1, 'title' => 'a title']
85: * ```
86: *
87: * @param string $type 'num' for positional columns, assoc for named columns
88: * @return array|false Result array containing columns and values or false if no results
89: * are left
90: */
91: public function fetch($type = parent::FETCH_TYPE_NUM)
92: {
93: if ($type === static::FETCH_TYPE_NUM) {
94: return $this->_statement->fetch(PDO::FETCH_NUM);
95: }
96: if ($type === static::FETCH_TYPE_ASSOC) {
97: return $this->_statement->fetch(PDO::FETCH_ASSOC);
98: }
99: if ($type === static::FETCH_TYPE_OBJ) {
100: return $this->_statement->fetch(PDO::FETCH_OBJ);
101: }
102:
103: return $this->_statement->fetch($type);
104: }
105:
106: /**
107: * Returns an array with all rows resulting from executing this statement
108: *
109: * ### Example:
110: *
111: * ```
112: * $statement = $connection->prepare('SELECT id, title from articles');
113: * $statement->execute();
114: * print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
115: * ```
116: *
117: * @param string $type num for fetching columns as positional keys or assoc for column names as keys
118: * @return array list of all results from database for this statement
119: */
120: public function fetchAll($type = parent::FETCH_TYPE_NUM)
121: {
122: if ($type === static::FETCH_TYPE_NUM) {
123: return $this->_statement->fetchAll(PDO::FETCH_NUM);
124: }
125: if ($type === static::FETCH_TYPE_ASSOC) {
126: return $this->_statement->fetchAll(PDO::FETCH_ASSOC);
127: }
128: if ($type === static::FETCH_TYPE_OBJ) {
129: return $this->_statement->fetchAll(PDO::FETCH_OBJ);
130: }
131:
132: return $this->_statement->fetchAll($type);
133: }
134: }
135: