Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / database / statement / DebugPreparedStatement.class.php
CommitLineData
e7190819 1<?php
a9229942 2
e7190819
AE
3namespace wcf\system\database\statement;
4
5/**
6 * Similar to the regular `PreparedStatement` class, but throws an exception when trying to read data
7 * before executing the statement at least once.
a9229942 8 *
e7190819
AE
9 * @author Alexander Ebert
10 * @copyright 2001-2019 WoltLab GmbH
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @package WoltLabSuite\Core\System\Database\Statement
13 */
a9229942
TD
14class DebugPreparedStatement extends PreparedStatement
15{
16 protected $debugDidExecuteOnce = false;
17
18 /**
19 * @inheritDoc
20 */
21 public function __call($name, $arguments)
22 {
23 if ($name === 'fetchAll' || $name === 'fetchColumn') {
24 $this->debugThrowIfNotExecutedBefore();
25 }
26
27 return parent::__call($name, $arguments);
28 }
29
30 /**
31 * @inheritDoc
32 */
33 public function execute(array $parameters = [])
34 {
35 $this->debugDidExecuteOnce = true;
36
37 parent::execute($parameters);
38 }
39
40 /**
41 * @inheritDoc
42 */
43 public function fetchArray($type = null)
44 {
45 $this->debugThrowIfNotExecutedBefore();
46
47 return parent::fetchArray($type);
48 }
49
50 /**
51 * @inheritDoc
52 */
53 public function fetchSingleRow($type = null)
54 {
55 $this->debugThrowIfNotExecutedBefore();
56
57 return parent::fetchSingleRow($type);
58 }
59
60 /**
61 * @inheritDoc
62 */
63 public function fetchSingleColumn($columnNumber = 0)
64 {
65 $this->debugThrowIfNotExecutedBefore();
66
67 return parent::fetchSingleColumn($columnNumber);
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function fetchObject($className)
74 {
75 $this->debugThrowIfNotExecutedBefore();
76
77 return parent::fetchObject($className);
78 }
79
80 /**
81 * @inheritDoc
82 */
83 public function fetchSingleObject($className)
84 {
85 $this->debugThrowIfNotExecutedBefore();
86
87 return parent::fetchSingleObject($className);
88 }
89
90 /**
91 * @inheritDoc
92 */
93 public function fetchObjects($className, $keyProperty = null)
94 {
95 $this->debugThrowIfNotExecutedBefore();
96
97 return parent::fetchObjects($className, $keyProperty);
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function fetchMap($keyColumn, $valueColumn, $uniqueKey = true)
104 {
105 $this->debugThrowIfNotExecutedBefore();
106
107 return parent::fetchMap($keyColumn, $valueColumn, $uniqueKey);
108 }
109
110 /**
111 * @inheritDoc
112 */
113 protected function debugThrowIfNotExecutedBefore()
114 {
115 if (!$this->debugDidExecuteOnce) {
116 throw new \RuntimeException('Attempted to fetch data from a statement without executing it at least once.');
117 }
118 }
e7190819 119}