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: * Constructor
28: *
29: * @param \PDOStatement|null $statement Original statement to be decorated.
30: * @param \Cake\Database\Driver|null $driver Driver instance.
31: */
32: public function __construct(Statement $statement = null, $driver = null)
33: {
34: parent::__construct($statement, $driver);
35: }
36:
37: /**
38: * Assign a value to a positional or named variable in prepared query. If using
39: * positional variables you need to start with index one, if using named params then
40: * just use the name in any order.
41: *
42: * You can pass PDO compatible constants for binding values with a type or optionally
43: * any type name registered in the Type class. Any value will be converted to the valid type
44: * representation if needed.
45: *
46: * It is not allowed to combine positional and named variables in the same statement
47: *
48: * ### Examples:
49: *
50: * ```
51: * $statement->bindValue(1, 'a title');
52: * $statement->bindValue(2, 5, PDO::INT);
53: * $statement->bindValue('active', true, 'boolean');
54: * $statement->bindValue(5, new \DateTime(), 'date');
55: * ```
56: *
57: * @param string|int $column name or param position to be bound
58: * @param mixed $value The value to bind to variable in query
59: * @param string|int $type PDO type or name of configured Type class
60: * @return void
61: */
62: public function bindValue($column, $value, $type = 'string')
63: {
64: if ($type === null) {
65: $type = 'string';
66: }
67: if (!ctype_digit($type)) {
68: list($value, $type) = $this->cast($value, $type);
69: }
70: $this->_statement->bindValue($column, $value, $type);
71: }
72:
73: /**
74: * Returns the next row for the result set after executing this statement.
75: * Rows can be fetched to contain columns as names or positions. If no
76: * rows are left in result set, this method will return false
77: *
78: * ### Example:
79: *
80: * ```
81: * $statement = $connection->prepare('SELECT id, title from articles');
82: * $statement->execute();
83: * print_r($statement->fetch('assoc')); // will show ['id' => 1, 'title' => 'a title']
84: * ```
85: *
86: * @param string $type 'num' for positional columns, assoc for named columns
87: * @return array|false Result array containing columns and values or false if no results
88: * are left
89: */
90: public function fetch($type = parent::FETCH_TYPE_NUM)
91: {
92: if ($type === static::FETCH_TYPE_NUM) {
93: return $this->_statement->fetch(PDO::FETCH_NUM);
94: }
95: if ($type === static::FETCH_TYPE_ASSOC) {
96: return $this->_statement->fetch(PDO::FETCH_ASSOC);
97: }
98: if ($type === static::FETCH_TYPE_OBJ) {
99: return $this->_statement->fetch(PDO::FETCH_OBJ);
100: }
101:
102: return $this->_statement->fetch($type);
103: }
104:
105: /**
106: * Returns an array with all rows resulting from executing this statement
107: *
108: * ### Example:
109: *
110: * ```
111: * $statement = $connection->prepare('SELECT id, title from articles');
112: * $statement->execute();
113: * print_r($statement->fetchAll('assoc')); // will show [0 => ['id' => 1, 'title' => 'a title']]
114: * ```
115: *
116: * @param string $type num for fetching columns as positional keys or assoc for column names as keys
117: * @return array list of all results from database for this statement
118: */
119: public function fetchAll($type = parent::FETCH_TYPE_NUM)
120: {
121: if ($type === static::FETCH_TYPE_NUM) {
122: return $this->_statement->fetchAll(PDO::FETCH_NUM);
123: }
124: if ($type === static::FETCH_TYPE_ASSOC) {
125: return $this->_statement->fetchAll(PDO::FETCH_ASSOC);
126: }
127: if ($type === static::FETCH_TYPE_OBJ) {
128: return $this->_statement->fetchAll(PDO::FETCH_OBJ);
129: }
130:
131: return $this->_statement->fetchAll($type);
132: }
133: }
134: