1 | <?php |
||
9 | class FakePDO extends PDO { |
||
10 | use EventHandlerTrait; |
||
11 | |||
12 | /** @var array */ |
||
13 | private $attributes = []; |
||
14 | /** @var bool */ |
||
15 | private $inTransaction = false; |
||
16 | /** @var MethodNameGenerator */ |
||
17 | private $methodNameGeneratorA = null; |
||
18 | |||
19 | /** |
||
20 | * @return array |
||
21 | */ |
||
22 | public static function getAvailableDrivers() { |
||
25 | |||
26 | /** |
||
27 | * @param EventHandler $eventHandler |
||
28 | */ |
||
29 | public function __construct(EventHandler $eventHandler = null) { |
||
33 | |||
34 | /** |
||
35 | * @param string $statement |
||
36 | * @param array|null $options |
||
37 | * @return FakePDOStatement |
||
38 | */ |
||
39 | public function prepare($statement, $options = NULL) { |
||
45 | |||
46 | /** |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function beginTransaction() { |
||
50 | $methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__); |
||
51 | return $this->invokeEventHandler($methodName, array(), function () { |
||
52 | $inTransaction = $this->inTransaction(); |
||
53 | if($inTransaction) { |
||
54 | return false; |
||
55 | } else { |
||
56 | $this->inTransaction = true; |
||
57 | return true; |
||
58 | } |
||
59 | }); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function commit() { |
||
66 | $methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__); |
||
67 | return $this->invokeEventHandler($methodName, array(), function () { |
||
68 | $inTransaction = $this->inTransaction(); |
||
69 | if($inTransaction) { |
||
70 | $this->inTransaction = false; |
||
71 | return true; |
||
72 | } else { |
||
73 | return false; |
||
74 | } |
||
75 | }); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function rollback() { |
||
82 | $methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__); |
||
83 | return $this->invokeEventHandler($methodName, array(), function () { |
||
84 | $inTransaction = $this->inTransaction(); |
||
85 | if($inTransaction) { |
||
86 | $this->inTransaction = false; |
||
87 | return true; |
||
88 | } else { |
||
89 | return false; |
||
90 | } |
||
91 | }); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function inTransaction() { |
||
103 | |||
104 | /** |
||
105 | * @param int $attribute |
||
106 | * @return mixed |
||
107 | */ |
||
108 | public function getAttribute($attribute) { |
||
109 | $methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__); |
||
110 | return $this->invokeEventHandler($methodName, array($attribute), function ($attribute) { |
||
111 | $attribute = json_encode($attribute); |
||
112 | if(array_key_exists($attribute, $this->attributes)) { |
||
113 | return $this->attributes[$attribute]; |
||
114 | } |
||
115 | return null; |
||
116 | }); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param int $attribute |
||
121 | * @param mixed $value |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function setAttribute($attribute, $value) { |
||
132 | |||
133 | /** |
||
134 | * @param string $statement |
||
135 | * @return int |
||
136 | */ |
||
137 | public function exec($statement) { |
||
143 | |||
144 | /** |
||
145 | * @param string $statement |
||
146 | * @return FakePDOStatement |
||
147 | */ |
||
148 | public function query($statement, ?int $fetchMode = null, ...$fetchModeArgs) { |
||
149 | $methodName = $this->methodNameGeneratorA->getQualifiedMethodName(__FUNCTION__); |
||
150 | return $this->invokeEventHandler($methodName, array($statement), function () { |
||
151 | return new FakePDOStatement($this->getEventHandler()); |
||
152 | }); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string|null $name |
||
157 | * @return string|null |
||
158 | */ |
||
159 | public function lastInsertId($name = null) { |
||
165 | |||
166 | /** |
||
167 | * @return mixed |
||
168 | */ |
||
169 | public function errorCode() { |
||
175 | |||
176 | /** |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function errorInfo() { |
||
185 | |||
186 | /** |
||
187 | * @param string $string |
||
188 | * @param int $parameter_type |
||
189 | * @return string|void |
||
190 | */ |
||
191 | public function quote($string, $parameter_type = PDO::PARAM_STR) { |
||
197 | } |
||
198 |