-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pdo.php
43 lines (33 loc) · 1.03 KB
/
Pdo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php namespace TooBasic;
class Pdo extends \Pdo
{
public function __construct(string $dsn, string $username = null, string $password = null, array $options = array())
{
parent::__construct($dsn, $username, $password, $options);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function fetchObject(string $query, array $params = [], string $class = 'StdClass')
{
$r = $this->fetchObjects($query, $params, $class);
if (1 != count($r))
throw new Exception('Query returned '. count($r) .' rows, no single result');
return $r[0];
}
public function fetchObjects(string $query, array $params, string $class = 'StdClass')
{
$s = $this->preparedQuery($query, $params);
return $s->fetchAll(PDO::FETCH_CLASS, $class);
}
public function preparedExec(string $query, array $params)
{
$s = $this->prepare($query);
$s->execute($params);
return $s->rowCount();
}
public function preparedQuery(string $query, array $params)
{
$s = $this->prepare($query);
$s->execute($params);
return $s;
}
}