Use composer for dependency installation
[stapibas.git] / src / stapibas / PDO.php
1 <?php
2 namespace stapibas;
3
4 /**
5  * Small error-handling wrapper around PDO
6  */
7 class PDO extends \PDO
8 {
9     public function query(
10         string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs
11     ): \PDOStatement|false {
12         $args = func_get_args();
13         $res = call_user_func_array(array('parent', 'query'), $args);
14         if ($res !== false) {
15             return $res;
16         }
17
18         $this->handleError();
19     }
20
21     public function exec(string $statement): int|false
22     {
23         $res = parent::exec($statement);
24         if ($res !== false) {
25             return $res;
26         }
27
28         $this->handleError();
29     }
30
31     protected function handleError()
32     {
33         echo "SQL error\n";
34         echo " " . $this->errorCode() . "\n";
35         echo " " . implode(' - ', $this->errorInfo()) . "\n";
36         exit(2);
37     }
38 }
39
40 ?>