-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.class.php
95 lines (82 loc) · 1.9 KB
/
database.class.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Database wrapper, extending PDO
*
* @author Gussi <[email protected]>
*/
class database extends PDO {
public function __construct($dsn, $user = null, $pass = null) {
parent::__construct($dsn, $user, $pass);
$this->setAttribute(self::ATTR_ERRMODE, self::ERRMODE_EXCEPTION);
}
/**
* Overwrite PDO::exec() with our own
*
* @param sql SQL statement
* @param ... Optional bind parameters
*/
public function exec() {
list($sql, $param) = $this->extract_params(func_get_args());
if(empty($param)) {
return parent::exec($sql);
} else {
$st = $this->prepare($sql);
return $st->execute($param);
}
}
/**
* Get all rows
*
* @param sql SQL statement
* @param ... Optional bind parameters
*/
public function get_all() {
list($sql, $param) = $this->extract_params(func_get_args());
$st = $this->prepare($sql);
$st->execute($param);
return $st->fetchAll(self::FETCH_ASSOC);
}
/**
* Get single row
*
* @param sql SQL statement
* @param ... Optional bind parameters
*/
public function get_row() {
list($sql, $param) = $this->extract_params(func_get_args());
$st = $this->prepare($sql);
$st->execute($param);
return $st->fetch(self::FETCH_ASSOC);
}
/**
* Get single field
*
* @param sql SQL statement
* @param ... Optional bind parameters
*/
public function get_field() {
list($sql, $param) = $this->extract_params(func_get_args());
$st = $this->prepare($sql);
$st->execute($param);
$row = $st->fetch(self::FETCH_NUM);
return $row[0];
}
/**
* Extract params for all of our sql methods
*/
private function extract_params($param) {
$sql = array_shift($param);
if(!isset($param[0])) {
$param = array();
} else if(is_array($param[0])) {
$param = $param[0];
}
return array($sql, $param);
}
/**
* Shorthand for lastInsertId()
*/
public function id() {
return $this->lastInsertId();
}
}